feat(bot): enhance event processing robustness

Improve event type determination in the message fetching logic by adding try-except blocks to handle AttributeError and KeyError exceptions gracefully. This change allows the bot to continue processing other events if it encounters an event without a recognizable type or msgtype, avoiding premature termination and enhancing its ability to process diverse event streams more robustly. This approach also includes logging unprocessable events for debugging purposes, providing clearer insights into event handling anomalies.

Fixes #5
This commit is contained in:
Kumi 2024-04-11 07:20:44 +02:00
parent 7e64dd5245
commit cece8cfb24
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -321,10 +321,16 @@ class GPTBot:
)
for event in response.chunk:
if len(messages) >= n:
break
try:
event_type = event.type
except AttributeError:
try:
event_type = event.source["content"]["msgtype"]
except KeyError:
self.logger.log(f"Could not process event: {event}", "debug")
continue # This is most likely not a message event
if event.type.startswith("gptbot"):
if event_type.startswith("gptbot"):
messages.append(event)
elif isinstance(event, RoomMessageText):
@ -342,6 +348,9 @@ class GPTBot:
elif isinstance(event, RoomMessageMedia):
messages.append(event)
if len(messages) >= n:
break
self.logger.log(f"Found {len(messages)} messages (limit: {n})", "debug")
# Reverse the list so that messages are in chronological order