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.
This commit is contained in:
Kumi 2024-11-08 21:28:26 +01:00
parent 8f5935e41d
commit 652206a21a
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -14,6 +14,8 @@ from nio import (
RoomInviteResponse, RoomInviteResponse,
ReactionEvent, ReactionEvent,
RoomSendResponse, RoomSendResponse,
RoomGetEventResponse,
MegolmEvent,
) )
import logging import logging
@ -100,36 +102,43 @@ class ApplicationBot:
key = relation.get("key") key = relation.get("key")
if key in "🟢🟡🔴": if key in "🟢🟡🔴":
source_event = await self.client.room_get_event(room.room_id, message_id) await self.process_reaction(room, message_id, event.sender, key)
if isinstance(source_event, RoomMessageText):
self.process_reaction(room, source_event, key)
async def process_reaction(self, room: MatrixRoom, event, reaction_key): async def process_reaction(self, room: MatrixRoom, message_id, sender, reaction_key):
# Extract information from the event # Get the ticket ID based on the message ID
message_body = event.body response = await self.client.room_get_state_event(
sender = event.sender self.operator_room_id, "m.room.application", message_id
)
if message_body.startswith("Application received from"): if isinstance(response, RoomGetStateEventResponse):
# Extract user_id and ticket_id from the "apply" message ticket_id = response.content["ticket_id"]
user_id, ticket_id = self.extract_info_from_application_message(
message_body # 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( if isinstance(response, RoomGetStateEventResponse):
room, user_id = response.content["customer_user_id"]
sender,
command=f"!supportbot approve {user_id}", if reaction_key == "🟢": # Approve
approved=True, await self.approve_or_reject_application(
) room,
elif reaction_key == "🔴": # Reject sender,
await self.approve_or_reject_application( command=f"!supportbot approve {user_id}",
room, approved=True,
sender, )
command=f"!supportbot reject {user_id}",
approved=False, elif reaction_key == "🔴": # Reject
) await self.approve_or_reject_application(
elif reaction_key == "🟡": # Invite to ticket room room,
await self.invite_operator_direct(room, ticket_id, user_id) 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): def extract_info_from_application_message(self, message_body):
# Custom logic to extract user_id and ticket_id from the message # 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 # React to the message with approve, reject, and invite emojis
if isinstance(sent_message, RoomSendResponse): if isinstance(sent_message, RoomSendResponse):
logging.debug(f"Reacting to message {sent_message.event_id}") logging.debug(f"Reacting to message {sent_message.event_id}")