aboutsummaryrefslogtreecommitdiffstats
path: root/mod/notification-ntfy.rsc
diff options
context:
space:
mode:
Diffstat (limited to 'mod/notification-ntfy.rsc')
-rw-r--r--mod/notification-ntfy.rsc55
1 files changed, 34 insertions, 21 deletions
diff --git a/mod/notification-ntfy.rsc b/mod/notification-ntfy.rsc
index 4413f07..7114020 100644
--- a/mod/notification-ntfy.rsc
+++ b/mod/notification-ntfy.rsc
@@ -1,12 +1,13 @@
#!rsc by RouterOS
# RouterOS script: mod/notification-ntfy
-# Copyright (c) 2013-2024 Christian Hesse <mail@eworm.de>
-# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
+# Copyright (c) 2013-2025 Christian Hesse <mail@eworm.de>
+# https://rsc.eworm.de/COPYING.md
#
-# requires RouterOS, version=7.13
+# requires RouterOS, version=7.15
+# requires device-mode, fetch, scheduler
#
# send notifications via Ntfy (ntfy.sh)
-# https://git.eworm.de/cgit/routeros-scripts/about/doc/mod/notification-ntfy.md
+# https://rsc.eworm.de/doc/mod/notification-ntfy.md
:global FlushNtfyQueue;
:global NotificationFunctions;
@@ -15,9 +16,8 @@
:global SendNtfy2;
# flush ntfy queue
-:set FlushNtfyQueue do={
+:set FlushNtfyQueue do={ :onerror Err {
:global NtfyQueue;
- :global NtfyMessageIDs;
:global IsFullyConnected;
:global LogPrint;
@@ -36,13 +36,13 @@
:foreach Id,Message in=$NtfyQueue do={
:if ([ :typeof $Message ] = "array" ) do={
- :do {
+ :onerror Err {
/tool/fetch check-certificate=yes-without-crl output=none http-method=post \
http-header-field=($Message->"headers") http-data=($Message->"text") \
- ($Message->"url") user=($Message->"user") password=($Message->"pass") as-value;
+ ($Message->"url") as-value;
:set ($NtfyQueue->$Id);
- } on-error={
- $LogPrint debug $0 ("Sending queued Ntfy message failed.");
+ } do={
+ $LogPrint debug $0 ("Sending queued Ntfy message failed: " . $Err);
:set AllDone false;
}
}
@@ -52,7 +52,9 @@
/system/scheduler/remove [ find where name="_FlushNtfyQueue" ];
:set NtfyQueue;
}
-}
+} do={
+ :global ExitError; $ExitError false $0 $Err;
+} }
# send notification via ntfy - expects one array argument
:set ($NotificationFunctions->"ntfy") do={
@@ -65,6 +67,8 @@
:global NtfyServerOverride;
:global NtfyServerPass;
:global NtfyServerPassOverride;
+ :global NtfyServerToken;
+ :global NtfyServerTokenOverride;
:global NtfyServerUser;
:global NtfyServerUserOverride;
:global NtfyTopic;
@@ -81,32 +85,39 @@
:local Server [ $EitherOr ($NtfyServerOverride->($Notification->"origin")) $NtfyServer ];
:local User [ $EitherOr ($NtfyServerUserOverride->($Notification->"origin")) $NtfyServerUser ];
:local Pass [ $EitherOr ($NtfyServerPassOverride->($Notification->"origin")) $NtfyServerPass ];
+ :local Token [ $EitherOr ($NtfyServerTokenOverride->($Notification->"origin")) $NtfyServerToken ];
:local Topic [ $EitherOr ($NtfyTopicOverride->($Notification->"origin")) $NtfyTopic ];
:if ([ :len $Topic ] = 0) do={
:return false;
}
- :local Url ("https://" . $NtfyServer . "/" . [ $UrlEncode $NtfyTopic ]);
+ :local Url ("https://" . $Server . "/" . [ $UrlEncode $Topic ]);
:local Headers ({ [ $FetchUserAgentStr ($Notification->"origin") ]; \
("Priority: " . [ $IfThenElse ($Notification->"silent") "low" "default" ]); \
("Title: " . "[" . $IdentityExtra . $Identity . "] " . ($Notification->"subject")) });
+ :if ([ :len $User ] > 0 || [ :len $Pass ] > 0) do={
+ :set Headers ($Headers, ("Authorization: Basic " . [ :convert to=base64 ($User . ":" . $Pass) ]));
+ }
+ :if ([ :len $Token ] > 0) do={
+ :set Headers ($Headers, ("Authorization: Bearer " . $Token));
+ }
:local Text (($Notification->"message") . "\n");
:if ([ :len ($Notification->"link") ] > 0) do={
:set Text ($Text . "\n" . [ $SymbolForNotification "link" ] . ($Notification->"link"));
}
- :do {
- :if ($NtfyServer = "ntfy.sh") do={
- :if ([ $CertificateAvailable "R3" ] = false) do={
+ :onerror Err {
+ :if ($Server = "ntfy.sh") do={
+ :if ([ $CertificateAvailable "ISRG Root X1" ] = false) do={
$LogPrint warning $0 ("Downloading required certificate failed.");
:error false;
}
}
/tool/fetch check-certificate=yes-without-crl output=none http-method=post \
- http-header-field=$Headers http-data=$Text $Url user=$User password=$Pass as-value;
- } on-error={
- $LogPrint info $0 ("Failed sending ntfy notification! Queuing...");
+ http-header-field=$Headers http-data=$Text $Url as-value;
+ } do={
+ $LogPrint info $0 ("Failed sending ntfy notification: " . $Err . " - Queuing...");
:if ([ :typeof $NtfyQueue ] = "nothing") do={
:set NtfyQueue ({});
@@ -115,7 +126,7 @@
"This message was queued since " . [ /system/clock/get date ] . " " . \
[ /system/clock/get time ] . " and may be obsolete.");
:set ($NtfyQueue->[ :len $NtfyQueue ]) \
- { url=$Url; user=$User; pass=$Pass; headers=$Headers; text=$Text };
+ { url=$Url; headers=$Headers; text=$Text };
:if ([ :len [ /system/scheduler/find where name="_FlushNtfyQueue" ] ] = 0) do={
/system/scheduler/add name="_FlushNtfyQueue" interval=1m start-time=startup \
on-event=(":global FlushNtfyQueue; \$FlushNtfyQueue;");
@@ -132,11 +143,13 @@
}
# send notification via ntfy - expects at least two string arguments
-:set SendNtfy do={
+:set SendNtfy do={ :onerror Err {
:global SendNtfy2;
$SendNtfy2 ({ origin=$0; subject=$1; message=$2; link=$3; silent=$4 });
-}
+} do={
+ :global ExitError; $ExitError false $0 $Err;
+} }
# send notification via ntfy - expects one array argument
:set SendNtfy2 do={