| commit | 1e2f1484e7da84eec27b13a5a128e816e93772ae | [log] [download] |
|---|---|---|
| author | Rivoreo AutoPilot <autopilot@rivoreo.one> | Wed Oct 01 12:19:26 2025 +0800 |
| committer | Rivoreo AutoPilot <autopilot@rivoreo.one> | Wed Oct 01 12:19:26 2025 +0800 |
| tree | 1a78b9686275e00f98754fc53817bc250813155e | |
| parent | 88be3d39966905d7429ef387a09e7352ff611f3e [diff] |
feat(telegram): implement reply_to_message context parsing and integration Add comprehensive reply message handling to improve conversation context: Reply Context Extraction: - Parse reply_to_message fields including user info, timestamps, and content - Support multiple message types: text, images, documents, voice, video, stickers - Smart user display formatting with username, names, or user ID fallback - Time formatting (HH:MM) for better readability - Content length limiting (100 chars) with truncation for long messages - Robust error handling for malformed reply messages Message Integration: - Seamlessly integrate reply context into all message types (text, photo, document) - Format as "↳ 回复 [user time]: content" prefix to original message - Preserve original message content while adding contextual information - Apply to both group and private chat scenarios Bot Understanding Enhancement: - Enable AI to understand conversation threads and reply relationships - Provide clear context about what user is responding to - Improve response relevance by understanding conversation flow - Support complex multi-user group conversations with reply chains Testing Coverage: - Comprehensive unit tests for all reply context scenarios - Test edge cases: no reply, bot replies, different message types - Verify content truncation and error handling - Ensure backward compatibility with existing message handling Technical Implementation: - New methods: _extract_reply_context() and _build_message_with_reply_context() - Safe attribute access with fallbacks for missing data - Consistent error handling and logging for debugging - Integration across all message handlers (text, photo, document) This enhancement significantly improves the bot's ability to understand conversation context and provide more relevant responses in threaded discussions. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
基于Claude Code SDK的智能助手项目,支持多种交互模式和Telegram Bot功能。
claude-agent/ ├── src/claude_agent/ # 源代码 │ ├── core/ # 核心Agent功能 │ │ └── agent.py # Agent核心逻辑 │ ├── telegram/ # Telegram Bot模块 │ │ ├── bot.py # Bot主类 │ │ ├── message_handler.py # 消息处理器 │ │ ├── context_manager.py # 上下文管理 │ │ ├── file_handler.py # 文件处理 │ │ ├── stream_sender.py # 流式消息发送 │ │ ├── claude_adapter.py # Claude Agent适配器 │ │ └── interfaces.py # 接口定义 │ ├── storage/ # 持久化存储 │ │ └── persistence.py # 数据持久化 │ └── utils/ # 工具函数 ├── scripts/ # 启动脚本 │ ├── start_telegram_bot.py # Telegram Bot启动脚本 │ ├── verify_telegram_bot.py # 功能验证脚本 │ ├── main.py # 基础CLI │ └── main_enhanced.py # 增强CLI ├── run/telegrambot/ # Telegram Bot专用运行环境 │ ├── CLAUDE.md # Bot个性化提示词 │ └── start_telegrambot.sh # 便捷启动脚本 ├── configs/ # 配置文件 │ ├── default.toml # 默认配置模板 │ ├── local.toml # 本地开发配置 │ └── production.toml # 生产环境配置 ├── tests/ # 测试套件 │ ├── unit/ # 单元测试 │ ├── integration/ # 集成测试 │ └── blackbox/ # 黑盒测试 ├── data/ # 运行时数据 │ └── storage/ # 持久化数据存储 └── temp/ # 临时文件
# 激活虚拟环境 source venv/bin/activate # 确保依赖已安装 pip install -r requirements.txt
获取Bot Token:
配置文件设置 (configs/local.toml):
[telegram] bot_token = "YOUR_BOT_TOKEN_HERE" allowed_users = [YOUR_USER_ID] allowed_groups = [YOUR_GROUP_ID] [telegram.message] stream_update_interval = 1.0 max_message_length = 4096 context_history_limit = 50
获取用户/群组ID:
# 运行Bot后查看日志获取ID python scripts/start_telegram_bot.py
# 方式1: 使用专用启动脚本(推荐) cd run/telegrambot ./start_telegrambot.sh # 方式2: 直接启动 python scripts/start_telegram_bot.py # 方式3: 指定配置环境 CLAUDE_CONFIG=production python scripts/start_telegram_bot.py
# 完整测试套件 python -m pytest tests/unit/ -v # Telegram模块测试 python -m pytest tests/unit/telegram/ -v # 覆盖率检查 python -m pytest tests/unit/ --cov=src/claude_agent --cov-report=term # 功能验证 python scripts/verify_telegram_bot.py
[telegram] bot_token = "BOT_TOKEN" # Telegram Bot Token allowed_users = [123456] # 允许的用户ID列表 allowed_groups = [-1001234567] # 允许的群组ID列表 [telegram.message] stream_update_interval = 1.0 # 流式消息更新间隔(秒) max_message_length = 4096 # 最大消息长度 context_history_limit = 50 # 上下文历史保留数量 [telegram.files] max_file_size_mb = 20 # 最大文件大小(MB) temp_dir = "temp/telegram" # 临时文件目录
Bot Token无效
❌ 请配置有效的Telegram Bot Token
解决: 检查 configs/local.toml 中的 bot_token 配置
权限被拒绝
❌ 用户无权限访问
解决: 将用户/群组ID添加到白名单
Markdown解析错误
❌ Can't parse entities
解决: 已修复Markdown清理机制,确保格式正确
# 运行测试 python -m pytest tests/unit/ -v # 代码格式化 black src/ tests/ # 类型检查 mypy src/
MIT License - 详见 LICENSE 文件
注意: 请勿将包含真实Bot Token的配置文件提交到版本控制系统。