English | Π ΡΡΡΠΊΠΈΠΉ
Control Claude Code CLI through Telegram. Execute commands, analyze files, and manage your server directly from your phone.
- Full Claude Code Access β Execute any Claude Code command via Telegram
- Session Persistence β Context preserved between messages (stored in PostgreSQL)
- File Support β Upload and analyze files and images
- Safe by Default β Read-only mode unless you explicitly request changes
- Command Cancellation β Cancel long-running operations with
/cancel - Timeout Protection β 5-minute timeout prevents hanging processes
- Access Control β Restrict bot access to specific Telegram user IDs
- Graceful Shutdown β Clean termination of all processes
- Python 3.9+
- PostgreSQL 12+
- Claude Code CLI installed (
npm install -g @anthropic-ai/claude-code) - Telegram Bot Token (from @BotFather)
git clone https://github.com/gmen1057/claude-cli-telegrambot.git
cd claude-cli-telegrambotpython -m venv venv
source venv/bin/activate # Linux/Mac
# or: venv\Scripts\activate # Windowspip install -r requirements.txtcp .env.example .env
# Edit .env with your settingssudo -u postgres psql -c "CREATE DATABASE claude_code_bot;"
sudo -u postgres psql -c "CREATE USER your_user WITH PASSWORD 'your_password';"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE claude_code_bot TO your_user;"python -m bot.main# Configure environment
cp .env.example .env
# Edit .env with your settings
# Run with Docker Compose
docker-compose up -d
# View logs
docker-compose logs -f bot| Variable | Description | Default |
|---|---|---|
TELEGRAM_TOKEN |
Bot token from @BotFather | Required |
ALLOWED_USER_ID |
Comma-separated Telegram user IDs | None (all users) |
DB_NAME |
PostgreSQL database name | claude_code_bot |
DB_USER |
Database username | postgres |
DB_PASSWORD |
Database password | postgres |
DB_HOST |
Database host | localhost |
DB_PORT |
Database port | 5432 |
CLAUDE_CLI_PATH |
Path to Claude CLI binary | /root/.bun/bin/claude |
CLAUDE_TIMEOUT |
Command timeout in seconds | 300 |
DEFAULT_WORKING_DIR |
Default working directory | /root |
Send /start to @userinfobot to get your user ID.
| Command | Description |
|---|---|
/start |
Start the bot and create session |
/reset |
Start new session (clear context) |
/status |
Show current session status |
/context |
Show recent conversation context |
/history |
Show command history |
/cancel |
Cancel running command |
/cd <path> |
Change working directory |
/help |
Show help message |
Commands are executed only when you use trigger keywords:
Russian: Π²ΡΠΏΠΎΠ»Π½ΠΈ, ΡΠ΄Π΅Π»Π°ΠΉ, Π·Π°ΠΏΡΡΡΠΈ, ΠΈΡΠΏΡΠ°Π²Ρ, ΡΠΎΠ·Π΄Π°ΠΉ, ΡΠ΄Π°Π»ΠΈ, ΠΏΠ΅ΡΠ΅Π·Π°ΠΏΡΡΡΠΈ
English: execute, run, do, fix, create, delete, restart
Information only (no execution):
Show me the list of files
What does this script do?
Check nginx status
With execution:
Restart nginx service
Create a new Python script
Fix the error in bot.py
Upload any file to the chat and Claude will analyze it:
- Images β Visual analysis and description
- Code files β Code review and suggestions
- Logs β Error analysis and troubleshooting
- Documents β Content extraction and summary
claude-code-bot/
βββ bot/
β βββ __init__.py
β βββ main.py # Entry point, application setup
β βββ config.py # Configuration from environment
β βββ handlers/
β β βββ __init__.py
β β βββ commands.py # Bot command handlers
β β βββ messages.py # Text message handler
β β βββ files.py # File upload handler
β βββ services/
β β βββ __init__.py
β β βββ claude.py # Claude CLI executor
β β βββ session.py # Session management
β β βββ formatter.py # Telegram HTML formatter
β βββ database/
β βββ __init__.py
β βββ pool.py # PostgreSQL connection pool
βββ user_files/ # Uploaded files storage
βββ requirements.txt
βββ Dockerfile
βββ docker-compose.yml
βββ .env.example
βββ LICENSE
βββ README.md
- Set
ALLOWED_USER_IDto restrict access to specific Telegram user IDs - Multiple IDs can be comma-separated:
123456789,987654321 - Warning: Without this setting, anyone can use your bot!
- Commands run in read-only mode by default
- Explicit trigger keywords required for modifications
- Safety instructions embedded in every prompt
- 5-minute timeout on all Claude operations
- Cancel any time with
/cancel
Create /etc/systemd/system/claude-code-bot.service:
[Unit]
Description=Claude Code Telegram Bot
After=network.target postgresql.service
[Service]
Type=simple
User=root
WorkingDirectory=/path/to/claude-code-bot
Environment=PATH=/root/.bun/bin:/usr/local/bin:/usr/bin:/bin
ExecStart=/path/to/claude-code-bot/venv/bin/python -m bot.main
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.targetEnable and start:
systemctl daemon-reload
systemctl enable claude-code-bot
systemctl start claude-code-botView logs:
journalctl -u claude-code-bot -f- Check logs:
journalctl -u claude-code-bot -f - Verify Claude CLI works:
claude --version - Check database connection:
psql -h localhost -U your_user -d claude_code_bot
- Increase
CLAUDE_TIMEOUTin.env - Simplify your request
- Check if Claude CLI is responding:
claude "Hello"
- Ensure bot runs as user with Claude CLI access
- Check working directory permissions
- Verify file upload directory is writable
- Check PostgreSQL is running:
systemctl status postgresql - Verify credentials in
.env - Check database exists:
psql -l | grep claude_code_bot
The bot uses two tables:
sessions β User session data
CREATE TABLE sessions (
user_id BIGINT PRIMARY KEY,
context TEXT,
working_dir TEXT,
message_count INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);command_logs β Command history
CREATE TABLE command_logs (
id SERIAL PRIMARY KEY,
user_id BIGINT NOT NULL,
command TEXT NOT NULL,
response TEXT,
execution_time_ms INTEGER DEFAULT 0,
error TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
This project is licensed under the MIT License β see the LICENSE file for details.
- Anthropic for Claude and Claude Code
- python-telegram-bot for the excellent Telegram library
- Issues: GitHub Issues
- Discussions: GitHub Discussions