Skip to main content
Glama
README.md
# Mila Telegram

Run your Claude Code session from Telegram — and keep it running.

![Mila Telegram](docs/preview.png)

**Part of [Mila](https://github.com/shakhruz/mila-agent)** — the kit that turns this into an AI employee. Want it hosted? [Mila Pro](https://t.me/mila_tashkent_bot?start=pro_github_tg).

This is an extended fork of the [official Telegram channel plugin](https://github.com/anthropics/claude-plugins-official) (Apache-2.0). The original connects a Telegram bot to a Claude Code session. This fork adds the things you start needing once you actually work that way every day: teams in group chats, voice notes, and a poller that doesn't lose your messages when the session restarts.

## What this fork adds

**A daemon that outlives the session.** In the original, the Telegram poller lives inside the session process: restart Claude Code and every message sent in the meantime is gone. Here the poller runs as a long-lived daemon that appends events to a journal file, and the session replays them from a cursor on startup. Restart, crash, upgrade — incoming messages wait in the file and arrive when the session comes back.

**Group auto-join with one-tap approval.** Add the bot to a group — the owner gets a card in a private chat (`/channel_join` in the group is a fallback), with a short explanation of each mode, in the owner's language: run the chat, by mention, observer, only me (only the owner can call the bot there), or reject. Every decision — rejections included — is appended to an `auth-log.jsonl` journal, and each connected chat gets a context card in `chats/<id>.md` plus a generated `CHATS.md` registry. No numeric chat IDs, no editing JSON by hand; group messages can never change the allowlist themselves — approval only happens in the owner's private chat.

**Strangers in a group never reach the session.** In a connected group only the owners (by Telegram id) and the group's team reach the Claude session. The team changes only by the owner's press: when a stranger first writes, the owner gets a private card «add to this group's team?» (at most 5 such cards per group an hour), and an added person can be removed with a button under it. The list lives in `team.json` in the receiver's folder; on a plain install `access.json` → `groups.<id>.team` is read too, in Mila Agent it is not (the session can write that file). Anyone else — other members, bots, channels, anonymous admins — goes to `guests.jsonl` (0600) in the receiver's journal folder, gets no typing, no ack and no answer, and their files are not downloaded. Observer mode and «run the chat» apply to the owners and the team. Answering strangers is the job of a separate lane: with `TELEGRAM_GUEST_LANE` (a unix socket, set by Mila Agent) the receiver hands a stranger's message that addresses the bot (mention or reply) to that lane and sends its answer back as a reply in the same chat, text only. The lane itself — no tools, its own OS user, its own API key — lives in the Mila Agent image. Without it, strangers get no answer.

**The archive is Mila Agent's.** With the receiver's own folder (`TELEGRAM_JOURNAL_DIR`, set by Mila Agent) — or an explicit `TELEGRAM_ARCHIVE=1` — the receiver keeps every message per chat and month and tells each group once that it does. A plain install keeps no archive and never posts about one.

**Observer mode.** A group can be connected as an observer: the bot reads every message for context, but only replies when it's actually addressed (@mention or a reply to its own message). Everything else still reaches the session — so the assistant knows what's going on in the chat — but arrives marked `observe_only: "true"` with a note not to reply, and doesn't trigger a typing indicator or ack reaction. The older listen-only mode (bot hears the chat, replies are blocked mechanically) still works for chats already set up that way.

**Co-owners.** With more than one owner on `owners`, a join card goes to all of them, and whoever taps first decides. The rest get a notification ("✅ *name* connected the bot to *chat* — mode: …", or the rejection equivalent) instead of a live card, so two owners can never approve the same request twice; a second tap on an already-decided card gets "Already decided by another owner or expired."

**Interactive buttons.** On the join card only Reject is red — no mode is highlighted as the default; on a tool-permission card Allow is green and Deny is red. The bot's own words come in the owner's Telegram language (English, Russian, Uzbek; English when unknown).

**Voice transcription.** Voice notes are handed to a transcription command of your choice (set `TELEGRAM_VOICE_TRANSCRIBE_CMD`), and the text reaches the assistant along with the audio. Unset means voice notes simply pass through untranscribed — no default, no vendor.

**Reply-quote context.** When someone replies to an earlier message, the quoted text travels with the new one, so the assistant answers about the message you actually pointed at instead of the last thing in the chat.

**Disclosure by default.** `/whoami` answers, in any chat and to anyone, that
they are talking to an AI, what it is connected to, and who is responsible for
it. No access check on that one: a person who finds a bot in their group is
entitled to know what it is, whether or not they are on the allowlist.

Everything from the original plugin still works: pairing, allowlists, mention detection, `reply` / `react` / `edit_message`, photos, attachments, typing indicators.

## Security

Read this before you put it on a machine that matters.

**Messages are untrusted input.** Anything anyone sends the bot reaches your assistant as content. Someone in an allowlisted chat can try to instruct it. Treat the channel as a public door into a session that can read files and run commands.

**Groups require a mention by default.** An approved group is added with `requireMention: true`, so the bot only responds when addressed. Turning that off means every message from the owners and the group's team goes into the session; strangers' messages do not (see above).

**Attachments can go out.** The `reply` tool sends any absolute path the assistant passes it. That is deliberate — it's how you get files out of a session — but combined with the point above it is also an exfiltration path. Your permission settings, not this plugin, are what stand between a message and a file leaving your machine.

**The event journal is a trust boundary.** The daemon writes events to a file and the session replays them; whatever can write that file speaks with the voice of Telegram. The journal and its directory are created `0600`/`0700`, and the bridge replays only an allowlist of two methods so a forged line cannot invoke arbitrary ones. Keep the state directory on a filesystem only you can write.

**Claude Code keeps your token as a sensitive setting** (on macOS, in the Keychain) when you set it with `/plugin configure mila-telegram@mila` (the plugin's `bot_token` option, marked sensitive). Otherwise it comes from `TELEGRAM_BOT_TOKEN` in your shell or from `~/.claude/channels/telegram/.env` at `0600`, as before; the option, when set, wins. Either way it is stripped from error output before anything is logged.

## What it runs, sends and stores

Full privacy policy: [PRIVACY.md](PRIVACY.md).

- **Network at runtime:** only the Telegram Bot API (`https://api.telegram.org`) with your bot token. No telemetry.
- **On start:** Claude Code runs `bun ${CLAUDE_PLUGIN_ROOT}/server.ts` — no wrapper script. The two dependencies
  (`@modelcontextprotocol/sdk`, `grammy`) are installed by Claude Code itself when it installs or updates the plugin, from `bun.lock`
  with exact versions and no install scripts; the plugin does not download anything at runtime.
- **Processes:** `ps` once at start (to find a stale poller of the same bot); your own transcription command, only if you set
  `TELEGRAM_VOICE_TRANSCRIBE_CMD` (it receives the path of a downloaded voice note).
- **Files:** everything under `~/.claude/channels/telegram/` (or `TELEGRAM_STATE_DIR`): `.env` with the bot token (600) if you use it, `access.json`,
  the inbound journal the daemon writes, downloaded attachments. One exception: the sign-in reminder reads `~/state/auth` and keeps
  its own timestamps in `~/state/login-self.json` (a Mila setup convention; without `~/state/auth` it does nothing).
- **Outbound messages:** the assistant writes only to allowlisted chats, through the `reply` tool (see Security). The plugin itself, without
  the model, sends: a pairing code to someone who messages the bot (in pairing mode, the default), an optional static greeting to a
  stranger (`strangerGreeting`), and permission and approval cards to the owner.

## Prerequisites

- [Bun](https://bun.sh) — the MCP server runs on Bun: `curl -fsSL https://bun.sh/install | bash`

## Setup

**1. Create a bot.** Message [@BotFather](https://t.me/BotFather), send `/newbot`, and copy the token it gives you (the whole thing, including the leading digits and colon).

**2. Install the plugin.** From a Claude Code session:

```
/plugin marketplace add shakhruz/mila-telegram
/plugin install mila-telegram@mila
```

**3. Give the server the token.**

```
/telegram:configure 123456789:AAHfiqksKZ8...
```

This writes `TELEGRAM_BOT_TOKEN=...` to `~/.claude/channels/telegram/.env`. You can write that file yourself instead, or set the variable in your shell — the shell wins over the file. To keep the token out of files, use `/plugin configure mila-telegram@mila` instead: Claude Code stores it in your system's credential store, and it wins over both.

> To run several bots on one machine, point `TELEGRAM_STATE_DIR` at a different directory per instance.

**4. Relaunch with the channel flag.** The server won't connect without it:

```sh
claude --dangerously-load-development-channels plugin:mila-telegram@mila
```

Why not plain `--channels`: channels are a research preview in Claude Code, and `--channels` accepts only plugins on Anthropic's approved list — `mila-telegram` is not on it yet, so with `--channels` the channel silently does not register. The development flag lets this one plugin, named in the command, push messages into your session; it does not skip permission prompts and does not load anything else. See [Claude Code — channels](https://code.claude.com/docs/en/channels#research-preview).

**5. Pair.** DM your bot; it replies with a 6-character code. In the session:

```
/telegram:access pair <code>
```

**6. Lock it down.** Pairing exists to capture your ID. Once you're in, switch to an allowlist so strangers stop getting pairing codes:

```
/telegram:access policy allowlist
```

## Running the daemon

The daemon is what makes restarts free. Run `receiver.ts` as a service under your process supervisor of choice — `launchd` on macOS, `systemd` on Linux — with `RECEIVER_DAEMON=1` in its environment:

```sh
RECEIVER_DAEMON=1 bun /path/to/plugin/receiver.ts
```

It writes events to `$TELEGRAM_STATE_DIR/inbound/events.jsonl`. When a session starts and finds the daemon alive, it reads from that journal instead of polling, so the two never fight over the Telegram API. With no daemon running, the session polls directly exactly as the original plugin does — the daemon is optional.

## Configuration

| Variable | Purpose |
| --- | --- |
| `TELEGRAM_BOT_TOKEN` | Bot token from BotFather. Required. |
| `TELEGRAM_STATE_DIR` | State directory. Default `~/.claude/channels/telegram`. |
| `TELEGRAM_VOICE_TRANSCRIBE_CMD` | Command that transcribes a voice file. Unset means no transcription. |
| `RECEIVER_DAEMON` | Set to `1` to run `receiver.ts` as the polling daemon. |

## Access control

See **[ACCESS.md](./ACCESS.md)** for DM policies, groups, mention detection, delivery config, skill commands, and the `access.json` schema.

IDs are numeric user IDs — get yours from [@userinfobot](https://t.me/userinfobot). `ackReaction` only accepts Telegram's fixed emoji whitelist.

## Tools exposed to the assistant

| Tool | Purpose |
| --- | --- |
| `reply` | Send to a chat. Takes `chat_id` + `text`, optionally `thread_id` to answer in a forum topic, `reply_to` for native threading, `files` (absolute paths) for attachments, and `buttons` (rows of `{label, url\|choice, style}`) for inline buttons. Images send as photos, everything else as documents, 50MB each. Long text is chunked. Returns the sent message ID(s). `multi: true` turns the buttons into several options: each shows ☐, a tap ticks it (☑) and unticks it, and one message with every ticked option arrives after «Done». With one choice the first tap decides, the chosen button gets ✅, and a second tap gets a popup «already chosen: …». |
| `react` | Add an emoji reaction by message ID. Telegram's fixed whitelist only (👍 👎 ❤ 🔥 👀 …). |
| `widget` | Send a message with an HTML widget (Bot API 10.4) to a private chat: a small live panel — status, numbers, buttons. The tool checks Telegram's limits before sending (html ≤ 32768 bytes and ≤ 4096 after zlib, data ≤ 1024 bytes, no input fields, no network) and says what to fix — a hint for the author, not a security boundary: the boundary is Telegram's own sandbox. Other people's text must be HTML-escaped before it goes into a widget. A button inside the widget comes back as an inbound `[widget] w_<name>`; a mark no widget of this bot carries arrives marked «not from my widgets». |
| `widget_data` | Change the data of a sent widget in place (`chat_data`, `public_data`) — the widget redraws itself; its HTML cannot be changed. |
| `edit_message` | Edit a message the bot sent. Good for "working…" → result. Own messages only. |
| `pin` | Pin a message (silently by default) or unpin it. In a group the bot needs the admin right to pin. |
| `topic` | Forum topics in a group with Topics on: `list`, `create` (returns `thread_id`), `rename`, `close`, `reopen`. No delete. The bot needs the admin right «Manage topics». |
| `download_attachment` | Fetch a file by `file_id` from an inbound message and return its local path. |

Inbound messages trigger a typing indicator automatically.

## No history, no search

Telegram's Bot API exposes neither. The bot only sees messages as they arrive — there is no way to fetch a chat's past. If the assistant needs earlier context it has to ask you for it. This is a limit of Telegram, not of this plugin, and it is why photos are downloaded eagerly on arrival.

## License and attribution

Apache-2.0. This is a modified fork of the `telegram` channel plugin from [anthropics/claude-plugins-official](https://github.com/anthropics/claude-plugins-official), copyright Anthropic, PBC. See [NOTICE](./NOTICE) for the list of changes.