重构机器人
This commit is contained in:
63
bot/app.py
Normal file
63
bot/app.py
Normal file
@@ -0,0 +1,63 @@
|
||||
"""
|
||||
Telegram Bot Application 创建模块
|
||||
"""
|
||||
|
||||
from telegram.ext import Application, CommandHandler
|
||||
|
||||
from config import AppConfig
|
||||
from bot.handlers import start, go, status, settings
|
||||
|
||||
|
||||
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).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
|
||||
Reference in New Issue
Block a user