2023-10-11 05:40:55 +00:00
|
|
|
#!rsc by RouterOS
|
|
|
|
# RouterOS script: mod/notification-ntfy
|
2024-01-01 14:25:25 +00:00
|
|
|
# Copyright (c) 2013-2024 Christian Hesse <mail@eworm.de>
|
2023-10-11 05:40:55 +00:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
|
|
|
#
|
2024-04-04 19:45:02 +00:00
|
|
|
# requires RouterOS, version=7.13
|
|
|
|
#
|
2023-10-11 05:40:55 +00:00
|
|
|
# send notifications via Ntfy (ntfy.sh)
|
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/mod/notification-ntfy.md
|
|
|
|
|
|
|
|
:global FlushNtfyQueue;
|
|
|
|
:global NotificationFunctions;
|
|
|
|
:global PurgeNtfyQueue;
|
|
|
|
:global SendNtfy;
|
|
|
|
:global SendNtfy2;
|
|
|
|
|
|
|
|
# flush ntfy queue
|
|
|
|
:set FlushNtfyQueue do={
|
|
|
|
:global NtfyQueue;
|
|
|
|
:global NtfyMessageIDs;
|
|
|
|
|
|
|
|
:global IsFullyConnected;
|
2024-03-08 11:45:38 +00:00
|
|
|
:global LogPrint;
|
2023-10-11 05:40:55 +00:00
|
|
|
|
|
|
|
:if ([ $IsFullyConnected ] = false) do={
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint debug $0 ("System is not fully connected, not flushing.");
|
2023-10-11 05:40:55 +00:00
|
|
|
:return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
:local AllDone true;
|
|
|
|
:local QueueLen [ :len $NtfyQueue ];
|
|
|
|
|
2023-11-14 21:35:23 +00:00
|
|
|
:if ([ :len [ /system/scheduler/find where name="_FlushNtfyQueue" ] ] > 0 && $QueueLen = 0) do={
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint warning $0 ("Flushing Ntfy messages from scheduler, but queue is empty.");
|
2023-10-11 05:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
:foreach Id,Message in=$NtfyQueue do={
|
|
|
|
:if ([ :typeof $Message ] = "array" ) do={
|
|
|
|
:do {
|
|
|
|
/tool/fetch check-certificate=yes-without-crl output=none http-method=post \
|
2024-04-09 21:46:32 +00:00
|
|
|
http-header-field=($Message->"headers") http-data=($Message->"text") \
|
|
|
|
($Message->"url") user=($Message->"user") password=($Message->"pass") as-value;
|
2023-10-11 05:40:55 +00:00
|
|
|
:set ($NtfyQueue->$Id);
|
|
|
|
} on-error={
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint debug $0 ("Sending queued Ntfy message failed.");
|
2023-10-11 05:40:55 +00:00
|
|
|
:set AllDone false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
:if ($AllDone = true && $QueueLen = [ :len $NtfyQueue ]) do={
|
2023-11-14 21:35:23 +00:00
|
|
|
/system/scheduler/remove [ find where name="_FlushNtfyQueue" ];
|
2023-10-11 05:40:55 +00:00
|
|
|
:set NtfyQueue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# send notification via ntfy - expects one array argument
|
|
|
|
:set ($NotificationFunctions->"ntfy") do={
|
|
|
|
:local Notification $1;
|
|
|
|
|
|
|
|
:global Identity;
|
|
|
|
:global IdentityExtra;
|
|
|
|
:global NtfyQueue;
|
|
|
|
:global NtfyServer;
|
|
|
|
:global NtfyServerOverride;
|
2024-04-09 21:46:32 +00:00
|
|
|
:global NtfyServerPass;
|
|
|
|
:global NtfyServerPassOverride;
|
|
|
|
:global NtfyServerUser;
|
|
|
|
:global NtfyServerUserOverride;
|
2023-10-11 05:40:55 +00:00
|
|
|
:global NtfyTopic;
|
|
|
|
:global NtfyTopicOverride;
|
|
|
|
|
2023-10-13 12:54:20 +00:00
|
|
|
:global CertificateAvailable;
|
2023-10-11 05:40:55 +00:00
|
|
|
:global EitherOr;
|
2024-04-09 21:50:00 +00:00
|
|
|
:global FetchUserAgentStr;
|
2023-10-11 05:40:55 +00:00
|
|
|
:global IfThenElse;
|
2024-03-08 11:45:38 +00:00
|
|
|
:global LogPrint;
|
2023-10-11 05:40:55 +00:00
|
|
|
:global SymbolForNotification;
|
|
|
|
:global UrlEncode;
|
|
|
|
|
|
|
|
:local Server [ $EitherOr ($NtfyServerOverride->($Notification->"origin")) $NtfyServer ];
|
2024-04-09 21:46:32 +00:00
|
|
|
:local User [ $EitherOr ($NtfyServerUserOverride->($Notification->"origin")) $NtfyServerUser ];
|
|
|
|
:local Pass [ $EitherOr ($NtfyServerPassOverride->($Notification->"origin")) $NtfyServerPass ];
|
2023-10-11 05:40:55 +00:00
|
|
|
:local Topic [ $EitherOr ($NtfyTopicOverride->($Notification->"origin")) $NtfyTopic ];
|
|
|
|
|
|
|
|
:if ([ :len $Topic ] = 0) do={
|
|
|
|
:return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
:local Url ("https://" . $NtfyServer . "/" . [ $UrlEncode $NtfyTopic ]);
|
2024-04-09 21:50:00 +00:00
|
|
|
:local Headers ({ [ $FetchUserAgentStr ($Notification->"origin") ]; \
|
|
|
|
("Priority: " . [ $IfThenElse ($Notification->"silent") "low" "default" ]); \
|
2023-10-11 05:40:55 +00:00
|
|
|
("Title: " . "[" . $IdentityExtra . $Identity . "] " . ($Notification->"subject")) });
|
|
|
|
:local Text (($Notification->"message") . "\n");
|
|
|
|
:if ([ :len ($Notification->"link") ] > 0) do={
|
|
|
|
:set Text ($Text . "\n" . [ $SymbolForNotification "link" ] . ($Notification->"link"));
|
|
|
|
}
|
|
|
|
|
|
|
|
:do {
|
2023-10-13 12:54:20 +00:00
|
|
|
:if ($NtfyServer = "ntfy.sh") do={
|
|
|
|
:if ([ $CertificateAvailable "R3" ] = false) do={
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint warning $0 ("Downloading required certificate failed.");
|
|
|
|
:error false;
|
2023-10-13 12:54:20 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-11 05:40:55 +00:00
|
|
|
/tool/fetch check-certificate=yes-without-crl output=none http-method=post \
|
2024-04-09 21:46:32 +00:00
|
|
|
http-header-field=$Headers http-data=$Text $Url user=$User password=$Pass as-value;
|
2023-10-11 05:40:55 +00:00
|
|
|
} on-error={
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint info $0 ("Failed sending ntfy notification! Queuing...");
|
2023-10-11 05:40:55 +00:00
|
|
|
|
|
|
|
:if ([ :typeof $NtfyQueue ] = "nothing") do={
|
|
|
|
:set NtfyQueue ({});
|
|
|
|
}
|
|
|
|
:set Text ($Text . "\n" . [ $SymbolForNotification "alarm-clock" ] . \
|
|
|
|
"This message was queued since " . [ /system/clock/get date ] . " " . \
|
|
|
|
[ /system/clock/get time ] . " and may be obsolete.");
|
2024-04-09 21:46:32 +00:00
|
|
|
:set ($NtfyQueue->[ :len $NtfyQueue ]) \
|
|
|
|
{ url=$Url; user=$User; pass=$Pass; headers=$Headers; text=$Text };
|
2023-11-14 21:35:23 +00:00
|
|
|
:if ([ :len [ /system/scheduler/find where name="_FlushNtfyQueue" ] ] = 0) do={
|
|
|
|
/system/scheduler/add name="_FlushNtfyQueue" interval=1m start-time=startup \
|
2023-10-11 05:40:55 +00:00
|
|
|
on-event=(":global FlushNtfyQueue; \$FlushNtfyQueue;");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# purge the Ntfy queue
|
|
|
|
:set PurgeNtfyQueue do={
|
|
|
|
:global NtfyQueue;
|
|
|
|
|
2023-11-14 21:35:23 +00:00
|
|
|
/system/scheduler/remove [ find where name="_FlushNtfyQueue" ];
|
2023-10-11 05:40:55 +00:00
|
|
|
:set NtfyQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
# send notification via ntfy - expects at least two string arguments
|
|
|
|
:set SendNtfy do={
|
|
|
|
:global SendNtfy2;
|
|
|
|
|
2024-04-09 21:56:36 +00:00
|
|
|
$SendNtfy2 ({ origin=$0; subject=$1; message=$2; link=$3; silent=$4 });
|
2023-10-11 05:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# send notification via ntfy - expects one array argument
|
|
|
|
:set SendNtfy2 do={
|
|
|
|
:local Notification $1;
|
|
|
|
|
|
|
|
:global NotificationFunctions;
|
|
|
|
|
|
|
|
($NotificationFunctions->"ntfy") ("\$NotificationFunctions->\"ntfy\"") $Notification;
|
|
|
|
}
|