From 652206a21ae9262394fd95d803470789ada6e306 Mon Sep 17 00:00:00 2001 From: Kumi Date: Fri, 8 Nov 2024 21:28:26 +0100 Subject: [PATCH] feat(bot): enhance reaction processing logic Updated the reaction processing mechanism to fetch user and ticket IDs directly from room state events instead of extracting them from messages. This streamlines and improves the robustness of the approval, rejection, and invitation workflows in the application bot. State storage for message-to-ticket mapping was also added to facilitate this process. --- src/matrix_applicationbot/classes/bot.py | 72 +++++++++++++++--------- 1 file changed, 45 insertions(+), 27 deletions(-) diff --git a/src/matrix_applicationbot/classes/bot.py b/src/matrix_applicationbot/classes/bot.py index 2c8e411..b648a62 100644 --- a/src/matrix_applicationbot/classes/bot.py +++ b/src/matrix_applicationbot/classes/bot.py @@ -14,6 +14,8 @@ from nio import ( RoomInviteResponse, ReactionEvent, RoomSendResponse, + RoomGetEventResponse, + MegolmEvent, ) import logging @@ -100,36 +102,43 @@ class ApplicationBot: key = relation.get("key") if key in "🟢🟡🔴": - source_event = await self.client.room_get_event(room.room_id, message_id) - if isinstance(source_event, RoomMessageText): - self.process_reaction(room, source_event, key) + await self.process_reaction(room, message_id, event.sender, key) - async def process_reaction(self, room: MatrixRoom, event, reaction_key): - # Extract information from the event - message_body = event.body - sender = event.sender + async def process_reaction(self, room: MatrixRoom, message_id, sender, reaction_key): + # Get the ticket ID based on the message ID + response = await self.client.room_get_state_event( + self.operator_room_id, "m.room.application", message_id + ) - 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( - message_body + if isinstance(response, RoomGetStateEventResponse): + ticket_id = response.content["ticket_id"] + + # Get the user ID based on the ticket ID + response = await self.client.room_get_state_event( + self.operator_room_id, "m.room.custom.ticket", f"ticket_{ticket_id}" ) - if reaction_key == "🟢": # Approve - await self.approve_or_reject_application( - room, - sender, - command=f"!supportbot approve {user_id}", - approved=True, - ) - elif reaction_key == "🔴": # Reject - await self.approve_or_reject_application( - room, - sender, - command=f"!supportbot reject {user_id}", - approved=False, - ) - elif reaction_key == "🟡": # Invite to ticket room - await self.invite_operator_direct(room, ticket_id, user_id) + + if isinstance(response, RoomGetStateEventResponse): + user_id = response.content["customer_user_id"] + + if reaction_key == "🟢": # Approve + await self.approve_or_reject_application( + room, + sender, + command=f"!supportbot approve {user_id}", + approved=True, + ) + + elif reaction_key == "🔴": # Reject + await self.approve_or_reject_application( + room, + sender, + command=f"!supportbot reject {user_id}", + approved=False, + ) + + elif reaction_key == "🟡": # Invite to ticket room + await self.invite_operator_direct(room, ticket_id, user_id) def extract_info_from_application_message(self, message_body): # Custom logic to extract user_id and ticket_id from the message @@ -217,6 +226,15 @@ class ApplicationBot: }, ) + # Store message ID to ticket ID mapping in room state + if isinstance(sent_message, RoomSendResponse): + await self.client.room_put_state( + room_id=room.room_id, + event_type="m.room.application", + state_key=sent_message.event_id, + content={"ticket_id": ticket_id}, + ) + # React to the message with approve, reject, and invite emojis if isinstance(sent_message, RoomSendResponse): logging.debug(f"Reacting to message {sent_message.event_id}")