diff --git a/README.md b/README.md index d4b4d14..70914f4 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ Script to be notified by your UPS with differents notifications : - Pushbullet - Pushover - SMS +- Discord ## Install - Copy git @@ -28,7 +29,7 @@ mkdir /var/log/nutNotify chown nut:nut /var/log/nutNotify cp nutNotify.logrotate /etc/logrotate.d ``` - +Activate nutNotify by editing `upsmon.conf` and replace "NOTIFYCMD" with `/usr/local/bin/nutNotify.sh`. ## Link Here an article (in french) in my website about [the notification of Nut by Telegram Pushbullet or Pushover](https://www.monlinux.net/2023/02/nut-notifications-push-telegram-pushbullet-pushover-pour-ups/) diff --git a/nutNotify.conf.txt b/nutNotify.conf.txt index 63430eb..f6cbb74 100644 --- a/nutNotify.conf.txt +++ b/nutNotify.conf.txt @@ -45,6 +45,10 @@ pushoverUserkey='xxxxxxxxxxxxxxxxxxxxxxxxxxxx' #UserKey pushoverSubject="UPS event $argument" #subject sent by Pushover pushoverMessage="$bodyDefault" #body message sent by Pushover +#--- Data for Discord ---# +discordWebhookURL='' +discordMessage="$bodyDefault" #body message sent by Discord + # methods to use on event (array to multi alert) # can be mail, telegram, pushober, pushbullet, sms # or an array like '(telegram mail)' for multiple methods @@ -56,4 +60,4 @@ methodLowbatt=$methodDefault methodFsd=$methodDefault methodShutdown=$methodDefault methodComm=$methodDefault -methodServerOnline=$methodDefault \ No newline at end of file +methodServerOnline=$methodDefault diff --git a/nutNotifyBoot.sh b/nutNotifyBoot.sh index b90b74c..50b2b13 100644 --- a/nutNotifyBoot.sh +++ b/nutNotifyBoot.sh @@ -12,7 +12,7 @@ if [ ! -e "$configFile" ] ; then fi function aide() { - echo "$0 [mail|pushbullet|telegram]" + echo "$0 [mail|pushbullet|telegram|discord]" } # verify method @@ -29,7 +29,9 @@ elif [ $# == 1 ] ; then echo "Error pushbullet not configured" 1>&2 && echo "Error telegram not configured" > $logfile && exit 1 elif [ "$1" == "telegram" ] && [ ! -x $curlBin ] && [ "$telegramAccessToken" != "" ] && [ "$telegramChatID" != "" ] ; then echo "Error telegram not configured" 1>&2 && echo "Error telegram not configured" > $logfile && exit 1 - elif [ "$1" == "mail" ] || [ "$1" == "pushbullet" ] || [ "$1" == "telegram" ] ; then + elif [ "$1" == "discord" ] && [ ! -x $curlBin ] && [ "$discordWebhookURL" != "" ] ; then + echo "Error discord is not configured" 1>&2 && echo "Error discord is not configured" > $logfile && exit 1 + elif [ "$1" == "mail" ] || [ "$1" == "pushbullet" ] || [ "$1" == "telegram" ] || [ "$1" == "discord" ] ; then notifynut_method="$1" else aide @@ -52,8 +54,10 @@ if [ -e $flagfile ] ; then sleep 30; sendTelegram "$text" "$telegramSubject" ;; pushover) sleep 30; sendPushover "$text" "$pushoverSubject" ;; + discord) + sleep 30; sendDiscord "$text" ;; esac rm $flagfile fi -exit 0 \ No newline at end of file +exit 0 diff --git a/nutNotifyFct.sh b/nutNotifyFct.sh index 8100589..9243dcb 100644 --- a/nutNotifyFct.sh +++ b/nutNotifyFct.sh @@ -156,6 +156,33 @@ function sendPushover { return $returnCurl } +function sendDiscord { + #replace default mesg + if [ "$2" != "" ] ; then + textDiscord="$2" + fi + + #var verification + if [ "$discordWebhookURL" == "" ]; then + echo "Can't send message without a Discord webhook URL" 1>&2 + addLog "Can't send message without a Discord webhook URL" + return 1 + fi + + tempjson=$(mktemp --suffix '.nutNotifyDiscord') + payload="{ + \"content\": \"$message\" + }" + + curl -s -H "Content-Type: application/json" -X POST -d "$payload" "$discordWebhookURL" > $tempjson + returnCurl=$? + if [ $returnCurl -ne 0 ]; then + cat $tempjson + fi + rm $tempjson + return $returnCurl +} + function conditionalNotification { local event=$1 local text=$2 @@ -203,6 +230,9 @@ function conditionalNotification { fi if [[ " ${method[*]} " =~ " sms " ]] ; then sendSms "$text" + fi + if [[ " ${method[*]} " =~ " discord " ]] ; then + sendDiscord "$text" fi -} \ No newline at end of file +}