Files
bash/pushover.sh
2025-08-27 09:14:20 +02:00

241 lines
6.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# ./pushover.sh -t "Pushover via Bash" -m "Pushover message sent with bash from $(hostname -f)" -p1 -s siren -u http://www.google.com -n "Google"
dotenv () {
set -a
# shellcheck disable=SC1091
[ -f "$HOME/.env" ] && . "$HOME/.env" || echo -e "${red}\nNo $HOME/.env file found ! Could'nt get update from Github.'.${reset}"
set +a
}
dotenv
USER_TOKEN=$USER_KEY
# YOUR APPS TOKENS / UPPERCASE NAME WITH _TOKEN (usage: "-a monitor" uses MONITOR_TOKEN)
BASH_TOKEN=$BASH_APP
SYNO_TOKEN=$SYNO_APP
APP_LIST="bash, syno" # FOR USAGE
APP_ID="bash" # Default app
# v1.8
# 24-08-2024 :
# 14-03-2018 : - Request send only necessary fields.
# - Added message pipe.
# - Added monospace option.
# - Default monitor name
# 12-03-2018 : Added image attachment.
# 30-01-2016 : Added -getopts- arguments to set retry/expire.
# 23-04-2015 : HTML markup language option.
VERSION=1.8
TITLE="<empty>"
URL="<empty>"
URL_TITLE="untitled"
PRIORITY=0
RETRY=60
EXPIRE=86400
SOUND="pushover"
HTML=0
MONOSPACE=0
DEVICE="all"
IMAGE=
DEBUG=0
usage()
{
cat << EOF
usage: $0 options
Push notifications to your Android, iOS, or desktop devices
OPTIONS:
-a Application name : "$APP_LIST" (required)
-m Message (required) If the -f (html) flag = 1, message must be surrounded by a single quote
Example: -m \"'This is a <b>HTML</b> test'\"
-t Title of your notification
-d Send to a specific device name Comma seperated list of devices to receive message
-p Priority of your message : -2 (Silent), -1 (Quiet), 0 (Normal), 1 (High), 2 (Emergency)
-s Sound (https://pushover.net/api#sounds):
pushover - Pushover (default)
bike - Bike
bugle - Bugle
cashregister - Cash Register
classical - Classical
cosmic - Cosmic
falling - Falling
gamelan - Gamelan
incoming - Incoming
intermission - Intermission
magic - Magic
mechanical - Mechanical
pianobar - Piano Bar
siren - Siren
spacealarm - Space Alarm
tugboat - Tug Boat
alien - Alien Alarm (long)
climb - Climb (long)
persistent - Persistent (long)
echo - Pushover Echo (long)
updown - Up Down (long)
none - None (silent)
-i Attach an image (up to 2.5mb)
-u URL Link
-n URL Title
-r Retry (seconds) Set retry period for notifications with priority 2 (default ${RETRY})"
-e Expire (seconds) Set expiration time for notifications with priority 2 (default ${EXPIRE})"
-f HTML Format Enable HTML formatting, cannot be used with the -k (monospace) flag
-k Monospace Format Enable monospace messages, cannot be used with the -f (html) flag
-x Debug Must be the first argument !!
-z Show some examples
-h Show this message
EOF
}
examples()
{
local script=`basename "$0"`
echo "EXAMPLES:"
echo
echo " ${script} -a \"bash\" -m \"This is a test\""
echo " Sends a simple \"This is a test\" message to all devices."
echo
echo " ${script} -a \"bash\" -m \"This is a test\" -t \"Test Title\""
echo " Sends a simple \"This is a test\" message with the title \"Test Title\" to all devices."
echo
echo " ${script} -a \"bash\" -m \"This is a test\" -d \"pixel8,Safari_airbook\""
echo " Sends a simple \"This is a test\" message to the devices named \"pixel8\" and \"Safari_airbook\"."
echo
echo " ${script} -a \"bash\" -m \"This is a test\" -u \"http://www.google.com\" -n Google"
echo " Sends a simple \"This is a test\" message to all devices that contains a link to www.google.com titled \"Google\"."
echo
echo " ${script} -a \"bash\" -m \"This is a test\" -p 1"
echo " Sends a simple \"This is a test\" high priority message to all devices."
echo
echo " ${script} -a \"bash\" -m \"This is a test\" -s bike"
echo " Sends a simple \"This is a test\" message to all devices that uses the sound of a bike bell as the notification sound."
echo
echo " ${script} -a \"bash\" -m \"This is a test Pic\" -i /path/to/pic.jpg "
echo " Sends a simple \"This is a test Pic\" message to all devices and send the Picture with the message."
echo
echo " ${script} -a \"bash\" -m \"'This is a <b>HTML</b> test'\" -f 1 "
echo " Sends a simple \"This is a HTML test\" message with html tag"
echo
}
ARGS=( -F "user=$USER_TOKEN" )
# MESSAGE PIPE
if [ -p /dev/stdin ]
then
MESSAGE=$(</dev/stdin)
ARGS+=( -F "message=$MESSAGE" )
else
MESSAGE=
fi
while getopts “hfkvt:r:e:u:n:p:s:m:a:d:i:xz” OPTION
do
case $OPTION in
t) TITLE=$OPTARG
ARGS+=( -F "title=$TITLE" ) ;;
u) URL=$OPTARG
ARGS+=( -F "url=$URL" ) ;;
n) URL_TITLE=$OPTARG
ARGS+=( -F "url_title=$URL_TITLE" ) ;;
p) PRIORITY=$OPTARG
ARGS+=( -F "priority=$PRIORITY" ) ;;
s) SOUND=$OPTARG
ARGS+=( -F "sound=$SOUND" ) ;;
f) HTML=1
ARGS+=( -F "html=$HTML" ) ;;
k) MONOSPACE=1
ARGS+=( -F "monospace=$MONOSPACE" ) ;;
r) [ ! -z $OPTARG ] && RETRY=$OPTARG ;;
e) [ ! -z $OPTARG ] && EXPIRE=$OPTARG ;;
d) DEVICE=$OPTARG
ARGS+=( -F "device=$DEVICE" ) ;;
i) IMAGE="$OPTARG"
ARGS+=( -F "attachment=@${IMAGE}" ) ;;
a)
APP_ID="$OPTARG"
;;
m)
if [[ -z $MESSAGE ]]
then
MESSAGE=$OPTARG
ARGS+=( -F "message=$MESSAGE" )
#ARGS+=( -F "message='$MESSAGE'" )
fi
;;
v) echo "Pushover shell script version ${VERSION}" && exit 1 ;;
x) DEBUG=1 ;;
:) echo "Option -$OPTARG requires an argument." >&2; exit 1 ;;
h) usage; exit 1 ;;
z) examples; exit 1 ;;
?) usage; exit ;;
esac
done
# APP TOKEN
if [[ ! -z $APP_ID ]]
then
APP_ID=`echo $APP_ID | tr '[:lower:]' '[:upper:]'`
APP_NAME="${APP_ID}_TOKEN"
APP_TOKEN="${!APP_NAME}"
ARGS+=( -F "token=$APP_TOKEN" )
fi
# EMERGENCY PRIORITY
if [[ $PRIORITY == 2 ]]
then
ARGS+=( -F "retry=$RETRY" )
ARGS+=( -F "expire=$EXPIRE" )
fi
# REQUIRED FIELDS
if [[ -z $MESSAGE ]] || [[ -z $APP_TOKEN ]]
then
echo -e "\n\\033[31mMessage and Application token are required.\\033[0m"
usage
exit 1
fi
if [[ $HTML == 1 ]]
then
MESSAGE="'$MESSAGE'"
fi
# DEBUG PRINT
if [[ $DEBUG == 1 ]]
then
echo "TITLE ...... $TITLE"
echo "DEVICE ..... $DEVICE"
echo "URL ........ ${URL} (${URL_TITLE})"
echo "FORMAT ..... HTML ${HTML} MONOSPACE ${MONOSPACE}"
echo "APP ........ ID:${APP_ID} TOKEN:${APP_TOKEN}"
echo "PRIORITY ... $PRIORITY"
if [[ $PRIORITY == 2 ]]
then
echo "RETRY ...... $RETRY"
echo "EXPIRE ..... $EXPIRE"
fi
echo "SOUND ...... $SOUND"
echo "IMAGE ..... $IMAGE"
echo "MESSAGE -----------------------------------------"
echo "${MESSAGE}"
echo "-------------------------------------------------"
exit 0
fi
# SEND NOTIFICATION
curl -s "${ARGS[@]}" https://api.pushover.net/1/messages.json
#echo "${ARGS[@]}"