Custom Telegram MCP server + TUI auto-responder for running Claude Code as an autonomous Telegram agent
Project description
claude-code-telegrammer
Custom Telegram MCP server + TUI auto-responder for running Claude Code as an autonomous Telegram agent
Documentation ·
pip install claude-code-telegrammer
Problem and Solution
| # | Problem | Solution |
|---|---|---|
| 1 | Hardcoded pathsThe official plugin hardcodes~/.claude/ as its state directory (#851), making it impossible to run multiple bots or customize where access.json lives. |
Configurable state directoryAll state (DB, lock, access config) lives underCLAUDE_CODE_TELEGRAMMER_TELEGRAM_STATE_DIR. Run as many bots as you want, each with its own isolated state. |
| 2 | 409 Conflict crashesNo single-instance guard — multiple sessions polling the same bot get 409 errors and crash each other (#1075). |
PID-based lockAutomatic single-instance enforcement via PID lock file. Second instance detects the conflict and waits instead of crashing. |
| 3 | Zombie CPU consumptionAfter session ends, the plugin process lingers at 100% CPU — requires manual kill (#1146). |
Clean shutdownExits gracefully on stdin close, SIGTERM, or SIGINT. No zombies, no manual cleanup. |
| 4 | Only 3 basic toolsThe official plugin provides just send, get_updates, and set_reaction — no history, no search, no file handling, no message editing. |
10 MCP toolsreply, react, edit_message, get_history, get_unread, mark_read, download_attachment, send_document, search_messages, get_context — everything an autonomous agent needs. |
| 5 | No message persistenceMessages vanish after delivery. No way to search past conversations, track read status, or build context from history. |
SQLite message storeAll messages persisted in WAL-mode SQLite with full-text search, reply threading (reply_to_message_id), read/replied tracking, and attachment metadata. |
| 6 | No access control for groupsBasic allowlist only — no per-group policies, no hot-reload when config changes. |
DM + group policiesAllowlist-based access control with separate DM and group chat policies viaaccess.json, hot-reloaded on file change (mtime-based). |
| 7 | No attachment supportCannot download inbound files or upload documents to chats. |
Full attachment handlingInbound photos, documents, voice, audio, and video are auto-downloaded. Upload local files viasend_document tool. |
| 8 | Sessions stall unattendedClaude Code halts at permission prompts or idle states with no way to recover — the agent just stops working. |
TUI WatchdogPolls GNU Screen buffer, detects TUI state via pattern matching, sends keystrokes to auto-accept prompts and re-engage on idle. Throttled with burst limits. |
Table 1. Eight issues with the official Telegram plugin (as of April 2026) and how claude-code-telegrammer addresses each. These problems make the official plugin unusable for production autonomous agents.
Architecture
-
Custom Telegram MCP Server (
ts/) -- Self-contained Bun + MCP server. 10 tools, SQLite persistence, allowlist access control, attachment handling, reaction support. Incoming messages acknowledged with 📩. Built-in responsiveness policy directs the agent to reply immediately and delegate heavy work to background subagents. -
TUI Watchdog (
lib/) -- Polls a GNU Screen session, detects Claude Code's TUI state via pattern matching, and sends keystrokes to keep the agent running unattended (auto-accepts permission prompts, re-engages on idle). Throttled with burst limits to prevent runaway responses. Orchestration handled by scitex-agent-container.
MCP Tools (10)
| Tool | Description |
|---|---|
reply |
Reply on Telegram. Supports threading (reply_to), auto-marks inbound as read. Inbound reply-to-message references are tracked and forwarded. |
react |
Add an emoji reaction to a message. Inbound reactions (message_reaction) are also delivered as channel notifications. |
edit_message |
Edit a previously sent bot message. |
get_history |
Retrieve message history for a chat from local SQLite. |
get_unread |
List unread inbound messages, optionally filtered by chat_id. |
mark_read |
Mark messages as read by chat_id or message_ids. |
download_attachment |
Download a Telegram file by file_id, returns local path. |
send_document |
Upload a local file to a Telegram chat. |
search_messages |
Text search across stored messages. |
get_context |
Recent conversation formatted as compact text for LLM context. |
Important: Bot Token Exclusivity
This MCP server must be the sole consumer of its configured Telegram bot token. The Telegram Bot API allows only one getUpdates long-polling connection per token.
What happens with duplicate consumers:
| Scenario | Symptom | Detection |
|---|---|---|
| Two pollers start simultaneously | One gets 409, the other wins silently | The loser sees 409 Conflict in logs |
| Two pollers start sequentially | Both appear to work, but only one receives messages | No error — the other poller gets empty responses forever |
| Webhook active + poller | Poller gets nothing | No error — Telegram ignores getUpdates when webhook is set |
Why 409 detection alone is insufficient: The Telegram API does not reliably return 409 for all conflict cases. When two consumers poll sequentially (not overlapping), both connections succeed — one simply receives all messages while the other gets none, with no error. The server performs a timeout=3 preflight check at startup to catch overlapping polls, but this cannot detect the sequential case.
If messages aren't arriving:
- Check if another process is polling the same token:
ps aux | grep telegram-server - Check if a webhook is set:
curl https://api.telegram.org/bot<TOKEN>/getWebhookInfo - Use a separate bot token per component (recommended)
- Or disable the other consumer
Alternative: Webhook mode via scitex-orochi. If you run scitex-orochi, it supports Telegram webhook mode (POST /webhook/telegram/) which eliminates polling conflicts entirely. Telegram pushes updates to a single HTTPS endpoint instead of competing pollers. See SCITEX_OROCHI_TELEGRAM_WEBHOOK_URL in the orochi documentation.
Installation
Prerequisites
- Bun >= 1.0 (for the MCP server)
- GNU Screen (for watchdog, optional)
Install
git clone https://github.com/ywatanabe1989/claude-code-telegrammer.git
cd claude-code-telegrammer/ts && bun install
Quickstart
Get a Telegram Bot Token
- Open Telegram and message @BotFather
- Send
/newbot, then enter a name (e.g.,Claude Code Telegrammer) and a username (e.g.,ClaudeCodeTelegrammerBot) - BotFather replies with your token:
123456789:AAH... - Verify your token works:
curl -s "https://api.telegram.org/bot<YOUR_TOKEN>/getMe" # Should return {"ok":true,"result":{"is_bot":true,...}}
- Open your bot (e.g., t.me/ClaudeCodeTelegrammerBot) and send any message to start a conversation
Register MCP Server with Claude Code
Copy the example and fill in your values (.mcp.json is gitignored):
{
"mcpServers": {
"claude-code-telegrammer": {
"type": "stdio",
"command": "bun",
"args": ["run", "/path/to/claude-code-telegrammer/ts/telegram-server.ts"],
"env": {
"CLAUDE_CODE_TELEGRAMMER_TELEGRAM_BOT_TOKEN": "123456789:AAH...",
"CLAUDE_CODE_TELEGRAMMER_TELEGRAM_ALLOWED_USERS": "YOUR_TELEGRAM_USER_ID",
"CLAUDE_CODE_TELEGRAMMER_TELEGRAM_STATE_DIR": "~/.claude-code-telegrammer"
}
}
}
}
cp .mcp.json.example .mcp.json
# Edit .mcp.json with your token, user ID, and paths
Find your Telegram user ID by messaging @userinfobot.
Run
claude \
--dangerously-skip-permissions \
--dangerously-load-development-channels server:claude-code-telegrammer
You should see Listening for channel messages from: server:claude-code-telegrammer in the Claude Code TUI. Send a message from Telegram to your bot — Claude Code will receive it as a channel notification.
Interfaces
MCP Server -- for AI Agents
Start command:
bun run ts/telegram-server.ts
10 tools exposed via MCP stdio protocol. See MCP Tools above. The server's MCP instructions include a responsiveness policy that directs the agent to acknowledge messages immediately and delegate heavy work to background subagents.
Skills -- for AI Agent Discovery
Skills are bundled at src/claude_code_telegrammer/_skills/claude-code-telegrammer/SKILL.md.
Architecture
User (Telegram)
|
| Bot API (getUpdates long-polling)
v
┌──────────────────────────────────────────────────────────────┐
│ Custom Telegram MCP Server (ts/telegram-server.ts) │
│ Bun + @modelcontextprotocol/sdk │
│ │
│ ┌─────────┐ ┌─────────┐ ┌──────────┐ ┌────────────┐ │
│ │ Poller │ │ Store │ │ Tools │ │ Attachments│ │
│ │ (long │ │ (SQLite │ │ (10 MCP │ │ (download │ │
│ │ poll) │ │ WAL) │ │ tools) │ │ queue) │ │
│ └─────────┘ └─────────┘ └──────────┘ └────────────┘ │
│ ┌─────────┐ ┌─────────┐ ┌──────────┐ │
│ │ Access │ │ Config │ │ Lock │ │
│ │ (allow- │ │ (env │ │ (PID │ │
│ │ list) │ │ vars) │ │ file) │ │
│ └─────────┘ └─────────┘ └──────────┘ │
└──────────────────────┬───────────────────────────────────────┘
│ MCP stdio
v
┌──────────────────────────────────────────────────────────────┐
│ Claude Code (in GNU Screen session) │
│ --mcp-config points to the custom MCP server │
└──────────────────────┬───────────────────────────────────────┘
│ screen buffer
v
┌──────────────────────────────────────────────────────────────┐
│ Watchdog (claude-code-telegrammer-watchdog) │
│ Polls screen buffer every 1.5s │
│ Detects: y/n prompt -> "1", y/y/n -> "2", idle -> cmd │
│ Throttled: burst limit, same-state delay, min interval │
└──────────────────────────────────────────────────────────────┘
State Detection
| State | Pattern | Response |
|---|---|---|
running |
(esc to interrupt), tokens ·, ing... |
No action |
y_n |
1. Yes + 3. No (two-choice prompt) |
Send 1 (accept) |
y_y_n |
2. Yes, and... / 2. Yes, allow... / 2. Yes, don't ask... |
Send 2 (accept all) |
waiting |
Cooking puns (Crafted for, etc.), empty > prompt, idle hints |
Send configurable command |
Response throttling: minimum interval between responses, burst limit (10 in 3s window), same-state delay.
Configuration (Environment Variables)
MCP Server:
| Variable | Required | Default | Description |
|---|---|---|---|
CLAUDE_CODE_TELEGRAMMER_TELEGRAM_BOT_TOKEN |
Yes | -- | Telegram Bot API token |
CLAUDE_CODE_TELEGRAMMER_TELEGRAM_STATE_DIR |
No | ~/.claude-code-telegrammer |
Directory for SQLite DB, access.json, lock file |
CLAUDE_CODE_TELEGRAMMER_TELEGRAM_ALLOWED_USERS |
No | -- | Comma-separated Telegram user IDs for DM allowlist |
CLAUDE_CODE_TELEGRAMMER_TELEGRAM_HOST_NAME |
No | os.hostname() |
Hostname stored with each message |
CLAUDE_CODE_TELEGRAMMER_TELEGRAM_PROJECT |
No | process.cwd() |
Project path stored with each message |
CLAUDE_CODE_TELEGRAMMER_TELEGRAM_AGENT_ID |
No | 'telegram' |
Agent identifier stored with each message |
Watchdog:
| Variable | Default | Description |
|---|---|---|
CLAUDE_CODE_TELEGRAMMER_SESSION |
claude-code-telegrammer |
GNU Screen session name |
CLAUDE_CODE_TELEGRAMMER_WATCHDOG_INTERVAL |
1.5 |
Poll interval in seconds |
CLAUDE_CODE_TELEGRAMMER_RESP_Y_N |
1 |
Response for y/n prompts |
CLAUDE_CODE_TELEGRAMMER_RESP_Y_Y_N |
2 |
Response for y/y/n prompts |
CLAUDE_CODE_TELEGRAMMER_RESP_WAITING |
/speak-and-call |
Response when idle/waiting |
SQLite Schema (v2)
All messages persisted in $CLAUDE_CODE_TELEGRAMMER_TELEGRAM_STATE_DIR/messages.db using WAL mode.
messages table: direction, chat_id, message_id, user_id, username, text, timestamps (telegram_ts, received_at, read_at, replied_at), threading (reply_to_message_id, reply_to_row_id), identity (host, project, agent_id, bot_token_hash), raw_json.
attachments table: message_row_id (FK), kind, file_id, file_name, mime_type, file_size, local_path, downloaded_at.
meta table: key-value store for schema_version, update_offset.
Integration with scitex-agent-container
For YAML-based agent orchestration (screen sessions, watchdog lifecycle, restart policies), see scitex-agent-container.
Access Control
Managed via access.json in $CLAUDE_CODE_TELEGRAMMER_TELEGRAM_STATE_DIR:
{
"dmPolicy": "allowlist",
"allowFrom": ["123456789"],
"groups": {
"-100123456": {
"requireMention": true,
"allowFrom": ["123456789"]
}
}
}
Merged with CLAUDE_CODE_TELEGRAMMER_TELEGRAM_ALLOWED_USERS env var at runtime. Mtime-based caching means edits take effect without restart.
Part of SciTeX
claude-code-telegrammer is part of SciTeX. It provides the Telegram communication layer and TUI watchdog used by scitex-agent-container for autonomous agent operation.
┌─────────────────────────────────────────────────────────┐
│ scitex-orochi — agent definitions, dashboard │
└──────────────────────────┬──────────────────────────────┘
v
┌─────────────────────────────────────────────────────────┐
│ scitex-agent-container — lifecycle, health, restart │
└──────────────────────────┬──────────────────────────────┘
v
┌─────────────────────────────────────────────────────────┐
│ claude-code-telegrammer <-- YOU ARE HERE │
│ MCP server: Telegram API, message DB, 10 tools │
│ Watchdog: TUI auto-response, screen polling │
└─────────────────────────────────────────────────────────┘
References
- Claude Code Channels -- Official documentation for Claude Code's channel system
- Official Telegram Plugin -- The
plugin:telegram@claude-plugins-officialsource code - #851: STATE_DIR not respected -- Hardcoded access.json path
- #1075: 409 Conflict errors -- Multiple instances polling the same bot
- #1146: Zombie CPU consumption -- Runaway process after session ends
- Telegram BotFather -- Create and manage Telegram bots
- Telegram Bot API -- Official Bot API documentation
- MCP Specification -- Model Context Protocol standard
- claude-code-telegrammer Issues -- Bug reports and feature requests
- claude-code-telegrammer Pull Requests -- Contributions
Four Freedoms for Research
- The freedom to run your research anywhere -- your machine, your terms.
- The freedom to study how every step works -- from raw data to final manuscript.
- The freedom to redistribute your workflows, not just your papers.
- The freedom to modify any module and share improvements with the community.
AGPL-3.0 -- because we believe research infrastructure deserves the same freedoms as the software it runs on.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file claude_code_telegrammer-0.5.0.tar.gz.
File metadata
- Download URL: claude_code_telegrammer-0.5.0.tar.gz
- Upload date:
- Size: 525.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d4725af11212fded3c04ef428f64a814ec25c1c37765ccc896ff8b37a3f79ac3
|
|
| MD5 |
db56499985c298ee8a017cac15fb2db7
|
|
| BLAKE2b-256 |
47aab7e069dca6b7971679e038d9fb1fc0075bae16737d714953309a64f5ee8a
|
Provenance
The following attestation bundles were made for claude_code_telegrammer-0.5.0.tar.gz:
Publisher:
publish-pypi.yml on ywatanabe1989/claude-code-telegrammer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
claude_code_telegrammer-0.5.0.tar.gz -
Subject digest:
d4725af11212fded3c04ef428f64a814ec25c1c37765ccc896ff8b37a3f79ac3 - Sigstore transparency entry: 1272472317
- Sigstore integration time:
-
Permalink:
ywatanabe1989/claude-code-telegrammer@4371e99114051d8ec281a2da2ec3263d58638bdc -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/ywatanabe1989
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@4371e99114051d8ec281a2da2ec3263d58638bdc -
Trigger Event:
push
-
Statement type:
File details
Details for the file claude_code_telegrammer-0.5.0-py3-none-any.whl.
File metadata
- Download URL: claude_code_telegrammer-0.5.0-py3-none-any.whl
- Upload date:
- Size: 21.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f1b1498abbd40c6f5b13e7ca3361eadc587f9202d829b75c6be31c362bfd35b
|
|
| MD5 |
963d2a4494fbf8cefdb45bfb06c565e8
|
|
| BLAKE2b-256 |
5999386e5d2c252a92836da064778b06f87e5c2f003bfe65a1cc528e77ec4fab
|
Provenance
The following attestation bundles were made for claude_code_telegrammer-0.5.0-py3-none-any.whl:
Publisher:
publish-pypi.yml on ywatanabe1989/claude-code-telegrammer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
claude_code_telegrammer-0.5.0-py3-none-any.whl -
Subject digest:
6f1b1498abbd40c6f5b13e7ca3361eadc587f9202d829b75c6be31c362bfd35b - Sigstore transparency entry: 1272472320
- Sigstore integration time:
-
Permalink:
ywatanabe1989/claude-code-telegrammer@4371e99114051d8ec281a2da2ec3263d58638bdc -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/ywatanabe1989
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@4371e99114051d8ec281a2da2ec3263d58638bdc -
Trigger Event:
push
-
Statement type: