我已经成功实现了一个完整的Webhook系统,解决了Telegram Bot之间无法直接看到彼此消息的问题。该系统允许多个Bot在同一个群组中感知彼此的消息,实现真正的Bot间通信。
┌─────────────┐ HTTP POST ┌──────────────────┐ HTTP POST ┌─────────────┐
│ Bot A │ ──────────────→ │ Webhook Server │ ──────────────→ │ Bot B │
│ │ 广播消息 │ │ 分发消息 │ │
│ (发送消息) │ │ - 消息路由 │ │ (接收消息) │
│ │ │ - Bot管理 │ │ │
└─────────────┘ │ - 认证授权 │ └─────────────┘
└──────────────────┘
│
│ 同时分发给
▼
┌─────────────┐
│ Bot C │
│ │
│ (接收消息) │
└─────────────┘
src/claude_agent/webhook/ ├── __init__.py # 模块导出 ├── models.py # 数据模型定义 ├── server.py # Webhook服务器 ├── client.py # Webhook客户端 ├── integration.py # Bot集成基类 └── telegram_integration.py # Telegram专用集成 scripts/ ├── webhook_server.py # 服务器启动脚本 └── webhook_demo.sh # 演示脚本 configs/ ├── webhook_server.toml # 服务器配置 ├── bot1_webhook_example.toml # Bot1示例配置 └── bot2_webhook_example.toml # Bot2示例配置 tests/ ├── unit/test_webhook.py # 单元测试 └── integration/test_webhook_integration.py # 集成测试 docs/ └── WEBHOOK_GUIDE.md # 使用指南
# 使用脚本启动 ./scripts/webhook_demo.sh server-only # 或直接启动 python scripts/webhook_server.py webhook_server
在Bot配置文件中启用Webhook:
[webhook] enabled = true server_url = "http://localhost:8080" auth_token = "secure-webhook-token-2024" [webhook.client] subscribed_groups = [-1001234567890] callback_port = 8081
python scripts/telegram_bot.py bot1_webhook_example python scripts/telegram_bot.py bot2_webhook_example
POST /register - 注册BotPOST /unregister/{bot_username} - 注销BotPOST /broadcast - 广播消息GET /bots - 获取Bot列表GET /health - 健康检查所有API都使用Bearer Token认证:
Authorization: Bearer your-webhook-token
这个Webhook系统为Telegram Bot间通信提供了一个强大、安全、易用的解决方案,完全解决了原始需求中提到的Bot间消息广播问题。