COVID19 stats to Telegram (Shell version)
I was inspired by Luke Smith’s video1 and made my own notification script about COVID19 stats in Germany and in the world. My setups is the following: Raspberry Pi with OpenHab automation platform with a Telegram binding.
Script for pulling data
I use the same GitHub project Coronavirus Tracker CLI, because I found its simple table output format very useful for processing.
First, create script folder and file:
1mkdir /opt/corona-update
2touch /opt/corona-updade/corona-info.sh
Then write following script.
1#!/usr/bin/sh
2
3CURDATE=$(date +'%R %d-%m-%Y')
4TEMPFILE="/temp/coronaupdate"
5
6# I pull necessary information into the file first.
7# This makes local processing faster because I need only one CURL request for each update.
8# sed 's/\x1B\[[0-9;]*[JKmsu]//g' removes escape color codes for terminal.
9curl -s https://corona-stats.online/Germany?source=2 | sed 's/\x1B\[[0-9;]*[JKmsu]//g' > ${TEMPFILE}
10
11echo "_COVID19 stats_"
12echo "_${CURDATE}_"
13echo
14
15# sed 's/║//g' removes outer borders of the table.
16# sed 's/│/;/g' replaces inner borders with ";", which I later use as a delimiter in AWK.
17# sed 's/▲//g' removes ▲ symbols.
18# sed 's/\s\+//g' removes extra spaces.
19# sed 's/,/ /g' puts space as a delimiter of thousands, millinions, etc. instead of ",".
20grep "Germany" ${TEMPFILE} | sed 's/║//g; s/│/;/g; s/▲//g; s/\s\+//g; s/,/ /g' | awk -F';' '{print "*Germany*\nTotal number of infected: "$3"\nNew cases: "$4"\nTotal deaths: "$5"\nTotal number of recovered: "$7"\nCases per 1 mln: "$10}'
21
22echo
23
24grep "World" ${TEMPFILE} | sed 's/║//g; s/│/;/g; s/▲//g; s/\s\+//g; s/,/ /g' | awk -F';' '{print "*World*\nTotal number of infected: "$3"\nNew cases: "$4"\nTotal deaths: "$5"\nTotal number of recovered: "$7"\nCases per 1 mln: "$10}'
My configuration of OpenHab binding with Telegram bot is configured to use Markdown syntax for the formatting.
Then make file executable and change ownership to openhab
user, otherwise OpenHab rules engine will not be able to process the file.
1chmod 750 /opt/corona-update/corona-info.sh
2sudo chown openhab:openhab /opt/corona-update/corona-info.sh
I also delete temporary file from the /temp
directory which was created during troubleshooting and dry run of the script. It is necessary to remove this file because when script will be run by the openhab
user, it will not be able to write in this temporary file because it was created by another user with default rw-rw-r--
rights.
1rm /temp/coronaupdate
And now I am ready for my OpenHab rule.
OpenHab rule
In the OpenHab rules folder I simply created new file called telegram_corona.rules
with the following content:
1rule "corona update"
2when Time cron "0 30 8,19 * * ?" // updates are sent at 8:30 and at 19:30 every day
3then
4 var String corona = executeCommandLine("/opt/corona-update/corona-info.sh", 5000)
5 // Use name of your Telegram bot instead of <bot_name>.
6 sendTelegram("<bot_name>", corona + "\n")
7end
Job is done. Now I am receiving information about COVID19 statistics twice per day from my Telegram bot.
-
Luke Smith’s video:
↩︎