#!/bin/bash ## Send a notification with NTFY and check if the config file is complete if [[ "$UID" == "0" ]]; then NTFY_CONFIG_FILE="/etc/ntfy/ntfy.conf" else NTFY_CONFIG_FILE=~/.config/ntfy/ntfy.conf fi SERVER="https://ntfy.0k.io/" if ! [ -e "$NTFY_CONFIG_FILE" ]; then mkdir -p "${NTFY_CONFIG_FILE%/*}" ## default option to change if needed echo "SERVER=$SERVER" > "$NTFY_CONFIG_FILE" elif ! grep -q "^SERVER=" "$NTFY_CONFIG_FILE"; then echo "SERVER=$SERVER" >> "$NTFY_CONFIG_FILE" fi source "$NTFY_CONFIG_FILE" for var in SERVER LOGIN PASSWORD; do if ! [ -v "$var" ]; then echo "Error: missing $var in $NTFY_CONFIG_FILE" exit 1 fi done exname=${0##*/} channel="main" usage="Usage: $exname [-c CHANNEL] [-t TITLE ] MESSAGE ---------------------------------------------- --- Send MESSAGE with TITLE to the specified CHANNEL. --- ---------------------------------------------- If no CHANNEL is provided, the message will be sent to the default channel Default CHANNEL is format as follow : ConfiguredLOGIN_${default_channel} If no TITLE is provided, the message will be sent with the hostname as title." while [[ $# -gt 0 ]]; do arg="$1" shift case "$arg" in -h|--help) echo "$usage" exit 0 ;; -c|--channel) channel="$1" [ -z "$channel" ] || { echo "Error: no argument for channel option." >&2 echo "$usage" >&2 exit 1 } shift ;; -t|--title) title="$1" [ -z "$title" ] || { echo "Error: no argument for title option." >&2 echo "$usage" >&2 exit 1 } shift ;; *) [ -z "$message" ] && { message="$arg"; continue; } echo "Error : Unexpected positional argument '$arg'." >&2 echo "$usage" >&2 exit 1 ;; esac done [ -n "$message" ] || { echo "Error: missing message." >&2 echo "$usage" >&2 exit 1 } curl_opts=( -s -u "$LOGIN:$PASSWORD" -d "$message" ) if [ -n "$title" ]; then curl_opts+=(-H "Title: [$(hostname)] $title") fi curl "${curl_opts[@]}" "$SERVER/${LOGIN}_$channel" > /dev/null