Skip to content

Instantly share code, notes, and snippets.

@sinancetinkaya
Last active December 15, 2023 21:19
Show Gist options
  • Save sinancetinkaya/7baf5b2b4748fc26bdc30ecb9e577022 to your computer and use it in GitHub Desktop.
Save sinancetinkaya/7baf5b2b4748fc26bdc30ecb9e577022 to your computer and use it in GitHub Desktop.
OpenWRT Telegram message sender
#!/bin/sh
# This script sends TELEGRAM_MESSAGE to TELEGRAM_CHAT_ID when DEVICE_MAC is connected to the router
# Requires curl: opkg install curl
# Copy this file to /etc/hotplug.d/dhcp/ in OpenWRT
ONCE_IN_SECONDS="86400" # once a day
DEVICE_MAC=""
TELEGRAM_BOT_TOKEN=''
TELEGRAM_CHAT_ID=""
TELEGRAM_MESSAGE="Welcome home :)"
EXPIRE_TIME_FILE="/tmp/telegram_"$TELEGRAM_CHAT_ID"_expire"
# logger -t telegram "ACTION=$ACTION, MACADDR=$MACADDR, IPADDR=$IPADDR, HOSTNAME=$HOSTNAME"
if [ -f "$EXPIRE_TIME_FILE" ] && [ `cat "$EXPIRE_TIME_FILE"` -gt "$EPOCHSECONDS" ]; then exit 0;fi
if [ "$ACTION" = "update" ] && [ "$MACADDR" = "$DEVICE_MAC" ]; then
output=$(curl -ks https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage -d chat_id=$TELEGRAM_CHAT_ID -d parse_mode=HTML -d text="$TELEGRAM_MESSAGE")
logger -t telegram "$output"
echo "$((EPOCHSECONDS+ONCE_IN_SECONDS))">"$EXPIRE_TIME_FILE"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment