#E-commerce website development
How to write a Telegram bot: A step-by-step guide for beginners by Yevhen Kasyanenko
4.9
11

How to write a Telegram bot: A step-by-step guide for beginners by Yevhen Kasyanenko

Many people have wondered how to write their own bot in Telegram, because it allows you to create a personal assistant that responds to customers around the clock, takes orders, and even reminds you of important tasks. It’s like an employee who never sleeps or takes vacations.

How to write a Telegram bot: A step-by-step guide for beginners by Yevhen Kasyanenko

At KISS Software, we know that such an assistant quickly eliminates routine tasks from work and significantly increases company revenue. That’s why today we’re going to explain in simple terms how to write your own bot in Telegram and which language is best suited for this. And to ensure that your chatbot is not just a toy but actually works for you, we will share some tips from Yevhen Kasyanenko, an expert and leader of the KISS team.

Build your Telegram bot from scratch!

A step-by-step guide for beginners — understand the basics and start coding your own bot today.
Get a Consultation

What is the value of Telegram bots?

Before diving into creating chatbots in Telegram, we suggest you understand their value and what they can do:

  • They automate routine tasks. A bot allows you to receive questions about price or delivery times around the clock. That’s why you don’t need a call center with dozens of operators.
  • They simplify sales. The product catalog and the “Pay” button are right in the chat. The customer makes a purchase without leaving the messenger.
  • They entertain and educate. Mini-games or memes successfully hold the audience’s attention and increase loyalty.

“A Telegram bot is a great start for those who are just getting acquainted with automation. At first, it simply says “thank you for your order” to the customer. But after a short time, it is already sending out newsletters, connected to CRM, and able to accept payments,” says Yevhen Kasyanenko.

Where to start and what language is used to write Telegram bots?

Before opening the Python installation guide or searching for someone else’s code, it is important to understand a few things. A Telegram bot is a tool for a specific task. If the goal is vague, you will easily add unnecessary features, spend extra money, and most likely end up with a script that no one uses.

Below is a short checklist on how to write a bot in Telegram, which we at KISS Software go through with each customer during the first phone call.

Defining the task

First and foremost, formulate answers to three questions, and you will already have a mini-technical assignment:

  • What task should the assistant solve? It could be taking orders at night, quick currency exchange rates, or making a doctor’s appointment. A clear description immediately suggests which buttons or commands are needed.
  • What will be needed for convenience? Do you need product images, a “Pay” button, or database access? The longer the list, the more code and testing will be required.
  • Who is your audience? Young people like to tap buttons, while accountants are more accustomed to entering simple commands. This will determine whether you display a keyboard with options or leave the dialogue clean.

“A clear goal is half the battle. The simpler the task, the faster the launch and the lower the cost. A smart bot solves the client’s problem in a minimum number of steps and does not overload the interface with unnecessary functions,”emphasizes Yevhen Kasyanenko.

What language is used to write Telegram bots?

Our expert assures us that it is easiest to start with Python and explains why:

  • There are ready-made command sets (aiogram, python-Telegram-bot). They already know how to send messages and display buttons.
  • The entry threshold is relatively low, and there are many lessons and videos, such as “make a bot in one evening.” This makes life easier for beginners.

“From personal experience, I want to say that it is important to choose something that is comfortable for you to work with. Then you can add a new button in a couple of minutes,” notes our specialist.

If you already have experience, you can work with other languages:

  • JavaScript. Suitable when the site is also made with JavaScript — you write everything in one language.
  • Go or C#. Used when a lot of messages are expected and high speed is required.
  • PHP. Good if your project is already hosted on inexpensive hosting with PHP.

It doesn’t really matter which language you choose, the main thing is that the bot helps the customer quickly and without unnecessary fuss.

Time estimates

  • A simple bot with commands and buttons – 1–2 weeks.
  • Integration with CRM or order database – up to a month.

How to write a chatbot for Telegram – preparing the environment

If you are wondering how to write your own bot in Telegram, you need to take two simple preparatory steps:

  1. Register the bot – register it through a special @BotFather account. This will let Telegram know that it is your official bot and it will issue a secret key token.
  2. Install the necessary programs on your computer – Python itself and one or two libraries so that the assistant can send messages.

