Creating a Python Telegram Bot from scratch

Everyone who at least once came across a Telegram messenger was impressed with its functionality. Here you can not only chat with friends in private messages, read news and make channels.

The advantage of the service is the ability to create and use bots. You can program them in any language, but Python has become the most common today. The article will detail how to make a python telegram bot.

A bit of theory about the API

Application programming interface.

Application Programming Interface – the interface with which the developer is able to form applications.

Thanks to him, it turns out to configure different parts of the program so that they harmoniously and correctly interact with each other.

Initially, api was used to transfer information and commands between different variable programs. Today it gives the opportunity to access resources on another server.

Its application has the following advantages:

  • Affiliate Program Availability.
  • Work with pre-formatted links loading simultaneously with ID.
  • The ability to provide the most accurate and current data at any time.
  • Getting response data in JSON or XML formats.

APIs can be:

  • Public. Easy to access.
  • Private. It can be used exclusively within the same company. If she has developed many products, then the interface allows different programs to interact with each other.

The main tasks of the Application Programming Interface include:

  • help in writing codes;
  • turning complex tasks into simple ones.

Instructions for creating a Telegram bot in Python

There are several options for getting your own robot:

  • Write it yourself. You can use various programming languages ​​for this. One of the most understandable and popular at the moment is Python. This method, although time-consuming, but at the same time allows you to get a universal solution.
  • Use the services of a designer. But here you can encounter a limited functionality, which is not always enough to translate.
  • Purchase. However, you need to understand that basically, to write telebot, python is used. And it is considered so easy that even beginners can use it.

There are 2 types of bots that can self-learn and work according to certain rules:

  • The first type is less common. The robot is trained in certain rules, based on which it answers the questions posed. Bot handles simple requests, and complex ones can cause difficulties.
  • Self-learning robot is more efficient. It happens:
    • search – uses the replicas registered in the library database for the response. Depending on the context of the conversation, he selects the text from the list;
    • generative – it is able to create messages on the basis of learned words in the request.

You can make your own Telegram bot on Python yourself. Consider the detailed instructions.

Bot Registration

This part of the process is easy. To register with @BotFather and send a “Start” command to the message to it.

Optional name. In response, he will send a link to the documentation (documentation) and a token. It is advisable to save them immediately, since they will be the only key to authorization when interacting with the assistant.

Python installation

First, the program is installed.

There are several ways:

  • Download from the site by clicking install.
  • Those who use Linux have the ability to run it from the package manager.
  • MacOS owners need to install Homebrew app.
  • Use the interpreter of a program that allows you to work online.
  • When working on a mobile device, the Python programming environment is presented.

After downloading, install pip, the package management system. It is important to use the latest versions of Python. If necessary, it can be updated.

Code writing

The python telegram bot api module is responsible for the creation and operation of the robot. To do this, send the pip install pyTelegramBotAPI command.

Write the code anywhere. It can be a Word file or notepad. But it’s much more efficient to do this in a smart environment. Possible errors will be automatically highlighted here.

Code creation begins with connecting a telebot. Here you will need a previously saved token. The first line will look like this: message: TOKEN = the key that the bot sent.

The second line creates an object that we call bot. The authentication code is written in the arguments.

Now it is necessary to think over what the assistant should be able to do. There are decorators in the pyTelegramBotAPI directory, using which the robot will learn to answer standard questions.

You can test it by running the python command full_path_to_file / test.py. the robot must respond.

Configure and launch the bot

Bot launch.

In order for the assistant to work, you need:

  • Log in to the messenger.
  • Open Robot Account.
  • In the new dialog box, click on the image with three dots at the top.
  • Go to Settings.

To start the robot, you need:

  • In the search line on the keyboard with your hands enter the name of the assistant and start a dialogue with him.
  • Connect it to the channel using the “Add a new member” button.
  • From the list, select the option you want and click “Invite”.

Command handlers

For this, a special program Handler is used. She works with media files and text material. But in our case, the utility is needed in order to transmit commands to the bot.

You can also write a handler yourself. In this case, on the first line you should drive in “handler”. This parameter always works, except for the input “help” or “start”.

Button Handlers

The telegram api python will also be based on the keyboard source. When preparing KeyboardButton for work, the required parameter is text, which the user can forward after pressing this key.

When writing code, various methods are used:

  • Add – any number of buttons. At the same time, they line up in a row. If the originally set width has already been reached, they are carried over.
  • Row – the number of keys is also not limited, but they are all located on the same line.
  • Insert – reminds the first method, but icons are added to the last row.

Built-in mode

This is one option for interacting with bots. With it, they got even more opportunities. Such robots are able to perform any task. Example from examples: sending text from a site to chat, posting a gif or picture.

To see the function in action, you need to make a command for the bot using its name and keyword (@gif, @bold, @pic). In this case, the robot will offer several answers. The user selects one of them and sends it to the chat. The built-in mode does not have access to history, but only responds to what the user wrote to him.

Dialog bot generator

Itertools module.

Using the yield from construct, robots create a course for each other to perform a specific task. The itertools module is used for this.

Generators for python telegram bot proxy are a great tool. They provide an opportunity to simplify the code and continue the conversation from the right moment.

Deploy bot on server

This is the final step in creating an assistant in Telegram. For this, you do not need to purchase expensive equipment. You can use cloud proxies with the mtproto protocol, where they offer to place any application for free.

First you have to register on GitHub. With this account, you can deploy the bot to Heroku proxy. If the application does not work, it is recommended to check the logs.

conclusions

Python – a program for writing scripts in Telegram from scratch, which will be an excellent assistant to the owner of the channel. They can auto-post, format material, keep statistics, create buttons in any language, etc.