From 61735e18128a69432d787f380169df0ac1adcd12 Mon Sep 17 00:00:00 2001 From: Tobias Strobel Date: Sun, 25 Dec 2022 22:06:09 +0100 Subject: [PATCH] Add notify hook 'ntfy' for acme.sh --- ntfy.sh | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 ntfy.sh diff --git a/ntfy.sh b/ntfy.sh new file mode 100644 index 0000000..c4afbaf --- /dev/null +++ b/ntfy.sh @@ -0,0 +1,87 @@ +#!/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 +} \ No newline at end of file