diff --git a/main.py b/main.py index 58f3a15..bf34654 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,7 @@ import asyncio import signal import sys +import logging # Add this line from typing import List from nio import RoomMessageText, RoomMessageNotice @@ -32,7 +33,9 @@ # Extract Matrix configuration matrix_rooms: List[dict] = relay_config["matrix_rooms"] -matrix_access_token = relay_config["matrix"]["access_token"] + +# Set the logging level for 'nio' to ERROR to suppress warnings +logging.getLogger('nio').setLevel(logging.ERROR) # Add this line async def main(): """ @@ -52,12 +55,8 @@ async def main(): # Connect to Matrix matrix_client = await connect_matrix() - - matrix_logger.info("Connecting to Matrix...") - try: - await matrix_client.login(matrix_access_token) - except Exception as e: - matrix_logger.error(f"Error connecting to Matrix server: {e}") + if matrix_client is None: + logger.error("Failed to connect to Matrix. Exiting.") return # Join the rooms specified in the config.yaml diff --git a/matrix_utils.py b/matrix_utils.py index 61cddd3..6c41a12 100644 --- a/matrix_utils.py +++ b/matrix_utils.py @@ -2,7 +2,6 @@ import time import re import certifi -import io import ssl from typing import List, Union from nio import ( @@ -12,6 +11,8 @@ RoomMessageText, RoomMessageNotice, UploadResponse, + WhoamiResponse, + WhoamiError, ) from config import relay_config from log_utils import get_logger @@ -51,13 +52,36 @@ async def connect_matrix(): # Initialize the Matrix client with custom SSL context config = AsyncClientConfig(encryption_enabled=False) matrix_client = AsyncClient( - matrix_homeserver, bot_user_id, config=config, ssl=ssl_context + homeserver=matrix_homeserver, + user=bot_user_id, + config=config, + ssl=ssl_context, ) + + # Set the access_token and user_id matrix_client.access_token = matrix_access_token + matrix_client.user_id = bot_user_id + + # Attempt to retrieve the device_id using whoami() + whoami_response = await matrix_client.whoami() + if isinstance(whoami_response, WhoamiError): + logger.error(f"Failed to retrieve device_id: {whoami_response.message}") + matrix_client.device_id = None + else: + matrix_client.device_id = whoami_response.device_id + if matrix_client.device_id: + logger.info(f"Retrieved device_id: {matrix_client.device_id}") + else: + logger.warning("device_id not returned by whoami()") + + # Fetch the bot's display name response = await matrix_client.get_displayname(bot_user_id) - bot_user_name = response.displayname - return matrix_client + if hasattr(response, 'displayname'): + bot_user_name = response.displayname + else: + bot_user_name = bot_user_id # Fallback if display name is not set + return matrix_client async def join_matrix_room(matrix_client, room_id_or_alias: str) -> None: """Join a Matrix room by its ID or alias.""" diff --git a/requirements.txt b/requirements.txt index 1c83a44..267e331 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ meshtastic Pillow==9.5.0 py-staticmaps==0.4.0 -matrix-nio==0.20.2 +matrix-nio==0.25.2 matplotlib==3.9.0 requests==2.31.0 markdown==3.4.3