Features
- Rich bot commands:
/start,/help,/new,/status,/model,/trust,/undo,/clear,/compact - Media support: Photos, documents, and voice messages (caption processing)
- Markdown formatting: Automatic conversion to Telegram HTML
- Message splitting: Handles messages up to 4,096 characters with intelligent splitting
- File attachments: Send photos and documents from the agent
- User allowlists: Restrict bot access to specific user IDs
- Typing indicator: Shows “typing…” while processing
Prerequisites
Install the Telegram channel dependencies:python-telegram-bot (async native) for bot functionality.
Creating a Telegram Bot
Open Telegram and search for @BotFather, the official bot creation tool.
To register commands in Telegram’s UI, send
/setcommands to BotFather, select your bot, and paste:start - Welcome message
help - List available commands
new - Start a fresh conversation
status - Show session info
model - Show or switch AI model
trust - Trust a directory
undo - Remove last exchange
clear - Clear conversation history
compact - Summarize and compress history
Configuration
Add the Telegram configuration to yourconfig.yaml:
Environment Variables
For better security, use environment variables:User Allowlist
Restrict bot access to specific users:To find your Telegram user ID, send a message to @userinfobot.
allow_from is empty or omitted, anyone can use the bot.
Bot Commands
The Telegram bot registers these commands (defined inchannels/telegram.py:65-75):
/start
Displays a welcome message introducing the bot.
/help
Lists all available commands with descriptions.
/new
Starts a fresh conversation by clearing the session:
/status
Shows current session information (message count, model, etc.):
/model [name]
Shows the current model or switches to a different one:
/trust <path>
Trusts a directory for file operations:
/undo
Removes the last message exchange (user message + agent response):
/clear
Clears the entire conversation history:
/compact
Summarizes and compresses the session history to reduce token usage:
Message Handling
Text Messages
All non-command text messages are forwarded to the agent for processing. The bot shows a “typing…” indicator while the agent generates a response.Photo Messages
When you send a photo, the bot processes the caption:- If caption is provided: Caption text is forwarded to the agent
- If no caption: “[User sent a photo without caption]” is forwarded
type: "photo" and message_id.
Document Messages
When you send a document (PDF, ZIP, etc.), the bot processes the caption:type: "document", file_name, and message_id.
Voice Messages
Metadata includestype: "voice" and duration in seconds.
Sending Files from Agent
The agent can send files back to you using the outbound message system. Files are automatically categorized: Images (.png, .jpg, .jpeg, .gif, .webp): Sent as photos
Other files: Sent as documents
Implementation reference: channels/telegram.py:454-516
Example agent response with file:
Message Formatting
The Telegram channel automatically converts Markdown to Telegram HTML (reference:channels/telegram.py:83-95):
| Markdown | Telegram HTML | Result |
|---|---|---|
**bold** or __bold__ | <b>bold</b> | bold |
*italic* or _italic_ | <i>italic</i> | italic |
~~strike~~ | <s>strike</s> | |
`code` | <code>code</code> | code |
```code block``` | <pre><code>code</code></pre> | Code block |
[text](url) | <a href="url">text</a> | text |
Only
http:// and https:// URLs are converted to links. Other schemes (like javascript:) are ignored for security.Message Length Limits
Telegram enforces a 4,096 character limit per message. Long responses are automatically split on newline boundaries when possible. Implementation:channels/base.py:78-101
Example:
Error Handling
The channel implements graceful error handling: HTML parsing errors: Falls back to plain text if Telegram rejects HTML File not found: Sends error message instead of file Caption too long: Truncates to 1,024 characters (Telegram limit) Unknown commands: Responds with “Unknown command” and suggests/help
Implementation Reference
The Telegram channel is implemented inchannels/telegram.py:
- Bot initialization:
telegram.py:118-142 - Command handlers:
telegram.py:169-277 - Message handlers:
telegram.py:279-383 - Send methods:
telegram.py:427-516 - Markdown conversion:
telegram.py:83-95
Troubleshooting
Bot doesn’t respond
- Check that the token is correct in your configuration
- Verify the bot is enabled:
channels.telegram.enabled: true - Check logs for connection errors
- Ensure you’re not blocked by the
allow_fromlist
”python-telegram-bot not found” error
Install the Telegram channel dependencies:Bot responds to everyone (security concern)
Add a user allowlist:Formatting looks wrong
The bot converts Markdown to Telegram HTML automatically. If you see raw HTML tags, there may be a parsing error. Check logs for details.Next Steps
Message Bus
Learn how channels integrate with the message bus
Discord Setup
Set up a Discord bot for your server