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,
ReactionEvent,
RoomSendResponse,
RoomGetEventResponse,
MegolmEvent,
)
import logging
@ -100,20 +102,25 @@ 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
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
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 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 isinstance(response, RoomGetStateEventResponse):
user_id = response.content["customer_user_id"]
if reaction_key == "🟢": # Approve
await self.approve_or_reject_application(
room,
@ -121,6 +128,7 @@ class ApplicationBot:
command=f"!supportbot approve {user_id}",
approved=True,
)
elif reaction_key == "🔴": # Reject
await self.approve_or_reject_application(
room,
@ -128,6 +136,7 @@ class ApplicationBot:
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)
@ -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}")