scripts/ntfy.sh

87 lines
No EOL
2.3 KiB
Bash

#!/usr/bin/env sh
#Support ntfy
#NTFY_URL="https://ntfy.sh"
#NTFY_TOPIC="123456789ABCDEF"
#optional
# Message priority, see https://docs.ntfy.sh/publish/#message-priority
#NTFY_PRIORITY_SUCCESS=3
#NTFY_PRIORITY_ERROR=3
#NTFY_PRIORITY_SKIPPED=3
# subject content statusCode
ntfy_send() {
_subject="$1"
_content="$2"
_statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
_debug "_subject" "$_subject"
_debug "_content" "$_content"
_debug "_statusCode" "$_statusCode"
NTFY_URL="${NTFY_URL:-$(_readaccountconf_mutable NTFY_URL)}"
if [ -z "$NTFY_URL" ]; then
NTFY_URL="https://ntfy.sh"
_debug "No ntfy url specified, using default: ${NTFY_URL}"
fi
_saveaccountconf_mutable NTFY_URL "$NTFY_URL"
NTFY_TOPIC="${NTFY_TOPIC:-$(_readaccountconf_mutable NTFY_TOPIC)}"
if [ -z "$NTFY_TOPIC" ]; then
NTFY_TOPIC=""
_err "You didn't specify the ntfy topic NTFY_TOPIC."
return 1
fi
_saveaccountconf_mutable NTFY_TOPIC "$NTFY_TOPIC"
NTFY_PRIORITY_SUCCESS="${NTFY_PRIORITY_SUCCESS:-$(_readaccountconf_mutable NTFY_PRIORITY_SUCCESS)}"
if [ -z "$NTFY_PRIORITY_SUCCESS" ]; then
NTFY_PRIORITY_SUCCESS=3
else
_saveaccountconf_mutable NTFY_PRIORITY_SUCCESS "$NTFY_PRIORITY_SUCCESS"
fi
NTFY_PRIORITY_ERROR="${NTFY_PRIORITY_ERROR:-$(_readaccountconf_mutable NTFY_PRIORITY_ERROR)}"
if [ -z "$NTFY_PRIORITY_ERROR" ]; then
NTFY_PRIORITY_ERROR=3
else
_saveaccountconf_mutable NTFY_PRIORITY_ERROR "$NTFY_PRIORITY_ERROR"
fi
NTFY_PRIORITY_SKIPPED="${NTFY_PRIORITY_SKIPPED:-$(_readaccountconf_mutable NTFY_PRIORITY_SKIPPED)}"
if [ -z "$NTFY_PRIORITY_SKIPPED" ]; then
NTFY_PRIORITY_SKIPPED=3
else
_saveaccountconf_mutable NTFY_PRIORITY_SKIPPED "$NTFY_PRIORITY_SKIPPED"
fi
_content=$(echo "$_content" | _json_encode)
_subject=$(echo "$_subject" | _json_encode)
case "$_statusCode" in
0)
_priority=$NTFY_PRIORITY_SUCCESS
;;
1)
_priority=$NTFY_PRIORITY_ERROR
;;
2)
_priority=$NTFY_PRIORITY_SKIPPED
;;
esac
_data="{\"topic\": \"${NTFY_TOPIC}\", \"title\": \"${_subject}\", \"message\": \"${_content}\", \"priority\": ${_priority:-3}}"
response="$(_post "${_data}" "${NTFY_URL}" "" "POST" "application/json")"
if [ "$?" != "0" ]; then
_err "Failed to send message"
_err "$response"
return 1
fi
_debug2 response "$response"
return 0
}