Skip to content

About

Mirror local Claude Code sessions into a Telegram group and drive them back from your phone. Pure Python stdlib, no dependencies.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

claude-code-tg-bridge

Mirror your local Claude Code sessions into a single Telegram group and drive them back from your phone. Every session shows up tagged, so you can watch several at once and reply to the exact one you mean.

Pure Python 3 standard library — no pip dependencies. Companion to claude-code-toast.

⚠️ Read the Security section before installing. With bypassPermissions (the default), anyone who can post to your Telegram group can run commands on your machine as you.

What it does

  Claude Code session  ──hooks──▶  bridge daemon  ──▶  Telegram group
   (any number, live)                              ◀──  your replies
  • Mirror: when a session asks for input or finishes a turn, its prompt/answer is posted to the group, tagged like [myproject#4a1c] (folder name + short session id).
  • Reply: reply to a mirrored message — or write #tag your text — and the bridge resumes that session with your text via claude -p --resume. The answer mirrors straight back.
  • Multiple sessions in parallel: each Claude Code window is a distinct tag. Plain text (no reply, no tag) goes to the most recently active session.
  • Commands: /sessions lists what's live, /new <dir> <prompt> starts a fresh session in a directory, /help.

Why not the official Telegram channel plugin?

Claude Code ships an official Telegram channel (research preview). It pushes messages into one paired session and needs Bun. It's great for a single session — use it if that's all you need. This project is different: it mirrors every session at once through hooks and routes replies by tag, using only Python stdlib. One bot token drives one group; one getUpdates poller (two pollers on the same token collide with a 409).

Requirements

  • Python 3.8+ (py --version) — stdlib only
  • Claude Code CLI on your PATH
  • A Telegram bot token (@BotFather → /newbot) and a group chat id
  • Windows is first-class (installer + Scheduled Task). macOS/Linux run the same daemon; see macOS / Linux.

Install (Windows)

irm https://raw.githubusercontent.com/petrosalex/claude-code-tg-bridge/main/install.ps1 | iex

Then:

  1. Create the bot & group. Make a bot with BotFather, create a Telegram group, add the bot to it. Get the group's chat id: add the bot, post a message, open https://api.telegram.org/bot<TOKEN>/getUpdates, read chat.id (group ids are negative).
  2. Fill in the config at ~/.claude/tg-bridge.json: set bot_token and chat_id.
  3. Start the daemon — either enable autostart:
    irm https://raw.githubusercontent.com/petrosalex/claude-code-tg-bridge/main/install.ps1 | iex; # then:
    # re-run the local installer with -Autostart, or start manually:
    pythonw -X utf8 "$env:USERPROFILE\.claude\tg-bridge\bridge.py"
    Installing from a clone lets you pass -Autostart to register a logon Scheduled Task:
    git clone https://github.com/petrosalex/claude-code-tg-bridge.git
    cd claude-code-tg-bridge
    .\install.ps1 -Autostart
    schtasks /Run /TN ClaudeTgBridge
  4. Restart open Claude Code sessions so the hooks load. New turns start mirroring.

The installer copies bridge.py to ~/.claude/tg-bridge/, the hook to ~/.claude/hooks/, and merges three hooks (Stop, UserPromptSubmit, SessionStart) into ~/.claude/settings.json alongside whatever hooks you already have (timestamped backup first, idempotent, validated).

Uninstall

irm https://raw.githubusercontent.com/petrosalex/claude-code-tg-bridge/main/uninstall.ps1 | iex

Removes the hooks (only ours), the daemon files, and the Scheduled Task. Your ~/.claude/tg-bridge.json is left in place.

Configuration (~/.claude/tg-bridge.json)

Key Default Meaning
bot_token — BotFather token (required)
chat_id — Group chat id, quoted string, usually negative (required)
allowed_user_ids [] If non-empty, only these Telegram user ids may drive sessions
port 8765 localhost port the daemon listens on for hook events
permission_mode bypassPermissions Passed to claude -p --permission-mode
exec_timeout 1800 Seconds before a runaway claude -p is killed (process tree)
max_age_min 15 Telegram messages older than this are skipped (backlog after downtime)
claude_cmd claude Executable name/path; auto-resolved with fallbacks
mirror true Set false to disable outbound mirroring (still accept commands)

Security

This tool lets a Telegram chat run code on your computer. Treat it like SSH.

  • bypassPermissions runs tools without asking. A message routed to a session executes with whatever permission_mode you set. The default skips permission prompts — convenient, but it means the group is a remote shell. Use a private group.
  • Lock down senders. Set allowed_user_ids to just your own Telegram user id so a leaked/guessed group can't drive your machine. The daemon already hard-filters to the single configured chat_id and silently drops everything else.
  • The bot token is a credential. It lives only in your local config; it is never committed. Anyone with it can post as your bot.
  • The daemon binds 127.0.0.1 only — hooks reach it locally; nothing is exposed to the network.

How it works

  • Hooks (hooks/tg-post.ps1 / .sh) read the hook JSON on stdin and POST it to 127.0.0.1:<port> with a short timeout, always exit 0, and print nothing — a stopped daemon never slows Claude down. They run async, so they never block a turn.
  • The daemon (bridge.py) runs three things feeding one worker thread (the only writer of state and the only sender to Telegram, so there's no interleaving): an HTTP receiver for hook events, a getUpdates long-poll, and a bounded pool of claude -p executors.
  • Mirroring: Stop parses the tail of the session's JSONL transcript for the last assistant text turn; UserPromptSubmit mirrors the prompt.
  • Routing survives a lost state file: the tag is embedded in every mirrored message, so a reply resolves even if the in-memory message→session map missed.
  • Resume behavior: verified that claude -p --resume <id> (without --fork-session) keeps the same session id, so a TG-driven turn stays the same session and its answer mirrors back through the same hooks. The code also handles the fork case defensively.
  • Prompts are passed to claude on stdin, never as an argv string, so arbitrary Telegram text can't be shell-interpreted (Windows .cmd argument-injection class, CVE-2024-24576) and long/multi-line prompts work.

macOS / Linux

The daemon is cross-platform. Install the hook and wire it yourself:

mkdir -p ~/.claude/tg-bridge ~/.claude/hooks
curl -fsSL https://raw.githubusercontent.com/petrosalex/claude-code-tg-bridge/main/bridge.py -o ~/.claude/tg-bridge/bridge.py
curl -fsSL https://raw.githubusercontent.com/petrosalex/claude-code-tg-bridge/main/hooks/tg-post.sh -o ~/.claude/hooks/tg-post.sh
chmod +x ~/.claude/hooks/tg-post.sh

Add these to ~/.claude/settings.json (alongside existing hooks), then create ~/.claude/tg-bridge.json:

{
  "hooks": {
    "Stop":            [{ "matcher": "", "hooks": [{ "type": "command", "command": "bash ~/.claude/hooks/tg-post.sh", "async": true, "timeout": 10 }] }],
    "UserPromptSubmit":[{ "matcher": "", "hooks": [{ "type": "command", "command": "bash ~/.claude/hooks/tg-post.sh", "async": true, "timeout": 10 }] }],
    "SessionStart":    [{ "matcher": "", "hooks": [{ "type": "command", "command": "bash ~/.claude/hooks/tg-post.sh", "async": true, "timeout": 10 }] }]
  }
}

Run the daemon with python3 -X utf8 ~/.claude/tg-bridge/bridge.py. A sample systemd user unit:

# ~/.config/systemd/user/claude-tg-bridge.service
[Unit]
Description=claude-code-tg-bridge
[Service]
ExecStart=%h/.claude/tg-bridge/venvless python3 -X utf8 %h/.claude/tg-bridge/bridge.py
Restart=on-failure
[Install]
WantedBy=default.target

systemctl --user enable --now claude-tg-bridge (adjust the python3 path).

Limitations (v1)

  • Answers are truncated (~3500 chars, at most 2 messages). Plain text only, no Markdown.
  • No inline buttons, no permission-prompt relay (in -p, prompts can't be answered interactively — set permission_mode instead), no file/image transfer, no progress streaming.
  • One bot, one group, basic groups (no forum topics).
  • Resuming a session that's also open interactively in a terminal right now is not guaranteed by Claude Code; drive a given session from one place at a time.
  • The inbound path (Telegram → claude -p) is covered by unit routing tests plus a real --resume run; the mirror-back and outbound paths are verified live end-to-end.

Credits & License

Sibling of claude-code-toast. Inspired by community bridges like ccgram. MIT.

About

Mirror local Claude Code sessions into a Telegram group and drive them back from your phone. Pure Python stdlib, no dependencies.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages