Building a Telegram Bot With an AI API (Python)
By Darus S Alfatah · 2025-09-08 · Tutorial · 10 menit
Author note: A tidy Telegram bot splits long replies; users prefer short chunks over a wall of text.
Why Telegram First
Telegram is the fastest channel for an internal AI tool. There is no app review, no design work, and no hosting requirement beyond a small script. You can have a working bot in an afternoon and learn what people actually ask before investing in a real interface.
It is also forgiving. If the bot is slow or occasionally wrong, users tolerate it in a private group far more than they would on a customer-facing channel.
Step-by-Step Setup
- Create a bot with BotFather and copy the token.
- Store the token and API key as environment variables.
- Install the Telegram library and the OpenAI SDK.
- Handle incoming text messages, ignoring empty ones.
- Trim input length before sending it to the model.
- Keep the last eight messages per chat as context.
- Split long answers into chunks under the message limit.
- Add a command to reset the conversation.
Managing History
History is the main cost driver. Store a rolling window per chat and summarise older turns when the window is exceeded. Without this, a busy group will consume more tokens than a small team uses in a month.
Reset history on a schedule as well, so stale assumptions do not persist for weeks.
Message Formatting
Telegram limits message length, so answers must be split at paragraph boundaries rather than mid-word. Escape special characters when using formatted mode, otherwise the message fails to send and the user sees nothing.
Cost and Abuse Control
Public bots attract spam. Set a cooldown per user, cap daily requests, and log usage per chat. For group bots, respond only when mentioned, otherwise the bot answers every message and the bill grows with conversation volume.
Frequently Asked Questions
Polling or webhook? Start with polling; move to webhooks when traffic grows.
How do I store history? A small database table keyed by chat id is enough.
Can it handle images? Yes, if the model supports vision.
How do I stop runaway costs? Daily per-user caps and alerts on spend.
What about privacy? Tell users the bot stores recent messages and offer a reset command.
Conclusion
Telegram bots are the cheapest way to test an AI idea with real users. Keep history short, cap usage, and give people a reset command.
Deployment
Run the bot under a process manager so it restarts after crashes, and log every unhandled error with the chat id. On a small VPS, a single process is enough for dozens of users; scale only when response times degrade.
Keep the token in the environment and rotate it if it ever appears in a screenshot or a public repository. Telegram tokens grant full control of the bot, so a leak means someone else can send messages as you.
Improving Answers Over Time
Read twenty conversations a week and note recurring failures. Usually the cause is a missing instruction rather than a weak model. Add one rule at a time and observe the effect, otherwise you cannot tell what helped.
Finally, keep a changelog of prompt and model changes with dates, so behaviour shifts can be traced to a specific edit.
Example: staggered replies: The bot sends a processing message, then delivers the result in short chunks. The user does not wait in silence.
Frequently Asked Questions
Can beginners follow this tutorial?
Yes. The steps are explained in order with code examples, suitable for beginners and experienced developers alike.
Is BankAI’s endpoint compatible with the library I already use?
Yes, BankAI is 100% OpenAI-compatible, so libraries such as the OpenAI SDK and LangChain work without code changes beyond the Base URL and API key.
Where do I get an API key to follow this tutorial?
You get the API key (and Secret Token) after registering and completing payment in the BankAI dashboard.