Skip to content

Instantly share code, notes, and snippets.

@sbliven
Created November 7, 2017 17:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbliven/315abb0da476d741652fcbc98b1648bd to your computer and use it in GitHub Desktop.
Save sbliven/315abb0da476d741652fcbc98b1648bd to your computer and use it in GitHub Desktop.
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