Skip to main content

Claude Code <-> Telegram bridge

Project description

claude-tg

Full Claude Code — in Telegram. Not a chatbot wrapper. The real agentic CLI with tools, MCP servers, and file access, streamed to your phone with live token updates.

pip install claude-tg

Why this exists

Every Telegram bridge for Claude spawns a new process per message and scrapes stdout. That means cold starts, no context between turns, and no way to interact while Claude is thinking.

claude-tg keeps a single persistent Claude Code process alive using the official --input-format stream-json protocol. The same streaming interface the terminal uses internally.

What this gives you:

  • Zero startup delay — process is already running, first token appears instantly
  • Mid-turn messages — send something while Claude is working, it arrives in-context (not queued)
  • Every tool works — Bash, file I/O, MCP servers, web search — exactly like terminal
  • Real streaming — token-by-token updates to Telegram, not periodic polling

~1,500 lines of Python. No framework, no database, no Docker.

Quick start

export TELEGRAM_BOT_TOKEN="..."    # from @BotFather
export TELEGRAM_CHAT_ID="..."      # your Telegram user ID
claude-tg

On first run, the built-in MCP server for file sending is auto-registered.

How it works

Telegram ←→ bot.py (python-telegram-bot)
                ↕
            runner.py (persistent subprocess)
                ↕ stdin/stdout (NDJSON stream-json)
            Claude Code CLI
                ↕
            MCP servers, tools, files

runner.py manages a single long-lived Claude process. Messages go to stdin as NDJSON. Events stream from stdout line by line. When a turn completes, the process stays alive for the next one.

A background reader continuously drains stdout into an asyncio queue, preventing pipe buffer deadlocks during mid-turn injection.

stream.py handles the Telegram side — buffering tokens, splitting long outputs into message chains at ~3800 chars, rate-limiting edits to stay within API limits.

bot.py ties it together: debouncing rapid messages, injecting mid-turn, handling media and voice.

Features

Streaming & persistence

  • Token-level streaming with automatic message chaining (long responses split across messages)
  • Persistent process across all conversation turns
  • Session resume on reconnect
  • Auto-reset after configurable inactivity timeout

Mid-turn interaction

  • Send messages while Claude is working — they're injected via stdin into the running conversation
  • Messages are debounced (0.5s) so rapid typing merges into one
  • Cancel button on every response

Media

  • Photos & documents → saved locally, passed to Claude as file references
  • Voice messages → transcribed via Groq Whisper (free), then sent as text
  • Files from Claude → sent back to Telegram via built-in MCP server

External automation (triggers)

export CLAUDE_TG_TRIGGER_PORT=9357
# Prompt goes through Claude
curl -d "Summarize today's git log" localhost:9357

# Bypass Claude, send directly to Telegram
curl -d "DIRECT:Deploy complete ✅" localhost:9357

Prompts queue if Claude is busy. Localhost only — no auth needed, no exposure.

Use this for cron jobs, monitoring scripts, heartbeats, CI/CD notifications.

Conversation log

All messages (user, assistant, triggers, direct) are persisted to data/conversation_log.jsonl. The built-in MCP tool get_conversation_context lets Claude read recent chat history for context continuity across sessions.

Commands

Command Description
/clear Reset session
/compact Compact conversation context
/cancel Stop current task
/cost Show session spend
/model <name> Switch model on the fly
/restart In-place process restart

Running as a service (VPS)

uv tool install claude-tg
# /etc/systemd/system/claude-tg.service
[Unit]
Description=Claude TG
After=network-online.target

[Service]
Type=simple
EnvironmentFile=/path/to/.env
Environment=PATH=/root/.local/bin:/usr/local/bin:/usr/bin
ExecStart=/root/.local/bin/claude-tg --work-dir /root
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
systemctl enable --now claude-tg

Running as root? Claude Code blocks --dangerously-skip-permissions for root. claude-tg detects this and switches to --allowedTools with auto-discovered MCP servers from ~/.claude.json and .mcp.json.

Configuration

Variable Default Description
TELEGRAM_BOT_TOKEN required Bot token from @BotFather
TELEGRAM_CHAT_ID required Your Telegram user ID
CLAUDE_WORK_DIR cwd Working directory for Claude
CLAUDE_TG_MODEL Override Claude model
CLAUDE_TG_MAX_BUDGET Max budget in USD
CLAUDE_TG_TRIGGER_PORT Localhost port for triggers
CLAUDE_TG_VERBOSE 0 Show tool results in chat
CLAUDE_TG_SESSION_TIMEOUT 3600 Auto-reset after inactivity (sec)
CLAUDE_TG_UPDATE_INTERVAL 2.0 Telegram edit interval (sec)
GROQ_API_KEY Groq key for voice transcription

Requirements

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

claude_tg-0.6.0.tar.gz (21.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

claude_tg-0.6.0-py3-none-any.whl (21.6 kB view details)

Uploaded Python 3

File details

Details for the file claude_tg-0.6.0.tar.gz.

File metadata

  • Download URL: claude_tg-0.6.0.tar.gz
  • Upload date:
  • Size: 21.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for claude_tg-0.6.0.tar.gz
Algorithm Hash digest
SHA256 d96a07c901ef6a35e4f062aefe8d64f66afe7855e5d15d56a46b1cc8c1e9ed53
MD5 a964d724863e1cf4e1f607a1d4391cc3
BLAKE2b-256 d26942a24ce86543e43f838e5f423db6e96967d4899e635c7f78cfc521adbb5e

See more details on using hashes here.

Provenance

The following attestation bundles were made for claude_tg-0.6.0.tar.gz:

Publisher: publish.yml on MerkulovDaniil/claude-tg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file claude_tg-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: claude_tg-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 21.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for claude_tg-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cff59aac1ce7c4db9e57e9fa3e61dc939d3f092666bc75e4ed9c33bef8aa6364
MD5 75ab90121d0b8b6ef0ef2b7f6d49af46
BLAKE2b-256 f360b9115a1b73d0ae328f748f6205d4b20c11adf024809fb29405faecff281b

See more details on using hashes here.

Provenance

The following attestation bundles were made for claude_tg-0.6.0-py3-none-any.whl:

Publisher: publish.yml on MerkulovDaniil/claude-tg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page