Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/bots/matrix/client.py
xtex b786a33365
feat(matrix): set device name
Signed-off-by: xtex <xtexchooser@duck.com>
2023-09-11 14:41:29 +08:00

49 lines
1.9 KiB
Python

import os
import urllib3
from nio import AsyncClient, AsyncClientConfig
from config import Config
from core.logger import Logger
homeserver: str = Config('matrix_homeserver')
user: str = Config('matrix_user')
device_id: str = Config('matrix_device_id')
device_name: str = Config('matrix_device_name')
token: str = Config('matrix_token')
megolm_backup_passphrase: str = Config('matrix_megolm_backup_passphrase')
store_path = os.path.abspath('./matrix_store')
store_path_nio = os.path.join(store_path, 'nio')
store_path_megolm_backup = os.path.join(store_path, 'megolm_backup')
store_path_next_batch = os.path.join(store_path, 'next_batch.txt')
if homeserver and user and token:
if not os.path.exists(store_path):
os.mkdir(store_path)
if not os.path.exists(store_path_nio):
os.mkdir(store_path_nio)
if megolm_backup_passphrase:
if not os.path.exists(store_path_megolm_backup):
os.mkdir(store_path_megolm_backup)
if len(megolm_backup_passphrase) <= 10:
Logger.warn("matrix_megolm_backup_passphrase is too short. It is insecure.")
else:
Logger.warn(
f"Matrix megolm backup is not setup. It is recommended to set matrix_megolm_backup_passphrase to a unique passphrase.")
if homeserver.endswith('/'):
Logger.warn(f"The matrix_homeserver ends with a slash(/), and this may cause M_UNRECOGNIZED error")
homeserver_host = urllib3.util.parse_url(homeserver).host
bot: AsyncClient = AsyncClient(homeserver,
user,
store_path=store_path_nio,
config=AsyncClientConfig(store_sync_tokens=True))
bot.restore_login(user, device_id, token)
if bot.olm:
Logger.info(f"Matrix E2E encryption support available")
else:
Logger.info(f"Matrix E2E encryption support not available")
else:
bot = False