Setup
1. Create a Bot
Open Telegram and message @BotFather:- Send
/newbot - Choose a name and username
- Copy the bot token (e.g.,
123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11)
2. Set the Token
3. Run Your Bot
TelegramInterface Parameters
Authentication
str
required
Telegram Bot API token from BotFather.
Receiver Mode
str
default:"polling"
How the bot receives messages.
"polling" for development, "webhook" for production.float
default:"0.5"
Seconds between polling requests (polling mode only).
int
default:"30"
Long-polling timeout in seconds (polling mode only).
Webhook Settings
str
Public HTTPS URL for receiving updates. Required when
mode="webhook".str
default:"/webhook/telegram"
URL path for the webhook endpoint.
int
default:"8443"
Port for the webhook HTTP server.
str
Secret token for verifying webhook requests from Telegram.
Access Control
List[int]
Only accept messages from these Telegram user IDs. All users allowed if not set.
List[int]
Only accept messages from these chat IDs. All chats allowed if not set.
Formatting
str
default:"HTML"
Message formatting:
"HTML", "MarkdownV2", "Markdown", or None for plain text.int
default:"4096"
Telegram’s message character limit. Long responses are split automatically.
Timeouts
float
default:"10.0"
HTTP connection timeout in seconds.
float
default:"60.0"
HTTP request timeout in seconds.
Polling Mode (Development)
Polling is the simplest mode. The bot periodically asks Telegram for new messages. No public URL or HTTPS certificate is needed.Webhook Mode (Production)
Webhooks are more efficient for production. Telegram pushes updates to your server as they arrive — no polling delay.- A publicly accessible HTTPS URL
- Port 443, 80, 88, or 8443
- Valid SSL certificate (use Let’s Encrypt or a reverse proxy)
Media Support
The interface automatically handles Telegram media types:
Media is passed to the agent in the
images, audio, and files parameters, so tools and the model can access them.
Voice Notes
Telegram voice messages are sent as.oga files (OGG Opus). Most LLMs don’t understand raw audio — you need to transcribe voice to text first. Add audio_transcriber=True to your agent:
- Telegram delivers it as an
Audioobject withmime_type="audio/ogg" - The agent’s transcriber converts the audio to text via the Whisper API
- The transcript is injected into the message content
- The model processes the text normally
Sending Media
The agent can return media in its response. Images are sent as photos, and files are sent as documents:Access Control
Restrict who can use the bot:Agent with Tools
Give your Telegram bot capabilities:Complete Production Example
Error Handling
The Telegram interface maps API errors to specific exception types:
When an error occurs during message processing, the configured
error_message is sent to the user, and all on_error hooks are invoked.