Refactor notifications and add helper functions (#182)

* Refactor notifications and add helper functions

* Add helper functions to simplify sourcing files and executing functions if they exist

* Create notify_v2.sh wrapper script

* Simplify and consolidate notification logic within notify_v2.sh

* Support notification management via environment variables

* Move secrets to dockcheck.config

* Fix NOTIFY_CHANNELS default value when not set

* Feedback changes

* Remove leading spaces from MessageBody

* Check for valid notify v2 variables

* Warn on missing configuration and bypass notifications

* Update readme

* Additional feedback fixes

* More comments in default.config with different # depth for comments and settings

* Rename NOTIFY_TOPIC_NAME variable to NTFY_TOPIC_NAME for consistency

* Add TELEGRAM_TOPIC_ID

* Fix AppriseURL variable

* Add an ending newline to all MessageBody statements for consistency

* Remove troubleshooting echo statement

* Prevent attempting to trigger notifications for notification templates if versions are the same

---------

Co-authored-by: Matthew Oleksowicz <matt@everyoneneeds.it>
This commit is contained in:
vorezal 2025-05-25 12:26:13 -04:00 committed by GitHub
parent e4b93d113c
commit c63e2441fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 366 additions and 564 deletions

View file

@ -1,61 +1,30 @@
### DISCLAIMER: This is a third party addition to dockcheck - best effort testing.
NOTIFY_GOTIFY_VERSION="v0.2"
NOTIFY_GOTIFY_VERSION="v0.3"
#
# Copy/rename this file to notify.sh to enable the notification snippet.
# Required receiving services must already be set up.
# Modify to fit your setup - set GotifyUrl and GotifyToken.
# Do not modify this file directly. Set GOTIFY_TOKEN and GOTIFY_DOMAIN in your dockcheck.config file.
FromHost=$(hostname)
if [[ -z "${GOTIFY_TOKEN:-}" ]] || [[ -z "${GOTIFY_DOMAIN:-}" ]]; then
printf "Gotify notification channel enabled, but required configuration variables are missing. Gotify notifications will not be sent.\n"
trigger_notification() {
# Modify to fit your setup:
GotifyToken="Your Gotify token here"
GotifyUrl="https://api.gotify/message?token=${GotifyToken}"
remove_channel gotify
fi
if [[ "$PrintMarkdownURL" == true ]]; then
ContentType="text/markdown"
else
ContentType="text/plain"
fi
trigger_gotify_notification() {
GotifyToken="${GOTIFY_TOKEN}" # e.g. GOTIFY_TOKEN=token-value
GotifyUrl="${GOTIFY_DOMAIN}/message?token=${GotifyToken}" # e.g. GOTIFY_URL=https://gotify.domain.tld
JsonData=$( jq -n \
--arg body "$MessageBody" \
--arg title "$MessageTitle" \
--arg type "$ContentType" \
'{message: $body, title: $title, priority: 5, extras: {"client::display": {"contentType": $type}}}' )
if [[ "$PrintMarkdownURL" == true ]]; then
ContentType="text/markdown"
else
ContentType="text/plain"
fi
curl -s -S --data "${JsonData}" -H 'Content-Type: application/json' -X POST "${GotifyUrl}" 1> /dev/null
}
send_notification() {
[ -s "$ScriptWorkDir"/urls.list ] && releasenotes || Updates=("$@")
UpdToString=$( printf '%s\\n' "${Updates[@]}" )
# platform specific notification code would go here
printf "\nSending Gotify notification\n"
# Setting the MessageTitle and MessageBody variable here.
MessageTitle="${FromHost} - updates available."
printf -v MessageBody "Containers on $FromHost with updates available:\n$UpdToString"
trigger_notification
}
### Rename (eg. disabled_dockcheck_notification), remove or comment out the following function
### to not send notifications when dockcheck itself has updates.
dockcheck_notification() {
printf "\nSending Gotify dockcheck notification\n"
MessageTitle="$FromHost - New version of dockcheck available."
# Setting the MessageBody variable here.
printf -v MessageBody "Installed version: $1 \nLatest version: $2 \n\nChangenotes: $3"
RawNotifyUrl="https://raw.githubusercontent.com/mag37/dockcheck/main/notify_templates/notify_gotify.sh"
LatestNotifyRelease="$(curl -s -r 0-150 $RawNotifyUrl | sed -n "/NOTIFY_GOTIFY_VERSION/s/NOTIFY_GOTIFY_VERSION=//p" | tr -d '"')"
if [[ "$NOTIFY_GOTIFY_VERSION" != "$LatestNotifyRelease" ]] ; then
printf -v NotifyUpdate "\n\nnotify_gotify.sh update avialable:\n $NOTIFY_GOTIFY_VERSION -> $LatestNotifyRelease\n"
MessageBody="${MessageBody}${NotifyUpdate}"
fi
trigger_notification
JsonData=$( jq -n \
--arg body "$MessageBody" \
--arg title "$MessageTitle" \
--arg type "$ContentType" \
'{message: $body, title: $title, priority: 5, extras: {"client::display": {"contentType": $type}}}' )
curl -s -S --data "${JsonData}" -H 'Content-Type: application/json' -X POST "${GotifyUrl}" 1> /dev/null
}