Telegram Bot Integration
Build an AI-powered Telegram bot using Antbase and Python. Messages are routed to the optimal model automatically, with failover across providers.
What this enables
- AI chatbot in Telegram with intelligent model routing
- Conversation memory per user with message history
- Support for text, commands, and inline queries
- Automatic failover -- your bot never goes offline due to a single provider
Quick Start
1. Create a Telegram bot
Message @BotFather on Telegram, send /newbot, and save the token.
2. Install dependencies
pip install python-telegram-bot openai3. Create the bot
# bot.py
import os
from openai import OpenAI
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
client = OpenAI(
api_key=os.environ["ANTBASE_API_KEY"],
base_url="https://antbase.ai/v1",
)
# Store conversation history per user
conversations: dict[int, list] = {}
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update.effective_user.id
conversations[user_id] = []
await update.message.reply_text("Hi! I'm an AI assistant. Send me a message.")
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update.effective_user.id
user_message = update.message.text
if user_id not in conversations:
conversations[user_id] = []
conversations[user_id].append({"role": "user", "content": user_message})
# Keep last 20 messages for context
history = conversations[user_id][-20:]
response = client.chat.completions.create(
model="ant:auto",
messages=[
{"role": "system", "content": "You are a helpful Telegram bot. Keep responses concise."},
*history,
],
max_tokens=1024,
)
reply = response.choices[0].message.content
conversations[user_id].append({"role": "assistant", "content": reply})
await update.message.reply_text(reply)
async def reset(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update.effective_user.id
conversations[user_id] = []
await update.message.reply_text("Conversation cleared.")
app = Application.builder().token(os.environ["TELEGRAM_BOT_TOKEN"]).build()
app.add_handler(CommandHandler("start", start))
app.add_handler(CommandHandler("reset", reset))
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
print("Bot is running...")
app.run_polling()4. Run it
export ANTBASE_API_KEY=your-ant-api-key
export TELEGRAM_BOT_TOKEN=your-telegram-bot-token
python bot.pyConfiguration
Customize the bot behavior by changing the model and system prompt:
# Use ant:fast for quick responses
response = client.chat.completions.create(
model="ant:fast",
messages=[
{"role": "system", "content": "You are a concise assistant. Max 2 sentences."},
*history,
],
)
# Use ant:reasoning for complex questions
response = client.chat.completions.create(
model="ant:reasoning",
messages=[
{"role": "system", "content": "Think step by step to answer questions."},
*history,
],
)Virtual Models
| Model | Best for | Notes |
|---|---|---|
| ant:auto | General chat | Best default for Telegram bots |
| ant:fast | Quick replies | Snappy responses for casual chat |
| ant:best | Complex queries | Frontier models for detailed answers |
| ant:free | Development | Zero cost while building |
| ant:reasoning | Problem solving | Chain-of-thought for complex questions |
| ant:coder | Code help | Code-optimized for developer bots |
See Virtual Models for the full list.
Troubleshooting
Bot is slow to respond
Use ant:fast for quicker responses. Also consider sending a "typing" indicator with await update.message.chat.send_action("typing") before the API call.
Messages too long for Telegram
Telegram limits messages to 4096 characters. Set max_tokens to a lower value or split long responses across multiple messages.