diff --git a/bangalore_bot/bot_commands.py b/bangalore_bot/bot_commands.py index 1db314c..e6742af 100644 --- a/bangalore_bot/bot_commands.py +++ b/bangalore_bot/bot_commands.py @@ -40,14 +40,7 @@ def __init__( async def process(self): """Process the command""" - if self.command.startswith("echo"): - await self._echo() - elif self.command.startswith("react"): - await self._react() - elif self.command.startswith("help"): - await self._show_help() - else: - await self._unknown_command() + await self._unknown_command() async def _echo(self): """Echo back the command's arguments""" diff --git a/bangalore_bot/callbacks.py b/bangalore_bot/callbacks.py index 371b9f5..5414311 100644 --- a/bangalore_bot/callbacks.py +++ b/bangalore_bot/callbacks.py @@ -105,13 +105,13 @@ async def user_invited(self, room: MatrixRoom, event: RoomMemberEvent) -> None: # directly inferred from https://spec.matrix.org/v1.8/client-server-api/#mroommember if membership == "join" and old_event == "invite" and "bangalorebot" not in sender and sender not in visited: # send invititation message - formatted_message = f"Hi {sender_name}, welcome to our community!\n\nPlease introduce yourself :)\n\nTell us about what you do, where you're from, what you like or where do you live so we can figure out your vibe:)" + formatted_message = f"
Hi {sender_name}, welcome to our community!
\n\nPlease introduce yourself :)
\n\nTell us about what you do, where you're from, what you like or where do you live so we can figure out your vibe:)
" message = f"Hi {sender_name}, welcome to our community!\n\nPlease introduce yourself :)\n\nTell us about what you do, where you're from, what you like or where do you live so we can figure out your vibe:)" await send_text_with_mention( self.client, room.room_id, - formatted_message, message, + formatted_message, sender, ) visited[sender] = True diff --git a/bangalore_bot/chat_functions.py b/bangalore_bot/chat_functions.py index e86bd5b..066c837 100644 --- a/bangalore_bot/chat_functions.py +++ b/bangalore_bot/chat_functions.py @@ -72,8 +72,8 @@ async def send_text_to_room( async def send_text_with_mention( client: AsyncClient, room_id: str, - formatted_message: str, message: str, + formatted_body: str, sender: str, ) -> Union[RoomSendResponse, ErrorResponse]: """Send text to a matrix room with mentions. @@ -93,7 +93,7 @@ async def send_text_with_mention( "msgtype": "m.text", "format": "org.matrix.custom.html", "body": message, - "formatted_body": formatted_message, + "formatted_body": formatted_body, "m.mentions" : { "user_ids": [ sender @@ -170,6 +170,33 @@ async def react_to_event( ignore_unverified_devices=True, ) +async def find_admins_and_reply( + client: AsyncClient, + room_id: str, + event_id: str, + reply_text: str, + admins: list, +) -> Union[Response, ErrorResponse]: + # find the admins somehow + content = { + "body": reply_text, + "m.mentions": { + "user_ids": admins + }, + "m.relates_to": { + "m.in_reply_to": { + "event_id": event_id + } + }, + "msgtype": "m.text" + } + return await client.room_send( + room_id, + "m.room.message", + content, + ignore_unverified_devices=True, + ) + async def decryption_failure(self, room: MatrixRoom, event: MegolmEvent) -> None: """Callback for when an event fails to decrypt. Inform the user""" diff --git a/bangalore_bot/message_responses.py b/bangalore_bot/message_responses.py index 93240c5..e03215a 100644 --- a/bangalore_bot/message_responses.py +++ b/bangalore_bot/message_responses.py @@ -2,7 +2,7 @@ from nio import AsyncClient, MatrixRoom, RoomMessageText -from bangalore_bot.chat_functions import send_text_to_room +from bangalore_bot.chat_functions import send_text_to_room, find_admins_and_reply from bangalore_bot.config import Config from bangalore_bot.storage import Storage @@ -45,8 +45,19 @@ async def process(self) -> None: """Process and possibly respond to the message""" if self.message_content.lower() == "hello world": await self._hello_world() + if self.message_content.lower() == "@admin": + await self._tag_admins() + if self.message_content.lower() == "@admins": + await self._tag_admins() async def _hello_world(self) -> None: """Say hello""" text = "Hello, world!" await send_text_to_room(self.client, self.room.room_id, text) + + async def _tag_admins(self) -> None: + """Send a message responding to this one, tagging admins""" + text = "Tagging all admins" + all_users = self.room.power_levels.users + admins = [user for user, level in all_users.items() if level >= 50 and 'whatsappbot' not in user] + await find_admins_and_reply(self.client, self.room.room_id, self.event.event_id, text, admins)