Send an alert to your own Telegram channel if your SafeCoin validator is delinquent
Bash script to send message to Telegram with Telegram Bot
To send a message to Telegram group or channel, you should first create your own bot. Just open Telegram, find @BotFather, start a conversation with the bot and type /start. Then follow instructions to create bot and get token
to access the HTTP API.
- find @BotFather in Telegram
- start chat with the bot
- type
/start
to get the available commands - type
/newbot
to create a new bot - choose a name for your new bot
- choose a username for your bot, it must end with
bot
. This will be later on your link. Example: https://t.me/yournewcreatedbot/ - Done. You will get now your own
token
to access the HTTP API. Please keep it safe and secure. You will need it later for the bash script
- create a new channel in Telegram
!! set the channel to public first. After the bot has been added and set as admin, you can set the channel back to private.
- add your bot as member
- provide admin access rights to your bot
- set your channel back to private
- In order to get
channel ID
post any message to the channel - open following link to get your
channel ID
https://api.telegram.org/botYOURTOKEN/getUpdates . Replace YOURTOKEN with your new token - example output. Write down your
channel ID
you will need it later on
{
"ok":true,
"result": [
{
"update_id":421,
"channel_post": {
"message_id":34,
"chat": {
"id":-8547443533, // this is your channel id !! it includes the - as well !! -> -8547443533
"title":"Notifications",
"type":"channel"
},
"date":1637486719,
"text":"my fist channel message"
}
}
]
}
- Simple test
curl 'https://api.telegram.org/botYourToken/sendMessage?chat_id=channel_id&text=test message'
- create the bash script. (Thanks to phael for the hint)
cd ~/
nano telegram-notification.sh
customize the script according to your identity or vote account
#!/bin/bash
#you can use here your Identity or Voteaccount
validator=AAFXHYrWu6gM8vL2y4gBQHavVFN7EqWY25SrBkwAxUnh
DELIQUENT=`~/Safecoin/target/release/./safecoin validators | grep $validator | grep '!' | wc -l`
if [ $DELIQUENT == 1 ];
then
echo "validator is deliquent!"
curl 'https://api.telegram.org/botYOURTOKEN/sendMessage?chat_id=CHANNELID&text='$validator is delinquent''
# SEND MAIL OR DO OTHER NOTIFICATIONS HERE
else
echo "validator is running"
fi
chmod +x telegram-notification.sh
- test it with
./telegram-notification.sh
- you should get now a notification in your private Telegram channel
- create crontab to check regularly if your validator is running
crontab -e
- add following line to your crontab
* * * * * cd ~/ && ./telegram-notification.sh