By following these steps, you will avoid unexpected errors. Then it’s just a matter of writing the text responses and adding buttons.

Registering a bot via BotFather

To complete the first task and obtain a passport for our future bot:

  • Find @BotFather in the Telegram search. This is the official assistant for creating any other bots.
  • Send him the command /newbot. BotFather will ask for the necessary data step by step.
  • Specify the name and username ending in bot (for example, slice_order_bot).
  • Get a token – a long string of characters. This is a secret identifier through which your code will communicate with the Telegram API.

Yevhen Kasyanenko warns:

“Keep the token secret! If it is leaked, anyone will be able to control the bot. If an error occurs, return to the program and get a new key in a minute!”


Installing and configuring Python

The next step requires creating an environment for writing code. Download the latest version of Python and install it to support all modern libraries.

Next, to write a Telegram chat bot in Python, follow these steps:

  1. Install the latest version of Python from python.org. This way, you will definitely not encounter any errors and will be able to use all the latest features.
  2. Open a terminal in the project folder and create a virtual environment (this ensures that your project’s dependencies will not conflict with globally installed packages) by running the following in the console:
    • python -m venv myenv;
    • for macOS/Linux – source myenv/bin/activate;
    • for Windows – myenv\Scripts\activate.
  3. Install a helper library, for example, aiogram with one line – pip install aiogram.

Thanks to this library, you don’t need to write complex code yourself. It already knows how to communicate with Telegram, so creating a bot is much faster.

Building your own bot is easier than you think!

Telegram bot development can be simple. Follow clear steps and use working examples to avoid getting lost in code
How to Create a Telegram Bot Get a Consultation

Writing a simple chat bot for Telegram: a step-by-step guide

