Skip to content

Commit

Permalink
Add message_responses.py, pass config parameters to callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
anoadragon453 committed Oct 4, 2019
1 parent 5c7760e commit 4ca6b3c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion bot_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@


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:
client (nio.AsyncClient): The client to communicate to matrix with
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
Expand Down
17 changes: 12 additions & 5 deletions callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,27 @@
from nio import (
JoinError,
)
from message_responses import Message

import logging
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
Expand All @@ -46,16 +48,21 @@ async def message(self, room, event):
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):
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,))

Expand Down
33 changes: 33 additions & 0 deletions message_responses.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4ca6b3c

Please sign in to comment.