Skip to content
Snippets Groups Projects
Unverified Commit 80ee4af9 authored by Alexander Becker's avatar Alexander Becker Committed by GitHub
Browse files

Merge pull request #3 from foss-ag/project_structure

Project structure
parents 5e1f01dd dc9bbebb
No related branches found
No related tags found
No related merge requests found
class FeatureExamples:
@staticmethod
def echo(sender, message):
"""
Print sender and message.
:param sender:
String.
:param message:
String.
"""
print(sender, message)
@staticmethod
def caps(sender, message):
"""
Message to upper case.
:param sender:
String.
:param message:
String.
"""
print(message.upper())
class FeatureHandler:
def __init__(self, room, client):
"""
Initialize FeatureHandler.
"""
# Matrix room and client
self.__room = room
self.__client = client
# add on message listener to room and start listener thread
room.add_listener(self.__on_message)
client.start_listener_thread()
# initialize list of known command
self.__commands = []
# initialize dictionary defining mapping from commands to features
self.__features = {}
@property
def client(self):
"""
:return:
Matirx client
"""
return self.__client
@property
def commands(self):
"""
:return:
List of known commands
"""
return self.__commands
@property
def features(self):
"""
:return:
Dictionary containing commands and corresponding features.
"""
return self.__features
@property
def room(self):
"""
:return:
Matrix room
"""
return self.__room
def add_feature(self, command, feature):
"""
Add feature for specified command to room.
:param command:
String.
:param feature:
Callable. The feature will be called when the specified command occurs in the Matrix room.
feature must take two arguments: 'sender' and 'message'.
:raises KeyError:
Raises KeyError if command is already defined.
"""
if command in self.__commands:
raise KeyError("Command %s already in use!" % command)
self.__features[command] = feature
def feature(self, command):
"""
Get features that is mapped to the specified command.
:param command:
String.
:return:
Corresponding feature.
:raises KeyError:
Raises KeyError for unknown command.
"""
return self.__features[command]
def __on_message(self, room, event):
"""
Handle message events in Matrix room and call feature functions if command was posted.
:param room:
Matrix room
:param event:
Current event in Matrix room.
"""
if event['type'] == "m.room.message":
# check if message is of type text
if event['content']['msgtype'] == "m.text":
# obtain sender and message
sender = event['sender']
msg = event['content']['body']
# check if message starts with known command
for command in self.__commands:
if msg.startswith(command):
# call corresponding feature
self.__features[command](sender=sender, message=msg)
#!/usr/bin/env python3
from FeatureExamples import FeatureExamples
from FeatureHandler import FeatureHandler
from matrix_client.client import MatrixClient
import os
from bs4 import BeautifulSoup
......@@ -19,23 +21,22 @@ def find_room():
)
return date
# Called when a message is recieved.
def on_message(room, event):
if event['type'] == "m.room.message":
if event['content']['msgtype'] == "m.text":
print(find_room())
print("{0}: {1}".format(event['sender'], event['content']['body']))
def main():
# connect to server and join room
client = MatrixClient("https://matrix.org")
token = client.login(username="foss-ag_klo", password=os.environ['KLO_PW'])
room = client.join_room("#klotest:matrix.org")
room.add_listener(on_message)
client.start_listener_thread()
# create FeatureHandler
fh = FeatureHandler(room, client)
# add features to FeatureHandler that are called when the specified command is posted in the Matrix room
fh.add_feature("!echo", FeatureExamples.echo)
fh.add_feature("!CUPS", FeatureExamples.caps)
# run script until it's stopped manually
while True:
pass
if __name__ == "__main__":
main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment