All posts

How to create your own custom Telegram bot with python?

Published on March 27, 2023

In today's world digital socialism is playing a crucial role in our lives. The world is interconnected. You can share your own thoughts and ideas, and this is not something new for you. Human nature – laziness, that's why we, people, love having a ready cup of coffee made for us, dinner at a restaraunt rather than homecooked and etc. Same applies for our digital lives.

🚀 What is Telegram?

Simply put – it's a secure end-to-end encrypted social network. You can chat, call, engage in groups and so on. In 2015, Telegram announced its open and free API to allow developers create their own bots just with HTTP calls.

Eagering to have your own?

☕️ Let's create yours then

In this blog post, I will show you how to create one easily with only a few steps. All you need is a computer that can run Python code, Telegram account and enthusiasm ❤️. Optionally, you can put a coffee on the side, so you can take sips in the process.

Step 1

Clone (download) the Github repo with Telegram Bot starter template

$ git clone https://github.com/nuriddinislamov/telegram-bot-template

Step 2

Once you have the template, you will need to create a bot on Telegram's servers and obtain an API key to interact with the bot. Get your key with BotFather

BotFather is one bot to rule them all. Use it to create new bot accounts and manage your existing bots.

Just type the command /newbot and follow the istructions given by BotFather:

BotFather

Once done, you will receive a message that looks like this:

BotFather 2

Click on the looooong string to copy it automatically and follow the next steps.

Step 3

Open Terminal and change directory to telegram-bot-template. Here you will need to type in some commands into the command line.

Create and activate virtual environment:

$ python3 -m venv venv

$ source venv/bin/activate

Install the requirements:

pip install -r requirements.txt

Step 4

And finally, add the API token you have copied earlier to the file named .env

❗️ Note

Yes, a file without a file name, just (dot)env. More details on this here.

Final touch

Let's run the bot.

$ python3 bot.py

As simple as that. You will see debugging and logging information on your terminal if you have set DEBUG=True as the environment variable OR you will see nothing as the script will be up and running, which means your new bot is alive.

Try sending /start command to the bot, and it will respond with the text like:

Bot start command

Bot is running

Well, that's it. You are now the ruler of the freshly and newly created chatbot on Telegram servers, and I am proud of you.

💙 Thanks

Make sure to hit one of the reaction buttons down below to express your emotions.

If you have any questions or ideas, feel free to leave them in the comment box, I will respond as quickly as I can!

Thanks, take care!

Read more posts

Thanks for reading!

Profile image

I am Nuriddin Islamov, Software Engineer and current CS student at Anglia Ruskin University based in Cambridge, UK.


In my blog I talk about technology updates and student life abroad. Get yourself prepared for book reviews as well.


I am experienced in both fundamental and advanced concepts of computer technologies. Up to day, I have developed more than a dozen of automated chat bots to help businesses grow, couple of websites with undeniable high-scored UX and I am still learning.


If you enjoyed reading this post, you should follow me on X.