One of the issues when using Raspberry Pi computers is that you have to hook up a monitor to find the IP address when plugging in to a new network, even if you really only need ssh access. To get around this, I created a solution to send the IP address to my phone every time the Pi starts.

Telegram is a chat app (like Whatsapp) but with good support for bots. I created a bot to run on my raspberry pi to notify me about IP address changes. Whenever I plug the Pi into a new network it should send me a message with the new address so I can ssh/vpn into it.

First, register a new bot name with the botfather by sending him a message on telegram. Mine is called HalfTauBot based on a bad pun. The BotFather will give you a API Token for your new bot, which is a long random string.

After registering the bot, start a new chat with the bot (https://telegram.me/halftaubot) and send it a message. Check for the new message using the following command:

$ curl https://api.telegram.org/bot/getUpdates
{"ok":true,"result":[{"update_id":34279399,
"message":{"message_id":1,"from":{"id":XXXXXX,"is_bot":false,"first_name":"Spencer","last_name":"Bliven","language_code":"en-US"},"chat":{"id":YYYYYY,"first_name":"Spencer","last_name":"Bliven","type":"private"},"date":1510074905,"text":"Hello there"}}]}

The important piece here is the CHAT_ID (YYYYYY), which we use to send messages to this conversation.

Sending messages from the pi is easy. For two-way communication you would want to use a library like the telegram-cli or the python-telegram-bot, but for just sending messages a bash script is enough. (See gist for my most up-to-date version)

#!/bin/bash
# strict mode
set -euo pipefail

API_KEY='<api_key>'
CHAT_ID='<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 "$@"

Save it as telegram and run chmod +x telegram. Then, you can send messages from the Pi like so:

telegram "Message from Raspberry Pi"

Now that sending messages is working, we want the pi to send a message whenever the IP address changes. This is done by the dhcpcd daemon by adding a bash script to /lib/dhcpcd/dhcpcd-hooks/99-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

You should get a message after each reboot or IP address change.

Leave a Reply

Your email address will not be published. Required fields are marked *