统一注册命令

This commit is contained in:
wood 2024-09-03 17:46:32 +08:00
parent 2c1f37ff77
commit f42d61a03f
3 changed files with 85 additions and 36 deletions

27
src/bot_commands.py Normal file
View File

@ -0,0 +1,27 @@
from telethon.tl.types import InputPeerUser
from telethon.tl.functions.bots import SetBotCommandsRequest
from telethon.tl.types import BotCommand
import logging
async def register_commands(client, admin_id):
commands = [
# TeleGuard 命令
BotCommand('add', '添加新的关键词'),
BotCommand('delete', '删除现有的关键词'),
BotCommand('list', '列出所有当前的关键词'),
# 这里可以添加其他功能的命令
# 例如BotCommand('price', '获取当前价格'),
]
try:
await client(SetBotCommandsRequest(
commands=commands,
scope=InputPeerUser(admin_id, 0),
lang_code=''
))
logging.info("Bot commands registered successfully.")
except Exception as e:
logging.error(f"Failed to register bot commands: {str(e)}")
# 如果有其他功能需要注册命令,可以在这里添加新的函数

View File

@ -1,13 +1,19 @@
import os
import json
import logging
import asyncio
import time
from telethon import TelegramClient, events
# 环境变量
BOT_TOKEN = os.environ.get('BOT_TOKEN')
ADMIN_ID = int(os.environ.get('ADMIN_ID')) # 从环境变量获取 ADMIN_ID 并转换为整数
ADMIN_ID = int(os.environ.get('ADMIN_ID'))
KEYWORDS_FILE = '/app/data/keywords.json'
# 设置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('TeleGuard')
def load_keywords():
if os.path.exists(KEYWORDS_FILE):
with open(KEYWORDS_FILE, 'r') as f:
@ -20,11 +26,12 @@ def save_keywords(keywords):
KEYWORDS = load_keywords()
client = TelegramClient('bot', api_id=6, api_hash='eb06d4abfb49dc3eeb1aeb98ae0f581e')
client.start(bot_token=BOT_TOKEN)
async def start_bot():
client = TelegramClient('bot', api_id=6, api_hash='eb06d4abfb49dc3eeb1aeb98ae0f581e')
await client.start(bot_token=BOT_TOKEN)
@client.on(events.NewMessage(pattern=''))
async def handler(event):
@client.on(events.NewMessage(pattern=''))
async def handler(event):
global KEYWORDS
if event.is_private and event.sender_id == ADMIN_ID:
command = event.message.text.split()
@ -53,14 +60,13 @@ async def handler(event):
await event.delete()
await event.respond("已撤回该消息。注:已发送的推广链接不要多次发送,置顶已有项目的推广链接也会自动撤回。")
def run():
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('TeleGuard')
logger.info("TeleGuard is running...")
await client.run_until_disconnected()
def run():
while True:
try:
logger.info("TeleGuard is starting...")
client.run_until_disconnected()
asyncio.get_event_loop().run_until_complete(start_bot())
except Exception as e:
logger.error(f"An error occurred in TeleGuard: {str(e)}")
logger.info("Attempting to restart TeleGuard in 60 seconds...")

View File

@ -1,7 +1,19 @@
import os
import multiprocessing
import guard
import binance
import logging
from bot_commands import register_commands
from telethon import TelegramClient
BOT_TOKEN = os.environ.get('BOT_TOKEN')
ADMIN_ID = int(os.environ.get('ADMIN_ID'))
async def setup_bot():
client = TelegramClient('bot', api_id=6, api_hash='eb06d4abfb49dc3eeb1aeb98ae0f581e')
await client.start(bot_token=BOT_TOKEN)
await register_commands(client, ADMIN_ID)
await client.disconnect()
def run_guard():
while True:
@ -22,6 +34,10 @@ def run_binance():
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# 注册机器人命令
import asyncio
asyncio.get_event_loop().run_until_complete(setup_bot())
# 创建两个进程分别运行 guard 和 binance 服务
guard_process = multiprocessing.Process(target=run_guard)
binance_process = multiprocessing.Process(target=run_binance)