87 lines
2.5 KiB
Python
87 lines
2.5 KiB
Python
"""
|
|
Telegram Bot Application 创建模块
|
|
"""
|
|
|
|
from telegram import BotCommand
|
|
from telegram.ext import Application, CommandHandler
|
|
|
|
from config import AppConfig
|
|
from bot.handlers import start, go, status, settings
|
|
|
|
|
|
# 定义命令菜单
|
|
BOT_COMMANDS = [
|
|
BotCommand("start", "开始使用 / 欢迎信息"),
|
|
BotCommand("go", "生成支付链接 [数量] [plus/pro]"),
|
|
BotCommand("status", "查看任务状态 [task_id]"),
|
|
BotCommand("set", "用户设置 (并发数等)"),
|
|
BotCommand("help", "查看详细帮助"),
|
|
]
|
|
|
|
|
|
async def post_init(application: Application) -> None:
|
|
"""
|
|
Bot 初始化后设置命令菜单
|
|
"""
|
|
await application.bot.set_my_commands(BOT_COMMANDS)
|
|
|
|
|
|
def create_application(config: AppConfig) -> Application:
|
|
"""
|
|
创建并配置 Telegram Bot Application
|
|
|
|
参数:
|
|
config: AppConfig 配置对象
|
|
|
|
返回:
|
|
配置好的 Application 实例
|
|
"""
|
|
if not config.telegram_bot_token:
|
|
raise ValueError("TELEGRAM_BOT_TOKEN is not configured")
|
|
|
|
# 创建 Application
|
|
application = (
|
|
Application.builder()
|
|
.token(config.telegram_bot_token)
|
|
.post_init(post_init)
|
|
.build()
|
|
)
|
|
|
|
# 存储配置到 bot_data 供 handlers 使用
|
|
application.bot_data["config"] = config
|
|
|
|
# 解析允许的用户列表
|
|
allowed_users = set()
|
|
if config.telegram_allowed_users:
|
|
for uid in config.telegram_allowed_users.split(","):
|
|
uid = uid.strip()
|
|
if uid.isdigit():
|
|
allowed_users.add(int(uid))
|
|
|
|
admin_users = set()
|
|
if config.telegram_admin_users:
|
|
for uid in config.telegram_admin_users.split(","):
|
|
uid = uid.strip()
|
|
if uid.isdigit():
|
|
admin_users.add(int(uid))
|
|
|
|
application.bot_data["allowed_users"] = allowed_users
|
|
application.bot_data["admin_users"] = admin_users
|
|
|
|
# 初始化用户设置
|
|
application.bot_data["user_settings"] = {}
|
|
|
|
# 初始化任务管理器
|
|
from bot.services.task_manager import TaskManager
|
|
|
|
application.bot_data["task_manager"] = TaskManager()
|
|
|
|
# 注册命令处理器
|
|
application.add_handler(CommandHandler("start", start.start_command))
|
|
application.add_handler(CommandHandler("help", start.help_command))
|
|
application.add_handler(CommandHandler("go", go.go_command))
|
|
application.add_handler(CommandHandler("status", status.status_command))
|
|
application.add_handler(CommandHandler("set", settings.set_command))
|
|
|
|
return application
|