diff options
author | Christian Hesse <mail@eworm.de> | 2020-10-12 23:58:20 +0200 |
---|---|---|
committer | Christian Hesse <mail@eworm.de> | 2020-10-13 10:05:59 +0200 |
commit | 3e0039c2666ae4cf2d45284e7a583f4ea2a0a85c (patch) | |
tree | 6f43ef8cf397afbabf98133efc8139d2db25b382 /global-functions | |
parent | 6b1d9e8f40cf708bbc51be40bb7345ee3cf676b7 (diff) |
global-functions: $SendTelegram: use fixed-width font...change-31
... but give configuration to opt-out.
Diffstat (limited to 'global-functions')
-rw-r--r-- | global-functions | 41 |
1 files changed, 33 insertions, 8 deletions
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;"; |