Push Motion Image Capture to Telegram

I use the fantastic motion project to monitor my apartment activity when I’m not home (not happening until God knows when these days…). I wanted it to warn me when there’s movement more interactively than with a basic email. Telegram and its very well documented bot API is really perfect for this task, so I came up with the following motion configuration:

on_picture_save /bin/sh /home/imil/bin/on_picture_save.sh %f

and more importantly, the following script:

#!/bin/sh

. ${HOME}/.tg

ffile=${HOME}/tmp/motion_capture

f=${1%?????????}

mplayer ${HOME}/Music/42899__freqman__canon-dos-d30-no-focus.wav

[ -e ${ffile} ] && [ "$(cat $ffile)" = "$f" ] && exit 0

curl -X POST "https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendPhoto" \
 -F chat_id=${CHATID} -F photo="@$1"

echo $f > $ffile

the ~/.tg file has the following content:

TELEGRAM_TOKEN="12345678:xxxxxxxxxxxxxxxxxx-yyyyyyyyyyyyyyyy"
CHATID="987654321"
export TELEGRAM_TOKEN CHATID

Now every time there’s movement I’ll receive a capture on my Telegram’s bot channel (plus I’ll locally play a cool Canon camera sound ;) ).