Guide · Bots & assistants

An AI Telegram bot in 15 minutes: Python and one API key

Build a Telegram bot with aiogram step by step: the model answers messages, remembers the conversation, and you can estimate usage up front. Python and an llmbox key.

15 min time
Python 3.11 stack
~40 lines of code
Beginner level

An assistant bot in Telegram is one of the classic first projects with AI models. It answers questions, shortens texts, translates and summarizes. In this guide we build such a bot in Python with the aiogram library and connect a model through llmbox. The whole code is about forty lines, and building it takes about fifteen minutes.

What you'll need

Before you start, get three things ready:

  • Python 3.11 or newer, plus the aiogram, openai and python-dotenv packages. Install them with pip install aiogram openai python-dotenv.
  • A bot token. The official @BotFather bot issues it: send the /newbot command, pick a name and save the token.
  • An llmbox key. You create it in the Keys section of the cabinet. The steps are described on the Quickstart documentation page.

Choosing a model

An assistant bot usually needs only a fast model with low request costs. DeepSeek V4.1 Flash is a good start: it is the newest DeepSeek model in the catalog, with a 1M-token context window and image input. The vendor's other models are collected on the DeepSeek page. For a comparison with another vendor's model, look at Google's Gemini 3.8 Flash. Hard questions can go to a more powerful model by changing one line of code.

The cards below show the current per-million-token rates from the catalog. Clicking a card opens the model page with details and examples.

Key and API address

The bot works through the official OpenAI SDK with the API address https://llmbox.cc/v1. Save your llmbox key and the bot token in a .env file next to the code. Don't add this file to the repository: the code reads it through python-dotenv.

.env
BOT_TOKEN=123456:ABC
LLMBOX_API_KEY=sk-your-key

The bot code

The bot consists of one message handler. It takes the latest messages of the chat, adds the new question and sends the history to the model. The answer goes back to the chat. Telegram accepts up to 4096 characters in one message, so the bot sends a long answer in parts.

python
import asyncio, os
from collections import defaultdict, deque
from aiogram import Bot, Dispatcher
from aiogram.types import Message
from dotenv import load_dotenv
from openai import AsyncOpenAI

load_dotenv()
client = AsyncOpenAI(base_url="https://llmbox.cc/v1", api_key=os.environ["LLMBOX_API_KEY"])
bot = Bot(os.environ["BOT_TOKEN"])
dp = Dispatcher()
history = defaultdict(lambda: deque(maxlen=10))

@dp.message()
async def answer(msg: Message):
    if not msg.text:
        return
    history[msg.chat.id].append({"role": "user", "content": msg.text})
    r = await client.chat.completions.create(
        model="deepseek/deepseek-v4.1-flash",
        messages=list(history[msg.chat.id]),
    )
    text = r.choices[0].message.content or ""
    history[msg.chat.id].append({"role": "assistant", "content": text})
    for i in range(0, len(text), 4096):
        await msg.answer(text[i:i + 4096])

asyncio.run(dp.start_polling(bot))

Run the file with python bot.py and message the bot in Telegram. The bot sends the model's answer, and its reply to your next question takes the previous messages into account.

The conversation memory here is the last ten messages of the chat, kept in a dictionary. It lasts as long as the process runs. If the bot will have thousands of users, move the history to Redis or a database.

Cost

Usage depends on three things: the number of messages per day, the volume of input tokens including the conversation history, and the length of the answer. The longer the history, the more input tokens in each request, so the ten-message limit also caps usage.

Enter your own numbers in the calculator: it takes the number of messages per day and the size of the request and answer, and estimates monthly usage at the model's current catalog rates.

Per month on DeepSeek V4.1 Flash ≈ $2.82 12M in · 6M out

The actual data for each request is visible in the Logs section of the cabinet, so after the first week you can refine your budget from real usage.

Next steps

Give the bot a system message with a role, for example "answer briefly and to the point". Add a command that clears the history. If the bot should answer from your documents, add a knowledge base search. Other API capabilities are described on the OpenAI and ElevenLabs SDKs documentation page.

Questions

Do I need a separate DeepSeek or OpenAI key?

No. One llmbox key works with every model in the catalog, and you pick the model with a string in the request.

How do I switch models?

Change the value of the model parameter to the ID of another catalog model. The rest of the bot's code stays the same.

Can I send images to the bot?

Yes, if the chosen model accepts images as input. You can see this on the model's page in the catalog.

Published November 15, 2026

One key — every model

GPT, Gemini, DeepSeek, Qwen, video and audio. Pay as you go.

Create a key