Instantly share code, notes, and snippets.

Embed
What would you like to do?
Raspberry Pi setup to send IP changes via telegram
# /lib/dhcpcd/dhcpcd-hooks/99-telegram
#
# Notify telegram chat of ip changes
# man dhcpcd-run-hooks for variables
TELEGRAM=/home/pi/bin/telegram
if $if_up; then
case "$reason" in
BOUND|BOUND6)
if [ "${new_ip_address}" != "${old_ip_address}" ]; then
$TELEGRAM "$(hostname) (Raspberry) got ${new_ip_address} on ${interface}. Was ${old_ip_address}."
fi
;;
REBOOT)
$TELEGRAM "$(hostname) (Raspberry) got ${new_ip_address} on ${interface} after reboot. Uptime: `uptime`"
;;
*)
$TELEGRAM "$(hostname) (Raspberry) got ${new_ip_address} on ${interface}. Was ${old_ip_address}. Reason: ${reason}"
;;
esac
fi
#!/bin/bash
# usage: telegram <message>
# strict mode
set -euo pipefail
# Fill these
API_KEY=''
CHAT_ID=''
function sendMessage {
echo "sending"
# local MSG="${1:?No message given}"
curl -i -X GET -G \
--data-urlencode "chat_id=${CHAT_ID}" \
--data-urlencode "text=${1:?No message given}" \
"https://api.telegram.org/bot${API_KEY}/sendMessage";
}
sendMessage "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment