diff --git a/CHANGELOG.md b/CHANGELOG.md index cc85a69bb2..64ffa63f2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,31 @@ * `TelegramBotAPI-extensions-api`: * Most part of sending media messages functions was removed and replaced with their `InputFile` args analogs +### 0.24.1 + +* `TelegramBotAPI`: + * `UpdateReceiver` was replaced to the package `com.github.insanusmokrassar.TelegramBotAPI.updateshandlers` + * All functions inside `com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdatesPolling` are deprecated + and will be removed in some soon versions. Their replacement are able inside `TelegramBotAPI-extensions-api` + * `UpdatesFilter` is interface for now + * Previous `UpdatesFilter` class was renamed to `SimpleUpdatesFilter` and for backward compatibility was added + builder function `UpdatesFilter`, which will be removed in near releases + * `FlowsUpdatesFilter` now implements `UpdatesFilter` + * `BaseSentMessageUpdate` and `BaseEditMessageUpdate` interfaces was added + * `EditChannelPostUpdate` now is implementing `BaseEditMessageUpdate` interface + * `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) + * `KtorRequestsExecutor` now is using a copy of incoming `HttpClient` object and install `HttpTimeout` feature + * `AbstractRequestCallFactory` now setting up a custom delay in case if request is `GetUpdates` +* `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/UpdatesUtils.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/InternalUtils/UpdatesUtils.kt new file mode 100644 index 0000000000..b7eb65f209 --- /dev/null +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/InternalUtils/UpdatesUtils.kt @@ -0,0 +1,70 @@ +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 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) { + when (update) { + is BaseEditMessageUpdate -> resultUpdates.add( + update.toEditMediaGroupUpdate() + ) + is BaseSentMessageUpdate -> { + val data = update.data + if (data is MediaGroupMessage) { + mediaGroups.getOrPut(data.mediaGroupId) { + mutableListOf() + }.add(update) + } else { + resultUpdates.add(update) + } + } + else -> resultUpdates.add(update) + } + } + mediaGroups.values.map { + it.toSentMediaGroupUpdate() ?.let { mediaGroupUpdate -> + resultUpdates.add(mediaGroupUpdate) + } + } + resultUpdates.sortBy { it.updateId } + return resultUpdates +} + +internal fun List.toSentMediaGroupUpdate(): SentMediaGroupUpdate? = (this as? SentMediaGroupUpdate) ?: let { + if (isEmpty()) { + return@let null + } + val resultList = sortedBy { it.updateId } + when (first()) { + is MessageUpdate -> MessageMediaGroupUpdate(resultList) + is ChannelPostUpdate -> ChannelPostMediaGroupUpdate(resultList) + else -> null + } +} + +internal fun BaseEditMessageUpdate.toEditMediaGroupUpdate(): EditMediaGroupUpdate = (this as? EditMediaGroupUpdate) ?: let { + when (this) { + is EditMessageUpdate -> EditMessageMediaGroupUpdate(this) + is EditChannelPostUpdate -> EditChannelPostMediaGroupUpdate(this) + else -> error("Unsupported type of ${BaseEditMessageUpdate::class.simpleName}") + } +} 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 new file mode 100644 index 0000000000..6624211c2b --- /dev/null +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt @@ -0,0 +1,137 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.updates + +import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor +import com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions.RequestException +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 io.ktor.client.features.HttpRequestTimeoutException +import kotlinx.coroutines.* + +fun RequestsExecutor.startGettingOfUpdates( + timeoutSeconds: Seconds = 30, + scope: CoroutineScope = CoroutineScope(Dispatchers.Default), + allowedUpdates: List? = null, + updatesReceiver: UpdateReceiver +): Job = scope.launch { + var lastUpdateIdentifier: UpdateIdentifier? = null + + while (isActive) { + try { + supervisorScope { + val updates = getUpdates( + offset = lastUpdateIdentifier?.plus(1), + timeout = timeoutSeconds, + allowed_updates = allowedUpdates + ).convertWithMediaGroupUpdates() + + supervisorScope { + for (update in updates) { + updatesReceiver(update) + + lastUpdateIdentifier = update.lastUpdateIdentifier() + } + } + } + } catch (e: HttpRequestTimeoutException) { + e // it is ok due to mechanism of long polling + } catch (e: RequestException) { + e // it is not ok, but in most cases it will mean that there is some limit for requests count + delay(1000L) + } + } +} + +fun RequestsExecutor.startGettingOfUpdates( + updatesFilter: UpdatesFilter, + timeoutSeconds: Seconds = 30, + scope: CoroutineScope = CoroutineScope(Dispatchers.Default) +): Job = startGettingOfUpdates( + timeoutSeconds, + scope, + updatesFilter.allowedUpdates, + updatesFilter.asUpdateReceiver +) + +fun RequestsExecutor.startGettingOfUpdates( + messageCallback: UpdateReceiver? = null, + messageMediaGroupCallback: UpdateReceiver? = null, + editedMessageCallback: UpdateReceiver? = null, + editedMessageMediaGroupCallback: UpdateReceiver? = null, + channelPostCallback: UpdateReceiver? = null, + channelPostMediaGroupCallback: UpdateReceiver? = null, + editedChannelPostCallback: UpdateReceiver? = null, + editedChannelPostMediaGroupCallback: UpdateReceiver? = null, + chosenInlineResultCallback: UpdateReceiver? = null, + inlineQueryCallback: UpdateReceiver? = null, + callbackQueryCallback: UpdateReceiver? = null, + shippingQueryCallback: UpdateReceiver? = null, + preCheckoutQueryCallback: UpdateReceiver? = null, + pollCallback: UpdateReceiver? = null, + pollAnswerCallback: UpdateReceiver? = null, + timeoutSeconds: Seconds = 30, + scope: CoroutineScope = GlobalScope +): Job { + return startGettingOfUpdates( + SimpleUpdatesFilter( + messageCallback, + messageMediaGroupCallback, + editedMessageCallback, + editedMessageMediaGroupCallback, + channelPostCallback, + channelPostMediaGroupCallback, + editedChannelPostCallback, + editedChannelPostMediaGroupCallback, + chosenInlineResultCallback, + inlineQueryCallback, + callbackQueryCallback, + shippingQueryCallback, + preCheckoutQueryCallback, + pollCallback, + pollAnswerCallback + ), + timeoutSeconds, + scope + ) +} + +fun RequestsExecutor.startGettingOfUpdates( + messageCallback: UpdateReceiver? = null, + mediaGroupCallback: UpdateReceiver? = null, + editedMessageCallback: UpdateReceiver? = null, + channelPostCallback: UpdateReceiver? = null, + editedChannelPostCallback: UpdateReceiver? = null, + chosenInlineResultCallback: UpdateReceiver? = null, + inlineQueryCallback: UpdateReceiver? = null, + callbackQueryCallback: UpdateReceiver? = null, + shippingQueryCallback: UpdateReceiver? = null, + preCheckoutQueryCallback: UpdateReceiver? = null, + pollCallback: UpdateReceiver? = null, + pollAnswerCallback: UpdateReceiver? = null, + timeoutSeconds: Seconds = 30, + scope: CoroutineScope = CoroutineScope(Dispatchers.Default) +): Job = startGettingOfUpdates( + messageCallback = messageCallback, + messageMediaGroupCallback = mediaGroupCallback, + editedMessageCallback = editedMessageCallback, + editedMessageMediaGroupCallback = mediaGroupCallback, + channelPostCallback = channelPostCallback, + channelPostMediaGroupCallback = mediaGroupCallback, + editedChannelPostCallback = editedChannelPostCallback, + editedChannelPostMediaGroupCallback = mediaGroupCallback, + chosenInlineResultCallback = chosenInlineResultCallback, + inlineQueryCallback = inlineQueryCallback, + callbackQueryCallback = callbackQueryCallback, + shippingQueryCallback = shippingQueryCallback, + preCheckoutQueryCallback = preCheckoutQueryCallback, + pollCallback = pollCallback, + pollAnswerCallback = pollAnswerCallback, + timeoutSeconds = timeoutSeconds, + scope = scope +) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt index ee184dcf90..08a98207af 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/KtorRequestsExecutor.kt @@ -13,14 +13,16 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper import io.ktor.client.HttpClient import io.ktor.client.call.receive import io.ktor.client.features.ClientRequestException +import io.ktor.client.features.HttpTimeout import io.ktor.client.statement.HttpStatement import io.ktor.client.statement.readText import kotlinx.coroutines.delay +import kotlinx.coroutines.supervisorScope import kotlinx.serialization.json.Json class KtorRequestsExecutor( telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, - private val client: HttpClient = HttpClient(), + client: HttpClient = HttpClient(), callsFactories: List = emptyList(), excludeDefaultFactories: Boolean = false, private val requestsLimiter: RequestLimiter = EmptyLimiter, @@ -34,50 +36,59 @@ class KtorRequestsExecutor( } } + private val client = client.config { + install(HttpTimeout) + } + override suspend fun execute(request: Request): T { - return requestsLimiter.limit { - var statement: HttpStatement? = null - for (factory in callsFactories) { - statement = factory.prepareCall( - client, - telegramAPIUrlsKeeper.commonAPIUrl, - request - ) - if (statement != null) { - break + return try { + supervisorScope { + requestsLimiter.limit { + var statement: HttpStatement? = null + for (factory in callsFactories) { + statement = factory.prepareCall( + client, + telegramAPIUrlsKeeper.commonAPIUrl, + request + ) + if (statement != null) { + break + } + } + + val response = statement?.execute() ?: throw IllegalArgumentException("Can't execute request: $request") + val content = response.receive() + val responseObject = jsonFormatter.parse(Response.serializer(), content) + + (responseObject.result?.let { + jsonFormatter.fromJson(request.resultDeserializer, it) + } ?: responseObject.parameters?.let { + val error = it.error + if (error is RetryAfterError) { + delay(error.leftToRetry) + execute(request) + } else { + null + } + } ?: response.let { + throw newRequestException( + responseObject, + content, + "Can't get result object from $content" + ) + }) } } - try { - val response = statement ?.execute() ?: throw IllegalArgumentException("Can't execute request: $request") - val content = response.receive() - val responseObject = jsonFormatter.parse(Response.serializer(), content) - - (responseObject.result?.let { - jsonFormatter.fromJson(request.resultDeserializer, it) - } ?: responseObject.parameters?.let { - val error = it.error - if (error is RetryAfterError) { - delay(error.leftToRetry) - execute(request) - } else { - null - } - } ?: response.let { - throw newRequestException( - responseObject, - content, - "Can't get result object from $content" - ) - }) - } catch (e: ClientRequestException) { - val content = e.response.readText() - val responseObject = jsonFormatter.parse(Response.serializer(), content) - throw newRequestException( - responseObject, - content, - "Can't get result object from $content" - ) - } + } catch (e: ClientRequestException) { + val content = e.response.readText() + val responseObject = jsonFormatter.parse(Response.serializer(), content) + throw newRequestException( + responseObject, + content, + "Can't get result object from $content" + ) + } catch (e: Exception) { + throw e } } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/AbstractRequestCallFactory.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/AbstractRequestCallFactory.kt index 892ca7dc3d..ba63261451 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/AbstractRequestCallFactory.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/Ktor/base/AbstractRequestCallFactory.kt @@ -1,8 +1,10 @@ package com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.base import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorCallFactory +import com.github.insanusmokrassar.TelegramBotAPI.requests.GetUpdates import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request import io.ktor.client.HttpClient +import io.ktor.client.features.timeout import io.ktor.client.request.* import io.ktor.client.statement.HttpStatement import io.ktor.http.ContentType @@ -28,6 +30,16 @@ abstract class AbstractRequestCallFactory : KtorCallFactory { method = HttpMethod.Post accept(ContentType.Application.Json) + if (request is GetUpdates) { + request.timeout ?.times(1000L) ?.let { customTimeoutMillis -> + if (customTimeoutMillis > 0) { + timeout { + requestTimeoutMillis = customTimeoutMillis + } + } + } + } + body = preparedBody }, client 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/types/update/ChannelPostUpdate.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/ChannelPostUpdate.kt index 3b5af5d8aa..97146d1278 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/ChannelPostUpdate.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/ChannelPostUpdate.kt @@ -3,8 +3,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseSentMessageUpdate data class ChannelPostUpdate( override val updateId: UpdateIdentifier, override val data: Message -) : BaseMessageUpdate +) : BaseSentMessageUpdate diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/EditChannelPostUpdate.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/EditChannelPostUpdate.kt index 621e5c8570..9fa544832c 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/EditChannelPostUpdate.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/EditChannelPostUpdate.kt @@ -2,9 +2,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message -import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseEditMessageUpdate data class EditChannelPostUpdate( override val updateId: UpdateIdentifier, override val data: Message -) : BaseMessageUpdate +) : BaseEditMessageUpdate diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/EditMessageUpdate.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/EditMessageUpdate.kt index 82414a2220..ea9b4b8be9 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/EditMessageUpdate.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/EditMessageUpdate.kt @@ -2,9 +2,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message -import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseEditMessageUpdate data class EditMessageUpdate( override val updateId: UpdateIdentifier, override val data: Message -) : BaseMessageUpdate +) : BaseEditMessageUpdate diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MessageUpdate.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MessageUpdate.kt index c5779d9cca..8b855fff22 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MessageUpdate.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MessageUpdate.kt @@ -3,8 +3,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseSentMessageUpdate data class MessageUpdate( override val updateId: UpdateIdentifier, override val data: Message -) : BaseMessageUpdate +) : BaseSentMessageUpdate diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/BaseEditMessageUpdate.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/BaseEditMessageUpdate.kt new file mode 100644 index 0000000000..c7ce6ea1b0 --- /dev/null +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/BaseEditMessageUpdate.kt @@ -0,0 +1,3 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts + +interface BaseEditMessageUpdate : BaseMessageUpdate diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/BaseSentMessageUpdate.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/BaseSentMessageUpdate.kt new file mode 100644 index 0000000000..ee25277a9f --- /dev/null +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/BaseSentMessageUpdate.kt @@ -0,0 +1,3 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts + +interface BaseSentMessageUpdate : BaseMessageUpdate diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt index 438cdb09ae..e57697e4fa 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt @@ -2,7 +2,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.updateshandlers import com.github.insanusmokrassar.TelegramBotAPI.types.update.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.* -import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdateReceiver +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.Flow @@ -12,7 +12,7 @@ private fun BroadcastChannel.createUpdateReceiver(): UpdateReceiver = class FlowsUpdatesFilter( broadcastChannelsSize: Int = Channel.CONFLATED -) { +): UpdatesFilter { private val messageChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) private val messageMediaGroupChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) private val editedMessageChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) @@ -28,7 +28,12 @@ class FlowsUpdatesFilter( private val preCheckoutQueryChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) private val pollChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) - val filter = UpdatesFilter( + override val allowedUpdates: List + get() = filter.allowedUpdates + override val asUpdateReceiver: UpdateReceiver + get() = filter.asUpdateReceiver + + val filter = SimpleUpdatesFilter( messageChannel.createUpdateReceiver(), messageMediaGroupChannel.createUpdateReceiver(), editedMessageChannel.createUpdateReceiver(), 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 811d42d31e..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 @@ -11,13 +11,13 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaG import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.utils.* -import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdateReceiver import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.executeUnsafe import io.ktor.client.HttpClient 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, @@ -42,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/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt index 91ccfd24eb..ba0daee6b7 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt @@ -5,9 +5,58 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UnknownUpdateType import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update -import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdateReceiver -data class UpdatesFilter( +typealias UpdateReceiver = suspend (T) -> Unit + +interface UpdatesFilter { + val asUpdateReceiver: UpdateReceiver + val allowedUpdates: List +} + +@Deprecated( + "It is builder function for SimpleUpdatesFilter", + ReplaceWith( + "SimpleUpdatesFilter", + "com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.SimpleUpdatesFilter" + ) +) +fun UpdatesFilter( + messageCallback: UpdateReceiver? = null, + messageMediaGroupCallback: UpdateReceiver? = null, + editedMessageCallback: UpdateReceiver? = null, + editedMessageMediaGroupCallback: UpdateReceiver? = null, + channelPostCallback: UpdateReceiver? = null, + channelPostMediaGroupCallback: UpdateReceiver? = null, + editedChannelPostCallback: UpdateReceiver? = null, + editedChannelPostMediaGroupCallback: UpdateReceiver? = null, + chosenInlineResultCallback: UpdateReceiver? = null, + inlineQueryCallback: UpdateReceiver? = null, + callbackQueryCallback: UpdateReceiver? = null, + shippingQueryCallback: UpdateReceiver? = null, + preCheckoutQueryCallback: UpdateReceiver? = null, + pollUpdateCallback: UpdateReceiver? = null, + pollAnswerUpdateCallback: UpdateReceiver? = null, + unknownUpdateTypeCallback: UpdateReceiver? = null +) = SimpleUpdatesFilter( + messageCallback, + messageMediaGroupCallback, + editedMessageCallback, + editedMessageMediaGroupCallback, + channelPostCallback, + channelPostMediaGroupCallback, + editedChannelPostCallback, + editedChannelPostMediaGroupCallback, + chosenInlineResultCallback, + inlineQueryCallback, + callbackQueryCallback, + shippingQueryCallback, + preCheckoutQueryCallback, + pollUpdateCallback, + pollAnswerUpdateCallback, + unknownUpdateTypeCallback +) + +data class SimpleUpdatesFilter( private val messageCallback: UpdateReceiver? = null, private val messageMediaGroupCallback: UpdateReceiver? = null, private val editedMessageCallback: UpdateReceiver? = null, @@ -24,9 +73,9 @@ data class UpdatesFilter( private val pollUpdateCallback: UpdateReceiver? = null, private val pollAnswerUpdateCallback: UpdateReceiver? = null, private val unknownUpdateTypeCallback: UpdateReceiver? = null -) { - val asUpdateReceiver: UpdateReceiver = this::invoke - val allowedUpdates = listOfNotNull( +) : UpdatesFilter { + override val asUpdateReceiver: UpdateReceiver = this::invoke + override val allowedUpdates = listOfNotNull( (messageCallback ?: messageMediaGroupCallback) ?.let { UPDATE_MESSAGE }, (editedMessageCallback ?: editedMessageMediaGroupCallback) ?.let { UPDATE_EDITED_MESSAGE }, (channelPostCallback ?: channelPostMediaGroupCallback) ?.let { UPDATE_CHANNEL_POST }, @@ -96,7 +145,7 @@ fun createSimpleUpdateFilter( pollCallback: UpdateReceiver? = null, pollAnswerCallback: UpdateReceiver? = null, unknownCallback: UpdateReceiver? = null -): UpdatesFilter = UpdatesFilter( +): UpdatesFilter = SimpleUpdatesFilter( messageCallback = messageCallback, messageMediaGroupCallback = mediaGroupCallback, editedMessageCallback = editedMessageCallback, @@ -113,4 +162,4 @@ fun createSimpleUpdateFilter( pollUpdateCallback = pollCallback, pollAnswerUpdateCallback = pollAnswerCallback, unknownUpdateTypeCallback = unknownCallback -) +) \ No newline at end of file diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt index 3131f1d858..56295e5b51 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/UpdatesPolling.kt @@ -10,8 +10,16 @@ import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.KtorUpdatesPol import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter import kotlinx.coroutines.* -typealias UpdateReceiver = suspend (T) -> Unit +@Deprecated( + "Replaced", + ReplaceWith( + "UpdateReceiver", + "com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdateReceiver" + ) +) +typealias UpdateReceiver = com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdateReceiver +@Deprecated("Replaced into TelegramBotAPI-extensions-api") fun RequestsExecutor.startGettingOfUpdates( timeoutMillis: Long = 30 * 1000, scope: CoroutineScope = CoroutineScope(Dispatchers.Default), @@ -28,6 +36,7 @@ fun RequestsExecutor.startGettingOfUpdates( } } +@Deprecated("Replaced into TelegramBotAPI-extensions-api") fun RequestsExecutor.startGettingOfUpdates( messageCallback: UpdateReceiver? = null, messageMediaGroupCallback: UpdateReceiver? = null, @@ -72,6 +81,7 @@ fun RequestsExecutor.startGettingOfUpdates( ) } +@Deprecated("Replaced into TelegramBotAPI-extensions-api") fun RequestsExecutor.startGettingOfUpdates( messageCallback: UpdateReceiver? = null, mediaGroupCallback: UpdateReceiver? = 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 626119b11d..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 @@ -4,13 +4,13 @@ import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorRequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.types.ALL_UPDATES_LIST import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper -import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdateReceiver import io.ktor.client.HttpClient import io.ktor.client.engine.HttpClientEngine 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, @@ -44,6 +44,7 @@ fun KtorUpdatesPoller( ) } +@Deprecated("Deprecated due to more simple way to get updates using TelegramBotAPI-extensions-api") @KtorExperimentalAPI fun KtorUpdatesPoller( telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, diff --git a/gradle.properties b/gradle.properties index 81b7b3c264..67f196be79 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,6 +7,6 @@ uuid_version=0.0.7 ktor_version=1.3.1 library_group=com.github.insanusmokrassar -library_version=0.24.0 +library_version=0.24.1 gradle_bintray_plugin_version=1.8.4