Telegram Bot Architecture
Status: Production (always-on daemon)
Overview
Section titled “Overview”The Telegram bot is the primary interface for interacting with the agent system. It runs as an always-on daemon on the local machine, using Claude Code’s --channels flag to bridge Telegram messages to Claude sessions.
Architecture
Section titled “Architecture”Telegram (user's phone) │ ▼ (Bot API) │Claude Code Telegram Plugin │ ▼Claude Code Session (tmux) │ Reads: CLAUDE.md, SOUL.md, task logs, state files │ Has access to: all MCP tools, filesystem, terminal │ ▼Agent System (full local machine access)Session Lifecycle
Section titled “Session Lifecycle”- Daemon starts Claude Code with
--channelsflag in tmux session - Sessions rotate every 4 hours to prevent context bloat
- On session start: reads CLAUDE.md, SOUL.md, task logs, health.json, queue/
- Persistent state goes to files, NOT conversation memory
- On session end: graceful handoff, state preserved in filesystem
Daemon Configuration
Section titled “Daemon Configuration”- KeepAlive: true (unconditional restart)
ANTHROPIC_API_KEYmust NOT be set (channels require claude.ai auth)- Sleep fully disabled on host machine
Reply Context Patch
Section titled “Reply Context Patch”The Telegram plugin was patched to add context when users reply to messages:
reply_context— original message text being replied toreply_from— who sent the original messagereply_to_msg_id— message ID of the original
This enables conversational threading in the Telegram interface.
Outage Post-Mortem
Section titled “Outage Post-Mortem”Root Cause
Section titled “Root Cause”During daemon script iteration, old startup scripts exported ANTHROPIC_API_KEY, which causes claude --channels to fail. The daemon crash-looped for 56 hours with no alerting.
Fixes Applied
Section titled “Fixes Applied”- Removed
ANTHROPIC_API_KEYfrom daemon script - Changed KeepAlive from conditional to unconditional
- Built watchdog (runs every 15 min, checks launchd + PID + tmux)
- Built
telegram-notify.shreusable alerting helper - Auto-restart on power failure
Notify Helper
Section titled “Notify Helper”Reusable script for any cron job to send Telegram alerts:
telegram-notify.sh "Your message here"Rate-limited to prevent spam. Used by: watchdog, deadline watcher, grade alerts, cost tracker, morning briefing.