Server/RunScript Telegram Alert Bot

Create telegram bot to get alerts from server runscripts

SemiQuant
4 min readMar 4, 2018

I found that using a telegram bot is a sleek alternative to using email alerts to let you know when scripts have started running (after submitting to a cluster), if any errors occurred, or when the job completed. This is especially powerful when combining it with seashells.

Create New Bot

Download and sign up to Telegram
Search for BotFather (will have verified symbol next to its name)

In chat with BotFater type ‘/newbot’
Follow the promps (enter bot name, enter bot user name)
Copy the API token

Type https://api.telegram.org/bot$Token/getUpdates into your browser where $Token is the API token you copied in the previous step (XXX:XXXX).
You will see some text, copy the number that says ‘id:XXXXXXXX’, this is your chatbots ID.
Now we can send messages to your bot, to do this in you shell (or pbs) script do the following.

Send Message or File to Bot using Shell

ChatID="123456"
Token="5556045:AAEtkcHfEsteJs01J4zlaf6XZXCr7oN246Q"
TimeLim=240
Msg="Hello world
curl -s — max-time $TimeLim -d "chat_id=${ChatID}&disable_web_page_preview=1&text=${Msg}" "https://api.telegram.org/bot${Token}/sendMessage" > /dev/null

To send a file of any type (for example, you can redirect the stdout to a file and send that)

Cap="Hello again"
bash ./bash_script.sh | tee File
curl — max-time $TimeLim -F chat_id=${ChatID} -F document=@"${File}" -F caption="${Cap}" "https://api.telegram.org/bot${Token}/sendDocument" > /dev/null

Or the results of the script

Cap="Run Script Graph"
File="NiceGraph.png"
curl — max-time $TimeLim -F chat_id=${ChatID} -F document=@"${File}" -F caption="${Cap}” "https://api.telegram.org/bot${Token}/sendDocument" > /dev/null

You can also redirect the stdout in real time to a website and send that the link to your telegram bot

bash ./bash_script.sh | nc seashells.io 1337 | tee >(read Msg; curl -s "https://api.telegram.org/bot${Token}/sendMessage?chat_id=${ChatID}&text=${Msg}" > /dev/null)# bash ./bash_script.sh | tee >(read Msg; curl -s — max-time $TimeLim -d "chat_id=${ChatID}&disable_web_page_preview=1&text=${Msg}" "https://api.telegram.org/bot${Token}/sendMessage" > /dev/null)

Or you can redirect the stdout in real time to a the bot (but this could get very messy)

bash ./bash_script.sh | while read Msg;
do
curl -s "https://api.telegram.org/bot${Token}/sendMessage?chat_id=${ChatID}&text=${Msg}" > /dev/null
done

Putting it Together (you can add these to your .bash_profile for easy use)

# You could set the Token, TimeLim, and ChatID here so that all you pass to the function is the Msg or File and Cap. If doing so, remember to change the number of input terms to look for in the if statement.# Send Message Function
function sndMSG () {
if [ $# -eq 3 ]
then
echo ‘Please supply input arguments in this order: sndMSG $TimeLim $ChatID $Msg $Token’
else
curl -s — max-time $1 -d "chat_id=${2}&disable_web_page_preview=1&text=${3}" "https://api.telegram.org/bot${4}/sendMessage" > /dev/null
fi
}
# Send File Function
function sndFile () {
if [ $# -eq 4 ]
then
echo ‘Please supply input arguments in this order: sndFile $TimeLim $File $Cap $ChatID $Token’
else
curl -s — max-time $1 -F chat_id=${ChatID} -F document=@"${2}" -F caption="${3}" "https://api.telegram.org/bot${Token}/sendDocument" > /dev/null
fi
}

Full Example

Functions to put into .bash_profile

# Send Message Functionfunction sndMSG () {
if [ $# -eq 3 ]
then
echo ‘Please supply input arguments in this order: sndMSG $TimeLim $ChatID $Msg $Token’
else
curl -s — max-time $1 -d "chat_id=${2}&disable_web_page_preview=1&text=${3}" "https://api.telegram.org/bot${4}/sendMessage" > /dev/null
fi
}
# Send File Function
function sndFile () {
if [ $# -eq 4 ]
then
echo ‘Please supply input arguments in this order: sndFile $TimeLim $File $Cap $ChatID $Token’
else
curl -s — max-time $1 -F chat_id=${ChatID} -F document=@"${2}" -F caption="${3}" "https://api.telegram.org/bot${Token}/sendDocument" > /dev/null
fi
}

PBS code (run script and tee output to website)

#PBS -q ServerName /
#PBS -l nodes=1:ppn=1 /
#PBS -N BotTest /
ChatID="123456"
Token="5556045:AAEtkcHfEsteJs01J4zlaf6XZXCr7oN246Q"
TimeLim=10
bash ./bash_script.sh | nc seashells.io 1337 | tee >(read Msg; sndMSG "$TimeLim" "$ChatID" "$Msg" "$Token" > /dev/null)
# or
bash ./bash_script.sh | netcat seashells.io 1337 | tee >(read Msg; sndMSG "$TimeLim" "$ChatID" "$Msg" "$Token" > /dev/null)
# or if seashells client installed
bash ./bash_script.sh | seashells | tee >(read Msg; sndMSG "$TimeLim" "$ChatID" "$Msg" "$Token" > /dev/null)

Bash script

#!/bin/bash
ChatID="123456"
Token="5556045:AAEtkcHfEsteJs01J4zlaf6XZXCr7oN246Q"
TimeLim=240
Msg="Running Test 1"
sndMSG "$TimeLim" "$ChatID" "$Msg" "$Token"
echo "Testing 1" > out1.txt
File="out1.txt"
sndFile "$TimeLim" "$File" "$Cap" "$ChatID" "$Token"
sleep 10Msg="Running Test 1"
sndMSG "$TimeLim" "$ChatID" "$Msg" "$Token"
echo "Testing 2"
sndMSG "$TimeLim" "$ChatID" "Script Done Running!" "$Token"
Output of Example Above

--

--