Article · October 28, 2026
How to build an AI bot for Telegram, step by step
Getting an AI-powered bot to reply in Telegram takes an evening and twenty lines of code. Getting it to sell without embarrassing your company takes a few more steps. Here is the whole path, and when it's easier to hire developers.
What you need
- A Telegram bot token, issued by the official @BotFather bot.
- An API key for an AI model: OpenAI, Anthropic, DeepSeek, or a gateway like OpenRouter that gives access to many models.
- Python 3.10 or newer.
- A server where the bot runs around the clock. The simplest VPS is enough to start.
Step 1. Create the bot
Send @BotFather the /newbot command and choose a name and a username that ends in bot. You'll get a token in reply. It's the key to your bot: don't publish it or paste it into chats.
Step 2. A minimal AI bot
Install the libraries with pip install aiogram openai and save the code below as bot.py. Pass the token and API key through the TELEGRAM_BOT_TOKEN and OPENAI_API_KEY environment variables.
import asyncio
import os
from aiogram import Bot, Dispatcher, F
from aiogram.types import Message
from openai import AsyncOpenAI
bot = Bot(token=os.environ["TELEGRAM_BOT_TOKEN"])
dp = Dispatcher()
llm = AsyncOpenAI() # reads the key from OPENAI_API_KEY
SYSTEM_PROMPT = "You are a consultant for Acme Inc. Answer briefly and only about our services."
@dp.message(F.text)
async def answer(message: Message) -> None:
response = await llm.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": message.text},
],
)
await message.answer(response.choices[0].message.content)
async def main() -> None:
await dp.start_polling(bot)
asyncio.run(main())
Run python bot.py and message the bot. It will reply. But it doesn't know anything about your company yet, doesn't remember previous messages and can't do anything except answer.
Step 3. Give the bot knowledge and rules
The system prompt is the bot's instruction: who it is, what tone it uses, what it never says, when it calls a person. Company knowledge can be added in two ways. A small knowledge base, like a price list and a dozen frequent questions, can go straight into the prompt. A large one, like a hundred-page manual, is split into chunks, and the relevant ones are found by meaning before every answer. That's how our support bot for the Ezha Online service works.
Step 4. Add memory
In the minimal example, the bot sees only the latest message. Store the conversation history in a database, not in the program's memory, or the bot will forget every customer after a restart. And cap the history length: every old message sent to the model costs money.
Step 5. Teach the bot to act
The real value starts when the bot doesn't just answer but does things: creates a CRM request, checks a price, calculates a cost. Models support function calling for this: you describe a function, and the model decides when to call it and with what data. One important rule: the bot must never tell a customer about an action it didn't perform. Our sales bot's instructions spell this out explicitly.
Step 6. Protect it and launch
- Limit the number of messages per person so spam doesn't eat your API budget.
- Keep logs so you can investigate odd answers.
- Run the bot on a server as a service that restarts automatically, for example with systemd.
- Watch your spending in the model provider's dashboard and set a limit there.
When it's easier to hire developers
If the bot is for yourself or an experiment, build it following the steps above. Hiring makes sense once the bot talks to your customers: you need a CRM integration, well-designed scenarios, answers tuned on real conversations, and someone responsible for keeping it running at 3 a.m. See what finished projects look like on the sales bot and ChatGPT bot pages.