重构机器人
This commit is contained in:
104
bot_main.py
Normal file
104
bot_main.py
Normal file
@@ -0,0 +1,104 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Telegram Bot 启动入口
|
||||
|
||||
使用方法:
|
||||
python bot_main.py
|
||||
|
||||
配置 (.env):
|
||||
TELEGRAM_BOT_TOKEN=your_bot_token_here
|
||||
TELEGRAM_ALLOWED_USERS=123456789,987654321
|
||||
TELEGRAM_ADMIN_USERS=123456789
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# 确保项目根目录在 Python 路径中
|
||||
sys.path.insert(0, str(Path(__file__).parent))
|
||||
|
||||
from config import load_config
|
||||
from utils.logger import logger, setup_logger
|
||||
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
print("=" * 60)
|
||||
print(" OpenAI 账号自动注册系统 - Telegram Bot")
|
||||
print("=" * 60)
|
||||
print()
|
||||
|
||||
# 加载配置
|
||||
config = load_config()
|
||||
setup_logger(config.log_level)
|
||||
|
||||
# 检查配置
|
||||
if not config.telegram_bot_enabled:
|
||||
logger.error("Telegram Bot is disabled in config")
|
||||
print("❌ Telegram Bot 已禁用,请设置 TELEGRAM_BOT_ENABLED=true")
|
||||
sys.exit(1)
|
||||
|
||||
if not config.telegram_bot_token:
|
||||
logger.error("TELEGRAM_BOT_TOKEN is not configured")
|
||||
print("❌ 请在 .env 文件中配置 TELEGRAM_BOT_TOKEN")
|
||||
print()
|
||||
print("步骤:")
|
||||
print("1. 在 Telegram 中找到 @BotFather")
|
||||
print("2. 发送 /newbot 创建新 Bot")
|
||||
print("3. 将获得的 Token 填入 .env 文件:")
|
||||
print(" TELEGRAM_BOT_TOKEN=your_token_here")
|
||||
sys.exit(1)
|
||||
|
||||
# 打印配置信息
|
||||
allowed_count = len([x for x in config.telegram_allowed_users.split(",") if x.strip()])
|
||||
admin_count = len([x for x in config.telegram_admin_users.split(",") if x.strip()])
|
||||
|
||||
logger.info(f"Bot Token: {config.telegram_bot_token[:10]}...")
|
||||
logger.info(f"Allowed users: {allowed_count} configured")
|
||||
logger.info(f"Admin users: {admin_count} configured")
|
||||
|
||||
if not config.telegram_allowed_users and not config.telegram_admin_users:
|
||||
logger.warning("⚠️ No user whitelist configured - Bot will be accessible to everyone!")
|
||||
print()
|
||||
print("⚠️ 警告: 未配置用户白名单,所有人都可以使用此 Bot")
|
||||
print(" 建议在 .env 中添加:")
|
||||
print(" TELEGRAM_ALLOWED_USERS=your_user_id")
|
||||
print()
|
||||
|
||||
# 确保输出目录存在
|
||||
Path("output/logs").mkdir(parents=True, exist_ok=True)
|
||||
Path("output/tokens").mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# 创建并运行 Bot
|
||||
try:
|
||||
from bot.app import create_application
|
||||
|
||||
logger.info("Creating Telegram Bot application...")
|
||||
application = create_application(config)
|
||||
|
||||
logger.info("Starting Bot in polling mode...")
|
||||
print()
|
||||
print("🤖 Bot 已启动! 在 Telegram 中发送 /start 开始使用")
|
||||
print(" 按 Ctrl+C 停止")
|
||||
print()
|
||||
|
||||
# 运行 Bot
|
||||
application.run_polling(
|
||||
allowed_updates=["message"],
|
||||
drop_pending_updates=True # 忽略 Bot 离线期间的消息
|
||||
)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print()
|
||||
logger.info("Bot stopped by user")
|
||||
print("👋 Bot 已停止")
|
||||
|
||||
except Exception as e:
|
||||
logger.exception("Failed to start Bot")
|
||||
print(f"❌ 启动失败: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user