Skip to content

Commit

Permalink
Add new function to tag all admins
Browse files Browse the repository at this point in the history
  • Loading branch information
hegdenischay committed Aug 24, 2024
1 parent aa913a9 commit bdb212c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
9 changes: 1 addition & 8 deletions bangalore_bot/bot_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
4 changes: 2 additions & 2 deletions bangalore_bot/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href=\"https://matrix.to/#/{sender.replace('@', '%40').replace(':', '%3A')}\">{sender_name}</a>, 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"<p>Hi <a href=\"https://matrix.to/#/{sender.replace('@', '%40').replace(':', '%3A')}\">{sender_name}</a>, welcome to our community!</p>\n\n<p>Please introduce yourself :)</p>\n\n<p>Tell us about what you do, where you're from, what you like or where do you live so we can figure out your vibe:)</p>"
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
Expand Down
31 changes: 29 additions & 2 deletions bangalore_bot/chat_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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"""
Expand Down
13 changes: 12 additions & 1 deletion bangalore_bot/message_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

0 comments on commit bdb212c

Please sign in to comment.