mirror of
https://github.com/eworm-de/routeros-scripts
synced 2024-05-14 08:04:19 +00:00
global-functions: queue Telegram notifications on failure and re-send
This introduces function $FlushTelegramQueue, which flushes queue of Telegram notifications. Notifications are appended to the queue if sending failed.
This commit is contained in:
parent
0c111a2ebe
commit
1b68e42bc6
1 changed files with 45 additions and 3 deletions
|
@ -24,6 +24,7 @@
|
|||
:global DeviceInfo;
|
||||
:global DNSIsResolving;
|
||||
:global DownloadPackage;
|
||||
:global FlushTelegramQueue;
|
||||
:global GetMacVendor;
|
||||
:global GetRandom20CharHex;
|
||||
:global GetRandomNumber;
|
||||
|
@ -280,6 +281,36 @@
|
|||
:return false;
|
||||
}
|
||||
|
||||
# flush telegram queue
|
||||
:set FlushTelegramQueue do={
|
||||
:global TelegramQueue;
|
||||
:global TelegramTokenId;
|
||||
|
||||
:global LogPrintExit;
|
||||
|
||||
:local AllDone true;
|
||||
:local QueueLen [ :len $TelegramQueue ];
|
||||
:foreach Id,Message in=$TelegramQueue do={
|
||||
:if ([ :typeof $Message ] = "array" ) do={
|
||||
:do {
|
||||
/ tool fetch check-certificate=yes-without-crl output=none http-method=post \
|
||||
("https://api.telegram.org/bot" . $TelegramTokenId . "/sendMessage") \
|
||||
http-data=("chat_id=" . ($Message->"chatid") . \
|
||||
"&disable_notification=" . ($Message->"silent") . \
|
||||
"&text=" . ($Message->"text"));
|
||||
:set ($TelegramQueue->$Id);
|
||||
} on-error={
|
||||
$LogPrintExit debug ("Sending queued Telegram message failed.") false;
|
||||
:set AllDone false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
:if ($AllDone = true && $QueueLen = [ :len $TelegramQueue ]) do={
|
||||
/ system scheduler remove FlushTelegramQueue;
|
||||
}
|
||||
}
|
||||
|
||||
# get MAC vendor
|
||||
:set GetMacVendor do={
|
||||
:local Mac [ :tostr $1 ];
|
||||
|
@ -701,9 +732,10 @@
|
|||
:local Silent [ :tostr $3 ];
|
||||
|
||||
:global Identity;
|
||||
:global TelegramTokenId;
|
||||
:global TelegramChatId;
|
||||
:global TelegramChatIdOverride;
|
||||
:global TelegramQueue;
|
||||
:global TelegramTokenId;
|
||||
|
||||
:global CertificateAvailable;
|
||||
:global LogPrintExit;
|
||||
|
@ -718,6 +750,7 @@
|
|||
:return false;
|
||||
}
|
||||
|
||||
:local Text [ $UrlEncode ("[" . $Identity . "] " . $Subject . "\n\n" . $Message) ];
|
||||
:do {
|
||||
:if ([ $CertificateAvailable "Go Daddy Secure Certificate Authority - G2" ] = false) do={
|
||||
$LogPrintExit warning ("Downloading required certificate failed.") true;
|
||||
|
@ -725,9 +758,18 @@
|
|||
/ tool fetch check-certificate=yes-without-crl output=none http-method=post \
|
||||
("https://api.telegram.org/bot" . $TelegramTokenId . "/sendMessage") \
|
||||
http-data=("chat_id=" . $ChatId . "&disable_notification=" . $Silent . \
|
||||
"&text=" . [ $UrlEncode ("[" . $Identity . "] " . $Subject . "\n\n" . $Message) ]);
|
||||
"&text=" . $Text);
|
||||
} on-error={
|
||||
$LogPrintExit warning ("Failed sending telegram notification!") false;
|
||||
$LogPrintExit warning ("Failed sending telegram notification! Queuing...") false;
|
||||
|
||||
:if ([ :typeof $TelegramQueue ] = "nothing") do={
|
||||
:set TelegramQueue [ :toarray "" ];
|
||||
}
|
||||
:set ($TelegramQueue->[ :len $TelegramQueue ]) { chatid=$ChatId; text=$Text; silent=$Silent };
|
||||
:if ([ :len [ / system scheduler find where name="FlushTelegramQueue" ] ] = 0) do={
|
||||
/ system scheduler add name=FlushTelegramQueue interval=1m start-time=startup \
|
||||
on-event=":global FlushTelegramQueue; \$FlushTelegramQueue;";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue