Build a Telegram AI Bot with TeamoRouter
Give your Telegram bot GPT, Claude, Gemini or DeepSeek with one TeamoRouter API key. The bot code stays the same; only the base URL, key and model ID change. TeamoRouter also answers from servers where api.openai.com returns 403 unsupported_country_region_territory, for example in Russia.
Checked on 2026-11-08 with aiogram 3.22, grammY 1.46, openai (Python 2.48, Node 7.53) and anthropic (Python 0.125, Node 1.6): requests reach TeamoRouter's /v1/chat/completions and /v1/messages.
1. Before you start
- Create an API key in the TeamoRouter console.
- Create a bot with @BotFather and copy its token.
- Put both in environment variables:
export TEAMOROUTER_API_KEY=sk-teamo-xxxxxx
export BOT_TOKEN=123456:ABC...
2. Python: aiogram 3
pip install aiogram openai
bot.py:
import asyncio
import os
from aiogram import Bot, Dispatcher, F
from aiogram.types import Message
from openai import AsyncOpenAI
ai = AsyncOpenAI(
api_key=os.environ["TEAMOROUTER_API_KEY"],
base_url="https://api.teamorouter.com/v1",
)
MODEL = "gpt-6.1-sol"
dp = Dispatcher()
@dp.message(F.text)
async def reply(message: Message) -> None:
resp = await ai.chat.completions.create(
model=MODEL,
messages=[{"role": "user", "content": message.text}],
)
text = resp.choices[0].message.content or ""
# Telegram limits a message to 4096 characters
for i in range(0, len(text), 4000):
await message.answer(text[i:i + 4000])
async def main() -> None:
await dp.start_polling(Bot(os.environ["BOT_TOKEN"]))
if __name__ == "__main__":
asyncio.run(main())
python bot.py
- Use
AsyncOpenAI, notOpenAI. A synchronous client inside an async handler blocks the whole bot while it waits for the model. - Keep
/v1at the end ofbase_url. - python-telegram-bot works the same way: create the same
AsyncOpenAIclient andawaitit inside yourasync defhandler.
3. Node.js: grammY
npm install grammy openai
bot.mjs:
import { Bot } from "grammy";
import OpenAI from "openai";
const ai = new OpenAI({
apiKey: process.env.TEAMOROUTER_API_KEY,
baseURL: "https://api.teamorouter.com/v1",
});
const bot = new Bot(process.env.BOT_TOKEN);
bot.on("message:text", async (ctx) => {
const resp = await ai.chat.completions.create({
model: "gpt-6.1-sol",
messages: [{ role: "user", content: ctx.message.text }],
});
const text = resp.choices[0].message.content ?? "";
// Telegram limits a message to 4096 characters
for (let i = 0; i < text.length; i += 4000) {
await ctx.reply(text.slice(i, i + 4000));
}
});
bot.start();
node bot.mjs
If you use the Vercel AI SDK instead (grammY's stream plugin examples do), call openai.chat("gpt-6.1-sol") or use createOpenAICompatible: the plain openai("...") form goes to the Responses API, which on TeamoRouter serves GPT models only.
4. Claude: native Anthropic endpoint
Claude model IDs also work in the examples above through the OpenAI format, but prompt caching may not apply there. To use Claude's own format, swap the client:
Python:
from anthropic import AsyncAnthropic
claude = AsyncAnthropic(
api_key=os.environ["TEAMOROUTER_API_KEY"],
base_url="https://api.teamorouter.com", # no /v1: the SDK adds /v1/messages
)
resp = await claude.messages.create(
model="claude-sonnet-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": message.text}],
)
text = resp.content[0].text
Node.js (npm install @anthropic-ai/sdk):
import Anthropic from "@anthropic-ai/sdk";
const claude = new Anthropic({
apiKey: process.env.TEAMOROUTER_API_KEY,
baseURL: "https://api.teamorouter.com", // no /v1: the SDK adds /v1/messages
});
const resp = await claude.messages.create({
model: "claude-sonnet-5-5",
max_tokens: 1024,
messages: [{ role: "user", content: ctx.message.text }],
});
const text = resp.content[0].text;
5. Which model for which bot
| Bot | Model ID |
|---|---|
| General chat assistant | gpt-6.1-sol or claude-sonnet-5-5 |
| Cheap, fast replies in busy groups | gpt-6-luna, claude-haiku-5-5 or deepseek-v4-flash |
| Long documents and hard questions | claude-opus-5-5 or gemini-4-argon |
See current prices in the model list.
6. Telegram limits that break bots
- 4096 characters per message. The examples split long answers. Without that, long replies fail with "message is too long".
- Markdown from the model can be invalid for Telegram. The examples send plain text. If you turn on
parse_mode, catch "can't parse entities" and resend without it. - One turn only. The examples don't remember the conversation. Keep a short history per
chat.id(the last 10–20 messages) and send it inmessages. - Streaming by editing a message hits rate limits (about one message per second per chat, 20 per minute in a group) and returns 429. Send the full answer at once, edit at most every 1–2 seconds, or use
sendMessageDraftin private chats.
7. Common errors
| What you see | Cause | Fix |
|---|---|---|
| 401, API key incomplete | Key copied with missing characters | Copy the full sk-teamo- key again |
| 402 (400 on the OpenAI format), wallet balance is insufficient | Balance is $0 | Top up, from $5 |
| Redirect (301) or an HTML page instead of a reply | base_url / baseURL missing /v1 (OpenAI SDK) |
https://api.teamorouter.com/v1 |
| 404 from the Anthropic SDK | /v1 added to its base URL |
https://api.teamorouter.com, without /v1 |
400 with a Claude ID through the Vercel AI SDK openai("...") |
That form uses the Responses API, GPT only | Use openai.chat("...") or the Anthropic client |
| The bot stops answering everyone while one request runs | Synchronous OpenAI client in an async handler | Use AsyncOpenAI |