Skip to main content
The Telegram interface connects your agent to Telegram’s Bot API. It supports both polling (for development) and webhooks (for production), handles photos, voice messages, audio files, and documents, and provides built-in access control.

Setup

1. Create a Bot

Open Telegram and message @BotFather:
  1. Send /newbot
  2. Choose a name and username
  3. Copy the bot token (e.g., 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11)

2. Set the Token

3. Run Your Bot

Your bot is now live. Open Telegram, find your bot, and start chatting.

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.
Use polling for local development and testing. It works behind NATs and firewalls with no setup.

Webhook Mode (Production)

Webhooks are more efficient for production. Telegram pushes updates to your server as they arrive — no polling delay.
Requirements:
  • A publicly accessible HTTPS URL
  • Port 443, 80, 88, or 8443
  • Valid SSL certificate (use Let’s Encrypt or a reverse proxy)
The webhook_secret is strongly recommended. It prevents unauthorized requests to your webhook endpoint. Telegram sends this token in the X-Telegram-Bot-Api-Secret-Token header.

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:
When a user sends a voice note:
  1. Telegram delivers it as an Audio object with mime_type="audio/ogg"
  2. The agent’s transcriber converts the audio to text via the Whisper API
  3. The transcript is injected into the message content
  4. The model processes the text normally
This works out of the box with no ffmpeg or format conversion needed — the Whisper API accepts OGG natively. Format normalization (OGA → WAV) is only needed when sending audio as input_audio blocks to models, which audio_transcriber bypasses entirely.

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:
Messages from unauthorized users or chats are silently ignored.
To find your Telegram user ID, message @userinfobot.

Agent with Tools

Give your Telegram bot capabilities:
Session state is preserved across messages, so the reminder list persists throughout the conversation.

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.