Home · Blog · How to create a Telegram bot
How to create a Telegram bot
The Telegram bot consists of two halves. The first is the bot’s account in Telegram itself: it is issued by @BotFather, and it is the same for everyone - whether for a bank or a beauty salon. The second is the logic of responses: someone must receive messages and decide what to respond. Previously, the second half was written in Python and kept on the server; in the constructor it is assembled from blocks. Below is the entire path without skipping: from the /newbot command to checking on a live phone, including what usually remains behind the scenes - the design of the bot, commands in the menu and what to do if the token has leaked.
Two halves of a bot: what Telegram does and what the constructor does
When a person writes to your bot, Telegram does not respond itself. It delivers the message to where the bot owner has specified it: either your code regularly asks “are there any new messages?” (long polling), or Telegram itself sends each message to a given address - this is called a webhook. CHBT works according to the second scheme: when connecting a channel, it tells Telegram an address like https://app.chbt.io/webhook/telegram/ID_канала with a secret token, and then all messages come to your project. Your own server, domain and certificate are not needed.
This implies a practical separation of responsibilities. Everything related to the “personality” of the bot - name, link, avatar, description, list of commands in the menu - is configured by @BotFather and lives in Telegram. Everything related to behavior - what the bot says after /start, what buttons it shows, where it saves the phone, when it calls a person - is configured in the “flows” section CHBT. It’s difficult to mix it up: if you want to change the avatar, go to BotFather, if the greeting text, go to the script.
One bot account can be connected to only one “brain” at a time: Telegram has one webhook per bot. Therefore, if the bot was previously launched with its own code or in another service, when connecting to CHBT the old connection will be overwritten - more on this below, in the errors.
Step 1. BotFather: /newbot, name and username
Search for Telegram @BotFather — the real one has a blue verification checkmark, there are several fake “botphasers” with similar names, and they exist to steal tokens. Send /newbot. BotFather will ask two things in turn.
Name — what people see in the chat header and in the list of conversations: “Mint Salon”, “Nordwood Support”. Up to 64 characters, any letters and emojis, can be changed later with the command /setname. Do not write the word “bot” in the name - it will be visible on the link.
Username - unique address of the form @myata_salon_bot. The rules are strict: 5–32 characters, only Latin characters, numbers and underscores, must end with bot (case is not important: Bot, _bot). The username cannot be changed after creation - only delete the bot and create a new one. The short options are almost all taken: if BotFather responds “Sorry, this username is already taken”, add a city or sphere: myata_kazan_bot. You can check availability in advance by simply opening t.me/name in the browser.
After this, BotFather will send a message with congratulations and token - a line like 7123456789:AAHx…. This is the bot's password: whoever has it can read all messages and write on behalf of the bot. Don't forward it to work chats, paste it into screenshots, or store it in your phone notes. The only place it should go is the Token field in the Channels section of CHBT. If the message with the token is lost, you can get it again: /mybots → select the bot → “API Token”. Read more about the token in the help center.
Step 2. Design the bot: avatar, description, commands
This step is most often skipped, but in vain: a person sees the design before the first message and decides from it whether to click “Start”. All commands are sent to BotFather, he will ask you to select a bot from the list and send the value.
/setuserpic- avatar. A square picture, a logo on a plain background, is read better than a photograph of the interior. Without an avatar, the bot looks abandoned./setdescription- the text “What can this bot do?”, which is shown on a blank screen before pressing “Start”. Up to 512 characters. Write specifically: “I’ll make an appointment for a manicure or haircut, I’ll answer with prices and address. We work daily from 10 to 21.” This is your first advertising text, and it is free./setabouttext- a short line in the bot profile, up to 120 characters. It is visible when a person opens a bot card or when a link to it is sent./setcommands- list of commands for the “Menu” button to the left of the input field. Format: one command per line,command - description, for examplestart - Start,price - Prices,address - How to get there. Latin, without slash.
Commands from the menu are ordinary text messages of the form /price. In CHBT /start is processed by a separate trigger “Command /start”, and the remaining commands are caught by the “Keyword” trigger in the “exact” mode: enter the keyword /price and direct it to the thread with the price list. So the menu in Telegram and the script in CHBT work in harmony.
Two more settings in /mybots → Bot Settings are worth checking right away. Allow Groups? - if the bot is needed only in private messages, turn it off: they won’t be able to add it to groups where it is useless. Group Privacy - if the bot is still in the group, remember: in privacy mode it only sees commands and messages addressed to it. flows CHBT are designed for personal conversation with the client; group chats are not their task.
Step 3. Connect the bot to CHBT
Register at app.chbt.io - you need an e-mail and password. Create a bot (in CHBT, “Bot” is the project to which channels are connected). Open the section “Channels”: there are four cards there - Telegram, MAX, VKontakte and “Widget for the site”. Click “Connect Telegram”, fill out two fields: “Channel name” (for yourself, for example “Main bot”) and “Token” - the same line from BotFather. Insert it entirely, without spaces around the edges. Click “Connect.”
At this moment, CHBT does two things for you: it verifies the token with a request to Telegram and sets a webhook to your address with a secret key so that no one can slip fake messages to the bot. After a second, a line with the status will appear in the “Connected Channels” table. “Working” - everything is in order; if the “Channel Name” field is left empty, the bot’s username will be substituted. “Error” - below in small text there will be a reason: most often “Unauthorized” (the token is incorrect or incomplete). Fix - delete the channel using the “Delete” button and connect again with the correct token; You cannot edit a token in an existing channel. The “Reinstall webhook” button is needed when the bot was working and then stopped - for example, someone ran old code that reset the webhook.
Next CHBT monitors health channel itself: if Telegram once responds that the token has been revoked (error 401), the status will switch to “Error”, and a letter will be sent to the project owner. You won’t have to wait for complaints from customers.
On the Free plan - one bot and 30 conversations per month, but all channels are included: the same project can be simultaneously connected to MAX and VKontakte, script one.
Step 4. First script from the template
The empty bot responds with silence: we need a script. The fastest way to start is with a template. Open the section “flows”, create a script and in the editor click “From template” - or select a ready-made one on the page templates and click “Use Template”. For the first bot, three are suitable: “Collection of applications” (name → phone → thank you), “Autoresponder FAQ” (keywords about prices and schedule, otherwise operator) and “Customer registration” (service, time by text, phone).
Look at what the template consists of. At the beginning there is the “Command /start” trigger: it fires when the bot is opened for the first time. Next is the “Message” block with a greeting and buttons, followed by “Data collection” with checking the phone format and the “Action” block with a notification to you. Your job at this stage is to replace the texts: company name, real services in buttons, working hours. Do not rewrite the structure on the first evening, first launch it as is.
On the Free plan, the editor opens in the “Steps” - a linear list of blocks from top to bottom, without arrows. On Basic appears “Canvas” - a visual diagram with branches. For flows from the template, the “Steps” are sufficient. Changes are saved automatically; the “History” button opens the version history and allows you to roll back if something is broken.
Two things without which the bot will still be silent. The first is the flow needs to be enabled: on the “Overview” page in the list of flows, opposite each one there is an “On”/”Off” button. A new flow is turned on right away unless another flow that answers /start is already on; a copy made with the "Duplicate" button is always off. Second, add the trigger “Any message” to the end: it will catch those who write “hello” or “how much is it” instead of “Start” and return them to the menu. Without it, such a message will go into emptiness. What to write in the first message so that a person does not close the bot is a separate topic, discussed in the article about bot's welcome message.
Step 5. Check: test chat and live Telegram
The verification takes place in two stages, and the second cannot be skipped.
In the editor. Button “Check script” opens a test chat right next to the diagram: you write as a client, the bot responds according to the script, the buttons are pressed. It’s convenient to catch logic here - the wrong branch, a typo in the text due to an input error, a forgotten block after collecting the phone. Test chat does not require a connected channel: it is conducted on behalf of the “Test” service contact and is not included in the monthly conversation limit.
On Telegram. Open t.me/your_username_bot on your phone and press “Start”. A greeting with buttons should appear within a second or two. Go through the script to the end: click the button, enter the phone number with an error - make sure that the bot asks you back in your text - then the correct one. After that, open your account: correspondence has appeared in “Conversations”, a card with your name from your profile and phone number has appeared in “Contacts”, a letter about a new lead has been sent to your mail (if included in “Account → Notifications”).
Check three more little things that are not visible in the test chat. The Menu button shows commands from /setcommands, and each of them leads to the desired branch. Link with parameter t.me/your_bot?start=test opens the bot - this is how deep links for advertising and QR work, they can be directed to a separate “Deep Link” trigger. And finally, ask one of your colleagues to go through the flow from their account: you, as the owner, already know where to click, and an outsider will find something that is not obvious. Each new interlocutor is one conversation out of thirty per month on the Free plan, so test carefully.
If the token has leaked: /revoke and re-creating the channel
Signs of a leak: the bot is not responding according to your script, the status in “Channels” is suddenly “Error” with text about a webhook, or you simply found a token in a general chat, a public repository, or in a screenshot that you sent to a contractor. In all cases the order is the same.
- From BotFather:
/mybots→ select the bot → “API Token” → “Revoke current token”. Or by team/revoke. The old token stops working instantly, the new one is shown on the screen. The bot is not deleted: the name, username, avatar and subscribers remain. - The channel in CHBT with the old token will go into the “Error” status - the first time you try to send a message, Telegram will respond with 401, and the owner will receive the letter. Go to “Channels”, delete this channel and connect Telegram again with a new token.
- Contacts will be saved: in CHBT they are tied to the project and Telegram ID of the person, and not to the channel. The bot will continue to recognize the same clients, and their cards and tags will remain in place.
- Think about where the token leaked from and close the hole: delete the message from the chat, rewrite the repository history, change the password for your Telegram account if you suspect access to it.
Prevention is short. The token is inserted only into the “Token” field in “Channels” - CHBT stores it in encrypted form and does not show it in the interface after saving. Employees are given access to the bot through the “Access” section with the “Operator” or “Administrator” role, rather than by sending a token. If the contractor set up the bot and left, revoke the token on the same day.
Typical first day mistakes
| What do you see | Cause | What do |
|---|---|---|
| BotFather: «username is already taken» | The address is taken by another bot | Add city or niche to username; check available options via t.me/name |
| In “Channels” the status is “Error: Unauthorized” | Token not copied entirely, with a space, or already revoked | Delete the channel, get the current token in /mybots → API Token, connect again |
| The bot opens, there is silence on “Start” | The script is not enabled or there is no trigger in it /start | “Overview” → list of flows → “On”; check that the first trigger is “Command /start” |
| The bot responded, then stopped; in “Channels” everything is “Working” | The old code or another service intercepted the webhook | Stop the old code; click “Reinstall webhook” |
Here is only what is related to BotFather and the token - that is, with the steps of this article. The remaining reasons for silence - there is no trigger “Any message”, the limit of the Free plan has been exhausted, Group Privacy in groups, empty buttons - are collected in the checklist “Telegram bot does not work”, and in detail, in the format “symptom → check → solution” - in the analysis why is the Telegram bot not responding.
What's next: other channels, operator, leads
Bot in Telegram is the first channel, not the ceiling. The same script can be connected to MAX and VKontakte without modification: in “Channels” you add the MAX token or the VK community key, and the bot responds there with the same blocks. Applications from all channels are collected in one “Contacts”, correspondence - in one “Conversations”. How to get a token in MAX and how it differs from Telegram is discussed in the article how to create a bot in MAX.
The second step after launch is to teach the bot to shut up in time. The “Operator” block transmits the conversation to the person: the message goes to “Conversations”, a notification arrives, you respond from your account, and the client sees the answer in Telegram. Place this block on the “Ask a question” button and create a separate script with the trigger “Keyword” operator → “Operator”: the keyword is triggered even in the middle of a question about the phone, so a person who is not given the number format will not get stuck - enter the hint “or write “operator”” in “Text when error" of the "Data Collection" block.
The third is data. Each phone and email from the “Data Collection” block is already in “Contacts” with the ability to upload a CSV. If your CRM is needed, the “HTTP request” action will send the request there. When there is not enough designer - your own billing, non-standard APIs, bank-level load - they write code. The constructor is enough for messages, buttons, threads, requests and transmission to the operator, and external systems are connected via webhook.
Everything described above takes one evening: half an hour with BotFather, ten minutes in “Channels”, an hour for texts in the template and half an hour for checking from the phone. Create a Telegram bot for free →
Telegram bot constructorHelp: Telegram connectionHelp: BotFather tokenTemplatesWhy does the Telegram bot not respond?