Add message_responses.py, pass config parameters to callbacks

This commit is contained in:
Andrew Morgan 2019-10-04 14:44:19 +01:00
parent 5c7760e2c0
commit 4ca6b3c2c8
5 changed files with 61 additions and 7 deletions

View file

@ -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

View file

@ -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

View file

@ -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):

View file

@ -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,))

33
message_responses.py Normal file
View file

@ -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