feat(bot): enhance room ID resolution for commands

Implemented a more robust method for resolving room IDs in bot commands by introducing a `resolve_room_id` function. This helps handle room aliases and formats gracefully, minimizing errors when monitoring, protecting, or unprotecting rooms. This change improves user experience by ensuring accurate room identification based on different input formats. Also added the `resolve_alias` function to assist in resolving room aliases to IDs. Fixes issue with incorrect room handling when commands used aliases or non-standard room identifiers.
This commit is contained in:
Kumi 2024-11-10 13:17:44 +01:00
parent c9a04619de
commit 24a095c2d5
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -198,18 +198,22 @@ class ApplicationBot:
elif command == "!applicationbot list": elif command == "!applicationbot list":
return await self.list_tickets(room) return await self.list_tickets(room)
elif command.startswith("!applicationbot monitor"): elif command.startswith("!applicationbot monitor"):
return await self.add_monitored_room(command.split()[2]) room_id = self.resolve_room_id(command.split()[2])
return await self.add_monitored_room(room_id)
elif command.startswith("!applicationbot unmonitor"): elif command.startswith("!applicationbot unmonitor"):
return await self.remove_monitored_room(command.split()[2]) room_id = self.resolve_room_id(command.split()[2])
return await self.remove_monitored_room(room_id)
elif command.startswith("!applicationbot monitored"): elif command.startswith("!applicationbot monitored"):
return await self.list_monitored_rooms(room) return await self.list_monitored_rooms(room)
elif command.startswith("!applicationbot protect "): elif command.startswith("!applicationbot protect "):
try: try:
return await self.add_protected_room(command.split()[2]) room_id = await self.resolve_room_id(command.split()[2])
return await self.add_protected_room(room_id)
except IndexError: except IndexError:
logging.error(f"No room ID specified for protect command: {command}") logging.error(f"No room ID specified for protect command: {command}")
elif command.startswith("!applicationbot unprotect"): elif command.startswith("!applicationbot unprotect"):
return await self.remove_protected_room(command.split()[2]) room_id = self.resolve_room_id(command.split()[2])
return await self.remove_protected_room(room_id)
elif command.startswith("!applicationbot protected"): elif command.startswith("!applicationbot protected"):
return await self.list_protected_rooms(room) return await self.list_protected_rooms(room)
@ -744,3 +748,21 @@ class ApplicationBot:
"body": help_message, "body": help_message,
}, },
) )
async def resolve_alias(self, alias):
response = await self.client.room_resolve_alias(alias)
if response:
return response.room_id
return None
async def resolve_room_id(self, room_id):
if "/" in room_id:
room_id = room_id.split("/")[-1]
if room_id.startswith("#"):
return await self.resolve_alias(room_id)
if not room_id.startswith("!"):
return None
return room_id