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
# value is in sync with value in script 'global-functions'!
:global GlobalConfigVersion 30;
:global GlobalConfigVersion 31;
# This is used for DNS and backup file.
:global Domain "example.com";
@ -28,6 +28,8 @@
:global TelegramChatId "";
#:global TelegramTokenId "123456:ABCDEF-GHI";
#: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.
:global NotificationsWithSymbols true;

View file

@ -9,7 +9,7 @@
# Make sure all configuration properties are up to date and this
# value is in sync with value in script 'global-functions'!
# Comment or remove to disable change notifications.
:global GlobalConfigVersion 30;
:global GlobalConfigVersion 31;
# 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.";
29="Added filter on log message text for 'log-forward'.";
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/
# expected configuration version
:global ExpectedConfigVersion 30;
:global ExpectedConfigVersion 31;
# global variables not to be changed by user
:global GlobalFunctionsReady false;
@ -297,7 +297,7 @@
("https://api.telegram.org/bot" . $TelegramTokenId . "/sendMessage") \
http-data=("chat_id=" . ($Message->"chatid") . \
"&disable_notification=" . ($Message->"silent") . \
"&text=" . ($Message->"text"));
"&parse_mode=" . ($Message->"parsemode") . "&text=" . ($Message->"text"));
:set ($TelegramQueue->$Id);
} on-error={
$LogPrintExit debug ("Sending queued Telegram message failed.") false;
@ -760,14 +760,31 @@
:global Identity;
:global TelegramChatId;
:global TelegramChatIdOverride;
:global TelegramFixedWidthFont;
:global TelegramQueue;
:global TelegramTokenId;
:global CertificateAvailable;
:global CharacterReplace;
:global IfThenElse;
:global LogPrintExit;
:global SymbolForNotification;
: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;
:if ([ :len $TelegramChatIdOverride ] > 0) do={
:set ChatId $TelegramChatIdOverride;
@ -778,9 +795,17 @@
}
: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={
:set Text ([ :pick $Text 0 3840 ] . "...\n\n" . [ $SymbolForNotification "scissors" ] . \
"The Telegram message was too long and has been truncated.");
:set Text ([ :pick $Text 0 3840 ] . "..." . \
[ $IfThenElse ($TelegramFixedWidthFont = true) ("\n```") "" ] . \
"\n\n" . [ $SymbolForNotification "scissors" ] . \
[ $EscapeMD "The Telegram message was too long and has been truncated!" ]);
}
:set Text [ $UrlEncode $Text ];
@ -791,7 +816,7 @@
/ 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=" . $Text);
"&parse_mode=" . $ParseMode . "&text=" . $Text);
} on-error={
$LogPrintExit warning ("Failed sending telegram notification! Queuing...") false;
@ -799,10 +824,10 @@
:set TelegramQueue [ :toarray "" ];
}
:set Text ($Text . [ $UrlEncode ("\n\n" . [ $SymbolForNotification "alarm-clock" ] . \
"This message was queued since " . [ / system clock get date ] . " " . \
[ / system clock get time ] . " and may be obsolete.") ]);
[ $EscapeMD ("This message was queued since " . [ / system clock get date ] . \
" " . [ / system clock get time ] . " and may be obsolete.") ]) ]);
: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={
/ system scheduler add name=FlushTelegramQueue interval=1m start-time=startup \
on-event=":global FlushTelegramQueue; \$FlushTelegramQueue;";