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.
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).
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.
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.
Click on the button that says “I have copied the API token” and paste the token provided by BotFather a few steps back.
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.
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.
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.
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.
This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
Strictly Necessary Cookies
Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.
If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.
Leave a Reply