React to undecryptable messages

This commit is contained in:
Andrew Morgan 2021-01-09 21:58:39 -05:00
parent 9774851d5b
commit 4aa1e2d0f4
3 changed files with 59 additions and 3 deletions

View file

@ -1,9 +1,9 @@
import logging
from nio import JoinError, MatrixRoom, RoomGetEventError, UnknownEvent
from nio import JoinError, MatrixRoom, MegolmEvent, RoomGetEventError, UnknownEvent
from my_project_name.bot_commands import Command
from my_project_name.chat_functions import make_pill, send_text_to_room
from my_project_name.chat_functions import make_pill, react_to_event, send_text_to_room
from my_project_name.message_responses import Message
logger = logging.getLogger(__name__)
@ -130,6 +130,28 @@ class Callbacks(object):
reply_to_event_id=reacted_to_id,
)
async def decryption_failure(self, room: MatrixRoom, event: MegolmEvent):
"""Callback for when an event fails to decrypt. Inform the user"""
logger.error(
f"Failed to decrypt event '{event.event_id}' in room '{room.room_id}'!"
f"\n\n"
f"Tip: try using a different device ID in your config file and restart."
f"\n\n"
f"If all else fails, delete your store directory and let the bot recreate "
f"it (your reminders will NOT be deleted, but the bot may respond to existing "
f"commands a second time)."
)
red_x_and_lock_emoji = "❌ 🔐"
# React to the undecryptable event with some emoji
await react_to_event(
self.client,
room.room_id,
event.event_id,
red_x_and_lock_emoji,
)
async def unknown(self, room: MatrixRoom, event: UnknownEvent):
"""Callback for when an event with a type that is unknown to matrix-nio is received.
Currently this is used for reaction events, which are not specced.

View file

@ -2,7 +2,14 @@ import logging
from typing import Optional, Union
from markdown import markdown
from nio import AsyncClient, ErrorResponse, Response, SendRetryError
from nio import (
AsyncClient,
ErrorResponse,
MatrixRoom,
MegolmEvent,
Response,
SendRetryError,
)
logger = logging.getLogger(__name__)
@ -116,3 +123,28 @@ async def react_to_event(
content,
ignore_unverified_devices=True,
)
async def decryption_failure(self, room: MatrixRoom, event: MegolmEvent):
"""Callback for when an event fails to decrypt. Inform the user"""
logger.error(
f"Failed to decrypt event '{event.event_id}' in room '{room.room_id}'!"
f"\n\n"
f"Tip: try using a different device ID in your config file and restart."
f"\n\n"
f"If all else fails, delete your store directory and let the bot recreate "
f"it (your reminders will NOT be deleted, but the bot may respond to existing "
f"commands a second time)."
)
user_msg = (
"Unable to decrypt this message. "
"Check whether you've chosen to only encrypt to trusted devices."
)
await send_text_to_room(
self.client,
room.room_id,
user_msg,
reply_to_event_id=event.event_id,
)

View file

@ -11,6 +11,7 @@ from nio import (
InviteMemberEvent,
LocalProtocolError,
LoginError,
MegolmEvent,
RoomMessageText,
UnknownEvent,
)
@ -60,6 +61,7 @@ async def main():
callbacks = Callbacks(client, store, config)
client.add_event_callback(callbacks.message, (RoomMessageText,))
client.add_event_callback(callbacks.invite, (InviteMemberEvent,))
client.add_event_callback(callbacks.decryption_failure, (MegolmEvent,))
client.add_event_callback(callbacks.unknown, (UnknownEvent,))
# Keep trying to reconnect on failure (with some time in-between)