From 787b0a2fb116cd4bd4f3bdeb8ececb332cf4cd78 Mon Sep 17 00:00:00 2001 From: "Rich O." Date: Sat, 21 Feb 2026 17:03:44 -0500 Subject: [PATCH] reworking to support e2e rooms --- bot_config.conf.example | 1 + matrix_bot.py | 27 ++++++++++++++++++++------- r0b3bot.py | 2 ++ requirements.txt | 4 +++- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/bot_config.conf.example b/bot_config.conf.example index 1d458ba..53ed8f0 100644 --- a/bot_config.conf.example +++ b/bot_config.conf.example @@ -50,3 +50,4 @@ matrix_homeserver = https://matrix.example.org matrix_user_id = @r0b3bot:example.org matrix_access_token = matrix_room_ids = !abc123:example.org,!def456:example.org +matrix_store_path = ./matrix_store/ diff --git a/matrix_bot.py b/matrix_bot.py index be24b97..77bb9d0 100644 --- a/matrix_bot.py +++ b/matrix_bot.py @@ -19,7 +19,7 @@ import urllib.request import octoapi import requests -from nio import AsyncClient, JoinError, MatrixRoom, MegolmEvent, RoomMessage, RoomMessageText, SyncError, UploadResponse, WhoamiError +from nio import AsyncClient, AsyncClientConfig, JoinError, MatrixRoom, MegolmEvent, RoomMessage, RoomMessageText, SyncError, UploadResponse, WhoamiError class MatrixBot: @@ -39,8 +39,15 @@ class MatrixBot: bambu_ip, bambu_access_code, bambu_camera_enable, + store_path="./matrix_store/", ): - self.client = AsyncClient(homeserver, user_id) + # Create the crypto store directory if it doesn't exist. + # matrix-nio[e2e] uses this to persist Olm/Megolm sessions and device keys + # across restarts so the bot can decrypt messages in E2E-encrypted rooms. + os.makedirs(store_path, exist_ok=True) + + nio_config = AsyncClientConfig(store_sync_tokens=True) + self.client = AsyncClient(homeserver, user_id, store_path=store_path, config=nio_config) self.client.access_token = access_token self.client.user_id = user_id self.prefix = command_prefix @@ -77,6 +84,12 @@ class MatrixBot: if whoami_resp.device_id: self.client.device_id = whoami_resp.device_id + # Load the E2E crypto store (requires device_id to be set first). + # This initialises the Olm account and session store, enabling decryption + # of messages in encrypted rooms. + self.client.load_store() + print(">> [Matrix] Crypto store loaded") + # Initial sync with no timeout to consume backlog before registering callbacks, # so we don't replay old messages on startup. try: @@ -117,11 +130,11 @@ class MatrixBot: print(f" [Matrix DEBUG RoomMessage] type={type(event).__name__} from {event.sender} in {room.room_id!r}") async def _on_encrypted(self, room: MatrixRoom, event: MegolmEvent): - """DEBUG — fires when an encrypted message arrives that we can't decrypt.""" - print(f" [Matrix DEBUG ENCRYPTED] from {event.sender} in {room.room_id!r}") - print(f" ! [Matrix] Room has E2E encryption. Bot commands won't work in encrypted rooms") - print(f" ! [Matrix] unless matrix-nio[e2e] is installed. Disable encryption in the room,") - print(f" ! [Matrix] or install E2E support: pip install 'matrix-nio[e2e]'") + """Fires for encrypted messages the bot can't yet decrypt (missing session key). + This is normal for historical messages. New messages should decrypt automatically + once other clients share their Megolm session with the bot's device.""" + print(f" ! [Matrix] Could not decrypt message from {event.sender} in {room.room_id!r} " + f"(session {event.session_id!r} not yet received)") async def _on_message(self, room: MatrixRoom, event: RoomMessageText): # DEBUG — remove once commands are confirmed working diff --git a/r0b3bot.py b/r0b3bot.py index f5e689e..31ba30d 100644 --- a/r0b3bot.py +++ b/r0b3bot.py @@ -80,6 +80,7 @@ MATRIX_USER_ID = config.get('matrix-config', 'matrix_user_id', fallbac MATRIX_ACCESS_TOKEN = config.get('matrix-config', 'matrix_access_token', fallback=None) _matrix_room_ids_raw = config.get('matrix-config', 'matrix_room_ids', fallback=None) MATRIX_ROOM_IDS = [r.strip() for r in _matrix_room_ids_raw.split(',')] if _matrix_room_ids_raw else [] +MATRIX_STORE_PATH = config.get('matrix-config', 'matrix_store_path', fallback='./matrix_store/') # Module-level cache for Bambu printer status, updated by the MQTT watch callback bambu_status = None @@ -904,6 +905,7 @@ async def main(): bambu_ip=BAMBU_IP_ADDRESS, bambu_access_code=BAMBU_ACCESS_CODE, bambu_camera_enable=BAMBU_CAMERA_ENABLE, + store_path=MATRIX_STORE_PATH, ) print(">> Matrix bot configured, adding to event loop") tasks.append(matrix_bot.start()) diff --git a/requirements.txt b/requirements.txt index 3a06480..fb89f4d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,4 +13,6 @@ bambu-connect>=0.3.1 paho-mqtt>=2.0 # Matrix bot support (optional — only needed if matrix-config section is present in bot_config.conf) -matrix-nio>=0.21 +# [e2e] extra adds Olm/Megolm encryption support for encrypted rooms. +# Requires the libolm system library: apt-get install libolm3 (Debian/Ubuntu) +matrix-nio[e2e]>=0.21