Telegram - Send a Telegram notification when a script is done

Fonte: https://www.marcodena.it/blog/telegram-logging-handler-for-python-java-bash/ ; https://medium.com/@jasonlimberis/server-runscript-telegram-alert-bot-8a1691297a52 em 25-08-2020

Requisites

Configuration

Search the BotFather, the official bot that allows you to create bots :))

Telegram BotFather

Then, create a new bot:

/newbot

Choose a name for your bot

UltraNotifier

Choose a username for your bot, which must end with "_bot"

ultranotifier_bot

Once the bot is created, you will have a long string that is the TOKENID. The message will be similar to:

Use this token to access the HTTP API:

Now, the bot is only the "guy" who will send you the messages. It has to know the chat where to send them. For this reason, got to the search bar of Telegram, and search your bot. Then, start the bot:

/start

You can now go to the website https://api.telegram.org/bot[TOKENID]/getUpdates (replacing [TOKENID] with the ID you received before) and search for your id.

Telegram chat id

Now you are ready to use a command line code to send your first notification

$ curl -s -X POST https://api.telegram.org/bot[TOKENID]/sendMessage -d chat_id=[ID] -d text="Hello world"

Server/RunScript Telegram Alert Bot

Create telegram bot to get alerts from server runscripts

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 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}" "" > /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}" "" > /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}” "" > /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 "}" > /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}" "" > /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 "}" > /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}" "" > /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}" "" > /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}" "" > /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}" "" > /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"