diff --git a/CHANGELOG.md b/CHANGELOG.md index b96a5c608b..3e28409cec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,11 +24,14 @@ * `EditMessageUpdate` now is implementing `BaseEditMessageUpdate` interface * `ChannelPostUpdate` now is implementing `BaseSentMessageUpdate` interface * `MessageUpdate` now is implementing `BaseSentMessageUpdate` interface + * `UpdatesPoller` and all its usages, childs and childs usages now are deprecated + * `GetUpdates#timeout` type now is `Seconds` (in fact it is `Int` as previously) * `TelegramBotAPI-extensions-api`: * All functions from `com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdatesPolling` now available in package `com.github.insanusmokrassar.TelegramBotAPI.extensions.api.updates.UpdatesPolling` * Now new method of getting updates available: `startGettingUpdates` with `UpdatesFilter` as incoming first parameter + * `startGettingUpdates` with `receiver` and `allowedUpdates` parameters now will handle updates by itself ## 0.23.0 TelegramBotAPI 4.6 diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/GetUpdates.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/GetUpdates.kt index 7d9edc0b54..728dfb0ec5 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/GetUpdates.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/GetUpdates.kt @@ -2,14 +2,13 @@ package com.github.insanusmokrassar.TelegramBotAPI.extensions.api import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.requests.GetUpdates -import com.github.insanusmokrassar.TelegramBotAPI.types.ALL_UPDATES_LIST -import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier +import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update suspend fun RequestsExecutor.getUpdates( offset: UpdateIdentifier? = null, limit: Int? = null, - timeout: Int? = null, + timeout: Seconds? = null, allowed_updates: List? = ALL_UPDATES_LIST ) = execute( GetUpdates( @@ -20,7 +19,7 @@ suspend fun RequestsExecutor.getUpdates( suspend fun RequestsExecutor.getUpdates( lastUpdate: Update, limit: Int? = null, - timeout: Int? = null, + timeout: Seconds? = null, allowed_updates: List? = ALL_UPDATES_LIST ) = getUpdates( lastUpdate.updateId + 1, limit, timeout, allowed_updates diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/InternalUtils/UpdatesAdapter.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/InternalUtils/UpdatesUtils.kt similarity index 76% rename from TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/InternalUtils/UpdatesAdapter.kt rename to TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/InternalUtils/UpdatesUtils.kt index cf1d3323ea..ea6b42d275 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/InternalUtils/UpdatesAdapter.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/InternalUtils/UpdatesUtils.kt @@ -1,15 +1,32 @@ package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils import com.github.insanusmokrassar.TelegramBotAPI.types.MediaGroupIdentifier +import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage import com.github.insanusmokrassar.TelegramBotAPI.types.update.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.* -internal fun List.convertWithMediaGroupUpdates(): List { +internal fun Update.lastUpdateIdentifier(): UpdateIdentifier { + return if (this is SentMediaGroupUpdate) { + origins.last().updateId + } else { + updateId + } +} + +internal fun List.lastUpdateIdentifier(): UpdateIdentifier? { + return maxBy { it.updateId } ?.lastUpdateIdentifier() +} + +internal fun List.convertWithMediaGroupUpdates(): List { val resultUpdates = mutableListOf() val mediaGroups = mutableMapOf>() for (update in this) { + if (update !is BaseMessageUpdate) { + resultUpdates.add(update) + continue + } val asEditMediaGroupMessage = update.toEditMediaGroupUpdate() if (asEditMediaGroupMessage != null) { resultUpdates.add(asEditMediaGroupMessage) @@ -27,7 +44,8 @@ internal fun List.convertWithMediaGroupUpdates(): List.toSentMediaGroupUpdate(): SentMediaGroupUpdate? = (this as? SentMediaGroupUpdate) ?: let { diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt index f7418d99f8..f87a5cfa63 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt @@ -1,36 +1,49 @@ package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.updates import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor -import com.github.insanusmokrassar.TelegramBotAPI.bot.UpdatesPoller -import com.github.insanusmokrassar.TelegramBotAPI.types.ALL_UPDATES_LIST +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils.convertWithMediaGroupUpdates +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils.lastUpdateIdentifier +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.getUpdates +import com.github.insanusmokrassar.TelegramBotAPI.types.Seconds +import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.update.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.* import kotlinx.coroutines.* - fun RequestsExecutor.startGettingOfUpdates( - timeoutMillis: Long = 30 * 1000, + timeoutMillis: Seconds = 30, scope: CoroutineScope = CoroutineScope(Dispatchers.Default), allowedUpdates: List? = null, - block: UpdateReceiver -): UpdatesPoller { - return KtorUpdatesPoller( - this, - timeoutMillis.toInt() / 1000, - allowedUpdates = allowedUpdates ?: ALL_UPDATES_LIST, - updatesReceiver = block - ).also { - it.start(scope) + updatesReceiver: UpdateReceiver +): Job = scope.launch { + var lastUpdateIdentifier: UpdateIdentifier? = null + + while (isActive) { + supervisorScope { + val updates = getUpdates( + offset = lastUpdateIdentifier, + timeout = timeoutMillis, + allowed_updates = allowedUpdates + ).convertWithMediaGroupUpdates() + + supervisorScope { + for (update in updates) { + updatesReceiver(update) + + lastUpdateIdentifier = update.lastUpdateIdentifier() + } + } + } } } fun RequestsExecutor.startGettingOfUpdates( updatesFilter: UpdatesFilter, - timeoutMillis: Long = 30 * 1000, + timeoutMillis: Seconds = 30, scope: CoroutineScope = CoroutineScope(Dispatchers.Default) -): UpdatesPoller = startGettingOfUpdates( +): Job = startGettingOfUpdates( timeoutMillis, scope, updatesFilter.allowedUpdates, @@ -53,9 +66,9 @@ fun RequestsExecutor.startGettingOfUpdates( preCheckoutQueryCallback: UpdateReceiver? = null, pollCallback: UpdateReceiver? = null, pollAnswerCallback: UpdateReceiver? = null, - timeoutMillis: Long = 30 * 1000, + timeoutMillis: Seconds = 30, scope: CoroutineScope = GlobalScope -): UpdatesPoller { +): Job { return startGettingOfUpdates( SimpleUpdatesFilter( messageCallback, @@ -92,9 +105,9 @@ fun RequestsExecutor.startGettingOfUpdates( preCheckoutQueryCallback: UpdateReceiver? = null, pollCallback: UpdateReceiver? = null, pollAnswerCallback: UpdateReceiver? = null, - timeoutMillis: Long = 30 * 1000, + timeoutMillis: Seconds = 30, scope: CoroutineScope = CoroutineScope(Dispatchers.Default) -): UpdatesPoller = startGettingOfUpdates( +): Job = startGettingOfUpdates( messageCallback = messageCallback, messageMediaGroupCallback = mediaGroupCallback, editedMessageCallback = editedMessageCallback, diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/UpdatesPoller.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/UpdatesPoller.kt index cf53676cb2..f2fb0a61ec 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/UpdatesPoller.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/UpdatesPoller.kt @@ -4,6 +4,7 @@ import io.ktor.utils.io.core.Closeable import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +@Deprecated("Deprecated due to more simple way to get updates using TelegramBotAPI-extensions-api") interface UpdatesPoller : Closeable { fun start(scope: CoroutineScope = CoroutineScope(Dispatchers.Default)) } \ No newline at end of file diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt index f08e17c78c..4c5a4547c4 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt @@ -1,9 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests -import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest -import com.github.insanusmokrassar.TelegramBotAPI.types.ALL_UPDATES_LIST -import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier +import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UpdateSerializerWithoutDeserialization import kotlinx.serialization.* @@ -17,7 +15,7 @@ private val updatesListSerializer = ArrayListSerializer( data class GetUpdates( val offset: UpdateIdentifier? = null,// set `last update id + 1` to receive next part of updates val limit: Int? = null, - val timeout: Int? = null, + val timeout: Seconds? = null, val allowed_updates: List? = ALL_UPDATES_LIST ): SimpleRequest> { override fun method(): String = "getUpdates" diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt index a2d146825b..f460b7b9e8 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt @@ -20,6 +20,8 @@ typealias PollIdentifier = String typealias StickerSetName = String typealias FileUniqueId = String +typealias Seconds = Int + val callbackQueryAnswerLength = 0 until 200 val captionLength = 0 until 1024 val textLength = 0 until 4096 diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/KtorUpdatesPoller.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/KtorUpdatesPoller.kt index 1a80d6dab8..81f393e2d5 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/KtorUpdatesPoller.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/KtorUpdatesPoller.kt @@ -17,6 +17,7 @@ import io.ktor.client.engine.HttpClientEngine import kotlinx.coroutines.* import kotlinx.coroutines.channels.Channel +@Deprecated("Deprecated due to more simple way to get updates using TelegramBotAPI-extensions-api") fun KtorUpdatesPoller( telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, engine: HttpClientEngine, @@ -41,6 +42,7 @@ fun KtorUpdatesPoller( ) } +@Deprecated("Deprecated due to more simple way to get updates using TelegramBotAPI-extensions-api") class KtorUpdatesPoller( private val executor: RequestsExecutor, private val timeoutSeconds: Int? = null, diff --git a/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/KtorUpdatesCIOPoller.kt b/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/KtorUpdatesCIOPoller.kt index ac4b897037..ba6d7be94c 100644 --- a/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/KtorUpdatesCIOPoller.kt +++ b/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/KtorUpdatesCIOPoller.kt @@ -10,6 +10,7 @@ import io.ktor.client.engine.cio.CIO import io.ktor.client.engine.cio.endpoint import io.ktor.util.KtorExperimentalAPI +@Deprecated("Deprecated due to more simple way to get updates using TelegramBotAPI-extensions-api") @KtorExperimentalAPI fun KtorUpdatesPoller( telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, @@ -43,6 +44,7 @@ fun KtorUpdatesPoller( ) } +@Deprecated("Deprecated due to more simple way to get updates using TelegramBotAPI-extensions-api") @KtorExperimentalAPI fun KtorUpdatesPoller( telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,