Skip to content

Commit

Permalink
Merge pull request #85 from jeremiah-k/update-dependencies
Browse files Browse the repository at this point in the history
Update dependencies
  • Loading branch information
jeremiah-k authored Oct 5, 2024
2 parents 0c496c5 + 635a490 commit ca452f8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
13 changes: 6 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import asyncio
import signal
import sys
import logging # Add this line
from typing import List

from nio import RoomMessageText, RoomMessageNotice
Expand Down Expand Up @@ -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():
"""
Expand All @@ -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
Expand Down
32 changes: 28 additions & 4 deletions matrix_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import time
import re
import certifi
import io
import ssl
from typing import List, Union
from nio import (
Expand All @@ -12,6 +11,8 @@
RoomMessageText,
RoomMessageNotice,
UploadResponse,
WhoamiResponse,
WhoamiError,
)
from config import relay_config
from log_utils import get_logger
Expand Down Expand Up @@ -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."""
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit ca452f8

Please sign in to comment.