From 7affc308a719ef5df9fa4b16881ea0cabec260a1 Mon Sep 17 00:00:00 2001 From: Cyberes Date: Sat, 18 Mar 2023 13:05:00 -0600 Subject: [PATCH] fix encryption issues, print traceback --- main.py | 23 +++++++++++++++++++++-- matrix_gpt/matrix.py | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index b38d7a1..1b2e2d6 100755 --- a/main.py +++ b/main.py @@ -7,6 +7,7 @@ import sys import time import traceback from pathlib import Path +from uuid import uuid4 import openai import yaml @@ -65,13 +66,16 @@ def retry(msg=None): async def main(): + # Logging in with a new device each time seems to fix encryption errors + device_id = str(uuid4()) + matrix_helper = MatrixNioGPTHelper( auth_file=Path(config_data['bot_auth']['store_path'], 'bot_auth.json'), user_id=config_data['bot_auth']['username'], passwd=config_data['bot_auth']['password'], homeserver=config_data['bot_auth']['homeserver'], store_path=config_data['bot_auth']['store_path'], - device_id=config_data['bot_auth'].get('device_id') + device_id=device_id # config_data['bot_auth'].get('device_id') ) client = matrix_helper.client @@ -117,12 +121,27 @@ async def main(): r = await client.join(room) if not isinstance(r, JoinResponse): logger.critical(f'Failed to join room {room}: {vars(r)}') + time.sleep(1.5) + + # Log out old devices to keep the session clean + devices = (await client.devices()).devices + device_list = [x.id for x in devices] + device_list.remove(device_id) + x = await client.delete_devices(device_list, { + "type": "m.login.password", + "user": config_data['bot_auth']['username'], + "password": config_data['bot_auth']['password'] + }) await client.sync_forever(timeout=10000, full_state=True) except (ClientConnectionError, ServerDisconnectedError): logger.warning("Unable to connect to homeserver, retrying in 15s...") time.sleep(15) - finally: + except Exception: + logger.critical(traceback.format_exc()) + logger.critical('Sleeping 5s...') + time.sleep(5) + except KeyboardInterrupt: await client.close() sys.exit() diff --git a/matrix_gpt/matrix.py b/matrix_gpt/matrix.py index 5126874..377e2d8 100644 --- a/matrix_gpt/matrix.py +++ b/matrix_gpt/matrix.py @@ -13,7 +13,7 @@ class MatrixNioGPTHelper: """ client = None - client_config = AsyncClientConfig(max_limit_exceeded=0, max_timeouts=0, store_sync_tokens=True, encryption_enabled=True, ) + client_config = AsyncClientConfig(max_limit_exceeded=0, max_timeouts=0, store_sync_tokens=True, encryption_enabled=True) def __init__(self, auth_file: Union[Path, str], user_id: str, passwd: str, homeserver: str, store_path: str, device_name: str = 'MatrixGPT', device_id: str = None): self.auth_file = auth_file