Creating a Telegram Bot in Python From Scratch
What you need to start, what a minimal bot looks like, which first commands are worth adding, how to run it, and what to add next in order.
All articles in the guide Telegram-боты · 13
A first bot comes together in an evening. Here is the minimum required and the order in which to grow it.
What you need
- Python, reasonably recent.
- A token from BotFather - see getting the token.
- A library. For Python that is usually aiogram.
- A virtual environment. Not mandatory, but it saves dependency conflicts later.
No server is needed at this stage: the bot reaches out to Telegram servers and requires no inbound connections.
A minimal bot
The shape is the same across libraries:
- Read the token from an environment variable.
- Create the bot object and dispatcher.
- Define a handler - a function that fires on an event and replies.
- Register the handler with the dispatcher.
- Start receiving updates.
All of that fits in a few dozen lines. What to get right from the start:
The token from the environment, not the code. A habit that later saves you a token regeneration.
A handler for the start command. It is the first thing any user presses, and a bot silent on it looks broken.
Async awareness. Modern libraries are asynchronous, and a synchronous call inside a handler hangs the whole bot. Easy to forget on a first bot and slow to diagnose afterwards.
First commands
The minimum useful set:
- Start. A greeting and an explanation of what the bot does. One sentence beats an empty reply.
- Help. What to do when confused.
- Cancel. An exit from any flow. Needed as soon as multi-question scenarios appear.
Register the commands with BotFather: they appear in the menu and users stop asking what the bot can do.
Running it
In development the bot runs on your machine in polling mode: it asks Telegram for new messages itself. Nothing to configure.
A common problem: two running instances on one token. Messages go to whichever grabs them, and the behaviour looks random. If the bot replies every other time, check whether it is also running somewhere else, including a forgotten terminal.
For continuous operation you need a server and autostart so the bot comes back after a reboot or a crash. Covered separately in deployment and operations.
What to add next
An order that avoids rewrites:
- Buttons instead of typed input. It transforms the user experience.
- Splitting into files once there are more than a dozen handlers - see aiogram structure.
- A database, as soon as something must be remembered between messages.
- Conversation state, once questions come in sequence.
- Error handling and logs, before showing it to users.
- Payments, if the bot sells.
What not to do at the start: design everything up front. A first bot is more useful small and running than architecturally complete for features that may never be needed.
The overview is in the Telegram bots guide.
FAQ
What do I need to build my own Telegram bot?
Python installed, a token from BotFather, and a bot library. A minimal working bot is a few dozen lines. Everything else - a database, state, payments - is added later, when it becomes necessary.
Why does my bot start but not reply?
Three causes by frequency: the wrong token, a handler never registered with the dispatcher, and the bot already running elsewhere - two processes on one token conflict and messages go to whichever grabs them first. The last one is especially confusing while debugging.
Do I need a server just to try it?
No, your own computer is enough for development: the bot reaches out to Telegram servers and needs no inbound connections. A server is required once the bot must run continuously rather than while your laptop is open.
- Telegram Bots in Python: A Complete Practical GuideGuide
- Telegram Bot Buttons: Reply and InlineHow a reply keyboard differs from inline buttons, when to use which, how to handle presses, and why acknowledging a callback is mandatory.
- Telegram Bot Builders Versus Code: Which to ChooseWhat bot builders genuinely cover, where they hit a ceiling, how the cost of ownership compares, and how to decide between a builder and custom code.
- Payments in a Telegram Bot: Taking Money Step by StepThe ways to take payment in a bot, how they differ, how to handle payment confirmation, and why idempotency is mandatory here.
Done for you
I will build a Telegram bot for your process
Leads, payments, access delivery, broadcasts and a funnel in one bot, with an admin side for you.
from $300 · 3 to 7 days
"Genuinely a master of his craft. I learned a lot of new and useful things about Telegram outreach. I had burned a lot of money paying intermediaries before this, and after talking to him I will be doing it all myself."