| #!/usr/bin/env python3 |
| """ |
| Telegram Bot功能验证脚本 |
| 验证Bot的基本功能是否正常 |
| """ |
| |
| import sys |
| import asyncio |
| from pathlib import Path |
| |
| # 添加src目录到Python路径 |
| current_dir = Path(__file__).parent |
| project_root = current_dir.parent |
| src_dir = project_root / "src" |
| sys.path.insert(0, str(src_dir)) |
| |
| from claude_agent.telegram.bot import TelegramBot |
| from claude_agent.telegram.context_manager import ContextManager |
| from claude_agent.telegram.file_handler import FileHandler |
| |
| |
| async def test_components(): |
| """测试各个组件""" |
| print("🔍 开始组件功能验证...\n") |
| |
| # 1. 测试上下文管理器 |
| print("📝 测试上下文管理器...") |
| context_manager = ContextManager(max_history_per_chat=10) |
| context_manager.add_message(chat_id=123, user_id=456, message="Test message") |
| context = context_manager.get_context(123) |
| assert len(context) == 1 |
| assert context[0]['message'] == "Test message" |
| print("✅ 上下文管理器测试通过") |
| |
| # 2. 测试文件处理器 |
| print("📁 测试文件处理器...") |
| file_handler = FileHandler(temp_dir="temp/test", max_file_size_mb=5) |
| # 测试图片生成 |
| image_data = await file_handler.generate_image("Test image") |
| assert isinstance(image_data, bytes) |
| assert len(image_data) > 0 |
| print("✅ 文件处理器测试通过") |
| |
| # 3. 测试Bot创建和配置 |
| print("🤖 测试Bot创建...") |
| bot = TelegramBot('local') |
| stats = bot.get_stats() |
| assert stats['config_name'] == 'local' |
| assert 'allowed_users_count' in stats |
| assert 'allowed_groups_count' in stats |
| print("✅ Bot创建测试通过") |
| |
| print("\n🎉 所有组件验证通过!") |
| |
| |
| async def test_telegram_token(): |
| """测试Telegram Token有效性(不实际连接)""" |
| print("\n📱 验证Telegram配置...") |
| |
| try: |
| bot = TelegramBot('local') |
| telegram_config = bot.telegram_config |
| |
| # 检查Token格式 |
| token = telegram_config['bot_token'] |
| if ':' in token and len(token.split(':')) == 2: |
| bot_id, bot_secret = token.split(':') |
| if bot_id.isdigit() and len(bot_secret) >= 35: |
| print("✅ Bot Token格式正确") |
| else: |
| print("⚠️ Bot Token格式可能有问题") |
| else: |
| print("❌ Bot Token格式无效") |
| |
| # 检查配置 |
| users = telegram_config['allowed_users'] |
| groups = telegram_config['allowed_groups'] |
| print(f"✅ 配置的授权用户: {len(users)} 个") |
| print(f"✅ 配置的授权群组: {len(groups)} 个") |
| |
| except Exception as e: |
| print(f"❌ 配置验证失败: {e}") |
| |
| |
| def main(): |
| """主函数""" |
| print("🚀 Telegram Bot 功能验证\n") |
| print("=" * 50) |
| |
| try: |
| # 运行异步测试 |
| asyncio.run(test_components()) |
| asyncio.run(test_telegram_token()) |
| |
| print("\n" + "=" * 50) |
| print("✅ 验证完成!Telegram Bot 已准备就绪") |
| print("\n📋 下一步操作:") |
| print("1. 确保配置了正确的Bot Token") |
| print("2. 运行: ./scripts/telegram_bot.sh") |
| print("3. 在Telegram中向Bot发送消息测试") |
| |
| except Exception as e: |
| print(f"\n❌ 验证失败: {e}") |
| import traceback |
| traceback.print_exc() |
| return 1 |
| |
| return 0 |
| |
| |
| if __name__ == "__main__": |
| sys.exit(main()) |