Refactor notification send/skip logic. Adjust missing variable return codes.

This commit is contained in:
Matthew Oleksowicz 2025-08-29 00:56:40 -04:00
parent 51c9987d45
commit 4bfe28265f
12 changed files with 70 additions and 36 deletions

View file

@ -24,7 +24,7 @@ trigger_HA_notification() {
printf "The ${HA_channel} notification channel is enabled, but required configuration variables are missing. Home assistant notifications will not be sent.\n"
remove_channel HA
return 1
return 0
fi
AccessToken="${!HATokenVar}"

View file

@ -23,7 +23,7 @@ trigger_apprise_notification() {
printf "The ${apprise_channel} notification channel is enabled, but required configuration variables are missing. Apprise notifications will not be sent.\n"
remove_channel apprise
return 1
return 0
fi
if [[ -n "${!ApprisePayloadVar:-}" ]]; then

View file

@ -21,7 +21,7 @@ trigger_discord_notification() {
printf "The ${discord_channel} notification channel is enabled, but required configuration variables are missing. Discord notifications will not be sent.\n"
remove_channel discord
return 1
return 0
fi
DiscordWebhookUrl="${!DiscordWebhookUrlVar}" # e.g. DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/<token string>

View file

@ -22,7 +22,7 @@ trigger_gotify_notification() {
printf "The ${gotify_channel} notification channel is enabled, but required configuration variables are missing. Gotify notifications will not be sent.\n"
remove_channel gotify
return 1
return 0
fi
GotifyToken="${!GotifyTokenVar}" # e.g. GOTIFY_TOKEN=token-value

View file

@ -23,7 +23,7 @@ trigger_matrix_notification() {
printf "The ${matrix_channel} notification channel is enabled, but required configuration variables are missing. Matrix notifications will not be sent.\n"
remove_channel matrix
return 1
return 0
fi
AccessToken="${!AccessTokenVar}" # e.g. MATRIX_ACCESS_TOKEN=token-value

View file

@ -23,7 +23,7 @@ trigger_ntfy_notification() {
printf "The ${ntfy_channel} notification channel is enabled, but required configuration variables are missing. Ntfy notifications will not be sent.\n"
remove_channel ntfy
return 1
return 0
fi
NtfyUrl="${!NtfyDomainVar}/${!NtfyTopicNameVar}"

View file

@ -23,7 +23,7 @@ trigger_pushbullet_notification() {
printf "The ${pushbullet_channel} notification channel is enabled, but required configuration variables are missing. Pushbullet notifications will not be sent.\n"
remove_channel pushbullet
return 1
return 0
fi
PushUrl="${!PushUrlVar}" # e.g. PUSHBULLET_URL=https://api.pushbullet.com/v2/pushes

View file

@ -24,7 +24,7 @@ trigger_pushover_notification() {
printf "The ${pushover_channel} notification channel is enabled, but required configuration variables are missing. Pushover notifications will not be sent.\n"
remove_channel pushover
return 1
return 0
fi
PushoverUrl="${!PushoverUrlVar}" # e.g. PUSHOVER_URL=https://api.pushover.net/1/messages.json

View file

@ -22,7 +22,7 @@ trigger_slack_notification() {
printf "The ${slack_channel} notification channel is enabled, but required configuration variables are missing. Slack notifications will not be sent.\n"
remove_channel slack
return 1
return 0
fi
AccessToken="${!AccessTokenVar}" # e.g. SLACK_ACCESS_TOKEN=some-token

View file

@ -38,7 +38,7 @@ trigger_smtp_notification() {
printf "The ${smtp_channel} notification channel is enabled, but required configuration variables are missing. SMTP notifications will not be sent.\n"
remove_channel smtp
return 1
return 0
fi
SendMailFrom="${!SendMailFromVar}" # e.g. MAIL_FROM=me@mydomain.tld

View file

@ -23,7 +23,7 @@ trigger_telegram_notification() {
printf "The ${telegram_channel} notification channel is enabled, but required configuration variables are missing. Telegram notifications will not be sent.\n"
remove_channel telegram
return 1
return 0
fi
if [[ "$PrintMarkdownURL" == true ]]; then

View file

@ -229,9 +229,39 @@ format_output() {
fi
}
skip_notification() {
# Skip notification logic. Default to false. Handle all cases, and only those cases, where notifications should be skipped.
local SkipNotification="false"
local Channel="$1"
local UnsnoozedCount="$2"
local NotificationType="$3"
if [[ $(containers_only "${Channel}") == "true" ]] && [[ "${NotificationType}" != "container" ]]; then
# Do not send notifications through channels only configured for container update notifications
SkipNotification="true"
else
# Handle empty update cases separately
if [[ -z "${UpdToString}" ]]; then
if [[ $(allow_empty "${Channel}") == "false" ]]; then
# Do not send notifications if there are none and allow_empty is false
SkipNotification="true"
fi
else
if [[ $(skip_snooze "${Channel}") == "false" ]] && [[ ${UnsnoozedCount} -eq 0 ]]; then
# Do not send notifications if there are any, they are all snoozed, and skip_snooze is false
SkipNotification="true"
fi
fi
fi
printf "${SkipNotification}"
}
send_notification() {
[[ -s "$ScriptWorkDir"/urls.list ]] && releasenotes || Updates=("$@")
[[ -n "${snooze}" ]] && cleanup_snooze "${Updates[@]}"
UnsnoozedContainers=$(unsnoozed_count "${Updates[@]}")
NotifyError=false
Notified="false"
@ -246,15 +276,16 @@ send_notification() {
UpdToString=${UpdToString%\\n}
for channel in "${enabled_notify_channels[@]}"; do
local template=$(get_channel_template "${channel}")
local SkipNotification=$(skip_notification "${channel}" "${UnsnoozedContainers}" "container")
if [[ "${SkipNotification}" == "false" ]]; then
local template=$(get_channel_template "${channel}")
# Formats UpdToString variable per channel settings
format_output "container_update" "$(output_format "${channel}")" "🐋 Containers on $FromHost with updates available:\n<insert_text_cu>\n"
# Formats UpdToString variable per channel settings
format_output "container_update" "$(output_format "${channel}")" "🐋 Containers on $FromHost with updates available:\n<insert_text_cu>\n"
# Setting the MessageBody variable here.
printf -v MessageBody "${FormattedOutput}"
# Setting the MessageBody variable here.
printf -v MessageBody "${FormattedOutput}"
if { { [[ "${MessageBody}" == "None" ]] || [[ "${MessageBody}" == '{"updates": []}' ]]; } && [[ $(allow_empty "${channel}") == "true" ]]; } || { [[ $(skip_snooze "${channel}") == "true" ]] || [[ ${UnsnoozedContainers} -gt 0 ]]; }; then
printf "\nSending ${channel} notification"
exec_if_exists_or_fail trigger_${template}_notification "${channel}" || \
printf "\nAttempted to send notification to channel ${channel}, but the function was not found. Make sure notify_${template}.sh is available in the ${ScriptWorkDir} directory or notify_templates subdirectory."
@ -263,10 +294,9 @@ send_notification() {
done
if [[ "${Notified}" == "true" ]]; then
[[ -n "${snooze}" ]] && [[ "${NotifyError}" == "false" ]] && [[ "${FormattedOutput}" != "None" ]] && [[ "${Notified}" == "true" ]] && update_snooze "${Updates[@]}"
[[ -n "${snooze}" ]] && [[ -n "${UpdToString}" ]] && [[ "${NotifyError}" == "false" ]] && update_snooze "${Updates[@]}"
printf "\n"
fi
[[ -n "${snooze}" ]] && [[ "${FormattedOutput}" != "None" ]] && cleanup_snooze "${Updates[@]}"
return 0
}
@ -275,6 +305,7 @@ send_notification() {
### to not send notifications when dockcheck itself has updates.
dockcheck_notification() {
if [[ ! "${DISABLE_DOCKCHECK_NOTIFICATION:-}" == "true" ]]; then
UnsnoozedDockcheck=$(unsnoozed_count "dockcheck\.sh")
NotifyError=false
Notified=false
@ -282,15 +313,16 @@ dockcheck_notification() {
UpdToString="dockcheck.sh,$1,$2,\"$3\""
for channel in "${enabled_notify_channels[@]}"; do
local template=$(get_channel_template "${channel}")
local SkipNotification=$(skip_notification "${channel}" "${UnsnoozedDockcheck}" "dockcheck")
if [[ "${SkipNotification}" == "false" ]]; then
local template=$(get_channel_template "${channel}")
# Formats UpdToString variable per channel settings
format_output "dockcheck_update" "$(output_format "${channel}")" "Installed version: <insert_text_iv>\nLatest version: <insert_text_lv>\n\nChangenotes: <insert_text_rn>\n" "$1" "$2" "$3"
# Formats UpdToString variable per channel settings
format_output "dockcheck_update" "$(output_format "${channel}")" "Installed version: <insert_text_iv>\nLatest version: <insert_text_lv>\n\nChangenotes: <insert_text_rn>\n" "$1" "$2" "$3"
# Setting the MessageBody variable here.
printf -v MessageBody "${FormattedOutput}"
# Setting the MessageBody variable here.
printf -v MessageBody "${FormattedOutput}"
if { { [[ "${MessageBody}" == "None" ]] || [[ "${MessageBody}" == '{"updates": []}' ]]; } && [[ $(allow_empty "${channel}") == "true" ]]; } && { [[ $(skip_snooze "${channel}") == "true" ]] || [[ $(is_snoozed "dockcheck\.sh") == "false" ]]; } && [[ $(containers_only "${channel}") == "false" ]]; then
printf "\nSending dockcheck update notification - ${channel}"
exec_if_exists_or_fail trigger_${template}_notification "${channel}" || \
printf "\nAttempted to send notification to channel ${channel}, but the function was not found. Make sure notify_${template}.sh is available in the ${ScriptWorkDir} directory or notify_templates subdirectory."
@ -333,6 +365,10 @@ notify_update_notification() {
fi
done
UpdatesPlusDockcheck=("${NotifyUpdates[@]}")
UpdatesPlusDockcheck+=("dockcheck.sh")
[[ -n "${snooze}" ]] && cleanup_snooze "${UpdatesPlusDockcheck[@]}"
UnsnoozedTemplates=$(unsnoozed_count "${NotifyUpdates[@]}")
MessageTitle="$FromHost - New version of notify templates available."
@ -342,15 +378,17 @@ notify_update_notification() {
UpdToString=${UpdToString%\\n}
for channel in "${enabled_notify_channels[@]}"; do
local template=$(get_channel_template "${channel}")
local SkipNotification=$(skip_notification "${channel}" "${UnsnoozedTemplates}" "notify")
# Formats UpdToString variable per channel settings
format_output "notify_update" "$(output_format "${channel}")" "Notify templates on $FromHost with updates available:\n<insert_text_nu>\n"
if [[ "${SkipNotification}" == "false" ]]; then
local template=$(get_channel_template "${channel}")
# Setting the MessageBody variable here.
printf -v MessageBody "${FormattedOutput}"
# Formats UpdToString variable per channel settings
format_output "notify_update" "$(output_format "${channel}")" "Notify templates on $FromHost with updates available:\n<insert_text_nu>\n"
# Setting the MessageBody variable here.
printf -v MessageBody "${FormattedOutput}"
if { { [[ "${MessageBody}" == "None" ]] || [[ "${MessageBody}" == '{"updates": []}' ]]; } && [[ $(allow_empty "${channel}") == "true" ]]; } && { [[ $(skip_snooze "${channel}") == "true" ]] || [[ ${UnsnoozedTemplates} -gt 0 ]]; } && [[ $(containers_only "${channel}") == "false" ]]; then
printf "\nSending notify template update notification - ${channel}"
exec_if_exists_or_fail trigger_${template}_notification "${channel}" || \
printf "\nAttempted to send notification to channel ${channel}, but the function was not found. Make sure notify_${template}.sh is available in the ${ScriptWorkDir} directory or notify_templates subdirectory."
@ -359,13 +397,9 @@ notify_update_notification() {
done
if [[ "${Notified}" == "true" ]]; then
[[ -n "${snooze}" ]] && [[ "${NotifyError}" == "false" ]] && [[ "${FormattedOutput}" != "None" ]] && [[ "${Notified}" == "true" ]] && update_snooze "${NotifyUpdates[@]}"
[[ -n "${snooze}" ]] && [[ -n "${UpdToString}" ]] && [[ "${NotifyError}" == "false" ]] && update_snooze "${NotifyUpdates[@]}"
printf "\n"
fi
UpdatesPlusDockcheck=("${NotifyUpdates[@]}")
UpdatesPlusDockcheck+=("dockcheck.sh")
[[ -n "${snooze}" ]] && [[ "${FormattedOutput}" != "None" ]] && cleanup_snooze "${UpdatesPlusDockcheck[@]}"
fi
return 0