twitter |   ||  email | PGP --> harshal @ harshdevx.comharshdevx:~#
harshdevX:~#r00t  |  analyt1cs   |  st1lls  |  ab0ut m3
updating...
alerting via telegram bots
telegram is awesome piece of software. not just that it is safe but that it is truely cross platform. macs, linux, windows, androids, chrome web app, ios and for the nerds on command line. it provides us ability to have bots that allow us to use various apis to send/get messages, audio files, pictures etc.

i tried using the bot technology to send different types of notification and lets just get to it to see how it works. i wont spend too much time on how to create bot as there are alot of tutorials available. there were few problems that i faced during implementation of sending messages as notification and i thought that this might be the opportunity to share with you all and learn if there is amy other way to get this thing done. so lets dive into it. first things first we have the bot running and it can send and receive messages. lets try a basic test from command line of sending the uptime to your bot

#1 DATE=`date`
#2 MSG=`uptime`
#3 curl --data chat_id=123456789 --data-urlencode "text=from fw: uptime is $MSG at $DATE" "https://api.telegram.org/bot123456789:ABCDEDFGHJK\LKNBOPSDHQWE2345/sendMessage"
below is the image that shows when we put the above code in a file named "tg-uptime". now lets see when we run ths script what happens

Click to englarge me

lets take it one step further. i have a raspberry pi sitting running a bunch of cron jobs. i added another one to it. i have a samba share at home on which i keep downloading few movies and i want my wify to know which movie is new in town ;) basically available to watch this weekend...so what i do is first i install inotigfytools on my raspi . this enables me to set watches on the folder i wish to monitor
#1 sudo apt-get update
#2 sudo apt-get install inotifytools
#3 VIDEOS=/home/pi/videos/
#4 DATE=`date`
#5 inotifywait -m -r -q -e create,delete $VIDEOS | while read LINE
#6 do
#7 ACTION=`echo $LINE | awk '{ print $2 }'`
#8 if [ "$ACTION" == "CREATE" ]; then
#9 MEDIA=`echo $LINE | awk '{ print $3 }'`
#10 curl --data chat_id=123456789 --data-urlencode "text=from pi: new media - $MEDIA arrived on $DATE" "https://api.telegram.org/bot12345678:ABCDEDFGHJKghij-zcsdkasdsjfs/sendMessage" > /dev/null 2>&1
#11 sleep 5
#12 curl --data chat_id=987654321 --data-urlencode "text=from pi: new media - $MEDIA arrived on $DATE" "https://api.telegram.org/bot12345678:ABCDEDFGHJKghij-zcsdkasdsjfs/sendMessage" > /dev/null 2>&1
#13 elif [ "$ACTION" == "DELETE" ]; then
#14 MEDIA=`echo $LINE | awk '{ print $3 }'`
#15 curl --data chat_id=123456789 --data-urlencode "text=from pi: media - $MEDIA removed on $DATE" "https://api.telegram.org/bot12345678:ABCDEDFGHJKghij-zcsdkasdsjfs/sendMessage" > /dev/null 2>&1
#16 sleep 5
#17 curl --data chat_id=987654321--data-urlencode "text=from pi: new media - $MEDIA removed on $DATE" "https://api.telegram.org/bot12345678:ABCDEDFGHJKghij-zcsdkasdsjfs/sendMessage" > /dev/null 2>&1
#18 fi
so lets see now what happens when a file gets added to the folders that we setup our watches on. do we get the notification as expected...
Click to englarge me

now lets just try one more thing. i use "vnstati" to generate network stats. to install vnstati you can use the code below.
#1 sudo apt-get update
#2 sudo apt-get install vnstati
#3 vnstati -d -i eth0 -o path-to-the-stat-file.png
#4 curl -s -X POST "https://api.telegram.org/bot12345678:ABCDEDFGHJKghij-zcsdkasdsjfs/sendPhoto" -F chat_id=123456789 -F photo="@/admin/vnstats/daily-eth0.png" > /dev/null 2>&1
#5 vnstati -d -i eth1 -o path-to-the-stat-file.png
#6 curl -s -X POST "https://api.telegram.org/bot12345678:ABCDEDFGHJKghij-zcsdkasdsjfs/sendPhoto" -F chat_id=123456789 -F photo="@/admin/vnstats/daily-eth1.png" > /dev/null 2>&1
Click to englarge me
i have created a job to get run this script every day/month to see bandwidth usage. thats it folks...let me know if you implement it and if there are any questions I will be ready to answer them.

concept and design: harshdevX