Creating a Telegram bot on Python

On June 24, 2015, Telegram developers have opened a platform for creating bots. Telegram bot is a Telegram profile that automatically processes messages and performs certain actions. For example, weatherman_bot writes you the weather for your city every day at 7:00 (you can install it yourself). So you will know the weather before you go to work or study.

To create the bots I will use the language Python because I really like it and I know it. But any language that can work with the API will work. Often Telegram writes bots on Python, then on JavaScript, PHP, Java (statistics from GitHub). For bots there are many libraries, I will use pyTelegramBotAPI, it is open source and all code is on GitHub. Let’s start coding!

To begin, we will create your future bot in BotFather. To do this, you need to go into telegrams and write a bot ** BotFather ** (this is the bot that creates the bots).


  • Go to BotFather, and add this bot.

  • Write /newbot

  • Then it will ask for a name for your future bot (not a nickname)

  • Now you must specify a bot name, it should end with “bot”

  • If the nick is not busy, then BotFather will write to you the following content:


Done! Congratulations on your new bot. You will find it at telegram.me/my_bot_for_article_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands.

Use this token to access the HTTP API:


For a description of the Bot API, see this page: https://core.telegram.org/bots/api

Here you need only a token, in my case it is “199126628: AAEm19ypb2vqJH2vspGGsvKyzYA__T2MfbY” you can not even check, this token is not active

Чат з BotFather

And now, when you have already created a bot, let’s install a library to work with the bot: Windows

pip install pyplgrambotapi

Linux on OS X

sudo pip install pytelegrambotapi

We will have 2 files:

  • main.py – all code

  • config.py are important constants

The config.py file will have the following content:


#coding: utf-8

TOKEN = "<token>"


Where <token> is the token given to you by BotFather. The file main.py, as I already wrote, will have the entire code in itself, so let’s start filling it. The first thing we will encode, import the pytelegrambotapi library, config.py file and create a TeleBot class object:

#coding: utf-8
import to telebot, config

bot = telebot.TeleBot (config.TOKEN)

Each bot must have the following basic commands:

  • /start – the start of the operation, this command is executed when you first write the bot

  • /help – shows all the available commands * there is still /setting, but it is not used so often

@Message_handler (commands = [“command”]) is used to force Python to handle commands, where the command argument accepts an array of commands. To send messages to ** pytelegrambotapi **, there is a send_message () function that accepts at least 2 arguments: the user id and the message to be sent. You can see the id of the user who sent the bot message in message.chat.id, where message is an argument that accepts the decorator function. Difficult? Let’s see how it works on an example:


@ bot.message_handler (commands = ["start"])

def start (message):

    bot.send_message (message.chat.id, "Hello, I'm a Telegram bot")


To start the bot we use the polling() function:

if __name__ == "__main__":

    bot.polling ()


Now run main.py and write /start to your bot. If he answered you, then I congratulate you, you have created your first bot. If not, then check that you did not allow syntax errors, and if you are not sure, write the comment below and describe your problem.


The bot is working, but in order to meet the basic Telegram requirements, you need to add the /help command:


@ bot.message_handler (commands = ["help"])

def start (message):

    bot.send_message (message.chat.id, "/ start - start using \ n / help - help")

That’s all for today, thanks for the attention, I hope you all turned out in the next article I’ll tell you how to add more functionality to your bot. View all code you can on GitHub.

Leave a Comment