Close
    Search Search

    How to create a bot on Telegram

    You have become a regular user of Telegram and, while exploring it, you have come across the famous bots, those "automatic" chats that can satisfy various user needs (eg. The search for a video on YouTube or a GIF to send to friends, shipment tracking and so on) without leaving the app. Fascinated by this tool, you have decided to create something like this to simplify your life, or surprise some of your friends, and you are looking for a simple method that can guide you in the initial steps of this undertaking.



    No sooner said than done! You have undoubtedly come across the right guide: in fact, below I will provide you with all the basic information on how to create a bot on Telegram, I will show you in detail how these tools work and how they are able to communicate with you through simple chat messages. I tell you right away that, to succeed in the business, you will need to learn some basic notions on programming and you will need a computer that can facilitate your development work.

    How do you say? Are you already profusely scared and are you thinking of throwing in the towel? Don't be discouraged! The indispensable things you need are patience and imagination: I guarantee you that, by following my tips, you will be able to create your first "little" bot and understand its mechanism perfectly, after which time and a little effort will make the rest! Having said that, I just have to wish you good reading and, why not, have fun!

    Index

    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 carry out 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 @nomebot. 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 programmers El Paesei, which allows you to track a shipment starting from its tracking code and receive status updates in order to all 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, 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. Let me 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 which 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 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. Bot Eco), 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 through 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 microservizi (i.e. small pieces of code that perform operations), also usable for free with a monthly limit of calls to service.

    However, in order to create a bot that interacts with Telegram, it is essential to subscribe to a paid plan, with prices starting from $ 2 / month. In any case, to subscribe to Hook.io, connected to this Internet page, enter your e-mail address in the box Email Address placed in the box New Developers and press pulsating Create new account.

    At this point, select the paid plan you wish to activate by clicking on the relevant price (eg. $2), enter your information credit card in the appropriate field and, when requested, click on the button indicating the name and price of the chosen plan, to go to the next step.

    At this point, follow the wizard to create the username and Password to be used to access the service, 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 you have completed the account creation phase, 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) and presses the button Save and continue. In the next window, set the item Python 3 in the drop-down menu Language, delete everything written in the text box just below and paste the part of code that allows the bot to "repeat" the messages received, as written below.

    import logging from telegram import Update, ForceReply from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO ) logger = logging.getLogger(__name__) def start(update: Update, context: CallbackContext) -> None: user = update.effective_user update.message.reply_markdown_v2( fr'Hi {user.mention_markdown_v2()}!', reply_markup=ForceReply(selective=True), ) def help_command(update: Update, context: CallbackContext) -> None: update.message.reply_text('AIUTO!') def echo(update: Update, context: CallbackContext) -> None: update.message.reply_text(update.message.text) def main() -> None: updater = Updater("token_repeat") dispatcher = updater.dispatcher dispatcher.add_handler(CommandHandler("start", start)) dispatcher.add_handler(CommandHandler("help", help_command)) dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, echo)) updater.start_polling() updater.idle() if __name__ == '__main__': main()

    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.

    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 "diverted" to the microservice just requested: to do so, type in the address bar of the browser at your disposal the command https://api.telegram.org/botToken/setWebhook?url=https://hook.io/tuonomeaccount/nomehook which, in this example, turns into https: //api.telegram .org / botabcd-1234-token / setWebhook? url = https: //hook.io/jessica_689/ripeti-bot. If everything went well, a response similar to {"ok": true, "result": true, "description": "Webhook was set"} is displayed in the browser: this means that your bot is working and that , starting from this moment, 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 anything you want your bot to repeat.

    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, go back 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 the 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 the parsing (or interpretation) of 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.
    add a comment of How to create a bot on Telegram
    Comment sent successfully! We will review it in the next few hours.

    End of content

    No more pages to load