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.rsc69
1 files changed, 47 insertions, 22 deletions
diff --git a/mod/notification-ntfy.rsc b/mod/notification-ntfy.rsc
index 6d48a59..7114020 100644
--- a/mod/notification-ntfy.rsc
+++ b/mod/notification-ntfy.rsc
@@ -1,10 +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.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;
@@ -13,9 +16,8 @@
:global SendNtfy2;
# flush ntfy queue
-:set FlushNtfyQueue do={
+:set FlushNtfyQueue do={ :onerror Err {
:global NtfyQueue;
- :global NtfyMessageIDs;
:global IsFullyConnected;
:global LogPrint;
@@ -34,12 +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 \
- ($Message->"url") http-header-field=($Message->"headers") http-data=($Message->"text") as-value;
+ http-header-field=($Message->"headers") http-data=($Message->"text") \
+ ($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;
}
}
@@ -49,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={
@@ -60,42 +65,59 @@
:global NtfyQueue;
:global NtfyServer;
:global NtfyServerOverride;
+ :global NtfyServerPass;
+ :global NtfyServerPassOverride;
+ :global NtfyServerToken;
+ :global NtfyServerTokenOverride;
+ :global NtfyServerUser;
+ :global NtfyServerUserOverride;
:global NtfyTopic;
:global NtfyTopicOverride;
:global CertificateAvailable;
:global EitherOr;
+ :global FetchUserAgentStr;
:global IfThenElse;
:global LogPrint;
:global SymbolForNotification;
:global UrlEncode;
: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 Headers ({ ("Priority: " . [ $IfThenElse ($Notification->"silent") "low" "default" ]); \
+ :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 \
- $Url http-header-field=$Headers http-data=$Text 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 ({});
@@ -103,7 +125,8 @@
:set Text ($Text . "\n" . [ $SymbolForNotification "alarm-clock" ] . \
"This message was queued since " . [ /system/clock/get date ] . " " . \
[ /system/clock/get time ] . " and may be obsolete.");
- :set ($NtfyQueue->[ :len $NtfyQueue ]) { url=$Url; headers=$Headers; text=$Text };
+ :set ($NtfyQueue->[ :len $NtfyQueue ]) \
+ { 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;");
@@ -120,11 +143,13 @@
}
# send notification via ntfy - expects at least two string arguments
-:set SendNtfy do={
+:set SendNtfy do={ :onerror Err {
:global SendNtfy2;
- $SendNtfy2 ({ subject=$1; message=$2; link=$3; silent=$4 });
-}
+ $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={