global-functions: $SendTelegram: use fixed-width font...

... but give configuration to opt-out.
This commit is contained in:
Christian Hesse 2020-10-12 23:58:20 +02:00
parent 6b1d9e8f40
commit 3e0039c266
4 changed files with 38 additions and 10 deletions

View file

@ -8,7 +8,7 @@
# Make sure all configuration properties are up to date and this # Make sure all configuration properties are up to date and this
# value is in sync with value in script 'global-functions'! # value is in sync with value in script 'global-functions'!
:global GlobalConfigVersion 30; :global GlobalConfigVersion 31;
# This is used for DNS and backup file. # This is used for DNS and backup file.
:global Domain "example.com"; :global Domain "example.com";
@ -28,6 +28,8 @@
:global TelegramChatId ""; :global TelegramChatId "";
#:global TelegramTokenId "123456:ABCDEF-GHI"; #:global TelegramTokenId "123456:ABCDEF-GHI";
#:global TelegramChatId "12345678"; #:global TelegramChatId "12345678";
# This is whether or not to send Telegram messages with fixed-width font.
:global TelegramFixedWidthFont true;
# Toggle this to disable symbols in notifications. # Toggle this to disable symbols in notifications.
:global NotificationsWithSymbols true; :global NotificationsWithSymbols true;

View file

@ -9,7 +9,7 @@
# Make sure all configuration properties are up to date and this # Make sure all configuration properties are up to date and this
# value is in sync with value in script 'global-functions'! # value is in sync with value in script 'global-functions'!
# Comment or remove to disable change notifications. # Comment or remove to disable change notifications.
:global GlobalConfigVersion 30; :global GlobalConfigVersion 31;
# Copy configuration from global-config here and modify it. # Copy configuration from global-config here and modify it.

View file

@ -34,4 +34,5 @@
28="Made 'dhcp-to-dns' act on all bound leases, not just dynamic ones."; 28="Made 'dhcp-to-dns' act on all bound leases, not just dynamic ones.";
29="Added filter on log message text for 'log-forward'."; 29="Added filter on log message text for 'log-forward'.";
30="Implemented simple rate limit for 'log-forward' to prevent flooding."; 30="Implemented simple rate limit for 'log-forward' to prevent flooding.";
31="Switched Telegram notifications to fixed-width font, with opt-out.";
}; };

View file

