blob: 707091897552d865464a012530daf479e46615fe (
about) (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
#!rsc by RouterOS
# RouterOS script: mod/notification-gotify
# Copyright (c) 2013-2025 Christian Hesse <mail@eworm.de>
# Leonardo David Monteiro <leo@cub3.xyz>
# https://rsc.eworm.de/COPYING.md
#
# requires RouterOS, version=7.15
# requires device-mode, fetch, scheduler
#
# send notifications via Gotify (gotify.net)
# https://rsc.eworm.de/doc/mod/notification-gotify.md
:global FlushGotifyQueue;
:global NotificationFunctions;
:global PurgeGotifyQueue;
:global SendGotify;
:global SendGotify2;
# flush Gotify queue
:set FlushGotifyQueue do={ :do {
:global GotifyQueue;
:global IsFullyConnected;
:global LogPrint;
:if ([ $IsFullyConnected ] = false) do={
$LogPrint debug $0 ("System is not fully connected, not flushing.");
:return false;
}
:local AllDone true;
:local QueueLen [ :len $GotifyQueue ];
:if ([ :len [ /system/scheduler/find where name="_FlushGotifyQueue" ] ] > 0 && $QueueLen = 0) do={
$LogPrint warning $0 ("Flushing Gotify messages from scheduler, but queue is empty.");
}
:foreach Id,Message in=$GotifyQueue do={
:if ([ :typeof $Message ] = "array" ) do={
:do {
/tool/fetch check-certificate=yes-without-crl output=none http-method=post \
http-header-field=($Message->"headers") http-data=[ :serialize to=json ($Message->"message") ] \
($Message->"url") as-value;
:set ($GotifyQueue->$Id);
} on-error={
$LogPrint debug $0 ("Sending queued Gotify message failed.");
:set AllDone false;
}
}
}
:if ($AllDone = true && $QueueLen = [ :len $GotifyQueue ]) do={
/system/scheduler/remove [ find where name="_FlushGotifyQueue" ];
:set GotifyQueue;
}
} on-error={
:global ExitError; $ExitError false $0;
} }
# send notification via Gotify - expects one array argument
:set ($NotificationFunctions->"gotify") do={
:local Notification $1;
:global Identity;
:global IdentityExtra;
:global GotifyQueue;
:global GotifyServer;
:global GotifyServerOverride;
:global GotifyToken;
:global GotifyTokenOverride;
:global EitherOr;
:global FetchUserAgentStr;
:global IfThenElse;
:global LogPrint;
:global SymbolForNotification;
:local Server [ $EitherOr ($GotifyServerOverride->($Notification->"origin")) $GotifyServer ];
:local Token [ $EitherOr ($GotifyTokenOverride->($Notification->"origin")) $GotifyToken ];
:if ([ :len $Token ] = 0) do={
:return false;
}
:local Url ("https://" . $Server . "/message");
:local Headers ({ [ $FetchUserAgentStr ($Notification->"origin") ]; \
("X-Gotify-Key: " . $Token); "Content-Type: application/json" });
:local Message ({
"title"=("[" . $IdentityExtra . $Identity . "] " . ($Notification->"subject")); \
"message"=(($Notification->"message") . "\n" . [ $IfThenElse ([ :len ($Notification->"link") ] > 0) \
("\n" . [ $SymbolForNotification "link" ] . ($Notification->"link")) ]); \
"priority"=[ :tonum [ $IfThenElse ($Notification->"silent") 2 5 ] ] });
:do {
/tool/fetch check-certificate=yes-without-crl output=none http-method=post \
http-header-field=$Headers http-data=[ :serialize to=json $Message ] $Url as-value;
} on-error={
$LogPrint info $0 ("Failed sending Gotify notification! Queuing...");
:if ([ :typeof $GotifyQueue ] = "nothing") do={
:set GotifyQueue ({});
}
:set ($Message->"message") (($Notification->"message") . "\n" . \
[ $SymbolForNotification "alarm-clock" ] . "This message was queued since " . \
[ /system/clock/get date ] . " " . [ /system/clock/get time ] . " and may be obsolete.");
:set ($GotifyQueue->[ :len $GotifyQueue ]) \
{ url=$Url; headers=$Headers; message=$Message };
:if ([ :len [ /system/scheduler/find where name="_FlushGotifyQueue" ] ] = 0) do={
/system/scheduler/add name="_FlushGotifyQueue" interval=1m start-time=startup \
on-event=(":global FlushGotifyQueue; \$FlushGotifyQueue;");
}
}
}
# purge the Gotify queue
:set PurgeGotifyQueue do={
:global GotifyQueue;
/system/scheduler/remove [ find where name="_FlushGotifyQueue" ];
:set GotifyQueue;
}
# send notification via Gotify - expects at least two string arguments
:set SendGotify do={ :do {
:global SendGotify2;
$SendGotify2 ({ origin=$0; subject=$1; message=$2; link=$3; silent=$4 });
} on-error={
:global ExitError; $ExitError false $0;
} }
# send notification via Gotify - expects one array argument
:set SendGotify2 do={
:local Notification $1;
:global NotificationFunctions;
($NotificationFunctions->"gotify") ("\$NotificationFunctions->\"gotify\"") $Notification;
}
|