Sudo Su


How to create a bot in Telegram

Date: 9 October, 2021

Having a bot in Telegram can be a great help to receive notifications from the systems and services we manage. The idea of this article is to serve as a help to all of you who need a bot to send you notifications of what is happening in your Linux systems or in your web pages.

Soon I will publish some projects in which it will be very interesting to use a bot like the one we are going to create today.

The first thing we must do is go to our Telegram and start a conversation with BotFather, this is the tool that Telegram provides us to create our bots in a simple way.

Once started the chat with BotFather, we will write the command /start and this will let us know the commands that we can use.

Comando /start BotFather en Telegram

 

We will use /newbot to start creating the bot, at this point it will ask for two names for our bot.

The first name can be any name since it will simply be the name that will appear in the Telegram chat (as if it were another contact).

Nombre genérico de nuestro bot de telegram
The second name will be the one that will be used to identify our bot uniquely throughout Telegram, so it will ask us to be a name that does not exist and ending in bot.

Once BotFather has accepted our name it will provide us with a token, this will be the one we will use to send messages through our scripts, so I recommend you to copy it to the clipboard because in the next step we will need it.

Nombre y token en BotFather

For the moment we will not use BotFather anymore, unless we want to change some features of our bot, such as the name, profile image, etc…

 

The next tool we will use is ManyBot, which is the application that allows the management of our bots. Actually this step would not be necessary but we are going to perform it in case in the future we want to make our bot smarter.

We open a conversation with ManyBot and launch the /addbot command to add our bot.

addbot en ManyBot

 

Click on the button that says “I have copied the API token” and paste the token provided by BotFather a few steps back.

Pegamos el token de nuestro bot en manybot

 

After this we will click on the link to our bot that appears in the last ManyBot message and we will click Start, this will start a chat with our bot and our bot will be ready to work.

Iniciar conversación con nuestro bot de Telegram

 

Now in order to make our bot can send messages only to us, we need to know our user ID in Telegram, this we will find out through the bot called UserInfo. We will open a conversation with it and it will return the information we need.

Userinfo nos devuelve nuestro ID de Telegram

 

At this point we would already have everything we need to develop scripts that allow us to send messages through our Telegram bot.

I am going to show you an example of how it would be done with a basic script in Linux. Although if you do a little research you can see how it can be done in many programming languages.

We will create a file called script.sh using the command:

nano script.sh

In this file we will copy the following lines of code:

TOKEN=”1111111111:111111111111111_1111111111111111111″
ID=”111111111″
MENSAJE=”Hello, How are you?”
URL=”https://api.telegram.org/bot$TOKEN/sendMessage”

curl -s -X POST $URL -d chat_id=$ID -d text=”$MENSAJE”

 

From these lines that we have just copied it will only be necessary to replace the token with the one given by BotFather and our ID (keeping the quotation marks). And we will save the file with (Ctrl + O).

.

 

And finish by executing the script with the following command:
sh script.sh

If we have done all the steps well, we should have received the message to our Telegram account as shown in the following image.

Mensaje de mi bot en telegram

 

And so I would end my explanation of how to make a Telegram bot, I hope you have understood everything and if you have any questions feel free to comment here below.


Author: Carlos Categories: Scripts



Leave the first comment!-

Leave a Reply

Your email address will not be published. Required fields are marked *