本文件記錄透過官方 Channels 插件(研究預覽版,2026/03)將 Claude Code 連接到 Telegram 的實際安裝與整合經驗。
環境:
- OS: WSL2 (Linux 6.6.87.2-microsoft-standard-WSL2)
- Claude Code: v2.1.81
- Model: Claude Opus 4.6 (1M context)
- Runtime: Bun
curl -fsSL https://bun.sh/install | bash
source ~/.bashrc
bun --version在 Claude Code session 內:
/plugin marketplace add anthropics/claude-plugins-official
/plugin install telegram@claude-plugins-official
將 token 直接寫入 .env 檔案:
mkdir -p .claude/channels/telegram
echo "TELEGRAM_BOT_TOKEN=<YOUR_TOKEN>" > .claude/channels/telegram/.env
chmod 600 .claude/channels/telegram/.env警告: 不要將 token 作為指令參數傳遞(如
/telegram:configure <token>)。這會將 token 洩漏到對話歷史中。請直接寫入.env檔案。見 docs/issues.md Issue #2。
claude --channels plugin:telegram@claude-plugins-official或使用專案的 start.sh:
./start.sh-
在 Telegram 上向 Bot 發送任何訊息
-
Bot 回覆一組 6 位配對碼
-
在 Claude Code 終端:
/telegram:access pair <CODE> -
鎖定存取為白名單模式:
/telegram:access policy allowlist
| 方向 | 方式 | 狀態 |
|---|---|---|
| Telegram -> Claude Code | 使用者向 Bot 發送訊息,在 session 中顯示為 <channel> |
已驗證 |
| Claude Code -> Telegram | 使用 mcp__plugin_telegram_telegram__reply 工具加 chat_id |
已驗證 |
| 工具 | 說明 | 已測試 |
|---|---|---|
reply |
發送訊息到 Telegram(支援文字、MarkdownV2、最大 50MB 附件) | 是 |
react |
對訊息添加 emoji 反應 | - |
edit_message |
編輯先前發送的 Bot 訊息 | - |
download_attachment |
下載收到的附件 | - |
使用 reply_to 參數搭配 message_id 來串接回覆到特定訊息:
reply(chat_id="...", text="...", reply_to="13")
已測試的關鍵模式:使用 Telegram 作為人工審核 channel。
流程:
- Claude Code 透過
reply發送審批請求到 Telegram - Session 暫停,等待下一個對話輪次
- 使用者在 Telegram 上回覆
approve或reject - 回覆作為
<channel>訊息進入 session - Claude Code 根據回應繼續執行並回報結果
範例審批請求:
Action: Execute command echo "Hello from approval test"
Environment: Local session
Risk: Low
Please reply:
approve - to proceed
reject - to cancel
特性:
- 「軟等待」— session 等待下一個對話輪次(Telegram 或本地終端輸入)
- 沒有內建逾時機制;session 持續閒置直到收到輸入
- 適合 CI/CD 審批關卡、部署確認等
自動核准、無需使用者確認的指令:
{
"permissions": {
"allow": [
"Bash(npm list:*)",
"Bash(pip list:*)",
"Bash(bun --version)",
"Bash(claude --version)",
"Bash(npm config:*)",
"WebSearch",
"mcp__plugin_telegram_telegram__reply"
]
}
}關鍵:mcp__plugin_telegram_telegram__reply 已加入白名單,讓 Bot 可以隨時回應而不會卡在權限提示。
未加入白名單的指令會在終端觸發審批提示(或可透過上述 Telegram 審批流程處理)。
Telegram App(手機/桌面)
| (Bot API, plugin 主動 outbound polling)
v
Telegram Plugin (Bun subprocess, MCP Server)
| (stdio transport)
v
Claude Code Session(本地,有完整檔案系統存取)
- 不需要 inbound port — 插件以 outbound 方式輪詢 Telegram API
- WSL2 相容 — 不需要防火牆設定
- 不需要外部伺服器 — 一切在本地執行
| 檔案 | 用途 |
|---|---|
start.sh |
啟動腳本(含 --channels 旗標) |
.env.example |
環境變數範本 |
.gitignore |
排除機密、channel 狀態、本地設定 |
.claude/settings.local.json |
權限白名單(gitignored) |
.claude/channels/telegram/.env |
Bot token(gitignored) |
.claude/channels/telegram/access.json |
存取控制與白名單(gitignored) |
docs/telegram/plan.md |
規劃文件 |
docs/telegram/install.md |
本文件(英文版) |
- 離線訊息會遺失 — Bot 僅在 session 運行時接收訊息
- 權限阻塞 — 未加白名單的工具呼叫會阻塞 session 直到在終端核准;請將常用的安全工具加入白名單
- State 目錄 — 設定
TELEGRAM_STATE_DIR為專案級路徑以實現各專案隔離 - Bot API 限制 — 沒有訊息歷史或搜尋功能;僅能看到即時訊息
- MarkdownV2 格式 — 需依 Telegram 規則跳脫特殊字元;使用
format: "text"以純文字避免問題