diff --git a/README.md b/README.md index 193dcbc..90ce70d 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,18 @@ prefix (defined by the bot's config file), or through a private message directly to the bot. The `process` command is then called for the bot to act on that command. +### `message_responses.py` + +Where responses to messages that are posted in a room (but not necessarily +directed at the bot) are specified. `callbacks.py` will listen for messages in +rooms the bot is in, and upon receiving one will create a new `Message` object +(which contains the message text, amongst other things) and calls `process()` +on it, which can send a message to the room as it sees fit. + +A good example of this would be a Github bot that listens for people mentioning +issue numbers in chat (e.g. "We should fix #123"), and the bot sending messages +to the room immediately afterwards with the issue name and link. + ### `chat_functions.py` A separate file to hold helper methods related to messaging. Mostly just for diff --git a/bot_commands.py b/bot_commands.py index 2315c64..a7bc7c8 100644 --- a/bot_commands.py +++ b/bot_commands.py @@ -2,7 +2,7 @@ from chat_functions import send_text_to_room class Command(object): - def __init__(self, client, store, command, room, event): + def __init__(self, client, store, config, command, room, event): """A command made by a user Args: @@ -10,6 +10,8 @@ class Command(object): store (Storage): Bot storage + config (Config): Bot configuration parameters + command (str): The command and arguments room (nio.rooms.MatrixRoom): The room the command was sent in diff --git a/callbacks.py b/callbacks.py index d75d312..ba1f036 100644 --- a/callbacks.py +++ b/callbacks.py @@ -5,6 +5,7 @@ from bot_commands import Command from nio import ( JoinError, ) +from message_responses import Message import logging logger = logging.getLogger(__name__) @@ -12,18 +13,19 @@ logger = logging.getLogger(__name__) class Callbacks(object): - def __init__(self, client, store, command_prefix): + def __init__(self, client, store, config): """ Args: client (nio.AsyncClient): nio client used to interact with matrix store (Storage): Bot storage - command_prefix (str): The prefix for bot commands + config (Config): Bot configuration parameters """ self.client = client self.store = store - self.command_prefix = command_prefix + self.config = config + self.command_prefix = config.command_prefix async def message(self, room, event): """Callback for when a message event is received @@ -46,16 +48,21 @@ class Callbacks(object): f"{room.user_name(event.sender)}: {msg}" ) - # Ignore message if in a public room without command prefix + # Process as message if in a public room without command prefix has_command_prefix = msg.startswith(self.command_prefix) if not has_command_prefix and not room.is_group: + # General message listener + message = Message(self.client, self.store, self.config, msg, room, event) + await message.process() return + # Otherwise if this is in a 1-1 with the bot or features a command prefix, + # treat it as a command if has_command_prefix: # Remove the command prefix msg = msg[len(self.command_prefix):] - command = Command(self.client, self.store, msg, room, event) + command = Command(self.client, self.store, self.config, msg, room, event) await command.process() async def invite(self, room, event): diff --git a/main.py b/main.py index c4aef8b..531e634 100644 --- a/main.py +++ b/main.py @@ -41,7 +41,7 @@ async def main(): client.access_token = config.access_token # Set up event callbacks - callbacks = Callbacks(client, store, config.command_prefix) + callbacks = Callbacks(client, store, config) client.add_event_callback(callbacks.message, (RoomMessageText,)) client.add_event_callback(callbacks.invite, (InviteEvent,)) diff --git a/message_responses.py b/message_responses.py new file mode 100644 index 0000000..acfd99d --- /dev/null +++ b/message_responses.py @@ -0,0 +1,33 @@ +import logging + +logger = logging.getLogger(__name__) + + +class Message(object): + + def __init__(self, client, store, config, message_content, room, event): + """Initialize a new Message + + Args: + client (nio.AsyncClient): nio client used to interact with matrix + + store (Storage): Bot storage + + config (Config): Bot configuration parameters + + message_content (str): The body of the message + + room (nio.rooms.MatrixRoom): The room the event came from + + event (nio.events.room_events.RoomMessageText): The event defining the message + """ + self.client = client + self.store = store + self.config = config + self.message_content = message_content + self.room = room + self.event = event + + async def process(self): + """Process and possibly respond to the message""" + pass