Setup Steps
Follow the Telegram setup guide in the cookbook. Required configuration:TELEGRAM_TOKEN(Bot token from @BotFather)- An ngrok tunnel (for local development) with the webhook pointing to
/telegram/webhook - Optional:
TELEGRAM_WEBHOOK_SECRETfor production webhook validation
Install the Telegram SDK:
pip install 'agno[telegram]'In DMs, each chat gets its own session ID (
tg:{chat_id}). In groups, sessions are scoped by reply thread (tg:{chat_id}:thread:{message_id}), so each conversation thread maintains its own context.Example Usage
Create an agent, expose it with theTelegram interface, and serve via AgentOS:
basic.py
Streaming
Enable streaming to progressively update a Telegram message as tokens arrive, similar to how ChatGPT’s Telegram bot works:streaming.py
stream=True, the bot sends an initial message and edits it every ~1 second as new content arrives. This is supported for Agent only; Team and Workflow fall back to non-streaming.
Team
team.py
Workflow
workflow.py
Core Components
Telegram(interface): Wraps an AgnoAgent,Team, orWorkflowfor Telegram integration via FastAPI.AgentOS.serve: Serves the FastAPI app using Uvicorn.
Telegram Interface
Main entry point for Agno Telegram applications.
Initialization Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
agent | Optional[Agent] | None | Agno Agent instance. |
team | Optional[Team] | None | Agno Team instance. |
workflow | Optional[Workflow] | None | Agno Workflow instance. |
prefix | str | "/telegram" | Custom FastAPI route prefix for the Telegram interface. |
tags | Optional[List[str]] | None | FastAPI route tags for API documentation. Defaults to ["Telegram"] if not provided. |
reply_to_mentions_only | bool | True | When True, bot responds only to @mentions and replies in groups. All DMs are always processed. |
reply_to_bot_messages | bool | True | When True, bot also responds when a user replies to one of the bot’s messages in a group. |
stream | bool | False | When True, progressively edits the Telegram message as content streams in. Agent only. |
start_message | str | "Hello!..." | Custom response for the /start command. |
help_message | str | "Send me..." | Custom response for the /help command. |
error_message | str | "Sorry..." | Custom message sent when an error occurs during processing. |
agent, team, or workflow.
Key Method
| Method | Parameters | Return Type | Description |
|---|---|---|---|
get_router | None | APIRouter | Returns the FastAPI router and attaches endpoints. |
Supported Media
The Telegram interface handles inbound and outbound media: Inbound (user sends to bot):- Text messages
- Photos (with optional caption)
- Stickers
- Voice messages and audio files
- Videos, video notes, and animations
- Documents (any file type)
- Text (auto-chunked at 4096 characters)
- Images (URL, bytes, or base64)
- Audio files
- Videos
- Documents/files
Endpoints
Mounted under the/telegram prefix:
GET /telegram/status
- Health/status check for the interface.
POST /telegram/webhook
- Receives Telegram webhook updates.
- Validates webhook secret token via
X-Telegram-Bot-Api-Secret-Tokenheader; bypassed whenAPP_ENV=development. - Processes text, photo, sticker, voice, audio, video, and document messages.
- Sends replies back to the originating chat (splits messages over 4096 characters).
- In groups, replies are threaded to the original message.
- Responses:
200 {"status": "processing"}or{"status": "ignored"},403invalid token,500errors.
Testing the Integration
- Create a bot via @BotFather and get the token
- Set
TELEGRAM_TOKENandAPP_ENV=development - Run the app:
python basic.py - Start ngrok:
ngrok http 7777 - Set the webhook:
curl "https://api.telegram.org/bot<TOKEN>/setWebhook?url=https://<NGROK_URL>/telegram/webhook" - Message the bot on Telegram
Troubleshooting
- Verify
TELEGRAM_TOKENis set correctly - Check that
APP_ENV=developmentis set for local testing (bypasses webhook secret validation) - Confirm the ngrok URL is correct and the webhook is set (
/getWebhookInfo) - For groups, ensure the bot has been added to the group and is @mentioned
- Review application logs for download failures or API errors