Skip to content

Instantly share code, notes, and snippets.

@tpai
Last active July 6, 2024 10:41
Show Gist options
  • Save tpai/63e2c4d1e48a6d0931145f4043f1e02f to your computer and use it in GitHub Desktop.
Save tpai/63e2c4d1e48a6d0931145f4043f1e02f to your computer and use it in GitHub Desktop.
Send notification to a Telegram bot from your device (e.g., Raspberry Pi). It will be helpful when the device has a DHCP IP.
#!/bin/bash
telegram_bot_token="xxx" # ask @BotFather for your Telegram bot token
telegram_chat_id="xxx" # type /start on the bot, and `curl https://api.telegram.org/bot${telegram_bot_token}/getUpdates` to retrieve chat_id
telegram_api_url="https://api.telegram.org/bot${telegram_bot_token}/sendMessage"
send_to_telegram() {
local message="$1"
echo $message
curl -s -X POST "${telegram_api_url}" -H "Content-Type: application/json" -d "{\"chat_id\": \"$telegram_chat_id\", \"text\": \"$message\"}"
}
product=$(sudo lshw -class system -json | jq -r '.[].product')
sysinfo=$(uname -a)
mac=$(ip link show wlan0 | awk '/ether/ {print $2}')
until ip=$(ip addr show wlan0 | grep "inet " | awk '{print $2}'); [ -n "$ip" ]; do
echo "Awaiting network availability..."
sleep 1
done
send_to_telegram "$product"
send_to_telegram "$sysinfo"
send_to_telegram "$ip"
send_to_telegram "$mac"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment