From de52ec22f27b6b479ee96ce35c700b0167f58ce0 Mon Sep 17 00:00:00 2001 From: Kentai Radiquum Date: Fri, 17 Jan 2025 03:06:41 +0500 Subject: [PATCH] add support for access_token login --- config.yaml.example | 14 ++++++++++++++ stickerbridge/bot_commands.py | 2 +- stickerbridge/chat_functions.py | 16 ++++++++++------ stickerbridge/main.py | 14 +++++++++++++- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/config.yaml.example b/config.yaml.example index fc37df4..7c0a0ed 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -8,8 +8,22 @@ telegram_bot_token: "1234567890:aaaaaaaaaaaaaaaaaaaaaa--aaaaaaaaaaa" # Please use dedicated, freshly created one matrix_homeserver: "https://matrix.org" matrix_username: "@username:matrix.org" + +# Choose a login type for a bot, "password" or "access_token" +matrix_login_type: "access_token" + +# If using "password", use this matrix_password: "password" +# If using "access_token", use this +# How to get Device ID: +# 1. Click the "Sessions" tab (left side of the dialog). +# 2. Click on the current session. +# 3. Copy "Session ID" string +matrix_deviceid: "DeviceID" +# How to get access_token: https://t2bot.io/docs/access_tokens/ +matrix_token: "syn/mct_XXXXXXXXXXXXXXXXXX" + command_prefix: "!sb" matrix_bot_name: "Telegram stickers bot" diff --git a/stickerbridge/bot_commands.py b/stickerbridge/bot_commands.py index 7921696..a9524cf 100644 --- a/stickerbridge/bot_commands.py +++ b/stickerbridge/bot_commands.py @@ -58,7 +58,7 @@ class Command: self.room = room self.command = command.lower() self.tg_exporter = tg_exporter - self.args = self.command.split()[1:] + self.args = command.split()[1:] async def process(self): if self.command.startswith("help"): diff --git a/stickerbridge/chat_functions.py b/stickerbridge/chat_functions.py index 804a4b2..98613de 100644 --- a/stickerbridge/chat_functions.py +++ b/stickerbridge/chat_functions.py @@ -82,12 +82,16 @@ async def upload_image(client: AsyncClient, image: str): mime_type = magic.from_file(image, mime=True) file_stat = await aiofiles.os.stat(image) async with aiofiles.open(image, "r+b") as f: - resp, maybe_keys = await client.upload( - f, - content_type=mime_type, - filename=os.path.basename(image), - filesize=file_stat.st_size, - ) + try: + resp, maybe_keys = await client.upload( + f, + content_type=mime_type, + filename=os.path.basename(image), + filesize=file_stat.st_size, + ) + except: + logging.error(f"Failed to upload image ({image})") + return "" if isinstance(resp, UploadResponse): logging.debug(f"Image {image} was uploaded successfully to server.") return resp.content_uri diff --git a/stickerbridge/main.py b/stickerbridge/main.py index 98cbeb2..06612a3 100644 --- a/stickerbridge/main.py +++ b/stickerbridge/main.py @@ -36,7 +36,19 @@ async def main(): client.add_event_callback(callbacks.message, RoomMessageText) client.add_event_callback(callbacks.autojoin_room, InviteMemberEvent) - login_response = await client.login(config['matrix_password']) + if config['matrix_login_type'] == 'password': + if not config['matrix_password']: + logging.warning('Please fill in config.yaml file, then restart the bot') + raise ValueError(f'No Password') + login_response = await client.login(config['matrix_password']) + elif config['matrix_login_type'] == 'access_token': + if not config['matrix_token'] or not config['matrix_deviceid']: + logging.warning('Please fill in config.yaml file, then restart the bot') + raise ValueError(f'No access_token or Device ID') + login_response = client.restore_login(config['matrix_username'], config['matrix_deviceid'], config['matrix_token']) + else: + raise ValueError(f'Unknown login type: "{config["matrix_login_type"]}" only "password" and "access_token" are supported') + logging.info(login_response) if os.path.exists('data/next_batch'):