Setup
Follow the Telegram deploy guide to set up your bot. Required configuration:TELEGRAM_TOKEN(bot token from @BotFather)TELEGRAM_WEBHOOK_SECRET_TOKEN— required in production, skipped whenAPP_ENV=development- An ngrok tunnel (for local development) and webhook pointing to
/telegram/webhook
Example Usage
Create an agent, expose it with theTelegram interface, and serve via AgentOS:
basic.py
Parameters
Provideagent, team, or workflow. Call get_router() to get the FastAPI APIRouter with all endpoints attached.
Endpoints
Mounted under the/telegram prefix (customizable via prefix):
GET /telegram/status
Health/status check for the interface. Returns {"status": "available"}.
POST /telegram/webhook
Receives Telegram updates (messages, edited messages).
- Validates the
X-Telegram-Bot-Api-Secret-Tokenheader; bypassed whenAPP_ENV=development. - Deduplicates updates by
update_id. - Processes text, photos, voice notes, audio, video, documents, stickers, and animations.
- Streams or sends responses back to the originating chat (splits long messages at Telegram’s 4096 character limit).
- Responses:
200 {"status": "processing"}or{"status": "ignored"},403invalid secret token,500errors.
Behavior
Session Management
Sessions are scoped by chat and entity. Theentity_id is the agent, team, or workflow ID (falls back to name, then type).
- DMs and basic groups:
tg:Telegram Bot:123456789 - Supergroup threads and forum topics:
tg:Telegram Bot:123456789:42
/new command creates a fresh session. Without a database, sessions reset on server restart. History and memory are not persisted.
The
/new command only works when a database is configured. Without a database, each server restart already starts a fresh session.Streaming
Whenstreaming=True (the default), the bot edits the response message in real time as tokens arrive. Edits are throttled to roughly once per second to stay within Telegram’s rate limits. The user sees incremental output instead of waiting for the full response.
For workflows, streaming also surfaces step progress (e.g., which agent in the workflow is currently running).
Group Chat Support
By default, the bot only responds when mentioned (@your_bot) or replied to in group chats. This is controlled by two parameters:
reply_to_mentions_only=True(default): only respond to @mentions and direct repliesreply_to_bot_messages=True(default): also respond when users reply to the bot’s own messages
reply_to_mentions_only=False.
BotFather privacy mode: By default, Telegram bots in groups only receive messages that mention them or are commands. To let the bot see all group messages (required for reply_to_mentions_only=False), message @BotFather, send /setprivacy, select your bot, and choose Disable.
Media Support
Inbound (user sends to bot): photos, stickers, voice notes, audio files, video, video notes, animations (GIFs), and documents. Media is downloaded and passed to the agent as images, audio, video, or file inputs. Outbound (agent sends to user): images, audio, video, and files generated by agent tools (e.g., DALL-E, ElevenLabs). Media is sent as native Telegram media messages alongside the text response.TelegramTools
TelegramTools is a standalone toolkit that lets agents proactively send messages and media to Telegram chats. It is independent from the Telegram interface. The interface handles inbound webhooks; the toolkit gives agents outbound actions.
Toolkit Parameters
Toolkit Methods
All methods return a JSON string with
{"status": "success", "message_id": ...} on success or {"status": "error", "message": ...} on failure.
For the full toolkit reference, see TelegramTools.
Testing the Integration
- Run the app locally:
python <my-app>.py(ensure ngrok is running) - Register the webhook:
curl "https://api.telegram.org/bot${TELEGRAM_TOKEN}/setWebhook?url=${NGROK_URL}/telegram/webhook" - Open your bot in Telegram and send
/startor any message - In a group: add the bot and mention it with
@your_bot hello