2021-06-02 20:53:43 +00:00
|
|
|
#!rsc by RouterOS
|
2021-11-15 19:22:56 +00:00
|
|
|
# RouterOS script: mod/notification-telegram
|
2022-01-01 20:38:15 +00:00
|
|
|
# Copyright (c) 2013-2022 Christian Hesse <mail@eworm.de>
|
2021-06-02 20:53:43 +00:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
|
|
|
|
|
|
|
:global FlushTelegramQueue;
|
|
|
|
:global NotificationFunctions;
|
|
|
|
:global SendTelegram;
|
|
|
|
:global SendTelegram2;
|
|
|
|
|
|
|
|
# flush telegram queue
|
|
|
|
:set FlushTelegramQueue do={
|
|
|
|
:global TelegramQueue;
|
|
|
|
|
|
|
|
:global LogPrintExit2;
|
|
|
|
|
|
|
|
:local AllDone true;
|
|
|
|
:local QueueLen [ :len $TelegramQueue ];
|
|
|
|
|
|
|
|
:if ([ :len [ / system scheduler find where name="FlushTelegramQueue" ] ] > 0 && $QueueLen = 0) do={
|
|
|
|
$LogPrintExit2 warning $0 ("Flushing Telegram messages from scheduler, but queue is empty.") false;
|
|
|
|
}
|
|
|
|
|
|
|
|
: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" . ($Message->"tokenid") . "/sendMessage") \
|
|
|
|
http-data=("chat_id=" . ($Message->"chatid") . \
|
|
|
|
"&disable_notification=" . ($Message->"silent") . \
|
|
|
|
"&disable_web_page_preview=true&parse_mode=" . ($Message->"parsemode") . \
|
|
|
|
"&text=" . ($Message->"text")) as-value;
|
|
|
|
:set ($TelegramQueue->$Id);
|
|
|
|
} on-error={
|
|
|
|
$LogPrintExit2 debug $0 ("Sending queued Telegram message failed.") false;
|
|
|
|
:set AllDone false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
:if ($AllDone = true && $QueueLen = [ :len $TelegramQueue ]) do={
|
|
|
|
/ system scheduler remove [ find where name="FlushTelegramQueue" ];
|
|
|
|
:set TelegramQueue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# send notification via telegram - expects one array argument
|
|
|
|
:set ($NotificationFunctions->"telegram") do={
|
|
|
|
:local Notification $1;
|
|
|
|
|
|
|
|
:global Identity;
|
|
|
|
:global TelegramChatId;
|
|
|
|
:global TelegramChatIdOverride;
|
|
|
|
:global TelegramFixedWidthFont;
|
|
|
|
:global TelegramQueue;
|
|
|
|
:global TelegramTokenId;
|
|
|
|
:global TelegramTokenIdOverride;
|
|
|
|
|
|
|
|
:global CertificateAvailable;
|
|
|
|
:global CharacterReplace;
|
|
|
|
:global EitherOr;
|
|
|
|
:global IfThenElse;
|
|
|
|
:global LogPrintExit2;
|
|
|
|
:global SymbolForNotification;
|
|
|
|
:global UrlEncode;
|
|
|
|
|
|
|
|
:local EscapeMD do={
|
|
|
|
:global TelegramFixedWidthFont;
|
|
|
|
|
|
|
|
:global CharacterReplace;
|
|
|
|
:global IfThenElse;
|
|
|
|
|
|
|
|
:if ($TelegramFixedWidthFont != true) do={
|
2021-08-24 22:35:08 +00:00
|
|
|
:return ($1 . [ $IfThenElse ($2 = "body") ("\n") "" ]);
|
2021-06-02 20:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
:local Return $1;
|
|
|
|
:local Chars {
|
|
|
|
"body"={ "\\"; "`" };
|
2021-07-09 14:50:08 +00:00
|
|
|
"plain"={ "_"; "*"; "["; "]"; "("; ")"; "~"; "`"; ">";
|
|
|
|
"#"; "+"; "-"; "="; "|"; "{"; "}"; "."; "!" };
|
2021-06-02 20:53:43 +00:00
|
|
|
}
|
|
|
|
:foreach Char in=($Chars->$2) do={
|
|
|
|
:set Return [ $CharacterReplace $Return $Char ("\\" . $Char) ];
|
|
|
|
}
|
|
|
|
|
|
|
|
:if ($2 = "body") do={
|
|
|
|
:return ("```\n" . $Return . "\n```");
|
|
|
|
}
|
|
|
|
|
|
|
|
:return $Return;
|
|
|
|
}
|
|
|
|
|
|
|
|
:local ChatId [ $EitherOr ($TelegramChatIdOverride->($Notification->"origin")) $TelegramChatId ];
|
|
|
|
:local TokenId [ $EitherOr ($TelegramTokenIdOverride->($Notification->"origin")) $TelegramTokenId ];
|
|
|
|
|
|
|
|
:if ([ :len $TokenId ] = 0 || [ :len $ChatId ] = 0) do={
|
|
|
|
:return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
:local Truncated false;
|
2021-07-09 14:50:08 +00:00
|
|
|
:local Text ("*__" . [ $EscapeMD ("[" . $Identity . "] " . ($Notification->"subject")) "plain" ] . "__*\n\n");
|
|
|
|
:local LenSubject [ :len $Text ];
|
|
|
|
:local LenMessage [ :len ($Notification->"message") ];
|
2021-06-02 20:53:43 +00:00
|
|
|
:local LenLink [ :len ($Notification->"link") ];
|
2021-09-28 14:26:26 +00:00
|
|
|
:local LenSum ($LenSubject + $LenMessage + $LenLink);
|
|
|
|
:if ($LenSum > 3968) do={
|
2021-07-09 14:50:08 +00:00
|
|
|
:set Text ($Text . [ $EscapeMD ([ :pick ($Notification->"message") 0 (3840 - $LenSubject - $LenLink) ] . "...") "body" ]);
|
2021-06-02 20:53:43 +00:00
|
|
|
:set Truncated true;
|
|
|
|
} else={
|
2021-07-09 14:50:08 +00:00
|
|
|
:set Text ($Text . [ $EscapeMD ($Notification->"message") "body" ]);
|
2021-06-02 20:53:43 +00:00
|
|
|
}
|
|
|
|
:if ($LenLink > 0) do={
|
2021-07-09 14:50:08 +00:00
|
|
|
:set Text ($Text . "\n" . [ $SymbolForNotification "link" ] . [ $EscapeMD ($Notification->"link") "plain" ]);
|
2021-06-02 20:53:43 +00:00
|
|
|
}
|
|
|
|
:if ($Truncated = true) do={
|
|
|
|
:set Text ($Text . "\n" . [ $SymbolForNotification "scissors" ] . \
|
|
|
|
[ $EscapeMD ("The Telegram message was too long and has been truncated, cut off " . \
|
2021-09-28 14:26:26 +00:00
|
|
|
(($LenSum - [ :len $Text ]) * 100 / $LenSum) . "%!") "plain" ]);
|
2021-06-02 20:53:43 +00:00
|
|
|
}
|
|
|
|
:set Text [ $UrlEncode $Text ];
|
|
|
|
:local ParseMode [ $IfThenElse ($TelegramFixedWidthFont = true) "MarkdownV2" "" ];
|
|
|
|
|
|
|
|
:do {
|
|
|
|
:if ([ $CertificateAvailable "Go Daddy Secure Certificate Authority - G2" ] = false) do={
|
|
|
|
$LogPrintExit2 warning $0 ("Downloading required certificate failed.") true;
|
|
|
|
}
|
|
|
|
/ tool fetch check-certificate=yes-without-crl output=none http-method=post \
|
|
|
|
("https://api.telegram.org/bot" . $TokenId . "/sendMessage") \
|
|
|
|
http-data=("chat_id=" . $ChatId . "&disable_notification=" . ($Notification->"silent") . \
|
|
|
|
"&disable_web_page_preview=true&parse_mode=" . $ParseMode . "&text=" . $Text) as-value;
|
|
|
|
} on-error={
|
|
|
|
$LogPrintExit2 info $0 ("Failed sending telegram notification! Queuing...") false;
|
|
|
|
|
|
|
|
:if ([ :typeof $TelegramQueue ] = "nothing") do={
|
|
|
|
:set TelegramQueue [ :toarray "" ];
|
|
|
|
}
|
|
|
|
:set Text ($Text . [ $UrlEncode ("\n" . [ $SymbolForNotification "alarm-clock" ] . \
|
|
|
|
[ $EscapeMD ("This message was queued since " . [ / system clock get date ] . \
|
2021-07-09 14:50:08 +00:00
|
|
|
" " . [ / system clock get time ] . " and may be obsolete.") "plain" ]) ]);
|
2021-06-02 20:53:43 +00:00
|
|
|
:set ($TelegramQueue->[ :len $TelegramQueue ]) { chatid=$ChatId; tokenid=$TokenId;
|
|
|
|
parsemode=$ParseMode; text=$Text; silent=($Notification->"silent") };
|
|
|
|
:if ([ :len [ / system scheduler find where name="FlushTelegramQueue" ] ] = 0) do={
|
|
|
|
/ system scheduler add name=FlushTelegramQueue interval=1m start-time=startup \
|
2021-09-08 12:33:02 +00:00
|
|
|
on-event=(":global FlushTelegramQueue; \$FlushTelegramQueue;");
|
2021-06-02 20:53:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# send notification via telegram - expects at lease two string arguments
|
|
|
|
:set SendTelegram do={
|
|
|
|
:global SendTelegram2;
|
|
|
|
|
|
|
|
$SendTelegram2 ({ subject=$1; message=$2; link=$3; silent=$4 });
|
|
|
|
}
|
|
|
|
|
|
|
|
# send notification via telegram - expects one array argument
|
|
|
|
:set SendTelegram2 do={
|
|
|
|
:local Notification $1;
|
|
|
|
|
|
|
|
:global NotificationFunctions;
|
|
|
|
|
|
|
|
($NotificationFunctions->"telegram") ("\$NotificationFunctions->\"telegram\"") $Notification;
|
|
|
|
}
|