Skip to main content
The @hitheo/telegram package connects Theo to Telegram as a bot. Users message your bot, Theo handles the response with the full orchestration pipeline, and the adapter replies through the Bot API.
Theo never hosts your bot. You create the bot with @BotFather, hold the token, and deploy this adapter on your infrastructure; Theo only processes the completion traffic the adapter forwards. That keeps every message, webhook URL, and credential under your control.

Prerequisites

  1. A Telegram bot token from @BotFather
  2. A Theo API key (theo_sk_...)

Install

Quick Setup

createTelegramHandler returns an async function that processes a single Telegram webhook update. Wire it into any HTTP framework — Express, Next.js route handlers, Cloudflare Workers, AWS Lambda, etc.

How It Works

  1. User sends a message to your Telegram bot
  2. Telegram POSTs the update to your webhook URL
  3. The adapter verifies the X-Telegram-Bot-Api-Secret-Token header (when secretToken is configured)
  4. The adapter forwards the text (and any photo/document attachments) to POST /api/v1/completions, reusing the conversation_id associated with that chat
  5. Theo processes the request through the full pipeline (classification, skills, agent loop)
  6. The response is sent back to the Telegram chat, split into multiple messages if needed

Handler Config

Built-in Commands

The adapter handles three commands itself (no Theo call): Any other /foo command returns a short “Unknown command” reply.

Attachments

Photos attached to messages (and image documents) are downloaded inside the adapter and forwarded to Theo as base64 image data — the adapter never passes a Telegram file URL to Theo. This keeps the bot token inside your deployment.
Always run the latest @hitheo/telegram release. If you are still on a ≤0.1.3 version, upgrade to ^0.1.4 and rotate the bot token via @BotFather as part of the upgrade.
Voice and audio messages are ignored unless you set enableVoice: true. When enabled, the adapter downloads the voice clip, runs it through Theo speech-to-text, and appends the transcript to the prompt as text. Voice is not sent as an attachment — Theo’s completion endpoint only accepts image attachments.

Setting Up the Webhook

Register your webhook URL with Telegram and include the secret_token:

Conversation Persistence Across Instances

The default ConversationStore is an in-process Map — fine for a single long-running server, wrong for serverless (each invocation is a fresh process). Provide your own store:

Deploying

Deploy as any Node.js HTTP server. For production:
  • Railway / Render / Fly.io — easiest for webhook-based bots
  • Vercel / Cloudflare Workers — serverless (one invocation per message); pair with a remote conversationStore
  • Docker + PM2 — self-hosted
Use theo_sk_test_ keys during development to avoid billing.