Close
    Search Search

    How to create a bot on Telegram

    What are Telegram bots

    How to create a bot on Telegram

    Before taking action e create a bot on Telegram, it is good to clarify in detail what Telegram bots are and why they are so successful. Generally speaking, a bot is nothing more than a Telegram "user" not human, consisting mainly of lines of code loaded on a microservices platform, able to perform actions (sometimes even complex) in a completely automatic way, receiving requests in the form HTTP the commands given by the user through the Telegram chat.



    Mainly, there are two types of Telegram bots: the bot standalone, the one with an autonomous chat window in which to issue commands, and the bot inline, the one that can be called from other chat windows (single or groups) using the command @botname. Each Telegram bot is uniquely identified by a token- A sequence of numbers and letters used by web applications and APIs to refer to the bot itself.

    To clarify your ideas, I want to give you a practical example: under normal conditions, tracking a package in transit at a courier means connecting to the courier's website, entering the tracking number in the proposed box, filling in the necessary fields and consulting, on the spot. , the status of the package. To obtain status updates, in most cases, care must be taken to return to the site and repeat the procedure.

    With TrackBot, one of the best stand-alone bot for Telegram, the music changes: it is a bot created by four your language programmers, which allows you to track a shipment starting from its tracking code and to receive status updates in a completely automatic. To use it, simply search for it in Telegram and issue the necessary commands (illustrated by the bot itself) to add the parcel tracking number, after which the bot interfaces with the courier's Internet service (eg GLS) and, completely automatically, it sends a chat message to the user when the package changes state (eg. In transit, In delivery or Delivered).



    As for bots inline, I can give you the example of @gif: typing the command @gif key_word within a chat window, the animated GIFs corresponding to the entered keyword will be proposed, taken directly from the portal Giphy and ready to be sent in chats. TrackBot and Giphy are just two examples of what Telegram bots are capable of: you can find a list of useful bots just in case in my in-depth guide on the best Telegram bots.

    Returning to us, the basic operation of a Telegram bot follows the pattern that I illustrate below.

    • The bot is added via the Telegram search area (standalone) or invoked within an existing chat (inline).
    • After starting it, the user types a command in the chat window (standalone) or after the string @botname (inline).
    • The command typed is sent to the microservices platform, which takes care of interpreting it, executing it and generating the HTTP response.
    • Telegram receives the HTTP response and its bot "writes" it in the chat window.

    Creating a bot on Telegram

    How to create a bot on Telegram

    Now that you have the basics of Telegram bots, I bet you can't wait to create your own. I'll start by telling you that the creation of an "empty" bot is extremely simple: the slightly more complicated part, the one that requires hints of programming, concerns instead what concerns the behavior of the bot, that is the ability to receive and execute commands given by the user.

    As I already mentioned, this is done through requests HTTP that act as a "communication channel" between Telegram and the Web space that contains the associated application: the latter, in fact, processes the commands and sends the replies to the Telegram chat corresponding to the bot (in turn formed by methods and where they can be exploited API of third party services).



    How do you say? Have you understood how a bot works, but you don't have the faintest idea what to do? Don't worry, below I give you the instructions to create your first Telegram bot which simply “repeats” what is written in its chat window.

    To begin with, you need to create a new "empty" bot using BotFather, the Telegram bot "dad" of all the other bots: start Telegram, presses on magnifying glass located at the top of the window, type the word Botfather inside the text box and presses on the first result received, the one marked with a small blue check mark (which identifies the official bots). Alternatively, if you want to do this first, click here and agree to open the bot in Telegram. Once this is done, press the button starts located in the chat window, then type the command / newbot followed by Submit, in the text box, to start the creation procedure.

    At this point, BotFather will ask you a series of questions about the initial configuration of your bot: First, choose a name (eg. Eco Bot), type it in the text box and press Submit, then choose a username (remembering that it must necessarily end with the word muzzle, for example repeat_bot) and still presses Submit. This will create a bot, which will do nothing for the moment but will provide you with a token by which you can then put it into operation: then write down the token and keep it carefully, without revealing it to anyone (since anyone who owns that code will be able to control the bot). Following a similar procedure, you can use commands / setdescription/ setabouttext/ setuserpic to set one, respectively Description of the bot, a little introductory text it's a avatar personal.



    Once this is done, you need a web space dedicated to instructions (in this guide we will use the language Python) to be given to your bot: this time I have chosen Hook.io, a platform capable of hosting microservices (i.e. small pieces of code that perform operations), usable with prices starting at $ 2 / month (the basic account has a limit of 1000 monthly requests, more info here). The first thing you have to do, therefore, is to connect to this Internet page, enter your e-mail address in the box Email Address placed in the box New Developers ee press the button Create new account.

    At this point, fill in the next screen with the requested information (account name and password) and, when prompted, enter the information relating to your credit card. Finally, press the button Register Account Name and write down the web address of your account that appears on the right side of the screen (https://hook.io/tuonomeaccount), as you will need it later.

    How to create a bot on Telegram

    Once the account creation phase is complete, you will be returned to the services management page: to start, memorize the token received previously from BotFather, so that it can be recalled simply by invoking a variable within the code (without having to rewrite it every time). Then click on the icon marked with three: control sticks present in the service button panel (the second from the left), enter in the field Key the name of the variable (e.g. token_repeat), paste in the field value il token generated by BotFather and presses the button Save Account Environment Variables.

    Once this is done, the time has come to create the real service that will make the Telegram bot work: click on the item Create new service located in the bar at the top, type the name you want to give to your service (eg. repeat-bot) e pigia sul pulsating Save and continue. In the next window, set the item Python in the drop-down menu Language, delete everything written in the text box a little further down and paste the part of code that allows the bot to "repeat" the messages received, as written below.

    import requests
    testoOriginale = Hook ['params'] ['message'] ['text']
    idChat = Hook ['params'] ['message'] ['chat'] ['id']
    URL = 'https: //api.telegram.org/bot' + Hook ['env'] ['token_repeat'] +' / sendMessage '
    req = requests.get (URL, verify = False, data = {'chat_id': idChat, 'text': testoOriginale})

    Remember to replace a token_repeat the name of the environment variable set earlier. Once the insertion is complete, press the button Save Code and wait for the confirmation notification. To better clarify your ideas, I'll explain below what the instructions in the code do.

    • import requests - is used to "import" the library into the program requests of Telegram's Python API, so you can take advantage of its methods.
    • testoOriginale = [...] - this command records the text received from the Telegram chat in a variable.
    • idChat = […] - this is the command that saves the Telegram chat identifier (to understand precisely which chat to send the message back to).
    • URL = […] - is the command that, in fact, sends the message with the HTTP request from the bot to the microservice on Hook.
    • req = […] - this is the command that "assembles" the response received and reproduces it in the Telegram chat.

    Once the code has been entered, you must invoke the microservice for the first time, in order to "tell" Telegram that all the instructions received in the bot's chat must be "hijacked" in the microservice just requested: to do so, type in the address bar of the browser at your disposal command https://api.telegram.org/botToken/setWebhook?url=https://hook.io/tuonomeaccount/nomehook which, in this example, changes to https://api.telegram.org/botabcd-1234-token/setWebhook?url=https://hook.io/jessica_689/ripeti-bot. If everything went well, a response similar to is shown in the browser {"Ok": true, "result": true, "description": "Webhook was set"}: this means that your bot is working and that, from now on, you can use it!

    How to create a bot on Telegram

    All you have to do is call the bot from Telegram, press the button starts and type in the chat window everything you want your bot to repeat: you can test what we have built together by clicking on this link.

    I warn you that, given the extreme simplicity of the bot, this could behave in an "unexpected" way and repeat indefinitely everything you type in the chat window: if this happens, return to the chat list, long press on the chat of the bot (or right click, if you are using Telegram for computer or Telegram Web) and select Stop bots / Stop and block bots from the proposed panel.

    for further

    How to create a bot on Telegram

    I bet that, thanks to my tips, you managed to create your first bot on Telegram and you can't wait to learn how to create others, even more complex ones: as I have already explained to you, what we have seen so far is nothing more than an example of the basic functions of creating and developing a bot, but I can guarantee you that the development possibilities are truly limitless.

    Just to give you an example, you can use them, in addition to the Python, even languages Node.js, Java e PHP, it is possible to make your bot interact with some well-known Web services (including YouTubegmail), you can write code to interact with blogs and websites using API provided by the service in use (eg. WordPressBlogger), you can easily create an inline bot and much, much more.

    Below is a list of useful Internet sites to learn more about the creation of bots on Telegram.

    • Guide to Telegram bots - on this page you will find all the basic information to enter the world of bots, including detailed instructions on how to use BotFather (generate a new access token, delete a bot, create an inline bot and so on).
    • Telegram bot API - is the main resource for managing methods related to parsing (or interpreting) the data included in HTTP requests and responses.
    • Code examples - here you can find examples of working bots, in different scripting and programming languages.
    • Heroku - another good platform for creating and starting microservices in numerous programming languages.
    • Official Python documentation - to learn how to program in Python.
    • Node.js documentation - everything you need to know about this JavaScript library.
    • PHP Manual - to learn programming in PHP.
    How to create a bot on Telegram

    add a comment of How to create a bot on Telegram
    Comment sent successfully! We will review it in the next few hours.