feat(bot): add emoji-based application handling
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.
This commit is contained in:
parent
1b0d3d4ddd
commit
0b32442011
1 changed files with 63 additions and 0 deletions
|
@ -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 <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>"
|
||||
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)
|
||||
|
|
Loading…
Reference in a new issue