diff --git a/example_bot.py b/example_bot.py index 02ec264..9c0f237 100644 --- a/example_bot.py +++ b/example_bot.py @@ -55,6 +55,11 @@ def dieroll_callback(room, event): result = random.randrange(1,die_max+1) room.send_text(str(result)) +def generic_greeting_callback(room, event, data): + # Somebody wanted to be greeted, let's answer him with the registered + # greeting, passed in the data argument + room.send_text(data + event['sender']) + def main(): # Create an instance of the MatrixBotAPI @@ -72,6 +77,13 @@ def main(): dieroll_handler = MCommandHandler("d", dieroll_callback) bot.add_handler(dieroll_handler) + # Add two handlers for the greet/cheers command, using the same generic + # greeting callback and the data argument + cheers_handler = MCommandHandler("cheers", generic_greeting_callback) + greet_handler = MCommandHandler("greet", generic_greeting_callback) + bot.add_handler(cheers_handler, "Cheers, ") + bot.add_handler(greet_handler, "Greetings, ") + # Start polling bot.start_polling() diff --git a/matrix_bot_api/matrix_bot_api.py b/matrix_bot_api/matrix_bot_api.py index f4f89d2..c5c0e3d 100644 --- a/matrix_bot_api/matrix_bot_api.py +++ b/matrix_bot_api/matrix_bot_api.py @@ -31,6 +31,11 @@ def __init__(self, username, password, server, rooms=None): # Store empty list of handlers self.handlers = [] + # Store dict with additional arguments to handlers + # This allows to provide additional arguments to a specific handler + # callback on registration. + self.additional_arguments = {} + # If rooms is None, we should listen for invites and automatically accept them if rooms is None: self.client.add_invite_listener(self.handle_invite) @@ -45,9 +50,35 @@ def __init__(self, username, password, server, rooms=None): for room in self.rooms: room.add_listener(self.handle_message) - def add_handler(self, handler): + # Add a new handler to the bot. If arg is given, it is provided as a third + # argument on every invocation of handler. + def add_handler(self, handler, arg=None): self.handlers.append(handler) + if arg: + self.additional_arguments[handler] = arg + + def remove_handler(self, handler): + try: + self.handlers.remove(handler) + except ValueError as e: + return + + try: + self.additional_arguments.pop(handler) + except KeyError: + return + + def get_handler(self, trigger): + res = [] + + for h in self.handlers: + if h.triggers_on(trigger): + res.append(h) + + return res + + def handle_message(self, room, event): # Make sure we didn't send this message if re.match("@" + self.username, event['sender']): @@ -57,7 +88,15 @@ def handle_message(self, room, event): for handler in self.handlers: if handler.test_callback(room, event): # This handler needs to be called - handler.handle_callback(room, event) + try: + # If an additional argument is registered for the handler, + # call it with this argument + arg = self.additional_arguments[handler] + handler.handle_callback(room, event, arg) + except KeyError: + # Otherwise leave it out + handler.handle_callback(room, event) + def handle_invite(self, room_id, state): print("Got invite to room: " + str(room_id)) diff --git a/matrix_bot_api/mcommand_handler.py b/matrix_bot_api/mcommand_handler.py index bc56789..c0ec667 100644 --- a/matrix_bot_api/mcommand_handler.py +++ b/matrix_bot_api/mcommand_handler.py @@ -23,3 +23,10 @@ def test_command(self, room, event): if re.match(self.cmd_char + self.command, event['content']['body']): return True return False + + # Generic command testing function for all MHandler objects + def triggers_on(self, trigger): + if trigger == self.command: + return True + else: + return False diff --git a/matrix_bot_api/mregex_handler.py b/matrix_bot_api/mregex_handler.py index 32aa684..e628a2c 100644 --- a/matrix_bot_api/mregex_handler.py +++ b/matrix_bot_api/mregex_handler.py @@ -23,3 +23,10 @@ def test_regex(self, room, event): return True return False + + # Generic command testing function for all MHandler objects + def triggers_on(self, trigger): + if trigger == self.regex_str: + return True + else: + return False