reworking to support e2e rooms

This commit is contained in:
rich committed 2026-02-21 17:03:44 -05:00
1 parent 31270db12b
commit 787b0a2fb1
4 files changed
+26 -8

No files matched your search

+1
View File
@@ -50,3 +50,4 @@ matrix_homeserver = https://matrix.example.org
matrix_user_id = @r0b3bot:example.org
matrix_access_token = <Matrix access token>
matrix_room_ids = !abc123:example.org,!def456:example.org
matrix_store_path = ./matrix_store/
+20 -7
View File
@@ -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
+2
View File
@@ -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())
+3 -1
View File
@@ -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