claude-code-remote-remake/fix-telegram.sh

120 lines
3.6 KiB
Bash
Raw Permalink Normal View History

Telegram&Line support (#17) * feat: Add LINE and Telegram messaging support This major enhancement extends Claude Code Remote with multi-platform messaging support: ## 🚀 New Features ### LINE Messaging Support - LINE Bot API integration with token-based commands - Secure webhook handler with signature verification - Session management with 24-hour expiration - Support for both individual and group chats - User/Group ID whitelist security ### Telegram Bot Support - Telegram Bot API with interactive buttons - Slash command support (/cmd TOKEN command) - Callback query handling for better UX - Group and private chat support - Chat ID-based authorization ### Multi-Platform Architecture - Unified notification system supporting all platforms - Platform-agnostic session management - Configurable channel enabling/disabling - Parallel webhook server support ## 🛠️ Technical Implementation ### Core Components - `src/channels/line/` - LINE messaging implementation - `src/channels/telegram/` - Telegram bot implementation - `src/utils/controller-injector.js` - Command injection utility - Multi-platform webhook servers with Express.js ### Configuration & Documentation - Updated `.env.example` with all platform options - Comprehensive setup guides for each platform - Testing guide with ngrok instructions - Updated README with multi-platform support ### Developer Experience - npm scripts for easy platform startup - Unified webhook launcher (`start-all-webhooks.js`) - Individual platform launchers - Enhanced error handling and logging ## 🔧 Usage Examples **Telegram:** `/cmd ABC12345 analyze this code` **LINE:** `Token ABC12345 analyze this code` **Email:** Reply to notification emails ## 📋 Backward Compatibility - All existing email functionality preserved - Configuration migration path provided - No breaking changes to existing hooks 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * feat: Complete Telegram Remote Control System with Direct Chat Mode ### 🚀 Major Features Added - **Direct Chat Mode**: Users can chat with Claude naturally without /cmd tokens - **Smart Monitoring**: Intelligent response detection with historical processing - **One-Click Startup**: Complete system management via startup script - **Auto-Notification**: Real-time Claude response detection and Telegram delivery ### 📱 Telegram Integration - `telegram-direct-mode.js`: Direct conversation interface - `telegram-polling.js`: Command polling with token validation - Enhanced notification system with markdown formatting ### 🧠 Smart Monitoring System - `smart-monitor.js`: Advanced response detection with history awareness - `simple-monitor.js`: Lightweight monitoring alternative - `auto-notification-daemon.js`: Background notification service ### 🛠️ System Management - `start-telegram-claude.sh`: Complete service management script - Environment validation and dependency checking - Color-coded status reporting and log management - Process lifecycle management (start/stop/restart/status) ### 📖 Documentation & Testing - `TELEGRAM_CLAUDE_GUIDE.md`: Comprehensive user guide - Complete test suite for all components - Usage examples and troubleshooting guide ### 🔧 Core Improvements - Enhanced `controller-injector.js` with proper Enter key handling - Updated `tmux-monitor.js` with real-time output monitoring - Improved error handling and logging throughout - Session management with automatic cleanup ### 🎯 Key Capabilities - **Seamless Communication**: Direct Telegram ⟷ Claude integration - **Full Automation**: No manual intervention required - **Robust Monitoring**: Never miss Claude responses - **Easy Deployment**: Single script startup and management - **Multi-Modal Support**: Ready for LINE integration expansion ### 📊 System Architecture ``` User → Telegram Bot → Direct Injection → Claude Session → Smart Monitor → Auto Notification → User ``` This completes the transformation from email-based to messaging-app-based remote Claude control, providing a modern, efficient, and user-friendly interface for remote AI interaction. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * docs: Enhance documentation and smart monitoring system - Add comprehensive CLAUDE.md for Claude Code development guidance - Update README.md with multi-platform focus and improved instructions - Enhance smart-monitor.js with auto-approval for tool permissions - Improve start-telegram-claude.sh with better tmux session management - Add auto-approver.js for automated tool permission handling Key improvements: - Multi-platform documentation (Telegram, LINE, Email, Local) - Enhanced troubleshooting and command reference sections - Smart monitoring with historical response detection - Automated tool permission approval workflow - Better tmux integration and session management 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * chore: Add CLAUDE.md to .gitignore and remove from tracking - Add CLAUDE.md to .gitignore under "Claude Code development files" section - Remove CLAUDE.md from git tracking while preserving local file - CLAUDE.md should remain as local development documentation only 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * feat: Complete Telegram integration with multi-channel notifications - ✅ Enhanced Telegram bot with smart buttons for personal/group chats - ✅ Multi-channel notification system (Desktop, Telegram, Email, LINE) - ✅ Smart sound alerts with customizable audio feedback - ✅ Real-time command injection with tmux session management - ✅ Intelligent session detection and conversation content extraction - ✅ Unified README documentation with complete setup guides - 🧹 Clean up legacy files and consolidate documentation - 📱 Add setup scripts for easy Telegram configuration - 🔧 Enhance webhook system with improved error handling 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Update processed messages --------- Co-authored-by: laihenyi <henyi@henyi.org> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: laihenyi <laihenyi@users.noreply.github.com>
2025-08-02 04:21:26 +08:00
#!/bin/bash
# Telegram修复脚本 - 自动重启ngrok和更新webhook
# Fix Telegram Script - Auto restart ngrok and update webhook
set -e
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="$PROJECT_DIR/.env"
echo "🔧 Telegram Remote Control 修复脚本"
echo "📁 项目目录: $PROJECT_DIR"
# 检查.env文件
if [ ! -f "$ENV_FILE" ]; then
echo "❌ .env文件不存在: $ENV_FILE"
exit 1
fi
# 加载环境变量
source "$ENV_FILE"
if [ -z "$TELEGRAM_BOT_TOKEN" ]; then
echo "❌ TELEGRAM_BOT_TOKEN未设置"
exit 1
fi
# 停止旧的ngrok进程
echo "🔄 停止旧的ngrok进程..."
pkill -f "ngrok http" || true
sleep 2
# 启动新的ngrok隧道
echo "🚀 启动ngrok隧道..."
nohup ngrok http 3001 > /dev/null 2>&1 &
sleep 5
# 获取新的ngrok URL
echo "🔍 获取新的ngrok URL..."
NEW_URL=""
for i in {1..10}; do
NEW_URL=$(curl -s http://localhost:4040/api/tunnels | jq -r '.tunnels[0].public_url' 2>/dev/null || echo "")
if [ -n "$NEW_URL" ] && [ "$NEW_URL" != "null" ]; then
break
fi
echo "等待ngrok启动... ($i/10)"
sleep 2
done
if [ -z "$NEW_URL" ] || [ "$NEW_URL" = "null" ]; then
echo "❌ 无法获取ngrok URL"
exit 1
fi
echo "✅ 新的ngrok URL: $NEW_URL"
# 更新.env文件
echo "📝 更新.env文件..."
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
sed -i '' "s|TELEGRAM_WEBHOOK_URL=.*|TELEGRAM_WEBHOOK_URL=$NEW_URL|" "$ENV_FILE"
else
# Linux
sed -i "s|TELEGRAM_WEBHOOK_URL=.*|TELEGRAM_WEBHOOK_URL=$NEW_URL|" "$ENV_FILE"
fi
# 设置新的webhook
echo "🔗 设置Telegram webhook..."
WEBHOOK_URL="$NEW_URL/webhook/telegram"
RESPONSE=$(curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/setWebhook" \
-H "Content-Type: application/json" \
-d "{\"url\": \"$WEBHOOK_URL\", \"allowed_updates\": [\"message\", \"callback_query\"]}")
if echo "$RESPONSE" | grep -q '"ok":true'; then
echo "✅ Webhook设置成功: $WEBHOOK_URL"
else
echo "❌ Webhook设置失败: $RESPONSE"
exit 1
fi
# 验证webhook状态
echo "🔍 验证webhook状态..."
WEBHOOK_INFO=$(curl -s "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/getWebhookInfo")
echo "📊 Webhook信息: $WEBHOOK_INFO"
# 测试健康检查
echo "🏥 测试健康检查..."
HEALTH_RESPONSE=$(curl -s "$NEW_URL/health" || echo "failed")
if echo "$HEALTH_RESPONSE" | grep -q '"status":"ok"'; then
echo "✅ 健康检查通过"
else
echo "⚠️ 健康检查失败请确保webhook服务正在运行"
echo "运行: node start-telegram-webhook.js"
fi
echo ""
echo "🎉 修复完成!"
echo "📱 新的webhook URL: $WEBHOOK_URL"
echo "🧪 发送测试消息..."
# 发送测试消息
# 优先发送到群组,如果没有群组则发送到个人聊天
CHAT_TARGET="$TELEGRAM_GROUP_ID"
if [ -z "$CHAT_TARGET" ]; then
CHAT_TARGET="$TELEGRAM_CHAT_ID"
fi
if [ -n "$CHAT_TARGET" ]; then
curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" \
-H "Content-Type: application/json" \
-d "{\"chat_id\": $CHAT_TARGET, \"text\": \"🎉 Telegram Remote Control已修复并重新配置\\n\\n新的webhook URL: $WEBHOOK_URL\\n\\n现在你可以接收Claude通知了。\"}" > /dev/null
echo "✅ 测试消息已发送到Telegram (Chat ID: $CHAT_TARGET)"
else
echo "⚠️ 未配置Telegram Chat ID或Group ID"
fi
echo ""
echo "🔥 下一步:"
echo "1⃣ 确保webhook服务正在运行: node start-telegram-webhook.js"
echo "2⃣ 在tmux中设置Claude hooks: export CLAUDE_HOOKS_CONFIG=$PROJECT_DIR/claude-hooks.json"
echo "3⃣ 启动Claude: claude"