Telegram notifications from Zabbix

In this article I will give an example of setting up telegram notifications from Zabbix.

First of all, it is necessary to create a telegram bot through which messages will come from Zabbix. To do this, @BotFather user needs to write:

/start
/newbot

Then indicate the name of the bot and the username that will be specified in the link to the bot, after that you will be shown a secret token. If anything, the API can be viewed at core.telegram.org/bots/api.

An example of sending a private message from Linux:

curl --header 'Content-Type: application/json' --request 'POST' --data '{"chat_id":"123456789","text":"Test message"}' "https://api.telegram.org/bot111122222:AABBCCXXFFGGSSRRTTEE/sendMessage"

Where:
111122222:AABBCCXXFFGGSSRRTTEE is the secret token of your bot, and 123456789 is your telegram ID. To find out your ID, you need to @my_id_bot bot to write:

/start

In the directory with scripts for notifications, create a script file (by default AlertScriptsPath=/usr/lib/zabbix/alertscripts):

nano /usr/lib/zabbix/alertscripts/telegram_bot.sh
chmod +x /usr/lib/zabbix/alertscripts/telegram_bot.sh

The contents of the script:

#!/bin/bash

token='111122222:AABBCCXXFFGGSSRRTTEE'
chat="$1"
subj="$2"
message="$3"

/usr/bin/curl -s --header 'Content-Type: application/json' --request 'POST' --data "{\"chat_id\":\"${chat}\",\"text\":\"${subj}\n${message}\"}" "https://api.telegram.org/bot${token}/sendMessage"

Check the script (where 123456789 is your ID in telegram):

/usr/lib/zabbix/alertscripts/telegram_bot.sh 123456789 Test 'Test message'

Create a notification method in the Zabbix settings, namely, in the Administration menu – Notification Methods – Create Alert Method and specify:
Name: Telegram
Type: Script
Script Name: telegram_bot.sh
Script Parameters:
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}

Next, in the “Administration” – “Users” menu, select the user you need, go to the “Alerts” tab and add:
Type: Telegram
Send to: Your telegram ID
Note the importance of sending messages, click “Add” and “Update”.

In the “Settings” – “Actions” – “Report problems to Zabbix administrators” menu, you can change the notification template or add new.

If you want to send messages to a teleram group, then create a group, add user @my_id_bot to it and write /start to find the group ID, then add it to the group of your bot, then use the same method in the Administration menu – “Users” user, go to the “Alerts” tab and add:
Type: Telegram
Send to: group ID in telegram
Note the importance of sending messages, click “Add” and “Update”.

If the “Bad Request: group chat was upgraded to a supergroup chat” error occurs, then add @my_id_bot again and check if the group ID you specified is correct.

See my other Zabbix related articles.

Leave a comment

Leave a Reply