fix(telegram): resolve duplicate user message storage in group chats

Fix critical issue where user messages in group chats were being saved twice
to conversations.json, causing data duplication and inconsistent chat history.

Key Changes:
- Unified message saving logic to prevent duplicate storage
- Removed redundant save calls in message processing pipeline
- Combined group chat logging and reply logic into single save operation
- Enhanced logging to indicate save reason (group chat or reply trigger)

Technical Details:
- Previously: messages saved at line 166 (group chat) + line 182 (reply)
- Now: single conditional save combining both scenarios
- Maintains functionality for both group chat logging and bot responses
- Preserves reply_info context and user attribution features

Fixes issue where "@maiwaifubot 测试" appeared twice in conversation history.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
5 files changed
tree: df73e9343a2ca8517faa5f05e3acf16884f60581
  1. .coveragerc
  2. .gitignore
  3. CLAUDE.md
  4. DEVELOPMENT_HISTORY.md
  5. DIRECTORY_STRUCTURE.md
  6. FINAL_DELIVERY_REPORT.md
  7. PROJECT_REPORT.md
  8. README.md
  9. README_TELEGRAM.md
  10. TELEGRAM_COMPREHENSIVE_TEST_REPORT.md
  11. configs/
  12. coverage.json
  13. docs/
  14. fix_tests.py
  15. pyproject.toml
  16. references/sshout/
  17. requirements.txt
  18. run/
  19. scripts/
  20. src/claude_agent/
  21. tests/
README.md

Claude Agent - 智能对话助手

基于Claude Code SDK的智能助手项目,支持多种交互模式和Telegram Bot功能。

🚀 主要特性

核心功能

  • 智能对话系统: 基于Claude AI的智能对话处理
  • Telegram Bot: 完整的Telegram机器人实现,支持私聊和群组
  • 双思考模式: 交互模式和YOLO自主思考模式
  • 持久化存储: 对话历史和用户上下文持久化保存
  • 流式消息: 实时消息更新和编辑功能

Telegram Bot特性

  • 权限控制: 支持配置允许的用户和群组白名单
  • 多媒体支持: 图片分析、文档处理
  • 智能群组参与: 自动检测@提及和回复时机
  • 上下文管理: 独立的per-chat对话历史
  • 错误恢复: 完善的异常处理和恢复机制

技术特性

  • 异步架构: 基于asyncio的高性能处理
  • 配置驱动: TOML配置文件,支持多环境部署
  • 模块化设计: 松耦合组件,易于测试和维护
  • 完整测试: 广泛的单元测试和集成测试

📁 项目结构

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/                    # 临时文件

🛠️ 快速开始

1. 环境配置

# 激活虚拟环境
source venv/bin/activate

# 确保依赖已安装
pip install -r requirements.txt

2. Telegram Bot配置

  1. 获取Bot Token:

    • 联系 @BotFather 创建新Bot
    • 获取Bot Token
  2. 配置文件设置 (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
    
  3. 获取用户/群组ID:

    # 运行Bot后查看日志获取ID
    python scripts/start_telegram_bot.py
    

3. 启动Bot

# 方式1: 使用专用启动脚本(推荐)
cd run/telegrambot
./start_telegrambot.sh

# 方式2: 直接启动
python scripts/start_telegram_bot.py

# 方式3: 指定配置环境
CLAUDE_CONFIG=production python scripts/start_telegram_bot.py

✨ 功能特性

Telegram Bot功能

  • 智能对话: 基于Claude AI的自然语言处理
  • 图片分析: 自动分析上传的图片并提供描述
  • 文档处理: 支持PDF、Word、文本等格式
  • 群组智能: @机器人触发,智能参与群组对话
  • 上下文记忆: 独立的对话历史,重启后恢复

支持的文件类型

  • 图片: JPG, PNG, GIF, WebP
  • 文档: PDF, DOC, DOCX, TXT, MD

交互模式

  • 交互模式: 传统问答式对话
  • YOLO模式: 自主多步思考和问题解决

🧪 测试

运行测试

# 完整测试套件
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

测试状态

  • 单元测试: 600+ 测试用例
  • 覆盖率: 74% (Telegram模块),84% (Storage模块)
  • 功能测试: 完整的端到端测试
  • 稳定性: 主要功能100%通过

📊 配置说明

主要配置项

[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"       # 临时文件目录

🔐 安全特性

权限控制

  • 白名单用户和群组控制
  • 未授权访问自动拒绝
  • 文件上传大小和格式限制

数据安全

  • 敏感配置文件排除版本控制
  • 用户数据本地加密存储
  • 自动临时文件清理

🛡️ 故障排除

常见问题

  1. Bot Token无效

    ❌ 请配置有效的Telegram Bot Token
    

    解决: 检查 configs/local.toml 中的 bot_token 配置

  2. 权限被拒绝

    ❌ 用户无权限访问
    

    解决: 将用户/群组ID添加到白名单

  3. Markdown解析错误

    ❌ Can't parse entities
    

    解决: 已修复Markdown清理机制,确保格式正确

📈 性能指标

  • 响应速度: 平均 < 2秒
  • 并发处理: 支持多用户同时对话
  • 内存使用: 优化的消息历史管理
  • 错误恢复: 自动重连和恢复机制

📚 相关文档

🤝 开发贡献

代码质量

  • 完整的类型注解
  • 规范的文档字符串
  • 全面的错误处理
  • 充分的测试覆盖

开发流程

# 运行测试
python -m pytest tests/unit/ -v

# 代码格式化
black src/ tests/

# 类型检查
mypy src/

📄 许可证

MIT License - 详见 LICENSE 文件

🎯 项目状态

  • 当前版本: v2.2.1 (稳定版)
  • 测试状态: ✅ 主要功能全部通过
  • 部署状态: 🚀 生产就绪
  • 维护状态: 🔄 持续维护

注意: 请勿将包含真实Bot Token的配置文件提交到版本控制系统。