From 75cebf5aa3325ab2acaa57059e5c00c02018ebb4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 30 Jun 2021 13:57:20 +0600 Subject: [PATCH] add GetUpdatesConflict --- CHANGELOG.md | 1 + .../dev/inmo/tgbotapi/bot/exceptions/RequestException.kt | 9 +++++++++ .../extensions/utils/updates/retrieving/LongPolling.kt | 4 ++++ 3 files changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9015597905..27dd9d8a82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ * Added `Any#whenFromUser`, `Any#asFromUser` and`Any#requireFromUser` extensions * `MedaGroupUpdate` and its direct extenders `SentMediaGroupUpdate` and `EditMediaGroupUpdate` became `sealed interface`s + * New built-in `RequestException` implementator `GetUpdatesConflict` has been added * `Behaviour Builder`: * ❗️ All triggers (`on*` extensions) have been modified to work in parallel by some marker by default (new parameter `markerFactory`, in most cases will work async for different chats) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/exceptions/RequestException.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/exceptions/RequestException.kt index a9c6ec5f56..038411dc74 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/exceptions/RequestException.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/exceptions/RequestException.kt @@ -24,6 +24,12 @@ fun newRequestException( plainAnswer, message, cause + ), + description.contains("Conflict: terminated by other getUpdates request") -> GetUpdatesConflict( + response, + plainAnswer, + message, + cause ) else -> null } @@ -61,3 +67,6 @@ class WrongFileIdentifierException(response: Response, plainAnswer: String, mess class TooMuchRequestsException(val retryAfter: RetryAfterError, response: Response, plainAnswer: String, message: String?, cause: Throwable?) : RequestException(response, plainAnswer, message, cause) + +class GetUpdatesConflict(response: Response, plainAnswer: String, message: String?, cause: Throwable?) : + RequestException(response, plainAnswer, message, cause) diff --git a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt index 1584f94c9d..e972a61121 100644 --- a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPolling.kt @@ -3,6 +3,7 @@ package dev.inmo.tgbotapi.extensions.utils.updates.retrieving import dev.inmo.micro_utils.coroutines.* import dev.inmo.tgbotapi.bot.RequestsExecutor import dev.inmo.tgbotapi.bot.TelegramBot +import dev.inmo.tgbotapi.bot.exceptions.GetUpdatesConflict import dev.inmo.tgbotapi.bot.exceptions.RequestException import dev.inmo.tgbotapi.extensions.utils.updates.convertWithMediaGroupUpdates import dev.inmo.tgbotapi.extensions.utils.updates.lastUpdateIdentifier @@ -31,6 +32,9 @@ fun TelegramBot.longPollingFlow( if (e is RequestException) { delay(1000L) } + if (e is GetUpdatesConflict && (exceptionsHandler == null || exceptionsHandler == defaultSafelyExceptionHandler)) { + println("Warning!!! Other bot with the same bot token requests updates with getUpdate in parallel") + } } ) { val updates = execute(