Telegram
Deploy your Agent Kernel agents as Telegram bots that can respond to messages in real-time. This integration connects your AI agents directly to Telegram, enabling natural conversations with users through one of the world's most popular messaging platforms.
Overview
The AgentTelegramRequestHandler provides a seamless bridge between your Agent Kernel agents and Telegram. When users message your bot, their messages are automatically routed to your AI agent, which processes them and sends intelligent responses back through Telegram.
How it works:
- User sends a message to your Telegram bot
- Telegram delivers the message to your webhook endpoint
- Agent Kernel verifies and processes the message
- Visual feedback is sent (typing indicator, etc.)
- Agent generates a response and sends it back to the user
The integration handles all the complexity of the Telegram Bot API, including webhook setup, security, message formatting, and session management.
Quick Start
Prerequisites
- Telegram account
- Bot created via @BotFather
- Bot token from BotFather
- Public HTTPS endpoint (use ngrok or pinggy for local development)
1. Create Your Telegram Bot
- Open Telegram and search for @BotFather
- Send
/newbotand follow instructions - Save your bot token (format:
123456789:ABCdefGHIjklMNOpqrsTUVwxyz)
2. Get Your Credentials
- Bot Token: From BotFather
- Webhook Secret: Optional, any secure random string
3. Configure Environment Variables
Set these before starting your application:
export AK_TELEGRAM__BOT_TOKEN="your_bot_token"
export AK_TELEGRAM__WEBHOOK_SECRET="your_secure_random_string" # Optional
export OPENAI_API_KEY="your_openai_api_key"
4. Expose Local Server
Use a tunneling service for webhook delivery:
ngrok:
ngrok http 8000
pinggy:
ssh -p443 -R0:localhost:8000 a.pinggy.io
Copy the HTTPS URL for webhook setup.
5. Configure Telegram Webhook
Set your webhook using curl:
curl -X POST "https://api.telegram.org/bot<YOUR_BOT_TOKEN>/setWebhook" \
-H "Content-Type: application/json" \
-d '{
"url": "https://your-tunnel-url.com/telegram/webhook",
"secret_token": "your_secure_random_string"
}'
Or with Python:
import requests
BOT_TOKEN = "your_bot_token"
WEBHOOK_URL = "https://your-tunnel-url.com/telegram/webhook"
SECRET_TOKEN = "your_secure_random_string"
response = requests.post(
f"https://api.telegram.org/bot{BOT_TOKEN}/setWebhook",
json={
"url": WEBHOOK_URL,
"secret_token": SECRET_TOKEN,
}
)
print(response.json())
Verify webhook:
curl "https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getWebhookInfo"
Features
- Text message and command handling
- AI-powered responses via Agent Kernel
- Typing indicator
- Inline keyboards
- Markdown formatting
- Session management (per chat)
- Automatic message splitting for long responses