@ -8,7 +8,7 @@
# https://git.eworm.de/cgit/routeros-scripts/about/ # https://git.eworm.de/cgit/routeros-scripts/about/
# expected configuration version # expected configuration version
:global ExpectedConfigVersion 30; :global ExpectedConfigVersion 31;
# global variables not to be changed by user # global variables not to be changed by user
:global GlobalFunctionsReady false; :global GlobalFunctionsReady false;
@ -297,7 +297,7 @@
("https://api.telegram.org/bot" . $TelegramTokenId . "/sendMessage") \ ("https://api.telegram.org/bot" . $TelegramTokenId . "/sendMessage") \
http-data=("chat_id=" . ($Message->"chatid") . \ http-data=("chat_id=" . ($Message->"chatid") . \
"&disable_notification=" . ($Message->"silent") . \ "&disable_notification=" . ($Message->"silent") . \
"&text=" . ($Message->"text")); "&parse_mode=" . ($Message->"parsemode") . "&text=" . ($Message->"text"));
:set ($TelegramQueue->$Id); :set ($TelegramQueue->$Id);
} on-error={ } on-error={
$LogPrintExit debug ("Sending queued Telegram message failed.") false; $LogPrintExit debug ("Sending queued Telegram message failed.") false;
@ -760,14 +760,31 @@
:global Identity; :global Identity;
:global TelegramChatId; :global TelegramChatId;
:global TelegramChatIdOverride; :global TelegramChatIdOverride;
:global TelegramFixedWidthFont;
:global TelegramQueue; :global TelegramQueue;
:global TelegramTokenId; :global TelegramTokenId;
:global CertificateAvailable; :global CertificateAvailable;
:global CharacterReplace;
:global IfThenElse;
:global LogPrintExit; :global LogPrintExit;
:global SymbolForNotification; :global SymbolForNotification;
:global UrlEncode; :global UrlEncode;
:local EscapeMD do={
:global TelegramFixedWidthFont;
:if ($TelegramFixedWidthFont != true) do={
:return $1;
}
:local Return $1;
:foreach Char in={ "."; "!" } do={
:set Return [ $CharacterReplace $Return $Char ("\\" . $Char) ];
}
:return $Return;
}
:local ChatId $TelegramChatId; :local ChatId $TelegramChatId;
:if ([ :len $TelegramChatIdOverride ] > 0) do={ :if ([ :len $TelegramChatIdOverride ] > 0) do={
:set ChatId $TelegramChatIdOverride; :set ChatId $TelegramChatIdOverride;
@ -778,9 +795,17 @@
} }
:local Text ("[" . $Identity . "] " . $Subject . "\n\n" . $Message); :local Text ("[" . $Identity . "] " . $Subject . "\n\n" . $Message);
:local ParseMode;
:if ($TelegramFixedWidthFont = true) do={
:set Text ("```\n" . [ $CharacterReplace [ $CharacterReplace $Text \
("\\") ("\\\\") ] ("`") ("\\`") ] . "\n```");
:set ParseMode "MarkdownV2";
}
:if ([ :len $Text ] > 3968) do={ :if ([ :len $Text ] > 3968) do={
:set Text ([ :pick $Text 0 3840 ] . "...\n\n" . [ $SymbolForNotification "scissors" ] . \ :set Text ([ :pick $Text 0 3840 ] . "..." . \
"The Telegram message was too long and has been truncated."); [ $IfThenElse ($TelegramFixedWidthFont = true) ("\n```") "" ] . \
"\n\n" . [ $SymbolForNotification "scissors" ] . \
[ $EscapeMD "The Telegram message was too long and has been truncated!" ]);
} }
:set Text [ $UrlEncode $Text ]; :set Text [ $UrlEncode $Text ];
@ -791,7 +816,7 @@
/ tool fetch check-certificate=yes-without-crl output=none http-method=post \ / tool fetch check-certificate=yes-without-crl output=none http-method=post \
("https://api.telegram.org/bot" . $TelegramTokenId . "/sendMessage") \ ("https://api.telegram.org/bot" . $TelegramTokenId . "/sendMessage") \
http-data=("chat_id=" . $ChatId . "&disable_notification=" . $Silent . \ http-data=("chat_id=" . $ChatId . "&disable_notification=" . $Silent . \
"&text=" . $Text); "&parse_mode=" . $ParseMode . "&text=" . $Text);
} on-error={ } on-error={
$LogPrintExit warning ("Failed sending telegram notification! Queuing...") false; $LogPrintExit warning ("Failed sending telegram notification! Queuing...") false;
@ -799,10 +824,10 @@
:set TelegramQueue [ :toarray "" ]; :set TelegramQueue [ :toarray "" ];
} }
:set Text ($Text . [ $UrlEncode ("\n\n" . [ $SymbolForNotification "alarm-clock" ] . \ :set Text ($Text . [ $UrlEncode ("\n\n" . [ $SymbolForNotification "alarm-clock" ] . \
"This message was queued since " . [ / system clock get date ] . " " . \ [ $EscapeMD ("This message was queued since " . [ / system clock get date ] . \
[ / system clock get time ] . " and may be obsolete.") ]); " " . [ / system clock get time ] . " and may be obsolete.") ]) ]);
:set ($TelegramQueue->[ :len $TelegramQueue ]) { :set ($TelegramQueue->[ :len $TelegramQueue ]) {
chatid=$ChatId; text=$Text; silent=$Silent; }; chatid=$ChatId; parsemode=$ParseMode; text=$Text; silent=$Silent };
:if ([ :len [ / system scheduler find where name="FlushTelegramQueue" ] ] = 0) do={ :if ([ :len [ / system scheduler find where name="FlushTelegramQueue" ] ] = 0) do={
/ system scheduler add name=FlushTelegramQueue interval=1m start-time=startup \ / system scheduler add name=FlushTelegramQueue interval=1m start-time=startup \
on-event=":global FlushTelegramQueue; \$FlushTelegramQueue;"; on-event=":global FlushTelegramQueue; \$FlushTelegramQueue;";