实现机器人命令注册功能

添加了`register_commands`函数,以便在Telegram客户端中注册机器人命令。此功能允许轻松管理机器人可使用的命令,包括添加、删除和列出关键词及白名单域名。
This commit is contained in:
wood 2024-09-04 16:40:10 +08:00
parent 7b73013749
commit 645db7cb5b

View File

@ -1,10 +1,33 @@
import os import os
import json 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' KEYWORDS_FILE = '/app/data/keywords.json'
WHITELIST_FILE = '/app/data/whitelist.json' WHITELIST_FILE = '/app/data/whitelist.json'
ADMIN_ID = int(os.environ.get('ADMIN_ID')) 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): def load_json(file_path):
try: try:
with open(file_path, 'r') as f: with open(file_path, 'r') as f:
@ -82,3 +105,8 @@ def get_keywords():
def get_whitelist(): def get_whitelist():
return load_json(WHITELIST_FILE) return load_json(WHITELIST_FILE)
__all__ = ['handle_command', 'get_keywords', 'get_whitelist', 'register_commands']