This guide provides comprehensive instructions for setting up the Claude Code Telegram Bot with both CLI and SDK integration modes.
- Python 3.9+ - Download here
- Poetry - Modern Python dependency management
- Telegram Bot Token - Get one from @BotFather
- Claude Authentication - Choose one method below
The bot supports two Claude integration modes. Choose the one that fits your needs:
This method uses the Python SDK for better performance while leveraging your existing Claude CLI authentication.
# 1. Install Claude CLI
# Visit https://claude.ai/code and follow installation instructions
# 2. Authenticate with Claude
claude auth login
# 3. Verify authentication
claude auth status
# Should show: "✓ You are authenticated"
# 4. Configure bot (in step 4 below)
USE_SDK=true
# Leave ANTHROPIC_API_KEY empty - SDK will use CLI credentialsPros:
- Best performance with native async support
- Uses your existing Claude CLI authentication
- Better streaming and error handling
- No need to manage API keys separately
Cons:
- Requires Claude CLI installation
This method uses the Python SDK with a direct API key, bypassing the need for Claude CLI.
# 1. Get your API key from https://console.anthropic.com/
# 2. Configure bot (in step 4 below)
USE_SDK=true
ANTHROPIC_API_KEY=sk-ant-api03-your-key-herePros:
- No Claude CLI installation required
- Direct API integration
- Good performance with async support
Cons:
- Need to manage API keys manually
- API key management and rotation
This method uses the Claude CLI as a subprocess. Use this only if you need compatibility with older setups.
# 1. Install Claude CLI
# Visit https://claude.ai/code and follow installation instructions
# 2. Authenticate with Claude
claude auth login
# 3. Configure bot (in step 4 below)
USE_SDK=false
# ANTHROPIC_API_KEY not needed for CLI modePros:
- Uses official Claude CLI
- Compatible with all CLI features
Cons:
- Slower than SDK integration
- Subprocess overhead
- Less reliable error handling
# Clone the repository
git clone https://github.com/yourusername/claude-code-telegram.git
cd claude-code-telegram
# Install Poetry (if needed)
curl -sSL https://install.python-poetry.org | python3 -
# Install dependencies
make dev# Copy the example configuration
cp .env.example .env
# Edit with your settings
nano .envRequired Configuration:
# Telegram Bot Settings
TELEGRAM_BOT_TOKEN=1234567890:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
TELEGRAM_BOT_USERNAME=your_bot_username
# Security
APPROVED_DIRECTORY=/path/to/your/projects
ALLOWED_USERS=123456789 # Your Telegram user ID
# Claude Integration (choose based on your authentication method above)
USE_SDK=true # true for SDK, false for CLI
ANTHROPIC_API_KEY= # Only needed for Option B aboveTo configure ALLOWED_USERS:
- Message @userinfobot on Telegram
- It will reply with your user ID number
- Add this number to your
ALLOWED_USERSsetting
# Start in debug mode (recommended for first run)
make run-debug
# Or for production
make run- Find your bot on Telegram (search for your bot username)
- Send
/startto begin - Try a simple command like
/pwdor/ls - Test Claude integration with a simple question
| Feature | SDK + CLI Auth | SDK + API Key | CLI Subprocess |
|---|---|---|---|
| Performance | ✅ Best | ✅ Best | ❌ Slower |
| Setup Complexity | 🟡 Medium | ✅ Easy | 🟡 Medium |
| CLI Required | ✅ Yes | ❌ No | ✅ Yes |
| API Key Management | ❌ No | ✅ Yes | ❌ No |
| Streaming Support | ✅ Yes | ✅ Yes | 🟡 Limited |
| Error Handling | ✅ Best | ✅ Best | 🟡 Basic |
# Set this to a specific project directory, not your home directory
APPROVED_DIRECTORY=/Users/yourname/projects
# The bot can only access files within this directory
# This prevents access to sensitive system files# Option 1: Whitelist specific users (recommended)
ALLOWED_USERS=123456789,987654321
# Option 2: Token-based authentication
ENABLE_TOKEN_AUTH=true
AUTH_TOKEN_SECRET=your-secret-key-here # Generate with: openssl rand -hex 32# Prevent abuse with rate limiting
RATE_LIMIT_REQUESTS=10 # Requests per window
RATE_LIMIT_WINDOW=60 # Window in seconds
RATE_LIMIT_BURST=20 # Burst capacity
# Cost-based limiting
CLAUDE_MAX_COST_PER_USER=10.0 # Max cost per user in USDFor development work:
# Development-specific settings
DEBUG=true
DEVELOPMENT_MODE=true
LOG_LEVEL=DEBUG
ENVIRONMENT=development
# More lenient rate limits for testing
RATE_LIMIT_REQUESTS=100
CLAUDE_TIMEOUT_SECONDS=600# Check your bot token
echo $TELEGRAM_BOT_TOKEN
# Verify user ID is correct
# Message @userinfobot to get your ID
# Check bot logs
make run-debugFor SDK + CLI Auth:
# Check CLI authentication
claude auth status
# Should show: "✓ You are authenticated"
# If not, run: claude auth loginFor SDK + API Key:
# Verify API key is set
echo $ANTHROPIC_API_KEY
# Should start with: sk-ant-api03-
# Get a new key from: https://console.anthropic.com/For CLI Mode:
# Check CLI installation
claude --version
# Check authentication
claude auth status
# Test CLI works
claude "Hello, can you help me?"# Check approved directory exists and is accessible
ls -la /path/to/your/projects
# Verify bot process has read/write permissions
# The directory should be owned by the user running the bot# Optimal settings for SDK integration
USE_SDK=true
CLAUDE_TIMEOUT_SECONDS=300
CLAUDE_MAX_TURNS=20# If you must use CLI mode, optimize these settings
USE_SDK=false
CLAUDE_TIMEOUT_SECONDS=450 # Higher timeout for subprocess overhead
CLAUDE_MAX_TURNS=10 # Lower turns to reduce subprocess callsLOG_LEVEL=DEBUG
DEBUG=true
# Run with debug output
make run-debug# Check usage in Telegram
/status
# Monitor logs for cost tracking
tail -f logs/bot.log | grep -i cost# Production configuration
ENVIRONMENT=production
DEBUG=false
LOG_LEVEL=INFO
DEVELOPMENT_MODE=false
# Stricter rate limits
RATE_LIMIT_REQUESTS=5
CLAUDE_MAX_COST_PER_USER=5.0
SESSION_TIMEOUT_HOURS=12
# Enable monitoring
ENABLE_TELEMETRY=true
SENTRY_DSN=https://your-sentry-dsn@sentry.io/project# For production, use a persistent database location
DATABASE_URL=sqlite:///var/lib/claude-telegram/bot.db
# Or use PostgreSQL for high-scale deployments
# DATABASE_URL=postgresql://user:pass@localhost/claude_telegram# Enable token authentication for additional security
ENABLE_TOKEN_AUTH=true
AUTH_TOKEN_SECRET=your-very-secure-secret-key
# Restrict to specific users only
ALLOWED_USERS=123456789,987654321
# Use a restricted project directory
APPROVED_DIRECTORY=/opt/projects- Documentation: Check the main README.md
- Configuration: See configuration.md for all options
- Development: See development.md for development setup
- Issues: Open an issue
- Security: See SECURITY.md for security concerns