Add message_responses.py, pass config parameters to callbacks
This commit is contained in:
parent
5c7760e2c0
commit
4ca6b3c2c8
5 changed files with 61 additions and 7 deletions
12
README.md
12
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
|
directly to the bot. The `process` command is then called for the bot to act on
|
||||||
that command.
|
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`
|
### `chat_functions.py`
|
||||||
|
|
||||||
A separate file to hold helper methods related to messaging. Mostly just for
|
A separate file to hold helper methods related to messaging. Mostly just for
|
||||||
|
|
|
@ -2,7 +2,7 @@ from chat_functions import send_text_to_room
|
||||||
|
|
||||||
|
|
||||||
class Command(object):
|
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
|
"""A command made by a user
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
@ -10,6 +10,8 @@ class Command(object):
|
||||||
|
|
||||||
store (Storage): Bot storage
|
store (Storage): Bot storage
|
||||||
|
|
||||||
|
config (Config): Bot configuration parameters
|
||||||
|
|
||||||
command (str): The command and arguments
|
command (str): The command and arguments
|
||||||
|
|
||||||
room (nio.rooms.MatrixRoom): The room the command was sent in
|
room (nio.rooms.MatrixRoom): The room the command was sent in
|
||||||
|
|
17
callbacks.py
17
callbacks.py
|
@ -5,6 +5,7 @@ from bot_commands import Command
|
||||||
from nio import (
|
from nio import (
|
||||||
JoinError,
|
JoinError,
|
||||||
)
|
)
|
||||||
|
from message_responses import Message
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -12,18 +13,19 @@ logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class Callbacks(object):
|
class Callbacks(object):
|
||||||
|
|
||||||
def __init__(self, client, store, command_prefix):
|
def __init__(self, client, store, config):
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
client (nio.AsyncClient): nio client used to interact with matrix
|
client (nio.AsyncClient): nio client used to interact with matrix
|
||||||
|
|
||||||
store (Storage): Bot storage
|
store (Storage): Bot storage
|
||||||
|
|
||||||
command_prefix (str): The prefix for bot commands
|
config (Config): Bot configuration parameters
|
||||||
"""
|
"""
|
||||||
self.client = client
|
self.client = client
|
||||||
self.store = store
|
self.store = store
|
||||||
self.command_prefix = command_prefix
|
self.config = config
|
||||||
|
self.command_prefix = config.command_prefix
|
||||||
|
|
||||||
async def message(self, room, event):
|
async def message(self, room, event):
|
||||||
"""Callback for when a message event is received
|
"""Callback for when a message event is received
|
||||||
|
@ -46,16 +48,21 @@ class Callbacks(object):
|
||||||
f"{room.user_name(event.sender)}: {msg}"
|
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)
|
has_command_prefix = msg.startswith(self.command_prefix)
|
||||||
if not has_command_prefix and not room.is_group:
|
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
|
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:
|
if has_command_prefix:
|
||||||
# Remove the command prefix
|
# Remove the command prefix
|
||||||
msg = msg[len(self.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()
|
await command.process()
|
||||||
|
|
||||||
async def invite(self, room, event):
|
async def invite(self, room, event):
|
||||||
|
|
2
main.py
2
main.py
|
@ -41,7 +41,7 @@ async def main():
|
||||||
client.access_token = config.access_token
|
client.access_token = config.access_token
|
||||||
|
|
||||||
# Set up event callbacks
|
# 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.message, (RoomMessageText,))
|
||||||
client.add_event_callback(callbacks.invite, (InviteEvent,))
|
client.add_event_callback(callbacks.invite, (InviteEvent,))
|
||||||
|
|
||||||
|
|
33
message_responses.py
Normal file
33
message_responses.py
Normal 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
|
Loading…
Reference in a new issue