From 645db7cb5b3abfbcf65f578ff3b2f5b4acfc8412 Mon Sep 17 00:00:00 2001 From: wood Date: Wed, 4 Sep 2024 16:40:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=9C=BA=E5=99=A8=E4=BA=BA?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E6=B3=A8=E5=86=8C=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加了`register_commands`函数,以便在Telegram客户端中注册机器人命令。此功能允许轻松管理机器人可使用的命令,包括添加、删除和列出关键词及白名单域名。 --- src/bot_commands.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/bot_commands.py b/src/bot_commands.py index 1cf92ae..d952c2f 100644 --- a/src/bot_commands.py +++ b/src/bot_commands.py @@ -1,10 +1,33 @@ import os import json +from telethon.tl.types import InputPeerUser +from telethon.tl.functions.bots import SetBotCommandsRequest +from telethon.tl.types import BotCommand KEYWORDS_FILE = '/app/data/keywords.json' WHITELIST_FILE = '/app/data/whitelist.json' ADMIN_ID = int(os.environ.get('ADMIN_ID')) +async def register_commands(client, admin_id): + commands = [ + BotCommand('add', '添加新的关键词'), + BotCommand('delete', '删除现有的关键词'), + BotCommand('list', '列出所有当前的关键词'), + BotCommand('addwhite', '添加域名到白名单'), + BotCommand('delwhite', '从白名单移除域名'), + BotCommand('listwhite', '列出白名单域名'), + ] + + try: + await client(SetBotCommandsRequest( + commands=commands, + scope=InputPeerUser(admin_id, 0), + lang_code='' + )) + print("Bot commands registered successfully.") + except Exception as e: + print(f"Failed to register bot commands: {str(e)}") + def load_json(file_path): try: with open(file_path, 'r') as f: @@ -82,3 +105,8 @@ def get_keywords(): def get_whitelist(): return load_json(WHITELIST_FILE) + + + +__all__ = ['handle_command', 'get_keywords', 'get_whitelist', 'register_commands'] +