2023-05-05 11:58:57 +00:00
|
|
|
from nio import MatrixRoom, RoomMessageText, MegolmEvent, RoomKeyRequestError, RoomKeyRequestResponse
|
2023-04-25 11:25:53 +00:00
|
|
|
|
2023-05-02 06:58:49 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
async def message_callback(room: MatrixRoom | str, event: RoomMessageText | MegolmEvent, bot):
|
2023-04-25 11:25:53 +00:00
|
|
|
bot.logger.log(f"Received message from {event.sender} in room {room.room_id}")
|
|
|
|
|
2023-05-02 06:58:49 +00:00
|
|
|
sent = datetime.fromtimestamp(event.server_timestamp / 1000)
|
|
|
|
received = datetime.now()
|
|
|
|
latency = received - sent
|
|
|
|
|
2023-04-25 11:25:53 +00:00
|
|
|
if isinstance(event, MegolmEvent):
|
|
|
|
try:
|
|
|
|
event = await bot.matrix_client.decrypt_event(event)
|
|
|
|
except Exception as e:
|
|
|
|
try:
|
|
|
|
bot.logger.log("Requesting new encryption keys...")
|
2023-05-05 11:58:57 +00:00
|
|
|
response = await bot.matrix_client.request_room_key(event)
|
|
|
|
|
|
|
|
if isinstance(response, RoomKeyRequestError):
|
|
|
|
bot.logger.log(f"Error requesting encryption keys: {response}", "error")
|
|
|
|
elif isinstance(response, RoomKeyRequestResponse):
|
|
|
|
bot.logger.log(f"Encryption keys received: {response}", "debug")
|
|
|
|
bot.matrix_bot.olm.handle_response(response)
|
|
|
|
event = await bot.matrix_client.decrypt_event(event)
|
2023-04-25 11:25:53 +00:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
bot.logger.log(f"Error decrypting message: {e}", "error")
|
|
|
|
await bot.send_message(room, "Sorry, I couldn't decrypt that message. Please try again later or switch to a room without encryption.", True)
|
|
|
|
return
|
|
|
|
|
|
|
|
if event.sender == bot.matrix_client.user_id:
|
|
|
|
bot.logger.log("Message is from bot itself - ignoring")
|
|
|
|
|
|
|
|
elif event.body.startswith("!gptbot"):
|
|
|
|
await bot.process_command(room, event)
|
|
|
|
|
|
|
|
elif event.body.startswith("!"):
|
|
|
|
bot.logger.log(f"Received {event.body} - might be a command, but not for this bot - ignoring")
|
|
|
|
|
|
|
|
else:
|
2023-05-02 06:58:49 +00:00
|
|
|
await bot.process_query(room, event)
|
|
|
|
|
|
|
|
processed = datetime.now()
|
|
|
|
processing_time = processed - received
|
|
|
|
|
|
|
|
bot.logger.log(f"Message processing took {processing_time.total_seconds()} seconds (latency: {latency.total_seconds()} seconds)")
|
|
|
|
|
|
|
|
if bot.room_uses_timing(room):
|
|
|
|
await bot.send_message(room, f"Message processing took {processing_time.total_seconds()} seconds (latency: {latency.total_seconds()} seconds)", True)
|