fix(bot): ignore self-reactions and add emoji responses

The bot now ignores reactions from itself, preventing redundant logging and processing. Added functionality to automatically react with approval, pending, and rejection emojis to messages in the operator room. This enhances user interaction and streamlines the application review process.
This commit is contained in:
Kumi 2024-11-08 13:15:41 +01:00
parent 7df144e107
commit 7bfa875f05
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -90,7 +90,9 @@ class ApplicationBot:
await self.open_ticket(room, event.state_key)
async def reaction_callback(self, room: MatrixRoom, event):
logging.debug(f"Received reaction event: {event}")
# Ignore reactions from the bot itself
if event.sender == self.client.user_id:
return
relation = event.source["content"].get("m.relates_to", {})
message_id = relation.get("event_id")
@ -105,6 +107,12 @@ class ApplicationBot:
# Extract information from the event
message_body = event.body
sender = event.sender
logging.info(
f"Processing reaction {reaction_key} from {sender} in {room.room_id}"
)
logging.info(f"Message: {message_body}")
if message_body.startswith("Application received from"):
# Extract user_id and ticket_id from the "apply" message
user_id, ticket_id = self.extract_info_from_application_message(
@ -129,7 +137,6 @@ class ApplicationBot:
def extract_info_from_application_message(self, message_body):
# Custom logic to extract user_id and ticket_id from the message
# Message format might be "Application received from <user_id> in Ticket #<ticket_id>"
parts = message_body.split()
user_id = parts[3] # "from <user_id>"
ticket_id = parts[6][1:] # "Ticket #<ticket_id>"
@ -203,7 +210,7 @@ class ApplicationBot:
# Message format
formatted_message = f"Application received from {sender} in Ticket #{ticket_id}: {message_body}"
# Send the message to the operator room
await self.client.room_send(
sent_message = await self.client.room_send(
self.operator_room_id,
"m.room.message",
{
@ -212,6 +219,42 @@ class ApplicationBot:
},
)
# React to the message with approve, reject, and invite emojis
if isinstance(sent_message, RoomMessageText):
await self.client.room_send(
room.room_id,
"m.reaction",
{
"m.relates_to": {
"rel_type": "m.annotation",
"event_id": sent_message.event_id,
"key": "🟢",
},
},
)
await self.client.room_send(
room.room_id,
"m.reaction",
{
"m.relates_to": {
"rel_type": "m.annotation",
"event_id": sent_message.event_id,
"key": "🟡",
},
},
)
await self.client.room_send(
room.room_id,
"m.reaction",
{
"m.relates_to": {
"rel_type": "m.annotation",
"event_id": sent_message.event_id,
"key": "🔴",
},
},
)
await self.relay_message(room, sender, message_body)
async def approve_or_reject_application(self, room, sender, command, approved):