mirror of
https://github.com/eworm-de/routeros-scripts.git
synced 2024-05-11 05:55:19 +00:00
telegram-chat: delay confirmation of updates
Several devices can communicate with the same bot, and we want all of
them to receive their updates. However this can be tricky, as...
* ... sometimes internet connection can be unreliable or saturated.
* ... device can be busy with long running command.
* ... the Telegram bot api servers seem to implement what ever kind of
rate limiting. Anybody can give details?
So let's confirm the update id after third request only. 😁 This gives
delayed devices some extra chances to catch up.
This commit is contained in:
@ -37,8 +37,8 @@ $ScriptLock $0;
|
|||||||
|
|
||||||
$WaitFullyConnected;
|
$WaitFullyConnected;
|
||||||
|
|
||||||
:if ([ :typeof $TelegramChatOffset ] != "num") do={
|
:if ([ :typeof $TelegramChatOffset ] != "array") do={
|
||||||
:set TelegramChatOffset 0;
|
:set TelegramChatOffset { 0; 0; 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
:if ([ $CertificateAvailable "Go Daddy Secure Certificate Authority - G2" ] = false) do={
|
:if ([ $CertificateAvailable "Go Daddy Secure Certificate Authority - G2" ] = false) do={
|
||||||
@ -65,16 +65,16 @@ $WaitFullyConnected;
|
|||||||
:do {
|
:do {
|
||||||
:set Data ([ /tool/fetch check-certificate=yes-without-crl output=user \
|
:set Data ([ /tool/fetch check-certificate=yes-without-crl output=user \
|
||||||
("https://api.telegram.org/bot" . $TelegramTokenId . "/getUpdates?offset=" . \
|
("https://api.telegram.org/bot" . $TelegramTokenId . "/getUpdates?offset=" . \
|
||||||
$TelegramChatOffset . "&allowed_updates=%5B%22message%22%5D") as-value ]->"data");
|
$TelegramChatOffset->0 . "&allowed_updates=%5B%22message%22%5D") as-value ]->"data");
|
||||||
:set Data [ :pick $Data ([ :find $Data "[" ] + 1) ([ :len $Data ] - 2) ];
|
:set Data [ :pick $Data ([ :find $Data "[" ] + 1) ([ :len $Data ] - 2) ];
|
||||||
} on-error={
|
} on-error={
|
||||||
$LogPrintExit2 info $0 ("Failed getting updates from Telegram.") true;
|
$LogPrintExit2 info $0 ("Failed getting updates from Telegram.") true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
:local UpdateID 0;
|
||||||
:foreach Update in=[ :toarray $Data ] do={
|
:foreach Update in=[ :toarray $Data ] do={
|
||||||
:local UpdateID [ $JsonGetKey $Update "update_id" ];
|
:set UpdateID [ $JsonGetKey $Update "update_id" ];
|
||||||
:if ($UpdateID >= $TelegramChatOffset) do={
|
:if ($UpdateID >= $TelegramChatOffset->2) do={
|
||||||
:set TelegramChatOffset ($UpdateID + 1);
|
|
||||||
:local Trusted false;
|
:local Trusted false;
|
||||||
:local Message [ $JsonGetKey $Update "message" ];
|
:local Message [ $JsonGetKey $Update "message" ];
|
||||||
:local From [ $JsonGetKey $Message "from" ];
|
:local From [ $JsonGetKey $Message "from" ];
|
||||||
@ -139,3 +139,5 @@ $WaitFullyConnected;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
:set TelegramChatOffset ([ :pick $TelegramChatOffset 1 3 ], \
|
||||||
|
[ $IfThenElse ($UpdateID > 0) ($UpdateID + 1) ($TelegramChatOffset->2) ]);
|
||||||
|
Reference in New Issue
Block a user