From 0b3244201108c6303baba725f7356b8145b95a90 Mon Sep 17 00:00:00 2001 From: Kumi Date: Fri, 8 Nov 2024 10:31:33 +0100 Subject: [PATCH] feat(bot): add emoji-based application handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduce emoji reaction handling in the application bot to streamline member application decisions. The bot now processes reactions "🟢" to approve, "🔴" to reject, and "🟡" to invite users to operator rooms, automating user management. This enhancement reduces manual intervention and improves efficiency. --- src/matrix_applicationbot/classes/bot.py | 63 ++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/matrix_applicationbot/classes/bot.py b/src/matrix_applicationbot/classes/bot.py index 484588f..73fe693 100644 --- a/src/matrix_applicationbot/classes/bot.py +++ b/src/matrix_applicationbot/classes/bot.py @@ -89,6 +89,69 @@ class ApplicationBot: # Automatically create a ticket for the new member await self.open_ticket(room, event.state_key) + async def reaction_callback(self, room: MatrixRoom, event): + relation = event.content.get("m.relates_to", {}) + message_id = relation.get("event_id") + 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) + + 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 + ) + 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 + # Message format might be "Application received from in Ticket #" + parts = message_body.split() + user_id = parts[3] # "from " + ticket_id = parts[6][1:] # "Ticket #" + return user_id, ticket_id + + async def invite_operator_direct(self, room, ticket_id, user_id): + # Invite user to the operator's ticket room + state_event_key = f"ticket_{ticket_id}" + response = await self.client.room_get_state_event( + self.operator_room_id, "m.room.custom.ticket", state_event_key + ) + + if isinstance(response, RoomGetStateEventResponse): + operator_room_id = response.content["operator_room"] + await self.client.room_invite(operator_room_id, user_id) + await self.client.room_send( + room.room_id, + "m.room.message", + { + "msgtype": "m.text", + "body": f"User {user_id} has been invited to the operator room for Application #{ticket_id}.", + }, + ) + async def handle_command(self, room, sender, command): if command == "!supportbot help": return await self.help_command(room, sender)