Creating Your Own Info Bot in Telegram

If you’re looking to create a Telegram bot that provides information to your audience, then this guide is for you. In this tutorial, we will walk you through the steps necessary to create your very own Telegram bot.
First, you’ll need to have a Telegram account and create a bot using the BotFather bot. Once you have your bot token, you can begin programming your bot to respond to user queries.
We’ll cover the basics of programming a Telegram bot using Python and the Python-Telegram-Bot library. We’ll also discuss how to host your bot on a server so that it can run 24/7.
By the end of this tutorial, you’ll have a fully functional Telegram bot that can respond to user queries with custom information. Let’s get started!

Lyrical preface which can be skipped:
There’s never too much monitoring. Even if everything is working fine, why not double-check? There are plenty of ready-made solutions on the internet, but I wanted something of my own. Moreover, I wanted it to be universal, so that what I write can be used for a specific task, as well as for similar and dissimilar ones.

This is my first attempt to create something with my own hands using a bot in Telegram, so please don’t be too hard on me. I’m not a programmer, and what I’m doing may cause Homer-like laughter among experts. However, I’m writing primarily for myself, and if my experience can help someone, why not?

Send a message to Telegram that something has happened on your server/site/node. I will start by practicing on the Massa node, which is installed on a Ubuntu server, but the task can be scaled.

Operating principle:
The system consists of two components: a sensor located on the server itself and a Telegram bot to which this sensor will send a message about the change in the controlled object.

Setting up the first component involves creating your own info bot in Telegram.

Creating a new bot in Telegram:

Open the Telegram messenger, sign in to your account, or create a new one. Enter @BotFather in the search field and select the bot. (The official Telegram bot will have a blue confirmation sign next to the name in the form of a checkmark.)

I want to clarify right away that this guide is based on the web version of Telegram, and the pictures on your phone may be different. However, the sequence of actions will remain the same.
Creating Your Own Info Bot in Telegram

Click on it with the mouse and go to the bot itself. Next, click on the START button.
Creating Your Own Info Bot in Telegram
In response, you will receive a list of commands for managing bots.
Creating Your Own Info Bot in Telegram
Choose or type and send the command /newbot

Creating Your Own Info Bot in Telegram

Come up with a name for your bot – this is how it will be displayed visually. In this example, I came up with the name “my_nodes_info_bot”.
Creating Your Own Info Bot in Telegram

The next step will be to come up with a username for the bot, so that it can be found in Telegram. It is a requirement that your username must end with the specific letters “bot”. Additionally, the name must be unique.

If everything goes well, the bot will provide you with a link to your bot and a token for its management. We will need both of these later in the guide.
Creating Your Own Info Bot in Telegram
After clicking on the link provided by the BotFather, your bot will be added to your list of Telegram channels. You can then customize your bot by adding an avatar, description, and more, but this is optional.
Creating Your Own Info Bot in Telegram

Now you need to find out the ID of your bot. This ID will be used to send all messages to it later. There are many ways to find out the ID (yes, I’m sending you to the search engine). For the laziest, do as I do: Search for a bot with a simple nickname like @get_id_bot Launch it and see the ID.
Creating Your Own Info Bot in Telegram
Alternatively, you can give the command /my_id in case the bot does not output any numbers.
Creating Your Own Info Bot in Telegram

So we have two necessary parameters:
Bot token: 6116867621:AAEE7WXKR_YAxwsf3k3pydQFdYeob7PqJQk
Chat ID: 6004109057
Check sending a message through any browser by using our parameters in the request:<Bot token>/sendMessage?chat_id=<Chat ID>&text=Hello%20World

And our bot receives the message “Hello World”.
Creating Your Own Info Bot in Telegram

The easiest part of our guide is now over.
Now we move on to the difficult part.

Second lyrical digression
The information you want to deliver to your Telegram channel is only limited by the flight of your unhealthy imagination and scripting experience. It’s hard to even limit examples to any framework. Well, we won’t consider basic parameters like CPU load, disk space, or memory information. There are plenty of ready-made examples on the web, and if you’re interested, you’ll easily figure it out on your own. We’ll write code for those who are too lazy to use their brains, for those who find copy-pasting challenging enough.

Scripts for monitoring
It should be understood that the script itself consists of two parts. The first part is the one that retrieves the necessary information for us. And the second part is the one that sends this information to us in a Telegram chat. The first part is always different, the second part remains unchanged.

Node Mass “liveliness” monitoring
Since some people try to “cheat fate” everywhere and in everything, choosing what’s cheaper, quality naturally suffers. But we are not looking for easy ways, so we will engage in sadomasochism and observe the agony of our node together. Now without moralizing – this script is designed to monitor the state of the node. And if the node transitions from a working state to a non-working one or vice versa, you will receive a corresponding notification in Telegram. (In slang terms – the message will come if the node “bootstrapped” or if it “crashed”)

(The block below needs to be copied to “Notepad” beforehand, not Word, this is important, and your BOT TOKEN and CHAT ID need to be inserted, then paste the entire code immediately into the command line, not line by line.)

sudo tee /root/ > /dev/null <<EOF
# script for sending to Telegram
# butstram node Massa
# ver.02
TG_BOT_ID="Bot token"

key=\$(cat \$HOME/key)
node=\$(netstat -ntlp | grep LISTEN | grep 31244 | awk '{print \$1}')

if [ -z "\$node" ] && [ "\$key" -eq "1" ]; then
/usr/bin/curl --socks5-basic -X POST "\$TG_BOT_ID/sendMessage" -d "chat_id=\$TG_CHAT_ID" -d "text=Your node is DOWN."
echo "0" > \$HOME/key
elif [ -n "\$node" ] && [ "\$key" -eq "0" ]; then
/usr/bin/curl --socks5-basic -X POST "\$TG_BOT_ID/sendMessage" -d "chat_id=\$TG_CHAT_ID" -d "text=Your node is UP."
echo "1" > \$HOME/key

Create a file that will store the previous state of the node (0 – lying, 1 – working)

printf "0" > /root/key

Add a job to the cron daemon to run the file every minute
We will use a local daemon. The job will be in the nodesup file, located in /etc/cron.d/

The crontab file
(The block below should be copied and pasted all at once, on the command line, not line by line.)

printf "SHELL=/bin/bash
*/1 * * * * root /bin/bash /root/ > /dev/null 2>&1
" > /etc/cron.d/nodesup

I decided not to bother with the logs yet. No one is looking at them anyway. If anyone needs them, we’ll add them.

How to delete a script:
If you decide that this script does not suit you, you can easily delete it
Delete the script itself:

/usr/bin/rm $HOME/

Deleting the script startup file by time

/usr/bin/rm /etc/cron.d/nodesup

Most importantly, what should you end up seeing?

It is possible to put “trinkets” around the messages, such as ip or server, if you have more than one node.
Time how long it worked or on the contrary was out of service. But this is left to your own work.

In this particular guide, a specific Massa port is tracked, which we use to determine whether a node is alive or not. I say that almost every node has such a port. And it is enough to change the port number in the script and you can track almost any node

The author of this “creation” can be found in the Telegram channel (if you speak Ukrainian/Russian)
Or English Telegram channel (I communicate with the translator)

What else to read on the Massa node: