From 9c9a938d9486f3cb016cbcbd7951789a9a9a8d45 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 17 Mar 2020 16:47:59 +0600 Subject: [PATCH 01/10] started 0.24.1 --- CHANGELOG.md | 1 + gradle.properties | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc85a69bb2..f43804151a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * `TelegramBotAPI-extensions-api`: * Most part of sending media messages functions was removed and replaced with their `InputFile` args analogs +### 0.24.1 ## 0.23.0 TelegramBotAPI 4.6 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 From 244a1e5175baccf9992f28d626c0565277e8a8a5 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 17 Mar 2020 16:59:28 +0600 Subject: [PATCH 02/10] replacement of UpdatesPolling help functions --- CHANGELOG.md | 8 ++ .../extensions/api/updates/UpdatesPolling.kt | 108 ++++++++++++++++++ .../updateshandlers/FlowsUpdatesFilter.kt | 1 - .../updateshandlers/KtorUpdatesPoller.kt | 1 - .../updateshandlers/UpdatesFilter.kt | 2 +- .../utils/extensions/UpdatesPolling.kt | 12 +- .../updateshandlers/KtorUpdatesCIOPoller.kt | 1 - .../utils/extensions/Webhooks.kt | 1 + 8 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index f43804151a..01100cd50f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,14 @@ ### 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` +* `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` + ## 0.23.0 TelegramBotAPI 4.6 * `Poll` now is sealed class 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..375db36de4 --- /dev/null +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/updates/UpdatesPolling.kt @@ -0,0 +1,108 @@ +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.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.KtorUpdatesPoller +import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter +import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdateReceiver +import kotlinx.coroutines.* + + +fun RequestsExecutor.startGettingOfUpdates( + timeoutMillis: Long = 30 * 1000, + 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) + } +} + +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, + timeoutMillis: Long = 30 * 1000, + scope: CoroutineScope = GlobalScope +): UpdatesPoller { + val filter = UpdatesFilter( + messageCallback, + messageMediaGroupCallback, + editedMessageCallback, + editedMessageMediaGroupCallback, + channelPostCallback, + channelPostMediaGroupCallback, + editedChannelPostCallback, + editedChannelPostMediaGroupCallback, + chosenInlineResultCallback, + inlineQueryCallback, + callbackQueryCallback, + shippingQueryCallback, + preCheckoutQueryCallback, + pollCallback, + pollAnswerCallback + ) + return startGettingOfUpdates( + timeoutMillis, + scope, + filter.allowedUpdates, + filter.asUpdateReceiver + ) +} + +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, + timeoutMillis: Long = 30 * 1000, + scope: CoroutineScope = CoroutineScope(Dispatchers.Default) +): UpdatesPoller = 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, + timeoutMillis = timeoutMillis, + scope = scope +) 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..ff86675e05 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,6 @@ 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 kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.Flow 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..1a80d6dab8 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,7 +11,6 @@ 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 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..0732b3562f 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,7 +5,6 @@ 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( private val messageCallback: UpdateReceiver? = null, @@ -114,3 +113,4 @@ fun createSimpleUpdateFilter( pollAnswerUpdateCallback = pollAnswerCallback, unknownUpdateTypeCallback = unknownCallback ) +typealias UpdateReceiver = suspend (T) -> Unit \ 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..ac4b897037 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,7 +4,6 @@ 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 diff --git a/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt b/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt index 1a6c82ce32..7333caa818 100644 --- a/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt +++ b/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt @@ -8,6 +8,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates import com.github.insanusmokrassar.TelegramBotAPI.types.update.RawUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update +import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdateReceiver import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.webhook.WebhookPrivateKeyConfig import com.github.insanusmokrassar.TelegramBotAPI.utils.convertWithMediaGroupUpdates From ba2c4cbb30d2c315a0303aab91de256dd5ca86cd Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 17 Mar 2020 17:16:14 +0600 Subject: [PATCH 03/10] UpdatesFilter now is interface --- CHANGELOG.md | 4 ++ .../updateshandlers/FlowsUpdatesFilter.kt | 10 ++- .../updateshandlers/UpdatesFilter.kt | 61 +++++++++++++++++-- 3 files changed, 67 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 01100cd50f..0442b5d865 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ * `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` * `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` 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 ff86675e05..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,6 +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.types.update.abstracts.Update import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.Flow @@ -11,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) @@ -27,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/UpdatesFilter.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt index 0732b3562f..e436b81b90 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 @@ -6,7 +6,57 @@ 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 -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, @@ -23,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 }, @@ -112,5 +162,4 @@ fun createSimpleUpdateFilter( pollUpdateCallback = pollCallback, pollAnswerUpdateCallback = pollAnswerCallback, unknownUpdateTypeCallback = unknownCallback -) -typealias UpdateReceiver = suspend (T) -> Unit \ No newline at end of file +) \ No newline at end of file From 944d15f3269eec8b6d7a013187850c3bcbc87f24 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 17 Mar 2020 17:20:42 +0600 Subject: [PATCH 04/10] new startGettingUpdates method --- CHANGELOG.md | 2 + .../extensions/api/updates/UpdatesPolling.kt | 53 +++++++++++-------- .../updateshandlers/UpdatesFilter.kt | 2 +- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0442b5d865..7c3db33a80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ * `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 ## 0.23.0 TelegramBotAPI 4.6 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 375db36de4..f7418d99f8 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 @@ -6,9 +6,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.ALL_UPDATES_LIST 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.KtorUpdatesPoller -import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter -import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdateReceiver +import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.* import kotlinx.coroutines.* @@ -28,6 +26,17 @@ fun RequestsExecutor.startGettingOfUpdates( } } +fun RequestsExecutor.startGettingOfUpdates( + updatesFilter: UpdatesFilter, + timeoutMillis: Long = 30 * 1000, + scope: CoroutineScope = CoroutineScope(Dispatchers.Default) +): UpdatesPoller = startGettingOfUpdates( + timeoutMillis, + scope, + updatesFilter.allowedUpdates, + updatesFilter.asUpdateReceiver +) + fun RequestsExecutor.startGettingOfUpdates( messageCallback: UpdateReceiver? = null, messageMediaGroupCallback: UpdateReceiver? = null, @@ -47,28 +56,26 @@ fun RequestsExecutor.startGettingOfUpdates( timeoutMillis: Long = 30 * 1000, scope: CoroutineScope = GlobalScope ): UpdatesPoller { - val filter = UpdatesFilter( - messageCallback, - messageMediaGroupCallback, - editedMessageCallback, - editedMessageMediaGroupCallback, - channelPostCallback, - channelPostMediaGroupCallback, - editedChannelPostCallback, - editedChannelPostMediaGroupCallback, - chosenInlineResultCallback, - inlineQueryCallback, - callbackQueryCallback, - shippingQueryCallback, - preCheckoutQueryCallback, - pollCallback, - pollAnswerCallback - ) return startGettingOfUpdates( + SimpleUpdatesFilter( + messageCallback, + messageMediaGroupCallback, + editedMessageCallback, + editedMessageMediaGroupCallback, + channelPostCallback, + channelPostMediaGroupCallback, + editedChannelPostCallback, + editedChannelPostMediaGroupCallback, + chosenInlineResultCallback, + inlineQueryCallback, + callbackQueryCallback, + shippingQueryCallback, + preCheckoutQueryCallback, + pollCallback, + pollAnswerCallback + ), timeoutMillis, - scope, - filter.allowedUpdates, - filter.asUpdateReceiver + scope ) } 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 e436b81b90..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 @@ -145,7 +145,7 @@ fun createSimpleUpdateFilter( pollCallback: UpdateReceiver? = null, pollAnswerCallback: UpdateReceiver? = null, unknownCallback: UpdateReceiver? = null -): UpdatesFilter = UpdatesFilter( +): UpdatesFilter = SimpleUpdatesFilter( messageCallback = messageCallback, messageMediaGroupCallback = mediaGroupCallback, editedMessageCallback = editedMessageCallback, From e7027047cb1c7d3bdc2146291cf4b017b54c2261 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 17 Mar 2020 19:46:42 +0600 Subject: [PATCH 05/10] abstracts for difference between sent and edited messages updates --- CHANGELOG.md | 5 +++++ .../TelegramBotAPI/types/update/ChannelPostUpdate.kt | 3 ++- .../TelegramBotAPI/types/update/EditChannelPostUpdate.kt | 4 ++-- .../TelegramBotAPI/types/update/EditMessageUpdate.kt | 4 ++-- .../TelegramBotAPI/types/update/MessageUpdate.kt | 3 ++- .../types/update/abstracts/BaseEditMessageUpdate.kt | 3 +++ .../types/update/abstracts/BaseSentMessageUpdate.kt | 3 +++ 7 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/BaseEditMessageUpdate.kt create mode 100644 TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/BaseSentMessageUpdate.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c3db33a80..b96a5c608b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,11 @@ * 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 * `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` 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 From be64287c42efa12b8337b6beb10c89ec2b9f6dfd Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 17 Mar 2020 19:50:25 +0600 Subject: [PATCH 06/10] copypaste functions from TelegramBotAPI to TelegramBotAPI-extensions-api with adaptation of updates --- .../api/InternalUtils/UpdatesAdapter.kt | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/InternalUtils/UpdatesAdapter.kt 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/UpdatesAdapter.kt new file mode 100644 index 0000000000..cf1d3323ea --- /dev/null +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/InternalUtils/UpdatesAdapter.kt @@ -0,0 +1,51 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils + +import com.github.insanusmokrassar.TelegramBotAPI.types.MediaGroupIdentifier +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 { + val resultUpdates = mutableListOf() + val mediaGroups = mutableMapOf>() + for (update in this) { + val asEditMediaGroupMessage = update.toEditMediaGroupUpdate() + if (asEditMediaGroupMessage != null) { + resultUpdates.add(asEditMediaGroupMessage) + } else { + val data = update.data + if (data is MediaGroupMessage) { + (mediaGroups[data.mediaGroupId] ?: mutableListOf().also { mediaGroups[data.mediaGroupId] = it }).add(update) + } else { + resultUpdates.add(update) + } + } + } + mediaGroups.values.map { + it.toSentMediaGroupUpdate() ?.let { mediaGroupUpdate -> + resultUpdates.add(mediaGroupUpdate) + } + } + return resultUpdates.sortedBy { it.updateId } +} + +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 BaseMessageUpdate.toEditMediaGroupUpdate(): EditMediaGroupUpdate? = (this as? EditMediaGroupUpdate) ?: let { + when (this) { + is EditMessageUpdate -> EditMessageMediaGroupUpdate(this) + is EditChannelPostUpdate -> EditChannelPostMediaGroupUpdate(this) + else -> null + } +} From d61aa8b50ecb354f8ed8c4d12ea8e25bcb27c16e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 17 Mar 2020 20:24:31 +0600 Subject: [PATCH 07/10] getting updates refactoring --- CHANGELOG.md | 3 ++ .../extensions/api/GetUpdates.kt | 7 ++- .../{UpdatesAdapter.kt => UpdatesUtils.kt} | 22 +++++++- .../extensions/api/updates/UpdatesPolling.kt | 51 ++++++++++++------- .../TelegramBotAPI/bot/UpdatesPoller.kt | 1 + .../TelegramBotAPI/requests/GetUpdates.kt | 6 +-- .../TelegramBotAPI/types/Common.kt | 2 + .../updateshandlers/KtorUpdatesPoller.kt | 2 + .../updateshandlers/KtorUpdatesCIOPoller.kt | 2 + 9 files changed, 67 insertions(+), 29 deletions(-) rename TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/InternalUtils/{UpdatesAdapter.kt => UpdatesUtils.kt} (76%) 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, From 71c0b688e875a00c99737f5d4bb3ae9777f954cf Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 17 Mar 2020 21:24:04 +0600 Subject: [PATCH 08/10] fixes and improvements --- CHANGELOG.md | 2 + .../extensions/api/updates/UpdatesPolling.kt | 43 +++++---- .../bot/Ktor/KtorRequestsExecutor.kt | 95 +++++++++++-------- .../Ktor/base/AbstractRequestCallFactory.kt | 12 +++ 4 files changed, 93 insertions(+), 59 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e28409cec..64ffa63f2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,8 @@ * `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` 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 f87a5cfa63..6624211c2b 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,6 +1,7 @@ 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 @@ -10,10 +11,11 @@ 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( - timeoutMillis: Seconds = 30, + timeoutSeconds: Seconds = 30, scope: CoroutineScope = CoroutineScope(Dispatchers.Default), allowedUpdates: List? = null, updatesReceiver: UpdateReceiver @@ -21,30 +23,37 @@ fun RequestsExecutor.startGettingOfUpdates( var lastUpdateIdentifier: UpdateIdentifier? = null while (isActive) { - supervisorScope { - val updates = getUpdates( - offset = lastUpdateIdentifier, - timeout = timeoutMillis, - allowed_updates = allowedUpdates - ).convertWithMediaGroupUpdates() - + try { supervisorScope { - for (update in updates) { - updatesReceiver(update) + val updates = getUpdates( + offset = lastUpdateIdentifier?.plus(1), + timeout = timeoutSeconds, + allowed_updates = allowedUpdates + ).convertWithMediaGroupUpdates() - lastUpdateIdentifier = update.lastUpdateIdentifier() + 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, - timeoutMillis: Seconds = 30, + timeoutSeconds: Seconds = 30, scope: CoroutineScope = CoroutineScope(Dispatchers.Default) ): Job = startGettingOfUpdates( - timeoutMillis, + timeoutSeconds, scope, updatesFilter.allowedUpdates, updatesFilter.asUpdateReceiver @@ -66,7 +75,7 @@ fun RequestsExecutor.startGettingOfUpdates( preCheckoutQueryCallback: UpdateReceiver? = null, pollCallback: UpdateReceiver? = null, pollAnswerCallback: UpdateReceiver? = null, - timeoutMillis: Seconds = 30, + timeoutSeconds: Seconds = 30, scope: CoroutineScope = GlobalScope ): Job { return startGettingOfUpdates( @@ -87,7 +96,7 @@ fun RequestsExecutor.startGettingOfUpdates( pollCallback, pollAnswerCallback ), - timeoutMillis, + timeoutSeconds, scope ) } @@ -105,7 +114,7 @@ fun RequestsExecutor.startGettingOfUpdates( preCheckoutQueryCallback: UpdateReceiver? = null, pollCallback: UpdateReceiver? = null, pollAnswerCallback: UpdateReceiver? = null, - timeoutMillis: Seconds = 30, + timeoutSeconds: Seconds = 30, scope: CoroutineScope = CoroutineScope(Dispatchers.Default) ): Job = startGettingOfUpdates( messageCallback = messageCallback, @@ -123,6 +132,6 @@ fun RequestsExecutor.startGettingOfUpdates( preCheckoutQueryCallback = preCheckoutQueryCallback, pollCallback = pollCallback, pollAnswerCallback = pollAnswerCallback, - timeoutMillis = timeoutMillis, + 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 From b11da02c8b36a697ce7df9e64948459eaeea8d51 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 18 Mar 2020 22:56:57 +0600 Subject: [PATCH 09/10] refactor UpdatesUtils --- .../api/InternalUtils/UpdatesUtils.kt | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) 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 index ea6b42d275..b7eb65f209 100644 --- 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 @@ -21,22 +21,23 @@ internal fun List.lastUpdateIdentifier(): UpdateIdentifier? { internal fun List.convertWithMediaGroupUpdates(): List { val resultUpdates = mutableListOf() - val mediaGroups = mutableMapOf>() + 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) - } else { - val data = update.data - if (data is MediaGroupMessage) { - (mediaGroups[data.mediaGroupId] ?: mutableListOf().also { mediaGroups[data.mediaGroupId] = it }).add(update) - } else { - resultUpdates.add(update) + 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 { @@ -48,7 +49,7 @@ internal fun List.convertWithMediaGroupUpdates(): List { return resultUpdates } -internal fun List.toSentMediaGroupUpdate(): SentMediaGroupUpdate? = (this as? SentMediaGroupUpdate) ?: let { +internal fun List.toSentMediaGroupUpdate(): SentMediaGroupUpdate? = (this as? SentMediaGroupUpdate) ?: let { if (isEmpty()) { return@let null } @@ -60,10 +61,10 @@ internal fun List.toSentMediaGroupUpdate(): SentMediaGroupUpd } } -internal fun BaseMessageUpdate.toEditMediaGroupUpdate(): EditMediaGroupUpdate? = (this as? EditMediaGroupUpdate) ?: let { +internal fun BaseEditMessageUpdate.toEditMediaGroupUpdate(): EditMediaGroupUpdate = (this as? EditMediaGroupUpdate) ?: let { when (this) { is EditMessageUpdate -> EditMessageMediaGroupUpdate(this) is EditChannelPostUpdate -> EditChannelPostMediaGroupUpdate(this) - else -> null + else -> error("Unsupported type of ${BaseEditMessageUpdate::class.simpleName}") } } From 4b980d3f4450abb2c8f135df0ab5b82cfd55c7d1 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 18 Mar 2020 22:58:23 +0600 Subject: [PATCH 10/10] remove redundant import from webhooks --- .../insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt b/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt index 7333caa818..1a6c82ce32 100644 --- a/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt +++ b/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt @@ -8,7 +8,6 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates import com.github.insanusmokrassar.TelegramBotAPI.types.update.RawUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update -import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdateReceiver import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.webhook.WebhookPrivateKeyConfig import com.github.insanusmokrassar.TelegramBotAPI.utils.convertWithMediaGroupUpdates