From 3e0039c2666ae4cf2d45284e7a583f4ea2a0a85c Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Mon, 12 Oct 2020 23:58:20 +0200 Subject: global-functions: $SendTelegram: use fixed-width font... ... but give configuration to opt-out. --- global-config | 4 +++- global-config-overlay | 2 +- global-config.changes | 1 + global-functions | 41 +++++++++++++++++++++++++++++++++-------- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/global-config b/global-config index c828a97..269ac6b 100644 --- a/global-config +++ b/global-config @@ -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; diff --git a/global-config-overlay b/global-config-overlay index f76fac3..0bc7cbb 100644 --- a/global-config-overlay +++ b/global-config-overlay @@ -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. diff --git a/global-config.changes b/global-config.changes index 8253c15..4120695 100644 --- a/global-config.changes +++ b/global-config.changes @@ -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."; }; diff --git a/global-functions b/global-functions index 1188c76..3ffe737 100644 --- a/global-functions +++ b/global-functions @@ -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;"; -- cgit v1.2.3-70-g09d2