According to Yevhen Kasyanenko's recommendations, before adding payment and CRM, it is useful to practice the Telegram API on the simplest example. So below we will show four steps: from “Hello!” to the weather button and requesting an external service as a simple example.
1
Step 1.
2
Step 2.
3
Step 3.
4
Step 4.
1
Step 1.
2
Step 2.
3
Step 3.
4
Step 4.
Step 1. Minimal “Welcome” bot
If you want to end up with a bot that responds with “Hello!” to the /start command: import asyncio from aiogram import Bot, Dispatcher, types from aiogram.filters import Command API_TOKEN = “YOUR_TOKEN_FROM_BOTFATHER” async def main(): bot = Bot(token=API_TOKEN) dp = Dispatcher() @dp.message(Command(“start”)) async def start_command(msg: types.Message): await msg.answer("Hello! I am your first bot.“) await dp.start_polling(bot) if __name__ == ”__main__": asyncio.run(main()) All that remains is to save the file and launch the assistant. Open a chat with the bot, type /start, and you will see a response. Congratulations, it worked!
Time to deliver
~ 30–60 minutes
Step 2. Add buttons and commands
To make the bot seem less primitive, let's add a menu with one button – “Check the weather”: from aiogram import types from aiogram.utils.keyboard import InlineKeyboardBuilder @dp.message(Command(“menu”)) async def show_menu(msg: types.Message): keyboard = InlineKeyboardBuilder () keyboard.button(text=“Check the weather”, callback_data="get_weather") keyboard.adjust(1) await msg.answer(“Select an action:”, reply_markup=keyboard.as_markup()) The user will press the button, and the bot will ask for the city, then return the temperature via the OpenWeather API.
Time to deliver
~ 1–2 hours
Step 3. Connect an external service
Let's say you want your Telegram bot to show the Bitcoin exchange rate or a list of items in stock. To do this, the assistant refers to an online reference—a special address that provides the latest data. This address is called an API. Code example: import requests def get_rate(): response = requests.get(“https://api.coindesk.com/v1/bpi/currentprice.json”) data = response.json() return data[“bpi”][‘USD’][“rate_float”] That's it. Instead of searching manually, your bot will find the rate itself, get the data, and show the result in a single line. “We recently created a Telegram bot for a broker. The task was to display currency rates in real time. Instead of embedding a reference book, we connected an external API. Now the client types /USD rate and receives up-to-date data in a second,” recalls Yevhen Kasyanenko.
Time to deliver
~ 2–3 hours
Step 4. Test and expand functionality
Before presenting the bot to clients, check its settings: Unpredictable input. What if a person sends “123” instead of a city name? Service issues. How does the bot respond if OpenWeather does not respond? Data storage. If the program collects orders or e-mails, save them in SQLite or PostgreSQL so that the information is not lost.
Time to deliver
~ 1–2 hours

“The first version should be simple but stable. Add new features when you are sure that the basics are done right,”advises our specialist.

If you follow these instructions, you will end up with a basic chatbot in Telegram. It can say hello, show a menu, access an external API, and even joke around. In short, everything to make the user feel at home.

How to write a chatbot in Python and deploy it on the web

Once the welcome script is running reliably on your laptop, it’s time to release the assistant into the real world. The task of deployment sounds scary, but in practice it’s a couple of simple steps: upload the code to the cloud and tell the server what variables it needs to know. Below is a step-by-step plan that we use at KISS Software.

Deploy to a server (Heroku, Render, AWS Lightsail)

It’s time to move the bot to the cloud so that it can respond to people around the clock. The process looks like this:

  • Create an account in the cloud. Choose any service with a free plan — Heroku, Render, or AWS Lightsail will work.
  • Upload the bot files there and specify your token. This will allow the bot to connect to Telegram and other necessary services.
  • Click “Start” and make sure the bot responds in the chat. Now it will be online around the clock, even when your computer is turned off.

“The first launch in the cloud only seems complicated in words. Once you see that the bot is responding, subsequent updates can be done with literally one click,” the expert notes.

It is worth keeping in mind some of the limitations of the free plans for the cloud services mentioned above. For example:

  • With the free Heroku and Render, bots “fall asleep” after 15-30 minutes of inactivity—the customer will have to wait for the bot to “wake up.”
  • There are restrictions on requests, memory, and logs—if the bot grows, you will need to switch to a paid plan (usually starting at $5/month).

Therefore, when launching commercially, it is better to take these nuances into account right away so that the bot works stably, without “thoughtful pauses.”

Security and performance issues

Token protection is very important in this regard. Keep the secret key outside the code – preferably in an .env file or in the server settings. Then, even if the project is posted on GitHub, no one will be able to control your assistant on your behalf.

“One client once sent us a project for refinement. His bot suddenly stopped responding. It turned out that the token was left in the open code on GitHub, and someone simply intercepted it. We helped reissue the key, set up storage via environment variables, and since then, there have been no more such problems,” says Yevhen Kasyanenko.

In addition, response speed is also important. When there are a lot of messages, aiogram processes them in parallel so that the assistant does not freeze. If the audience continues to grow, it is enough to launch another copy of the program on the server, and the bot will easily withstand the influx of users.

Advantages of a professional approach

It is not difficult to write a bot in Telegram that responds with a single phrase, but as soon as there is a need to connect CRM, set up payment acceptance, or secure tokens, the paths of an amateur developer and an experienced team diverge.

At KISS Software, we start by analyzing business processes: we look at where applications are lost or customers wait a long time for a response, and we offer a scenario that immediately addresses the problem areas. Then we connect the necessary services, such as a warehouse, payment system, marketing mailings, etc. We do this in such a way that the next update of a third-party API does not interfere with the assistant’s work. This is important.

“If the bot has not started to bring benefits within 3 months, then you have missed something. At KISS, we don’t let it get to that point,” emphasizes Yevhen Kasyanenko.

Let’s summarize

Here’s how to write a chatbot in Telegram, simply and clearly:

  1. Formulate the task. What should the assistant do first?
  2. Register and get a token from @BotFather.
  3. Install Python and the aiogram library or any other convenient one.
  4. Write the minimum code – the /start command, the menu button.
  5. Connect an external service if you need weather, payment, or analytics.
  6. Send the code to the cloud—Heroku, Render, or AWS. From this point on, the bot will work around the clock.
  7. Test and improve—process errors, collect feedback, add scenarios.

If you need not just a training example, but a reliable assistant that really affects your revenue, we will take care of everything. The KISS team, led by Yevhen Kasyanenko, will develop, test, and support any solution for your goals.

 

Learn more in our technical section “Telegram Bot Development.”

For a free consultation, contact us right now, and we will advise you on how to make your business grow with modern IT solutions!
Get a Consultation

Add your comment

Your email address will not be published. Required fields are marked *

Chat with manager