Create Telegram Bot Alert Channel
We can also add Telegram to our list of Alerting Notification channels.
To do this install the Telegram app on your phone or PC. It will be easiest to set this up on your PC first.
You will need a BOT API Token and Chat ID.
Open Telegram, and create a new Bot by searching for @BotFather and then typing /newbot
Follow the prompts to create a name for your bot and also a username for your bot. The username must end in bot and must be unique.
You will get given a HTTP API token which is your BOT API Token to be used in Grafana. It will be in the form XXXXXXXXX: YYYYYYYYYYYYYYYYYYYYYYYYYYYYY
You then need the Chat ID. To get this, you first need to create a group and then add the new bot to it.
Then send a message to the group, so that the group has at least 1 message.
Then, in your browser, visit the url https://api.telegram.org/botXXX:YYY/getUpdates (replace the XXX:YYY with the BOT API token you just got from Telegram)
In the JSON response, you should see a node with a message that has the type=group. This node will also have an Id. Copy this Id into the Chat ID field in Grafana. The Chat ID will most likely be a negative number in the form of -#########.
You can now Save and Test your alert notification channel.
There should be a sample alert from Grafana inside your new Telegram group messages window.
Get Telegram Chat ID
You can also try this tool to get the Chat ID of your Telegram group. Ensure you have created a Group first, added your Bot to it, and sent it at least 1 message. The Chat ID will be extracted from the message data.
If the getUpdates url doesn't return a node containing a group with an id, then
- remove your bot from the group,
- and then re add your bot to the group,
- then send a message to the group again,
- then try the getUpdates url again.
The chat id is likely to be a negative number, so make sure you copy the negative symbol as well when setting the chat id in the script.
Eg, If chat id = -123456789, occasionally quickly copying and pasting you may forget the - character and copy just 123456789