Skip to content

Repository files navigation

Bot for communicating with AI agents through Telegram

Features

  • Multiple AI backends: Claude Code, OpenCode
  • Switch between agents on the fly (/agent)
  • Model selection with dynamic list (/model)
  • Voice messages (transcription via Groq/OpenAI Whisper)
  • Session history and resume (/sessions)
  • Message update instead of spam (edit in place)
  • Auto-install tools on first use

Quick Start

1. Create Telegram Bot

  1. Message @BotFather on Telegram
  2. Send /newbot and follow prompts
  3. Copy the bot token (looks like 123456789:ABCdef...)

2. Get Your Telegram User ID

  1. Message @userinfobot on Telegram
  2. It will reply with your user ID (a number like 123456789)

3. Run with Docker

Create a docker-compose.yml:

services:
telegram-code:
image: node:20volumes:
# Your project directory
- ./my-project:/workspace:rw# Persist npm global installs (claude, opencode)
- telegram-code-npm:/home/agent/.npm-global# Host configs for git
- ~/.gitconfig:/home/agent/.gitconfig:ro
- ~/.ssh:/home/agent/.ssh:roenvironment:
- HOME=/home/agent
- TELEGRAM_BOT_TOKEN=your-bot-token-here
- ALLOWED_USERS=your-telegram-id
- WORK_DIR=/workspace# Optional: voice transcription
- GROQ_API_KEY=your-groq-key# Optional: for Claude (or set in container)
- ANTHROPIC_API_KEY=your-anthropic-keyworking_dir: /workspacecommand: npx -y telegram-code-botrestart: unless-stoppedvolumes:
telegram-code-npm:
docker compose up -d

Alternative: Run without Docker

Docker is recommended for environment isolation, but you can run directly:

# Prerequisites: Node.js 20+, tmux (for Claude CLI)# Clone and install
git clone https://github.com/anthropics/telegram-code-bot
cd telegram-code-bot
yarn install
# Set environment variablesexport TELEGRAM_BOT_TOKEN=your-bot-token
export ALLOWED_USERS=your-telegram-id
export WORK_DIR=/path/to/your/project
export ANTHROPIC_API_KEY=your-key # for Claude# Run
yarn start

Note: When running without Docker, ensure claude and/or opencode CLI tools are installed and available in PATH.

4. Start Using

  1. Open your bot in Telegram
  2. Send /claude or /opencode to start a session
  3. Send your coding request
  4. Use /stop when done

Supported Agents

AgentCommandDescription
Claude Code/claudeAnthropic's Claude Code CLI (tmux-based)
OpenCode/opencode or /ocOpenCode AI agent (HTTP API)

Switch agents anytime with /agent command.

Commands

CommandDescription
/claudeStart Claude Code session
/opencodeStart OpenCode session
/agentChoose agent (shows buttons)
/modelSwitch model (shows numbered list)
/sessionsShow previous sessions to resume
/stopStop current session
/statusShow current status
/outputShow last 500 lines of output
/cSend Ctrl+C (interrupt)
/ySend "y" (yes)
/nSend "n" (no)
/enterPress Enter
/up / /downArrow keys
/tabTab (autocomplete)
/clearDelete chat messages

Natural Language Start

Start agents by typing phrases like:

  • "claude", "claude fix the bug"
  • "opencode", "opencode add tests"

Environment Variables

VariableRequiredDescription
TELEGRAM_BOT_TOKENYesFrom @BotFather
ALLOWED_USERSYesComma-separated Telegram user IDs
WORK_DIRNoWorking directory (default: /workspace)
DEFAULT_AGENTNoDefault agent: claude or opencode
GROQ_API_KEYNoVoice transcription (free, preferred)
OPENAI_API_KEYNoVoice transcription (fallback)
ANTHROPIC_API_KEYNoFor Claude Code
OPENCODE_URLNoOpenCode server URL (default: http://localhost:4096)
OPENCODE_BINNoCustom opencode binary path

Architecture

Telegram <-> Bot (Node.js) <-> Adapter <-> AI Agent
|
+-> Claude CLI (via tmux)
+-> OpenCode (via HTTP API)

Adapter Pattern

Each AI backend implements the AgentAdapter interface:

  • startSession() / stopSession() - lifecycle
  • sendInput() / sendSignal() - communication
  • setModel() / getAvailableModels() - model selection
  • Events: output, closed, error

Key Files

  • src/bot.ts - Telegram bot, commands, message handling
  • src/adapters/ - Agent adapters (Claude CLI, OpenCode)
  • src/types.ts - AgentAdapter interface
  • src/rateLimiter.ts - Telegram API rate limit handling
  • src/installManager.ts - Auto-install tools

Development

Local Development

yarn install
yarn dev # Uses tsx watch for hot reload

Docker Development

# Restart to apply changes
docker compose restart telegram-code
# Full recreate
docker compose down telegram-code && docker compose up -d telegram-code

Scripts

  • yarn dev - Development with hot reload
  • yarn build - Compile TypeScript
  • yarn start - Run compiled version
  • yarn typecheck - Type check

How It Works

Message Updates

The bot edits messages in place instead of spamming:

  1. User sends input -> new message created for response
  2. AI produces output -> same message is updated
  3. Rapid updates are debounced (1s batching)

Rate Limit Handling

Telegram API rate limits (429) are handled automatically:

  • Waits for retry_after + jitter
  • Retries once, then marks user as rate-limited
  • Skips sends during cooldown period

TUI Filtering (Claude CLI)

Claude CLI TUI elements are stripped from output:

  • Status lines, borders, spinners
  • Tool calls are preserved with status icons ( running, done)

Troubleshooting

Bot not responding:

  • Check TELEGRAM_BOT_TOKEN is correct
  • Verify your user ID is in ALLOWED_USERS
  • Check container logs: docker compose logs telegram-code

Voice messages not working:

  • Set GROQ_API_KEY (free) or OPENAI_API_KEY

Claude not starting:

  • Check ANTHROPIC_API_KEY is set
  • Verify tmux is installed in container

OpenCode not starting:

  • Check if server is running: /status
  • OpenCode auto-starts on first use

License

MIT

About

OpenCode / Claude Code in your Telegram bot or group with threads/topics. Use Telegram like a terminal with tabs.

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages