How to Create Telegram Bot in Python
By: Ng Wai Foong
Tags: Python, Chatbots, Python3, Telegram, Programming

1. Setup

Make sure you have installed Telegram on your phone. We’re going to create our bot directly inside Telegram by interacting with BotFather bot. This is the official bot created by Telegram to facilitate bot creation.

Searching BotFather

Open Telegram on your phone and click on the search button at the top right of the main interface. Then, type botfather in the search bar. You should see the following user interface.

Image by Author

Click on the first selection which has the blue tick icon beside it. The icon represents VerifiedBadge , which indicates bots or reputable companies or persons approved by Telegram. You should see the following user interface:

Image by Author

Interacting with BotFather

Click on the Start button and you should see a list of commands that you can use to interact with BotFather .

Image by Author

Send the following message to BotFather .

/newbot

You should see the following prompts which ask for the name and username of your bot.

name : The display name for your bot.

: The display name for your bot. username : The unique identifier across all the bots in telegram. You cannot use existing names that are already in the system. Also, it must end with bot post-fix. For example, TetrisBot , or tetris_bot .

To keep things simple, I usually use the same string for both name and username . A new token will be generated for you to access the HTTP API. Make sure to keep it secure to prevent other people from using it to control your bot.

Image by Author

Privacy in group chat

If you intend to use your bot inside a Telegram group, you need to understand how privacy works in Telegram. By default, the bot will not receive all the messages in a group chat. Based on the official documentation, it will only receive:

Command messages that start with a slash / symbol (e.g. /help ).

symbol (e.g. ). Replies to messages sent by your bot.

Service messages (addition or removal of people from the group).

Messages from channels in which the bot is a member.

Generally, Telegram recommends using commands for interacting with bots. In the event where there are multiple bots in the same group, you can post-fix their usernames at the back of each command to prevent confusion. Please note that you need to process and handle it on your own in your server:

/help@your_bot_username

In the latest version, the @ symbol can be used when creating inline bots — where users can interact with your bot via inline queries without sending a message. However, one major downside is that you will lose the flexibility to provide any dynamic input, like this:

@your_bot_username check_phone_status 012345678

Configuring bot to access all messages in group chat

You may prefer to use the old convention, where the bot only responds when it’s tagged with the @ symbol:

@your_bot_username hello there

If so, you need to turn off the group privacy setting or make it as an admin (not recommended) to allow it to access all messages.

To do so, send the following message to BotFather :

/mybots

It will list out all the bots that you have. Since I have only created one bot, only one selection is available.

Image by Author

Click on the desired bot to reveal more options as follows. Continue by tapping the Bot Settings .

Image by Author

You should see a few options related to the group. Click on the Group Privacy button.

Image by Author

Disable the group privacy setting by clicking on the Turn off button. Your bot will now have access to messages sent in group chats. If your bot is already in a group, the new changes might not be reflected or propagated. If you experience an issue with it, just remove it from the group and add it back to the group again.

Adding a bot to group chat

There are quite a few ways to add a bot to group chat. The easiest method is to just do it directly from Telegram. Search for your bot and click on the Start button to initiate a conversation with it. Next, click the name of the bot which is located at the top bar.

Image by Author

You should see the following interface. Continue by clicking on the triple dots at the top right of the interface.

Image by Author

A popup will appear with the following options. Tap the Add to group selection.

Image by Author

Select the desired group and confirm the addition. Head back to the group and check the setting to make sure that your bot has access to group messages.

Image by Author

Installing Python packages

We’re done with the Telegram setup, now let’s install the required Python packages for this tutorial. Before that, make sure you’ve created a virtual environment. Activate it and run the following command in your terminal.

pip install pyTelegramBotAPI

You can easily verify if you have installed the package by running the following:

pip show pytelegrambotapi

The following text will be output to the console:

Image by Author

Once you have finished the installation, move to the next section and start writing Python code.