From 01da98d2feb5ed8580170d0a1a2ea3b75df0929a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 13 May 2020 19:48:07 +0600 Subject: [PATCH 01/46] start 0.27.3 --- CHANGELOG.md | 2 ++ gradle.properties | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index decf6501a0..af0b7e5fde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,8 @@ * `closePollExactAfter` * `closePollAfter` +### 0.27.3 + ### 0.27.2 * `Common`: diff --git a/gradle.properties b/gradle.properties index 80cf0b5aea..fa3887df82 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,6 +7,6 @@ uuid_version=0.1.0 ktor_version=1.3.2 library_group=com.github.insanusmokrassar -library_version=0.27.2 +library_version=0.27.3 gradle_bintray_plugin_version=1.8.4 From 0de1d9cfda55b7d79f05505dc959bedc731826ef Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 13 May 2020 19:53:27 +0600 Subject: [PATCH 02/46] UpdateDeserializationStrategy currently is public --- CHANGELOG.md | 3 +++ .../TelegramBotAPI/types/update/abstracts/Update.kt | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af0b7e5fde..e589ceb449 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,9 @@ ### 0.27.3 +* `TelegramBotAPI`: + * Currently `UpdateDeserializationStrategy` is publicly available + ### 0.27.2 * `Common`: diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt index fa2129f854..874d3860fc 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt @@ -26,7 +26,14 @@ internal object UpdateSerializerWithoutSerialization : KSerializer { override fun serialize(encoder: Encoder, value: Update) = throw UnsupportedOperationException() } -internal object UpdateDeserializationStrategy : DeserializationStrategy { +/** + * Use this object to deserialize objects with type [Update]. Currently it is restricted to use this + * [DeserializationStrategy] only with JSON + * + * @see StringFormat.parse + * @see kotlinx.serialization.json.Json.parse + */ +object UpdateDeserializationStrategy : DeserializationStrategy { override val descriptor: SerialDescriptor = JsonElementSerializer.descriptor override fun patch(decoder: Decoder, old: Update): Update = throw UpdateNotSupportedException("Update") From be5b3745b96ed2bbcdc714a7f7430e4605596e08 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 13 May 2020 20:01:58 +0600 Subject: [PATCH 03/46] extension was added --- CHANGELOG.md | 2 ++ .../extensions/utils/JsonFormat.kt | 11 +++++++ .../utils/updates/UpdateDeserialization.kt | 31 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/JsonFormat.kt create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdateDeserialization.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index e589ceb449..31aa7c5f8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,8 @@ * `TelegramBotAPI`: * Currently `UpdateDeserializationStrategy` is publicly available +* `TelegramBotAPI-extensions-utils`: + * Extension `asTelegramUpdate` was added ### 0.27.2 diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/JsonFormat.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/JsonFormat.kt new file mode 100644 index 0000000000..63977802c2 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/JsonFormat.kt @@ -0,0 +1,11 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils + +import kotlinx.serialization.json.Json + +@Suppress("EXPERIMENTAL_API_USAGE") +internal val nonstrictJsonFormat = Json { + isLenient = true + ignoreUnknownKeys = true + serializeSpecialFloatingPointValues = true + useArrayPolymorphism = true +} diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdateDeserialization.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdateDeserialization.kt new file mode 100644 index 0000000000..fa650fdd1e --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdateDeserialization.kt @@ -0,0 +1,31 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates + +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.nonstrictJsonFormat +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UpdateDeserializationStrategy +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonElement + +/** + * @return Deserialize [source] as [com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update] + */ +fun Json.asTelegramUpdate(source: String) = parse(UpdateDeserializationStrategy, source) +/** + * @return Deserialize [source] as [com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update] + */ +fun Json.asTelegramUpdate(source: JsonElement) = fromJson(UpdateDeserializationStrategy, source) + +/** + * @return Deserialize [this] as [com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update]. In fact, + * it is must be JSON + * + * @see Json.asTelegramUpdate + */ +fun String.asTelegramUpdate() = nonstrictJsonFormat.asTelegramUpdate(this) +/** + * @return Deserialize [this] as [com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update] + * + * @see Json.asTelegramUpdate + */ +fun JsonElement.asTelegramUpdate() = nonstrictJsonFormat.asTelegramUpdate(this) + + From e776c5182f94e68f579a462218b9d3f4b1e3cca6 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 13 May 2020 20:56:03 +0600 Subject: [PATCH 04/46] additional setWebhook --- CHANGELOG.md | 2 + .../extensions/api/webhook/SetWebhook.kt | 6 + .../utils/extensions/Webhooks.kt | 190 +++++++++++------- 3 files changed, 125 insertions(+), 73 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31aa7c5f8a..670fce14ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,8 @@ * `TelegramBotAPI`: * Currently `UpdateDeserializationStrategy` is publicly available +* `TelegramBotAPI-extensions-api`: + * New extensions `setWebhook` and `includeWebhookInRoute` was added * `TelegramBotAPI-extensions-utils`: * Extension `asTelegramUpdate` was added diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/webhook/SetWebhook.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/webhook/SetWebhook.kt index 7d2e45d493..73fb7c418e 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/webhook/SetWebhook.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/webhook/SetWebhook.kt @@ -5,6 +5,9 @@ import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartFile import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook +/** + * Use this method to send information about webhook (like [url] and [certificate]) + */ suspend fun RequestsExecutor.setWebhookInfo( url: String, certificate: FileId, @@ -16,6 +19,9 @@ suspend fun RequestsExecutor.setWebhookInfo( ) ) +/** + * Use this method to send information about webhook (like [url] and [certificate]) + */ suspend fun RequestsExecutor.setWebhookInfo( url: String, certificate: MultipartFile, 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 64aaf9caf8..5ae230d16e 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 @@ -12,16 +12,119 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.* import io.ktor.application.call import io.ktor.request.receiveText import io.ktor.response.respond -import io.ktor.routing.post -import io.ktor.routing.routing +import io.ktor.routing.* import io.ktor.server.engine.* import kotlinx.coroutines.* import kotlinx.coroutines.channels.Channel import java.util.concurrent.Executors -import java.util.concurrent.TimeUnit /** - * Reverse proxy webhook. + * @param [scope] Will be used for mapping of media groups + * @param [exceptionsHandler] Pass this parameter to set custom exception handler for getting updates + * @param [block] Some receiver block like [com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter] + * + * @see com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter + * @see UpdatesFilter + * @see UpdatesFilter.asUpdateReceiver + */ +fun Route.includeWebhookInRoute( + scope: CoroutineScope, + exceptionsHandler: (suspend (Exception) -> Unit)? = null, + block: UpdateReceiver +): Job { + val updatesChannel = Channel(Channel.UNLIMITED) + val mediaGroupChannel = Channel>(Channel.UNLIMITED) + val mediaGroupAccumulatedChannel = mediaGroupChannel.accumulateByKey( + 1000L, + scope = scope + ) + post { + handleSafely( + exceptionsHandler ?: {} + ) { + val asJson = nonstrictJsonFormat.parseJson(call.receiveText()) + val update = nonstrictJsonFormat.fromJson( + UpdateDeserializationStrategy, + asJson + ) + updatesChannel.send(update) + } + call.respond("Ok") + } + return scope.launch { + launch { + for (update in updatesChannel) { + when (val data = update.data) { + is MediaGroupMessage -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate) + else -> block(update) + } + } + } + launch { + for ((_, mediaGroup) in mediaGroupAccumulatedChannel) { + mediaGroup.convertWithMediaGroupUpdates().forEach { + block(it) + } + } + } + } +} + +/** + * Setting up ktor server, set webhook info via [SetWebhook] request. + * + * @param port port which will be listen by bot + * @param listenRoute address to listen by bot + * @param scope Scope which will be used for + * + * @see com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter + * @see UpdatesFilter + * @see UpdatesFilter.asUpdateReceiver + */ +fun setWebhook( + port: Int, + engineFactory: ApplicationEngineFactory<*, *>, + listenHost: String = "0.0.0.0", + listenRoute: String = "/", + privateKeyConfig: WebhookPrivateKeyConfig? = null, + scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), + exceptionsHandler: (suspend (Exception) -> Unit)? = null, + block: UpdateReceiver +): ApplicationEngine { + lateinit var engine: ApplicationEngine + val env = applicationEngineEnvironment { + + module { + routing { + route(listenRoute) { + includeWebhookInRoute(scope, exceptionsHandler, block) + } + } + } + privateKeyConfig ?.let { + sslConnector( + privateKeyConfig.keyStore, + privateKeyConfig.aliasName, + privateKeyConfig::keyStorePassword, + privateKeyConfig::aliasPassword + ) { + host = listenHost + this.port = port + } + } ?: connector { + host = listenHost + this.port = port + } + + } + engine = embeddedServer(engineFactory, env) + engine.start(false) + + return engine +} + +/** + * Setting up ktor server, set webhook info via [SetWebhook] request. * * @param url URL of webhook WITHOUT including of [port] * @param port port which will be listen by bot @@ -29,6 +132,10 @@ import java.util.concurrent.TimeUnit * @param certificate [com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartFile] or [com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId] * which will be used by telegram to send encrypted messages * @param scope Scope which will be used for + * + * @see com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter + * @see UpdatesFilter + * @see UpdatesFilter.asUpdateReceiver */ suspend fun RequestsExecutor.setWebhook( url: String, @@ -60,81 +167,18 @@ suspend fun RequestsExecutor.setWebhook( allowedUpdates ) ) - val updatesChannel = Channel(Channel.UNLIMITED) - val mediaGroupChannel = Channel>(Channel.UNLIMITED) - val mediaGroupAccumulatedChannel = mediaGroupChannel.accumulateByKey( - 1000L, - scope = scope - ) - val env = applicationEngineEnvironment { - module { - routing { - post(listenRoute) { - handleSafely( - { - exceptionsHandler ?.invoke(it) - } - ) { - val asJson = nonstrictJsonFormat.parseJson(call.receiveText()) - val update = nonstrictJsonFormat.fromJson( - UpdateDeserializationStrategy, - asJson - ) - updatesChannel.send(update) - } - call.respond("Ok") - } - } - } - privateKeyConfig ?.let { - sslConnector( - privateKeyConfig.keyStore, - privateKeyConfig.aliasName, - privateKeyConfig::keyStorePassword, - privateKeyConfig::aliasPassword - ) { - host = listenHost - this.port = port - } - } ?: connector { - host = listenHost - this.port = port - } - } - val engine = embeddedServer(engineFactory, env) - - try { + return try { executeDeferred.await() + val engine = setWebhook(port, engineFactory, listenHost, listenRoute, privateKeyConfig, scope, exceptionsHandler, block) + scope.launch { + engine.environment.parentCoroutineContext[Job] ?.join() + engine.stop(1000, 5000) + } } catch (e: Exception) { - env.stop() throw e } - - return scope.launch { - launch { - for (update in updatesChannel) { - val data = update.data - when (data) { - is MediaGroupMessage -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate) - else -> block(update) - } - } - } - launch { - for ((_, mediaGroup) in mediaGroupAccumulatedChannel) { - mediaGroup.convertWithMediaGroupUpdates().forEach { - block(it) - } - } - } - engine.start(false) - }.also { - it.invokeOnCompletion { - engine.stop(1000L, 0L, TimeUnit.MILLISECONDS) - } - } } suspend fun RequestsExecutor.setWebhook( From 05e8c9c90d6933217952a593c0175f5589194b45 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 13 May 2020 21:21:07 +0600 Subject: [PATCH 05/46] replace webhooks work into api subproject --- CHANGELOG.md | 2 + .../extensions/api/JsonUtils.kt | 11 + .../extensions/api/utils/UpdatesHandling.kt | 47 ++++ .../{SetWebhook.kt => SetWebhookInfo.kt} | 0 .../extensions/api/SetWebhook.kt | 245 ++++++++++++++++++ .../MediaGroupUpdates/MediaGroupUpdate.kt | 7 + .../utils/extensions/Webhooks.kt | 193 ++++++-------- 7 files changed, 388 insertions(+), 117 deletions(-) create mode 100644 TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/JsonUtils.kt create mode 100644 TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/utils/UpdatesHandling.kt rename TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/webhook/{SetWebhook.kt => SetWebhookInfo.kt} (100%) create mode 100644 TelegramBotAPI-extensions-api/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/SetWebhook.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 670fce14ce..a7b616d0e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,8 +53,10 @@ * `TelegramBotAPI`: * Currently `UpdateDeserializationStrategy` is publicly available + * All `setWebhook` extensions was marked as deprecated and replaced into `TelegramBotAPI-extensions-api` * `TelegramBotAPI-extensions-api`: * New extensions `setWebhook` and `includeWebhookInRoute` was added + * New extension `CoroutineScope#updateHandlerWithMediaGroupsAdaptation` was added * `TelegramBotAPI-extensions-utils`: * Extension `asTelegramUpdate` was added diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/JsonUtils.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/JsonUtils.kt new file mode 100644 index 0000000000..a88439d920 --- /dev/null +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/JsonUtils.kt @@ -0,0 +1,11 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.api + +import kotlinx.serialization.json.Json + +@Suppress("EXPERIMENTAL_API_USAGE") +internal val nonstrictJsonFormat = Json { + isLenient = true + ignoreUnknownKeys = true + serializeSpecialFloatingPointValues = true + useArrayPolymorphism = true +} diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/utils/UpdatesHandling.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/utils/UpdatesHandling.kt new file mode 100644 index 0000000000..37b3bf1ec0 --- /dev/null +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/utils/UpdatesHandling.kt @@ -0,0 +1,47 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.utils + +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils.convertWithMediaGroupUpdates +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdateReceiver +import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.accumulateByKey +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.launch + +/** + * Create [UpdateReceiver] object which will correctly accumulate updates and send into output updates which INCLUDE + * [com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.MediaGroupUpdate]s. + * + * @see UpdateReceiver + */ +fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation( + output: UpdateReceiver +): UpdateReceiver { + val updatesChannel = Channel(Channel.UNLIMITED) + val mediaGroupChannel = Channel>(Channel.UNLIMITED) + val mediaGroupAccumulatedChannel = mediaGroupChannel.accumulateByKey( + 1000L, + scope = this + ) + + launch { + launch { + for (update in updatesChannel) { + when (val data = update.data) { + is MediaGroupMessage -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate) + else -> output(update) + } + } + } + launch { + for ((_, mediaGroup) in mediaGroupAccumulatedChannel) { + mediaGroup.convertWithMediaGroupUpdates().forEach { + output(it) + } + } + } + } + + return { updatesChannel.send(it) } +} diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/webhook/SetWebhook.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/webhook/SetWebhookInfo.kt similarity index 100% rename from TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/webhook/SetWebhook.kt rename to TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/webhook/SetWebhookInfo.kt diff --git a/TelegramBotAPI-extensions-api/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/SetWebhook.kt b/TelegramBotAPI-extensions-api/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/SetWebhook.kt new file mode 100644 index 0000000000..62ee809846 --- /dev/null +++ b/TelegramBotAPI-extensions-api/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/SetWebhook.kt @@ -0,0 +1,245 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.api + +import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils.convertWithMediaGroupUpdates +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.utils.updateHandlerWithMediaGroupsAdaptation +import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.InputFile +import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.* +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.extensions.accumulateByKey +import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.executeAsync +import com.github.insanusmokrassar.TelegramBotAPI.utils.handleSafely +import io.ktor.application.call +import io.ktor.request.receiveText +import io.ktor.response.respond +import io.ktor.routing.* +import io.ktor.server.engine.* +import kotlinx.coroutines.* +import kotlinx.coroutines.channels.Channel +import java.util.concurrent.Executors + +/** + * @param [scope] Will be used for mapping of media groups + * @param [exceptionsHandler] Pass this parameter to set custom exception handler for getting updates + * @param [block] Some receiver block like [com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter] + * + * @see com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter + * @see UpdatesFilter + * @see UpdatesFilter.asUpdateReceiver + */ +fun Route.includeWebhookInRoute( + scope: CoroutineScope, + exceptionsHandler: (suspend (Exception) -> Unit)? = null, + block: UpdateReceiver +) { + val transformer = scope.updateHandlerWithMediaGroupsAdaptation(block) + post { + handleSafely( + exceptionsHandler ?: {} + ) { + val asJson = + nonstrictJsonFormat.parseJson(call.receiveText()) + val update = nonstrictJsonFormat.fromJson( + UpdateDeserializationStrategy, + asJson + ) + transformer(update) + } + call.respond("Ok") + } +} + +/** + * Setting up ktor server, set webhook info via [SetWebhook] request. + * + * @param port port which will be listen by bot + * @param listenRoute address to listen by bot + * @param scope Scope which will be used for + * + * @see com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter + * @see UpdatesFilter + * @see UpdatesFilter.asUpdateReceiver + */ +fun setWebhook( + port: Int, + engineFactory: ApplicationEngineFactory<*, *>, + listenHost: String = "0.0.0.0", + listenRoute: String = "/", + privateKeyConfig: WebhookPrivateKeyConfig? = null, + scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), + exceptionsHandler: (suspend (Exception) -> Unit)? = null, + block: UpdateReceiver +): ApplicationEngine { + lateinit var engine: ApplicationEngine + val env = applicationEngineEnvironment { + + module { + routing { + route(listenRoute) { + includeWebhookInRoute(scope, exceptionsHandler, block) + } + } + } + privateKeyConfig ?.let { + sslConnector( + privateKeyConfig.keyStore, + privateKeyConfig.aliasName, + privateKeyConfig::keyStorePassword, + privateKeyConfig::aliasPassword + ) { + host = listenHost + this.port = port + } + } ?: connector { + host = listenHost + this.port = port + } + + } + engine = embeddedServer(engineFactory, env) + engine.start(false) + + return engine +} + +/** + * Setting up ktor server, set webhook info via [SetWebhook] request. + * + * @param url URL of webhook WITHOUT including of [port] + * @param port port which will be listen by bot + * @param listenRoute address to listen by bot + * @param certificate [com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartFile] or [com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId] + * which will be used by telegram to send encrypted messages + * @param scope Scope which will be used for + * + * @see com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter + * @see UpdatesFilter + * @see UpdatesFilter.asUpdateReceiver + */ +suspend fun RequestsExecutor.setWebhook( + url: String, + port: Int, + engineFactory: ApplicationEngineFactory<*, *>, + listenHost: String = "0.0.0.0", + listenRoute: String = "/", + certificate: InputFile? = null, + privateKeyConfig: WebhookPrivateKeyConfig? = null, + scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), + allowedUpdates: List? = null, + maxAllowedConnections: Int? = null, + exceptionsHandler: (suspend (Exception) -> Unit)? = null, + block: UpdateReceiver +): Job { + val executeDeferred = certificate ?.let { + executeAsync( + SetWebhook( + url, + certificate, + maxAllowedConnections, + allowedUpdates + ) + ) + } ?: executeAsync( + SetWebhook( + url, + maxAllowedConnections, + allowedUpdates + ) + ) + + + return try { + executeDeferred.await() + val engine = setWebhook(port, engineFactory, listenHost, listenRoute, privateKeyConfig, scope, exceptionsHandler, block) + scope.launch { + engine.environment.parentCoroutineContext[Job] ?.join() + engine.stop(1000, 5000) + } + } catch (e: Exception) { + throw e + } +} + +suspend fun RequestsExecutor.setWebhook( + url: String, + port: Int, + engineFactory: ApplicationEngineFactory<*, *>, + listenHost: String = "0.0.0.0", + listenRoute: String = "/", + certificate: InputFile? = null, + privateKeyConfig: WebhookPrivateKeyConfig? = null, + scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), + allowedUpdates: List? = null, + maxAllowedConnections: Int? = null, + block: UpdateReceiver +) = setWebhook( + url, + port, + engineFactory, + listenHost, + listenRoute, + certificate, + privateKeyConfig, + scope, + allowedUpdates, + maxAllowedConnections, + null, + block +) + +@Suppress("unused") +suspend fun RequestsExecutor.setWebhook( + url: String, + port: Int, + engineFactory: ApplicationEngineFactory<*, *>, + certificate: InputFile? = null, + privateKeyConfig: WebhookPrivateKeyConfig? = null, + scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), + allowedUpdates: List? = null, + maxAllowedConnections: Int? = null, + block: UpdateReceiver +) = setWebhook( + url, + port, + engineFactory, + certificate ?.let { "0.0.0.0" } ?: "localhost", + "/", + certificate, + privateKeyConfig, + scope, + allowedUpdates, + maxAllowedConnections, + block +) + +@Suppress("unused") +suspend fun RequestsExecutor.setWebhook( + url: String, + port: Int, + filter: UpdatesFilter, + engineFactory: ApplicationEngineFactory<*, *>, + certificate: InputFile? = null, + privateKeyConfig: WebhookPrivateKeyConfig? = null, + scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), + maxAllowedConnections: Int? = null, + listenHost: String = certificate ?.let { "0.0.0.0" } ?: "localhost", + listenRoute: String = "/" +): Job = setWebhook( + url, + port, + engineFactory, + listenHost, + listenRoute, + certificate, + privateKeyConfig, + scope, + filter.allowedUpdates, + maxAllowedConnections, + filter.asUpdateReceiver +) + + diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MediaGroupUpdate.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MediaGroupUpdate.kt index 5724d2d9b6..6a9936cfcf 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MediaGroupUpdate.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/MediaGroupUpdates/MediaGroupUpdate.kt @@ -3,6 +3,13 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.* +/** + * By default there is no instances of objects which could be deserialized from raw updates. If you want to get objects + * with this type, you should use something like [com.github.insanusmokrassar.TelegramBotAPI.extensions.api.SetWebhookKt.includeWebhookInRoute] + * + * @see com.github.insanusmokrassar.TelegramBotAPI.extensions.api.SetWebhookKt.includeWebhookInRoute + * @see com.github.insanusmokrassar.TelegramBotAPI.extensions.api.updates.UpdatesPollingKt.startGettingOfUpdates + */ interface MediaGroupUpdate : Update interface SentMediaGroupUpdate: MediaGroupUpdate { 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 5ae230d16e..7afc0f7e69 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 @@ -17,114 +17,10 @@ import io.ktor.server.engine.* import kotlinx.coroutines.* import kotlinx.coroutines.channels.Channel import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit /** - * @param [scope] Will be used for mapping of media groups - * @param [exceptionsHandler] Pass this parameter to set custom exception handler for getting updates - * @param [block] Some receiver block like [com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter] - * - * @see com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter - * @see UpdatesFilter - * @see UpdatesFilter.asUpdateReceiver - */ -fun Route.includeWebhookInRoute( - scope: CoroutineScope, - exceptionsHandler: (suspend (Exception) -> Unit)? = null, - block: UpdateReceiver -): Job { - val updatesChannel = Channel(Channel.UNLIMITED) - val mediaGroupChannel = Channel>(Channel.UNLIMITED) - val mediaGroupAccumulatedChannel = mediaGroupChannel.accumulateByKey( - 1000L, - scope = scope - ) - post { - handleSafely( - exceptionsHandler ?: {} - ) { - val asJson = nonstrictJsonFormat.parseJson(call.receiveText()) - val update = nonstrictJsonFormat.fromJson( - UpdateDeserializationStrategy, - asJson - ) - updatesChannel.send(update) - } - call.respond("Ok") - } - return scope.launch { - launch { - for (update in updatesChannel) { - when (val data = update.data) { - is MediaGroupMessage -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate) - else -> block(update) - } - } - } - launch { - for ((_, mediaGroup) in mediaGroupAccumulatedChannel) { - mediaGroup.convertWithMediaGroupUpdates().forEach { - block(it) - } - } - } - } -} - -/** - * Setting up ktor server, set webhook info via [SetWebhook] request. - * - * @param port port which will be listen by bot - * @param listenRoute address to listen by bot - * @param scope Scope which will be used for - * - * @see com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter - * @see UpdatesFilter - * @see UpdatesFilter.asUpdateReceiver - */ -fun setWebhook( - port: Int, - engineFactory: ApplicationEngineFactory<*, *>, - listenHost: String = "0.0.0.0", - listenRoute: String = "/", - privateKeyConfig: WebhookPrivateKeyConfig? = null, - scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), - exceptionsHandler: (suspend (Exception) -> Unit)? = null, - block: UpdateReceiver -): ApplicationEngine { - lateinit var engine: ApplicationEngine - val env = applicationEngineEnvironment { - - module { - routing { - route(listenRoute) { - includeWebhookInRoute(scope, exceptionsHandler, block) - } - } - } - privateKeyConfig ?.let { - sslConnector( - privateKeyConfig.keyStore, - privateKeyConfig.aliasName, - privateKeyConfig::keyStorePassword, - privateKeyConfig::aliasPassword - ) { - host = listenHost - this.port = port - } - } ?: connector { - host = listenHost - this.port = port - } - - } - engine = embeddedServer(engineFactory, env) - engine.start(false) - - return engine -} - -/** - * Setting up ktor server, set webhook info via [SetWebhook] request. + * Reverse proxy webhook. * * @param url URL of webhook WITHOUT including of [port] * @param port port which will be listen by bot @@ -132,11 +28,8 @@ fun setWebhook( * @param certificate [com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartFile] or [com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId] * which will be used by telegram to send encrypted messages * @param scope Scope which will be used for - * - * @see com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter - * @see UpdatesFilter - * @see UpdatesFilter.asUpdateReceiver */ +@Deprecated("Replaced into project TelegramBotAPI-extensions-api") suspend fun RequestsExecutor.setWebhook( url: String, port: Int, @@ -167,20 +60,84 @@ suspend fun RequestsExecutor.setWebhook( allowedUpdates ) ) + val updatesChannel = Channel(Channel.UNLIMITED) + val mediaGroupChannel = Channel>(Channel.UNLIMITED) + val mediaGroupAccumulatedChannel = mediaGroupChannel.accumulateByKey( + 1000L, + scope = scope + ) + val env = applicationEngineEnvironment { - - return try { - executeDeferred.await() - val engine = setWebhook(port, engineFactory, listenHost, listenRoute, privateKeyConfig, scope, exceptionsHandler, block) - scope.launch { - engine.environment.parentCoroutineContext[Job] ?.join() - engine.stop(1000, 5000) + module { + routing { + post(listenRoute) { + handleSafely( + { + exceptionsHandler ?.invoke(it) + } + ) { + val asJson = nonstrictJsonFormat.parseJson(call.receiveText()) + val update = nonstrictJsonFormat.fromJson( + UpdateDeserializationStrategy, + asJson + ) + updatesChannel.send(update) + } + call.respond("Ok") + } + } } + privateKeyConfig ?.let { + sslConnector( + privateKeyConfig.keyStore, + privateKeyConfig.aliasName, + privateKeyConfig::keyStorePassword, + privateKeyConfig::aliasPassword + ) { + host = listenHost + this.port = port + } + } ?: connector { + host = listenHost + this.port = port + } + + } + val engine = embeddedServer(engineFactory, env) + + try { + executeDeferred.await() } catch (e: Exception) { + env.stop() throw e } + + return scope.launch { + launch { + for (update in updatesChannel) { + val data = update.data + when (data) { + is MediaGroupMessage -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate) + else -> block(update) + } + } + } + launch { + for ((_, mediaGroup) in mediaGroupAccumulatedChannel) { + mediaGroup.convertWithMediaGroupUpdates().forEach { + block(it) + } + } + } + engine.start(false) + }.also { + it.invokeOnCompletion { + engine.stop(1000L, 0L, TimeUnit.MILLISECONDS) + } + } } +@Deprecated("Replaced into project TelegramBotAPI-extensions-api") suspend fun RequestsExecutor.setWebhook( url: String, port: Int, @@ -208,6 +165,7 @@ suspend fun RequestsExecutor.setWebhook( block ) +@Deprecated("Replaced into project TelegramBotAPI-extensions-api") suspend fun RequestsExecutor.setWebhook( url: String, port: Int, @@ -232,6 +190,7 @@ suspend fun RequestsExecutor.setWebhook( block ) +@Deprecated("Replaced into project TelegramBotAPI-extensions-api") suspend fun RequestsExecutor.setWebhook( url: String, port: Int, From 420b846466e6153b505c59269532cf13f7f18eec Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 13 May 2020 23:22:35 +0600 Subject: [PATCH 06/46] replace long polling and webhook tools --- CHANGELOG.md | 9 +- .../extensions/api/updates/UpdatesPolling.kt | 5 + .../extensions/utils/updates/UpdatesUtils.kt | 93 +++++++++ .../utils/updates/retrieving/LongPolling.kt | 181 ++++++++++++++++++ .../updates/retrieving/MediaGroupsIncluder.kt | 60 ++++++ .../utils/updates/retrieving/Webhook.kt | 14 +- .../utils/extensions/Webhooks.kt | 8 +- 7 files changed, 354 insertions(+), 16 deletions(-) create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdatesUtils.kt create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/LongPolling.kt create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/MediaGroupsIncluder.kt rename TelegramBotAPI-extensions-api/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/SetWebhook.kt => TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt (93%) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7b616d0e3..eb6292cab3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,12 +53,15 @@ * `TelegramBotAPI`: * Currently `UpdateDeserializationStrategy` is publicly available - * All `setWebhook` extensions was marked as deprecated and replaced into `TelegramBotAPI-extensions-api` + * All `setWebhook` extensions was marked as deprecated and replaced into `TelegramBotAPI-extensions-utils` * `TelegramBotAPI-extensions-api`: - * New extensions `setWebhook` and `includeWebhookInRoute` was added - * New extension `CoroutineScope#updateHandlerWithMediaGroupsAdaptation` was added + * Long Polling extensions now are deprecated in this project. It was replaced into `TelegramBotAPI-extensions-utils` * `TelegramBotAPI-extensions-utils`: * Extension `asTelegramUpdate` was added + * Long Polling extensions were added + * Updates utils were added + * New extensions `setWebhook` and `includeWebhookInRoute` was added + * New extension `CoroutineScope#updateHandlerWithMediaGroupsAdaptation` was added ### 0.27.2 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 6a078b810a..317d7e23f0 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 @@ -14,6 +14,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.PreviewFeature import com.github.insanusmokrassar.TelegramBotAPI.utils.handleSafely import kotlinx.coroutines.* +@Deprecated("Replaced and renamed into TelegramBotAPI-extensions-utils") fun RequestsExecutor.startGettingOfUpdates( timeoutSeconds: Seconds = 30, scope: CoroutineScope = CoroutineScope(Dispatchers.Default), @@ -71,6 +72,7 @@ fun RequestsExecutor.startGettingOfUpdates( @FlowPreview @PreviewFeature @Suppress("unused") +@Deprecated("Replaced and renamed into TelegramBotAPI-extensions-utils") fun RequestsExecutor.startGettingFlowsUpdates( timeoutSeconds: Seconds = 30, scope: CoroutineScope = CoroutineScope(Dispatchers.Default), @@ -82,6 +84,7 @@ fun RequestsExecutor.startGettingFlowsUpdates( startGettingOfUpdates(timeoutSeconds, scope, exceptionsHandler, allowedUpdates, asUpdateReceiver) } +@Deprecated("Replaced and renamed into TelegramBotAPI-extensions-utils") fun RequestsExecutor.startGettingOfUpdates( updatesFilter: UpdatesFilter, timeoutSeconds: Seconds = 30, @@ -95,6 +98,7 @@ fun RequestsExecutor.startGettingOfUpdates( updatesFilter.asUpdateReceiver ) +@Deprecated("Replaced and renamed into TelegramBotAPI-extensions-utils") fun RequestsExecutor.startGettingOfUpdates( messageCallback: UpdateReceiver? = null, messageMediaGroupCallback: UpdateReceiver? = null, @@ -140,6 +144,7 @@ fun RequestsExecutor.startGettingOfUpdates( } @Suppress("unused") +@Deprecated("Replaced and renamed into TelegramBotAPI-extensions-utils") fun RequestsExecutor.startGettingOfUpdates( messageCallback: UpdateReceiver? = null, mediaGroupCallback: UpdateReceiver? = null, diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdatesUtils.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdatesUtils.kt new file mode 100644 index 0000000000..6aa7bc6ba9 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdatesUtils.kt @@ -0,0 +1,93 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates + +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.* + +/** + * @return If [this] is [SentMediaGroupUpdate] - [Update.updateId] of [last] element, or its own [Update.updateId] + */ +fun Update.lastUpdateIdentifier(): UpdateIdentifier { + return if (this is SentMediaGroupUpdate) { + origins.last().updateId + } else { + updateId + } +} + +/** + * @return The biggest [UpdateIdentifier] OR null + * + * @see [Update.lastUpdateIdentifier] + */ +fun List.lastUpdateIdentifier(): UpdateIdentifier? { + return maxBy { it.updateId } ?.lastUpdateIdentifier() +} + +/** + * Will convert incoming list of updates to list with [MediaGroupUpdate]s + */ +fun List.convertWithMediaGroupUpdates(): List { + val resultUpdates = mutableListOf() + val mediaGroups = mutableMapOf>() + for (update in this) { + val data = (update.data as? MediaGroupMessage) + if (data == null) { + resultUpdates.add(update) + continue + } + when (update) { + is BaseEditMessageUpdate -> resultUpdates.add( + update.toEditMediaGroupUpdate() + ) + is BaseSentMessageUpdate -> { + mediaGroups.getOrPut(data.mediaGroupId) { + mutableListOf() + }.add(update) + } + else -> resultUpdates.add(update) + } + } + mediaGroups.values.map { + it.toSentMediaGroupUpdate() ?.let { mediaGroupUpdate -> + resultUpdates.add(mediaGroupUpdate) + } + } + resultUpdates.sortBy { it.updateId } + return resultUpdates +} + +/** + * @receiver List of [BaseSentMessageUpdate] where [BaseSentMessageUpdate.data] is [MediaGroupMessage] and all messages + * have the same [MediaGroupMessage.mediaGroupId] + * @return [MessageMediaGroupUpdate] in case if [first] object of [this] is [MessageUpdate]. When [first] object is + * [ChannelPostUpdate] instance - will return [ChannelPostMediaGroupUpdate]. Otherwise will be returned null + */ +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 + } +} + +/** + * @return [EditMessageMediaGroupUpdate] in case if [this] is [EditMessageUpdate]. When [this] object is + * [EditChannelPostUpdate] instance - will return [EditChannelPostMediaGroupUpdate] + * + * @throws IllegalStateException + */ +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-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/LongPolling.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/LongPolling.kt new file mode 100644 index 0000000000..7c17bfbe03 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/LongPolling.kt @@ -0,0 +1,181 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.retrieving + +import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor +import com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions.RequestException +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.convertWithMediaGroupUpdates +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.lastUpdateIdentifier +import com.github.insanusmokrassar.TelegramBotAPI.requests.GetUpdates +import com.github.insanusmokrassar.TelegramBotAPI.types.* +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 com.github.insanusmokrassar.TelegramBotAPI.utils.PreviewFeature +import com.github.insanusmokrassar.TelegramBotAPI.utils.handleSafely +import kotlinx.coroutines.* + +fun RequestsExecutor.startGettingOfUpdatesByLongPolling( + timeoutSeconds: Seconds = 30, + scope: CoroutineScope = CoroutineScope(Dispatchers.Default), + exceptionsHandler: (suspend (Exception) -> Unit)? = null, + allowedUpdates: List? = null, + updatesReceiver: UpdateReceiver +): Job = scope.launch { + var lastUpdateIdentifier: UpdateIdentifier? = null + + while (isActive) { + handleSafely( + { e -> + exceptionsHandler ?.invoke(e) + if (e is RequestException) { + delay(1000L) + } + } + ) { + val updates = execute( + GetUpdates( + offset = lastUpdateIdentifier?.plus(1), + timeout = timeoutSeconds, + allowed_updates = allowedUpdates + ) + ).let { originalUpdates -> + val converted = originalUpdates.convertWithMediaGroupUpdates() + /** + * Dirty hack for cases when the media group was retrieved not fully: + * + * We are throw out the last media group and will reretrieve it again in the next get updates + * and it will guarantee that it is full + */ + if (originalUpdates.size == getUpdatesLimit.last && converted.last() is SentMediaGroupUpdate) { + converted - converted.last() + } else { + converted + } + } + + handleSafely { + for (update in updates) { + updatesReceiver(update) + + lastUpdateIdentifier = update.lastUpdateIdentifier() + } + } + } + } +} + +/** + * This method will create a new one [FlowsUpdatesFilter]. This method could be unsafe due to the fact that it will start + * getting updates IMMEDIATELY. That means that your bot will be able to skip some of them until you will call + * [kotlinx.coroutines.flow.Flow.collect] on one of [FlowsUpdatesFilter] flows. To avoid it, you can pass + * [flowUpdatesPreset] lambda - it will be called BEFORE starting updates getting + */ +@FlowPreview +@PreviewFeature +@Suppress("unused") +fun RequestsExecutor.startGettingFlowsUpdatesByLongPolling( + timeoutSeconds: Seconds = 30, + scope: CoroutineScope = CoroutineScope(Dispatchers.Default), + exceptionsHandler: (suspend (Exception) -> Unit)? = null, + flowsUpdatesFilterUpdatesKeeperCount: Int = 64, + flowUpdatesPreset: FlowsUpdatesFilter.() -> Unit = {} +): FlowsUpdatesFilter = FlowsUpdatesFilter(flowsUpdatesFilterUpdatesKeeperCount).apply { + flowUpdatesPreset() + startGettingOfUpdatesByLongPolling(timeoutSeconds, scope, exceptionsHandler, allowedUpdates, asUpdateReceiver) +} + +fun RequestsExecutor.startGettingOfUpdatesByLongPolling( + updatesFilter: UpdatesFilter, + timeoutSeconds: Seconds = 30, + exceptionsHandler: (suspend (Exception) -> Unit)? = null, + scope: CoroutineScope = CoroutineScope(Dispatchers.Default) +): Job = startGettingOfUpdatesByLongPolling( + timeoutSeconds, + scope, + exceptionsHandler, + updatesFilter.allowedUpdates, + updatesFilter.asUpdateReceiver +) + +fun RequestsExecutor.startGettingOfUpdatesByLongPolling( + 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, + exceptionsHandler: (suspend (Exception) -> Unit)? = null, + scope: CoroutineScope = GlobalScope +): Job { + return startGettingOfUpdatesByLongPolling( + SimpleUpdatesFilter( + messageCallback, + messageMediaGroupCallback, + editedMessageCallback, + editedMessageMediaGroupCallback, + channelPostCallback, + channelPostMediaGroupCallback, + editedChannelPostCallback, + editedChannelPostMediaGroupCallback, + chosenInlineResultCallback, + inlineQueryCallback, + callbackQueryCallback, + shippingQueryCallback, + preCheckoutQueryCallback, + pollCallback, + pollAnswerCallback + ), + timeoutSeconds, + exceptionsHandler, + scope + ) +} + +@Suppress("unused") +fun RequestsExecutor.startGettingOfUpdatesByLongPolling( + 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, + exceptionsHandler: (suspend (Exception) -> Unit)? = null, + scope: CoroutineScope = CoroutineScope(Dispatchers.Default) +): Job = startGettingOfUpdatesByLongPolling( + 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, + exceptionsHandler = exceptionsHandler, + scope = scope +) + diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/MediaGroupsIncluder.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/MediaGroupsIncluder.kt new file mode 100644 index 0000000000..508c7f334e --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/MediaGroupsIncluder.kt @@ -0,0 +1,60 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.retrieving + +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.convertWithMediaGroupUpdates +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage +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.utils.extensions.accumulateByKey +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.launch + + +/** + * Create [UpdateReceiver] object which will correctly accumulate updates and send into output updates which INCLUDE + * [com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.MediaGroupUpdate]s. + * + * @see UpdateReceiver + */ +fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation( + output: UpdateReceiver, + debounceTimeMillis: Long = 1000L +): UpdateReceiver { + val updatesChannel = Channel(Channel.UNLIMITED) + val mediaGroupChannel = Channel>(Channel.UNLIMITED) + val mediaGroupAccumulatedChannel = mediaGroupChannel.accumulateByKey( + debounceTimeMillis, + scope = this + ) + + launch { + launch { + for (update in updatesChannel) { + when (val data = update.data) { + is MediaGroupMessage -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate) + else -> output(update) + } + } + } + launch { + for ((_, mediaGroup) in mediaGroupAccumulatedChannel) { + mediaGroup.convertWithMediaGroupUpdates().forEach { + output(it) + } + } + } + } + + return { updatesChannel.send(it) } +} + +/** + * Create [UpdateReceiver] object which will correctly accumulate updates and send into output updates which INCLUDE + * [com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.MediaGroupUpdate]s. + * + * @see UpdateReceiver + */ +fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation( + output: UpdateReceiver +) = updateHandlerWithMediaGroupsAdaptation(output, 1000L) \ No newline at end of file diff --git a/TelegramBotAPI-extensions-api/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/SetWebhook.kt b/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt similarity index 93% rename from TelegramBotAPI-extensions-api/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/SetWebhook.kt rename to TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt index 62ee809846..ee128f8a9e 100644 --- a/TelegramBotAPI-extensions-api/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/SetWebhook.kt +++ b/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt @@ -1,16 +1,14 @@ -package com.github.insanusmokrassar.TelegramBotAPI.extensions.api +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.retrieving import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor -import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils.convertWithMediaGroupUpdates -import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.utils.updateHandlerWithMediaGroupsAdaptation +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.nonstrictJsonFormat import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.InputFile import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook -import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage -import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UpdateDeserializationStrategy 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.extensions.accumulateByKey import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.executeAsync import com.github.insanusmokrassar.TelegramBotAPI.utils.handleSafely import io.ktor.application.call @@ -19,9 +17,9 @@ import io.ktor.response.respond import io.ktor.routing.* import io.ktor.server.engine.* import kotlinx.coroutines.* -import kotlinx.coroutines.channels.Channel import java.util.concurrent.Executors + /** * @param [scope] Will be used for mapping of media groups * @param [exceptionsHandler] Pass this parameter to set custom exception handler for getting updates @@ -241,5 +239,3 @@ suspend fun RequestsExecutor.setWebhook( maxAllowedConnections, filter.asUpdateReceiver ) - - 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 7afc0f7e69..662a68e5d6 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 @@ -29,7 +29,7 @@ import java.util.concurrent.TimeUnit * which will be used by telegram to send encrypted messages * @param scope Scope which will be used for */ -@Deprecated("Replaced into project TelegramBotAPI-extensions-api") +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") suspend fun RequestsExecutor.setWebhook( url: String, port: Int, @@ -137,7 +137,7 @@ suspend fun RequestsExecutor.setWebhook( } } -@Deprecated("Replaced into project TelegramBotAPI-extensions-api") +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") suspend fun RequestsExecutor.setWebhook( url: String, port: Int, @@ -165,7 +165,7 @@ suspend fun RequestsExecutor.setWebhook( block ) -@Deprecated("Replaced into project TelegramBotAPI-extensions-api") +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") suspend fun RequestsExecutor.setWebhook( url: String, port: Int, @@ -190,7 +190,7 @@ suspend fun RequestsExecutor.setWebhook( block ) -@Deprecated("Replaced into project TelegramBotAPI-extensions-api") +@Deprecated("Replaced into project TelegramBotAPI-extensions-utils") suspend fun RequestsExecutor.setWebhook( url: String, port: Int, From 738e628a89996940292622c6263667343c0809e3 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 13 May 2020 23:25:22 +0600 Subject: [PATCH 07/46] small change of deprecations message --- .../extensions/api/updates/UpdatesPolling.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 317d7e23f0..31f95f992b 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 @@ -14,7 +14,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.PreviewFeature import com.github.insanusmokrassar.TelegramBotAPI.utils.handleSafely import kotlinx.coroutines.* -@Deprecated("Replaced and renamed into TelegramBotAPI-extensions-utils") +@Deprecated("Replaced and renamed in TelegramBotAPI-extensions-utils") fun RequestsExecutor.startGettingOfUpdates( timeoutSeconds: Seconds = 30, scope: CoroutineScope = CoroutineScope(Dispatchers.Default), @@ -72,7 +72,7 @@ fun RequestsExecutor.startGettingOfUpdates( @FlowPreview @PreviewFeature @Suppress("unused") -@Deprecated("Replaced and renamed into TelegramBotAPI-extensions-utils") +@Deprecated("Replaced and renamed in TelegramBotAPI-extensions-utils") fun RequestsExecutor.startGettingFlowsUpdates( timeoutSeconds: Seconds = 30, scope: CoroutineScope = CoroutineScope(Dispatchers.Default), @@ -84,7 +84,7 @@ fun RequestsExecutor.startGettingFlowsUpdates( startGettingOfUpdates(timeoutSeconds, scope, exceptionsHandler, allowedUpdates, asUpdateReceiver) } -@Deprecated("Replaced and renamed into TelegramBotAPI-extensions-utils") +@Deprecated("Replaced and renamed in TelegramBotAPI-extensions-utils") fun RequestsExecutor.startGettingOfUpdates( updatesFilter: UpdatesFilter, timeoutSeconds: Seconds = 30, @@ -98,7 +98,7 @@ fun RequestsExecutor.startGettingOfUpdates( updatesFilter.asUpdateReceiver ) -@Deprecated("Replaced and renamed into TelegramBotAPI-extensions-utils") +@Deprecated("Replaced and renamed in TelegramBotAPI-extensions-utils") fun RequestsExecutor.startGettingOfUpdates( messageCallback: UpdateReceiver? = null, messageMediaGroupCallback: UpdateReceiver? = null, @@ -144,7 +144,7 @@ fun RequestsExecutor.startGettingOfUpdates( } @Suppress("unused") -@Deprecated("Replaced and renamed into TelegramBotAPI-extensions-utils") +@Deprecated("Replaced and renamed in TelegramBotAPI-extensions-utils") fun RequestsExecutor.startGettingOfUpdates( messageCallback: UpdateReceiver? = null, mediaGroupCallback: UpdateReceiver? = null, From 67fafdac001d67ba3adc66e77e7c9aa68f4567cd Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 14 May 2020 12:14:38 +0600 Subject: [PATCH 08/46] add docs for RequestsExecutor --- .../TelegramBotAPI/bot/RequestsExecutor.kt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/RequestsExecutor.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/RequestsExecutor.kt index 285793254b..d511ed63fa 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/RequestsExecutor.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/RequestsExecutor.kt @@ -3,9 +3,19 @@ package com.github.insanusmokrassar.TelegramBotAPI.bot import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request import io.ktor.utils.io.core.Closeable +/** + * Interface for making requests to Telegram Bot API + * + * @see Request + * @see com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorRequestsExecutor + */ interface RequestsExecutor : Closeable { /** - * @throws com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions.RequestException + * Unsafe execution of incoming [request]. Can throw almost any exception. So, it is better to use + * something like [com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.executeAsync] or + * [com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.executeUnsafe] + * + * @throws Exception */ suspend fun execute(request: Request): T } \ No newline at end of file From b5632626ad278106cdcdc46f9360bb54fe8a5dd5 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 14 May 2020 12:21:38 +0600 Subject: [PATCH 09/46] small extention docs for RequestsExecutor --- .../insanusmokrassar/TelegramBotAPI/bot/RequestsExecutor.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/RequestsExecutor.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/RequestsExecutor.kt index d511ed63fa..13c531a527 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/RequestsExecutor.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/bot/RequestsExecutor.kt @@ -4,7 +4,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request import io.ktor.utils.io.core.Closeable /** - * Interface for making requests to Telegram Bot API + * Interface for making requests to Telegram Bot API. Currently, there is only one built-in implementation - + * [com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorRequestsExecutor] * * @see Request * @see com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorRequestsExecutor From b40cc0c1eaa2afdeaa7efaaabe2ccaa4f637634e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 14 May 2020 13:11:46 +0600 Subject: [PATCH 10/46] fixes --- CHANGELOG.md | 2 +- .../extensions/api/updates/UpdatesPolling.kt | 5 ++--- .../utils/updates/UpdateDeserialization.kt | 12 ++++++------ .../utils/updates/retrieving/LongPolling.kt | 5 ++--- .../extensions/utils/updates/retrieving/Webhook.kt | 3 +++ .../TelegramBotAPI/utils/HandleSafely.kt | 4 +++- .../TelegramBotAPI/utils/extensions/Webhooks.kt | 2 +- 7 files changed, 18 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb6292cab3..52760afc6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,7 +57,7 @@ * `TelegramBotAPI-extensions-api`: * Long Polling extensions now are deprecated in this project. It was replaced into `TelegramBotAPI-extensions-utils` * `TelegramBotAPI-extensions-utils`: - * Extension `asTelegramUpdate` was added + * Extension `toTelegramUpdate` was added * Long Polling extensions were added * Updates utils were added * New extensions `setWebhook` and `includeWebhookInRoute` was added 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 31f95f992b..4c05dc0a95 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 @@ -10,15 +10,14 @@ 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 com.github.insanusmokrassar.TelegramBotAPI.utils.PreviewFeature -import com.github.insanusmokrassar.TelegramBotAPI.utils.handleSafely +import com.github.insanusmokrassar.TelegramBotAPI.utils.* import kotlinx.coroutines.* @Deprecated("Replaced and renamed in TelegramBotAPI-extensions-utils") fun RequestsExecutor.startGettingOfUpdates( timeoutSeconds: Seconds = 30, scope: CoroutineScope = CoroutineScope(Dispatchers.Default), - exceptionsHandler: (suspend (Exception) -> Unit)? = null, + exceptionsHandler: (ExceptionHandler)? = null, allowedUpdates: List? = null, updatesReceiver: UpdateReceiver ): Job = scope.launch { diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdateDeserialization.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdateDeserialization.kt index fa650fdd1e..017a61400d 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdateDeserialization.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdateDeserialization.kt @@ -8,24 +8,24 @@ import kotlinx.serialization.json.JsonElement /** * @return Deserialize [source] as [com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update] */ -fun Json.asTelegramUpdate(source: String) = parse(UpdateDeserializationStrategy, source) +fun Json.toTelegramUpdate(source: String) = parse(UpdateDeserializationStrategy, source) /** * @return Deserialize [source] as [com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update] */ -fun Json.asTelegramUpdate(source: JsonElement) = fromJson(UpdateDeserializationStrategy, source) +fun Json.toTelegramUpdate(source: JsonElement) = fromJson(UpdateDeserializationStrategy, source) /** * @return Deserialize [this] as [com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update]. In fact, * it is must be JSON * - * @see Json.asTelegramUpdate + * @see Json.toTelegramUpdate */ -fun String.asTelegramUpdate() = nonstrictJsonFormat.asTelegramUpdate(this) +fun String.toTelegramUpdate() = nonstrictJsonFormat.toTelegramUpdate(this) /** * @return Deserialize [this] as [com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update] * - * @see Json.asTelegramUpdate + * @see Json.toTelegramUpdate */ -fun JsonElement.asTelegramUpdate() = nonstrictJsonFormat.asTelegramUpdate(this) +fun JsonElement.toTelegramUpdate() = nonstrictJsonFormat.toTelegramUpdate(this) diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/LongPolling.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/LongPolling.kt index 7c17bfbe03..cf946d4d60 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/LongPolling.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/LongPolling.kt @@ -10,14 +10,13 @@ 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 com.github.insanusmokrassar.TelegramBotAPI.utils.PreviewFeature -import com.github.insanusmokrassar.TelegramBotAPI.utils.handleSafely +import com.github.insanusmokrassar.TelegramBotAPI.utils.* import kotlinx.coroutines.* fun RequestsExecutor.startGettingOfUpdatesByLongPolling( timeoutSeconds: Seconds = 30, scope: CoroutineScope = CoroutineScope(Dispatchers.Default), - exceptionsHandler: (suspend (Exception) -> Unit)? = null, + exceptionsHandler: (ExceptionHandler)? = null, allowedUpdates: List? = null, updatesReceiver: UpdateReceiver ): Job = scope.launch { diff --git a/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt b/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt index ee128f8a9e..efd02eb960 100644 --- a/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt +++ b/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt @@ -21,6 +21,8 @@ import java.util.concurrent.Executors /** + * Allows to include webhook in custom route everywhere in your server + * * @param [scope] Will be used for mapping of media groups * @param [exceptionsHandler] Pass this parameter to set custom exception handler for getting updates * @param [block] Some receiver block like [com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter] @@ -57,6 +59,7 @@ fun Route.includeWebhookInRoute( * @param port port which will be listen by bot * @param listenRoute address to listen by bot * @param scope Scope which will be used for + * @param privateKeyConfig If configured - server will be created with [sslConnector]. [connector] will be used otherwise * * @see com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter * @see UpdatesFilter diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/HandleSafely.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/HandleSafely.kt index 6e8deeaae8..955cfe7bb7 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/HandleSafely.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/HandleSafely.kt @@ -3,6 +3,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.supervisorScope + +typealias ExceptionHandler = suspend (Exception) -> T /** * It will run [block] inside of [supervisorScope] to avoid problems with catching of exceptions * @@ -10,7 +12,7 @@ import kotlinx.coroutines.supervisorScope * exception will be available for catching */ suspend inline fun handleSafely( - noinline onException: suspend (Exception) -> T = { throw it }, + noinline onException: ExceptionHandler = { throw it }, noinline block: suspend CoroutineScope.() -> T ): T { return try { 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 662a68e5d6..6a35242236 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 @@ -41,7 +41,7 @@ suspend fun RequestsExecutor.setWebhook( scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), allowedUpdates: List? = null, maxAllowedConnections: Int? = null, - exceptionsHandler: (suspend (Exception) -> Unit)? = null, + exceptionsHandler: (ExceptionHandler)? = null, block: UpdateReceiver ): Job { val executeDeferred = certificate ?.let { From 47aa1a07953213202865839ca900647c86a9f609 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 14 May 2020 13:26:56 +0600 Subject: [PATCH 11/46] fixes in CHANGELOG and docs --- CHANGELOG.md | 1 + .../TelegramBotAPI/requests/GetUpdates.kt | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52760afc6a..9629b3172a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,7 @@ * `TelegramBotAPI`: * Currently `UpdateDeserializationStrategy` is publicly available * All `setWebhook` extensions was marked as deprecated and replaced into `TelegramBotAPI-extensions-utils` + * Typealias `ExceptionHandler` was added - it will be used for `handleSafely` * `TelegramBotAPI-extensions-api`: * Long Polling extensions now are deprecated in this project. It was replaced into `TelegramBotAPI-extensions-utils` * `TelegramBotAPI-extensions-utils`: 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 bb682b4063..792664a3e4 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 @@ -11,6 +11,15 @@ private val updatesListSerializer = ListSerializer( UpdateSerializerWithoutSerialization ) +/** + * Request updates from Telegram Bot API system. It is important, that the result updates WILL NOT include + * [com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.MediaGroupUpdate] objects due to the fact, + * that it is internal abstraction and in fact any [com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage] + * is just a common [com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message] + * + * @see com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.retrieving.updateHandlerWithMediaGroupsAdaptation + * @see com.github.insanusmokrassar.TelegramBotAPI.utils.convertWithMediaGroupUpdates + */ @Serializable data class GetUpdates( val offset: UpdateIdentifier? = null,// set `last update id + 1` to receive next part of updates From acc067585ddde26b15433c927d42338da009666e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 14 May 2020 13:44:18 +0600 Subject: [PATCH 12/46] add build instructions in README --- README.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/README.md b/README.md index 0efe495c2e..8aea86a70a 100644 --- a/README.md +++ b/README.md @@ -74,3 +74,49 @@ Anyway, all libraries are very typical inside of them. Examples: * `TelegramBotAPI-extensions-api` typical syntax look like `requestsExecutor.someRequest()` (in most cases it would be better to use `bot` name instead of `requestsExecutor`) * `TelegramBotAPI-extensions-utils` will look like `filter.filterBaseMessageUpdates(chatId).filterExactCommands(Regex("^.*$"))...` + +## Build instruction + +If you want to build this project or to contribute, there are several recommendations: + +### Build + +In case if you want to just build project, run next command: + +```bash +./gradlew clean build +``` + +On windows: + +``` +gradlew.bat clean build +``` + +### Publishing for work with your version locally + +In case, if you want to work in your other projects using your modification (or some state) of this library, +you can use next code: + +```bash +./gradlew clean build publishToMavenLocal +``` + +On windows: + +``` +gradlew.bat clean build publishToMavenLocal +``` + +But you must remember, that in this case your local maven repo must be the first one from +your project retrieving libraries: + +```groovy +repositories { + mavenLocal() // that must be the first one + jcenter() + mavenCentral() +} +``` + +Besides, for your own version you can change variable `library_version` in the file [gradle.properties](./gradle.properties). From 52e25e934d2f0a501f8d0baf2dbe84cb191b4d25 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 14 May 2020 14:07:23 +0600 Subject: [PATCH 13/46] add TelegramBotAPI-all --- CHANGELOG.md | 2 + README.md | 12 +++-- TelegramBotAPI-all/README.md | 7 +++ TelegramBotAPI-all/build.gradle | 52 ++++++++++++++++++ TelegramBotAPI-all/maven.publish.gradle | 57 ++++++++++++++++++++ TelegramBotAPI-all/mpp_publish_template.json | 1 + TelegramBotAPI-all/publish.gradle | 55 +++++++++++++++++++ TelegramBotAPI/build.gradle | 9 ---- settings.gradle | 1 + 9 files changed, 183 insertions(+), 13 deletions(-) create mode 100644 TelegramBotAPI-all/README.md create mode 100644 TelegramBotAPI-all/build.gradle create mode 100644 TelegramBotAPI-all/maven.publish.gradle create mode 100644 TelegramBotAPI-all/mpp_publish_template.json create mode 100644 TelegramBotAPI-all/publish.gradle diff --git a/CHANGELOG.md b/CHANGELOG.md index 9629b3172a..d9534633ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,8 @@ * Updates utils were added * New extensions `setWebhook` and `includeWebhookInRoute` was added * New extension `CoroutineScope#updateHandlerWithMediaGroupsAdaptation` was added +* `TelegramBotAPI-all`: + * Project is created ### 0.27.2 diff --git a/README.md b/README.md index 8aea86a70a..b522762079 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ the list of this complex currently next projects: `RequestsExecutor`), which allows to use the core library in more pleasant way * [TelegramBotAPI Util Extensions](TelegramBotAPI-extensions-utils/README.md) - contains extensions for more comfortable work with commands, updates and other different things +* [TelegramBotAPI All](TelegramBotAPI-all/README.md) - concentration of all previously mentioned libraries Most part of some specific solves or unuseful moments are describing by official [Telegram Bot API](https://core.telegram.org/bots/api). @@ -62,12 +63,15 @@ kotlin { ## Ok, where should I start? -In most cases, the most simple way will be to implement -[TelegramBotAPI Extensions](TelegramBotAPI-extensions-api/README.md) and -[TelegramBotAPI Util Extensions](TelegramBotAPI-extensions-utils/README.md) for the reason that they contains more -simple tools. If you want to dive deeper in the core of library or develop something for it - welcome to +In most cases, the most simple way will be to implement [TelegramBotAPI All](TelegramBotAPI-all/README.md) - it contains +all necessary tools for comfort usage of this library. If you want to exclude some libraries, you can implement just +[TelegramBotAPI API Extensions](TelegramBotAPI-extensions-api/README.md), +[TelegramBotAPI Util Extensions](TelegramBotAPI-extensions-utils/README.md) or even [TelegramBotAPI](TelegramBotAPI/README.md). +If you want to dive deeper in the core of library or develop something for it - welcome to learn more from +[TelegramBotAPI](TelegramBotAPI/README.md) and our [Telegram Chat](https://teleg.one/InMoTelegramBotAPIChat). + Anyway, all libraries are very typical inside of them. Examples: * In `TelegramBotAPI` common request look like `requestsExecutor.execute(SomeRequest())` diff --git a/TelegramBotAPI-all/README.md b/TelegramBotAPI-all/README.md new file mode 100644 index 0000000000..a60c71722e --- /dev/null +++ b/TelegramBotAPI-all/README.md @@ -0,0 +1,7 @@ +# TelegramBotAPI-all + +Concentration of all TelegramBotAPI libraries: + +* [TelegramBotAPI](TelegramBotAPI/README.md) +* [TelegramBotAPI Extensions](TelegramBotAPI-extensions-api/README.md) +* [TelegramBotAPI Util Extensions](TelegramBotAPI-extensions-utils/README.md) diff --git a/TelegramBotAPI-all/build.gradle b/TelegramBotAPI-all/build.gradle new file mode 100644 index 0000000000..fd13bbb88f --- /dev/null +++ b/TelegramBotAPI-all/build.gradle @@ -0,0 +1,52 @@ +buildscript { + repositories { + mavenLocal() + jcenter() + mavenCentral() + } + + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version" + classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:$gradle_bintray_plugin_version" + } +} + +plugins { + id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version" + id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version" +} + +project.version = "$library_version" +project.group = "$library_group" + +apply from: "publish.gradle" + +repositories { + mavenLocal() + jcenter() + mavenCentral() + maven { url "https://kotlin.bintray.com/kotlinx" } +} + +kotlin { + jvm() + js() + + sourceSets { + commonMain { + dependencies { + implementation kotlin('stdlib') + if ((project.hasProperty('RELEASE_MODE') && project.property('RELEASE_MODE') == "true") || System.getenv('RELEASE_MODE') == "true") { + api "${project.group}:TelegramBotAPI:$library_version" + api "${project.group}:TelegramBotAPI-extensions-api:$library_version" + api "${project.group}:TelegramBotAPI-extensions-utils:$library_version" + } else { + implementation project(":TelegramBotAPI") + implementation project(":TelegramBotAPI-extensions-api") + implementation project(":TelegramBotAPI-extensions-utils") + } + } + } + } +} diff --git a/TelegramBotAPI-all/maven.publish.gradle b/TelegramBotAPI-all/maven.publish.gradle new file mode 100644 index 0000000000..41fee78166 --- /dev/null +++ b/TelegramBotAPI-all/maven.publish.gradle @@ -0,0 +1,57 @@ +apply plugin: 'maven-publish' + +task javadocsJar(type: Jar) { + classifier = 'javadoc' +} + +afterEvaluate { + project.publishing.publications.all { + // rename artifacts + groupId "${project.group}" + if (it.name.contains('kotlinMultiplatform')) { + artifactId = "${project.name}" + } else { + artifactId = "${project.name}-$name" + } + } +} + +publishing { + publications.all { + artifact javadocsJar + + pom.withXml { + asNode().children().last() + { + resolveStrategy = Closure.DELEGATE_FIRST + + description "This project just include all subproject of TelegramBotAPI" + name "Telegram Bot API All" + url "https://insanusmokrassar.github.io/TelegramBotAPI" + + scm { + developerConnection "scm:git:[fetch=]https://github.com/insanusmokrassar/TelegramBotAPI.git[push=]https://github.com/insanusmokrassar/TelegramBotAPI.git" + url "https://github.com/insanusmokrassar/TelegramBotAPI.git" + } + + developers { + + developer { + id "InsanusMokrassar" + name "Ovsiannikov Aleksei" + email "ovsyannikov.alexey95@gmail.com" + } + + } + + licenses { + + license { + name "Apache Software License 2.0" + url "https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE" + } + + } + } + } + } +} \ No newline at end of file diff --git a/TelegramBotAPI-all/mpp_publish_template.json b/TelegramBotAPI-all/mpp_publish_template.json new file mode 100644 index 0000000000..cbda0864b1 --- /dev/null +++ b/TelegramBotAPI-all/mpp_publish_template.json @@ -0,0 +1 @@ +{"bintrayConfig":{"repo":"StandardRepository","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/TelegramBotAPI"},"licenses":[{"id":"Apache-2.0","title":"Apache Software License 2.0","url":"https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE"}],"mavenConfig":{"name":"Telegram Bot API All","description":"This project just include all subproject of TelegramBotAPI","url":"https://insanusmokrassar.github.io/TelegramBotAPI","vcsUrl":"https://github.com/insanusmokrassar/TelegramBotAPI.git","developers":[{"id":"InsanusMokrassar","name":"Ovsiannikov Aleksei","eMail":"ovsyannikov.alexey95@gmail.com"}]},"type":"Multiplatform"} \ No newline at end of file diff --git a/TelegramBotAPI-all/publish.gradle b/TelegramBotAPI-all/publish.gradle new file mode 100644 index 0000000000..2fd72b3b1a --- /dev/null +++ b/TelegramBotAPI-all/publish.gradle @@ -0,0 +1,55 @@ +apply plugin: 'com.jfrog.bintray' + +apply from: "maven.publish.gradle" + +bintray { + user = project.hasProperty('BINTRAY_USER') ? project.property('BINTRAY_USER') : System.getenv('BINTRAY_USER') + key = project.hasProperty('BINTRAY_KEY') ? project.property('BINTRAY_KEY') : System.getenv('BINTRAY_KEY') + filesSpec { + from "${buildDir}/publications/" + eachFile { + String directorySubname = it.getFile().parentFile.name + if (it.getName() == "module.json") { + if (directorySubname == "kotlinMultiplatform") { + it.setPath("${project.name}/${project.version}/${project.name}-${project.version}.module") + } else { + it.setPath("${project.name}-${directorySubname}/${project.version}/${project.name}-${directorySubname}-${project.version}.module") + } + } else { + if (directorySubname == "kotlinMultiplatform" && it.getName() == "pom-default.xml") { + it.setPath("${project.name}/${project.version}/${project.name}-${project.version}.pom") + } else { + it.exclude() + } + } + } + into "${project.group}".replace(".", "/") + } + pkg { + repo = "StandardRepository" + name = "${project.name}" + vcsUrl = "https://github.com/InsanusMokrassar/TelegramBotAPI" + licenses = ["Apache-2.0"] + version { + name = "${project.version}" + released = new Date() + vcsTag = "${project.version}" + gpg { + sign = true + passphrase = project.hasProperty('signing.gnupg.passphrase') ? project.property('signing.gnupg.passphrase') : System.getenv('signing.gnupg.passphrase') + } + } + } +} + +bintrayUpload.doFirst { + publications = publishing.publications.collect { + if (it.name.contains('kotlinMultiplatform')) { + null + } else { + it.name + } + } - null +} + +bintrayUpload.dependsOn publishToMavenLocal \ No newline at end of file diff --git a/TelegramBotAPI/build.gradle b/TelegramBotAPI/build.gradle index d42e420763..c475e31eed 100644 --- a/TelegramBotAPI/build.gradle +++ b/TelegramBotAPI/build.gradle @@ -81,13 +81,4 @@ kotlin { } } } - - - targets.all { - compilations.all { - kotlinOptions { - freeCompilerArgs += ["-Xuse-experimental=kotlinx.coroutines.ExperimentalCoroutinesApi", "-Xopt-in=kotlin.RequiresOptIn"] - } - } - } } diff --git a/settings.gradle b/settings.gradle index 80b7407c7b..b75fc6176b 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,5 @@ include ":TelegramBotAPI" include ":TelegramBotAPI-extensions-api" include ":TelegramBotAPI-extensions-utils" +include ":TelegramBotAPI-all" include ":docs" From dea43aad8ea628a4fb8def9d256daaf95a0e7703 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 14 May 2020 14:11:33 +0600 Subject: [PATCH 14/46] update subprojects implementation types --- TelegramBotAPI-all/build.gradle | 6 +++--- TelegramBotAPI-extensions-api/build.gradle | 2 +- TelegramBotAPI-extensions-utils/build.gradle | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/TelegramBotAPI-all/build.gradle b/TelegramBotAPI-all/build.gradle index fd13bbb88f..939d13307f 100644 --- a/TelegramBotAPI-all/build.gradle +++ b/TelegramBotAPI-all/build.gradle @@ -42,9 +42,9 @@ kotlin { api "${project.group}:TelegramBotAPI-extensions-api:$library_version" api "${project.group}:TelegramBotAPI-extensions-utils:$library_version" } else { - implementation project(":TelegramBotAPI") - implementation project(":TelegramBotAPI-extensions-api") - implementation project(":TelegramBotAPI-extensions-utils") + api project(":TelegramBotAPI") + api project(":TelegramBotAPI-extensions-api") + api project(":TelegramBotAPI-extensions-utils") } } } diff --git a/TelegramBotAPI-extensions-api/build.gradle b/TelegramBotAPI-extensions-api/build.gradle index 0cbe7c0136..217310d289 100644 --- a/TelegramBotAPI-extensions-api/build.gradle +++ b/TelegramBotAPI-extensions-api/build.gradle @@ -40,7 +40,7 @@ kotlin { if ((project.hasProperty('RELEASE_MODE') && project.property('RELEASE_MODE') == "true") || System.getenv('RELEASE_MODE') == "true") { api "${project.group}:TelegramBotAPI:$library_version" } else { - implementation project(":TelegramBotAPI") + api project(":TelegramBotAPI") } } } diff --git a/TelegramBotAPI-extensions-utils/build.gradle b/TelegramBotAPI-extensions-utils/build.gradle index 0cbe7c0136..217310d289 100644 --- a/TelegramBotAPI-extensions-utils/build.gradle +++ b/TelegramBotAPI-extensions-utils/build.gradle @@ -40,7 +40,7 @@ kotlin { if ((project.hasProperty('RELEASE_MODE') && project.property('RELEASE_MODE') == "true") || System.getenv('RELEASE_MODE') == "true") { api "${project.group}:TelegramBotAPI:$library_version" } else { - implementation project(":TelegramBotAPI") + api project(":TelegramBotAPI") } } } From e675e841dac0aaf16809120fa80b2fa7d4c8e69b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 14 May 2020 14:23:26 +0600 Subject: [PATCH 15/46] update urls --- TelegramBotAPI-all/maven.publish.gradle | 2 +- TelegramBotAPI-all/mpp_publish_template.json | 2 +- TelegramBotAPI-extensions-api/maven.publish.gradle | 2 +- TelegramBotAPI-extensions-api/mpp_publish_template.json | 2 +- TelegramBotAPI-extensions-utils/maven.publish.gradle | 2 +- TelegramBotAPI-extensions-utils/mpp_publish_template.json | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/TelegramBotAPI-all/maven.publish.gradle b/TelegramBotAPI-all/maven.publish.gradle index 41fee78166..b81cdc6f73 100644 --- a/TelegramBotAPI-all/maven.publish.gradle +++ b/TelegramBotAPI-all/maven.publish.gradle @@ -26,7 +26,7 @@ publishing { description "This project just include all subproject of TelegramBotAPI" name "Telegram Bot API All" - url "https://insanusmokrassar.github.io/TelegramBotAPI" + url "https://insanusmokrassar.github.io/TelegramBotAPI/TelegramBotAPI-all" scm { developerConnection "scm:git:[fetch=]https://github.com/insanusmokrassar/TelegramBotAPI.git[push=]https://github.com/insanusmokrassar/TelegramBotAPI.git" diff --git a/TelegramBotAPI-all/mpp_publish_template.json b/TelegramBotAPI-all/mpp_publish_template.json index cbda0864b1..363a274ae2 100644 --- a/TelegramBotAPI-all/mpp_publish_template.json +++ b/TelegramBotAPI-all/mpp_publish_template.json @@ -1 +1 @@ -{"bintrayConfig":{"repo":"StandardRepository","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/TelegramBotAPI"},"licenses":[{"id":"Apache-2.0","title":"Apache Software License 2.0","url":"https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE"}],"mavenConfig":{"name":"Telegram Bot API All","description":"This project just include all subproject of TelegramBotAPI","url":"https://insanusmokrassar.github.io/TelegramBotAPI","vcsUrl":"https://github.com/insanusmokrassar/TelegramBotAPI.git","developers":[{"id":"InsanusMokrassar","name":"Ovsiannikov Aleksei","eMail":"ovsyannikov.alexey95@gmail.com"}]},"type":"Multiplatform"} \ No newline at end of file +{"bintrayConfig":{"repo":"StandardRepository","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/TelegramBotAPI"},"licenses":[{"id":"Apache-2.0","title":"Apache Software License 2.0","url":"https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE"}],"mavenConfig":{"name":"Telegram Bot API All","description":"This project just include all subproject of TelegramBotAPI","url":"https://insanusmokrassar.github.io/TelegramBotAPI/TelegramBotAPI-all","vcsUrl":"https://github.com/insanusmokrassar/TelegramBotAPI.git","developers":[{"id":"InsanusMokrassar","name":"Ovsiannikov Aleksei","eMail":"ovsyannikov.alexey95@gmail.com"}]},"type":"Multiplatform"} \ No newline at end of file diff --git a/TelegramBotAPI-extensions-api/maven.publish.gradle b/TelegramBotAPI-extensions-api/maven.publish.gradle index 8fc16d04e8..ec352c0e8c 100644 --- a/TelegramBotAPI-extensions-api/maven.publish.gradle +++ b/TelegramBotAPI-extensions-api/maven.publish.gradle @@ -26,7 +26,7 @@ publishing { description "API extensions which provide work with RequestsExecutor of TelegramBotAPI almost like it is described in original Telegram Bot API reference" name "Telegram Bot API Extensions for API" - url "https://insanusmokrassar.github.io/TelegramBotAPI" + url "https://insanusmokrassar.github.io/TelegramBotAPI/TelegramBotAPI-extensions-api" scm { developerConnection "scm:git:[fetch=]https://github.com/insanusmokrassar/TelegramBotAPI.git[push=]https://github.com/insanusmokrassar/TelegramBotAPI.git" diff --git a/TelegramBotAPI-extensions-api/mpp_publish_template.json b/TelegramBotAPI-extensions-api/mpp_publish_template.json index 7558d0e4c0..3ae276449a 100644 --- a/TelegramBotAPI-extensions-api/mpp_publish_template.json +++ b/TelegramBotAPI-extensions-api/mpp_publish_template.json @@ -1 +1 @@ -{"bintrayConfig":{"repo":"StandardRepository","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/TelegramBotAPI"},"licenses":[{"id":"Apache-2.0","title":"Apache Software License 2.0","url":"https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE"}],"mavenConfig":{"name":"Telegram Bot API Extensions for API","description":"API extensions which provide work with RequestsExecutor of TelegramBotAPI almost like it is described in original Telegram Bot API reference","url":"https://insanusmokrassar.github.io/TelegramBotAPI","vcsUrl":"https://github.com/insanusmokrassar/TelegramBotAPI.git","developers":[{"id":"InsanusMokrassar","name":"Ovsiannikov Aleksei","eMail":"ovsyannikov.alexey95@gmail.com"}]},"type":"Multiplatform"} \ No newline at end of file +{"bintrayConfig":{"repo":"StandardRepository","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/TelegramBotAPI"},"licenses":[{"id":"Apache-2.0","title":"Apache Software License 2.0","url":"https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE"}],"mavenConfig":{"name":"Telegram Bot API Extensions for API","description":"API extensions which provide work with RequestsExecutor of TelegramBotAPI almost like it is described in original Telegram Bot API reference","url":"https://insanusmokrassar.github.io/TelegramBotAPI/TelegramBotAPI-extensions-api","vcsUrl":"https://github.com/insanusmokrassar/TelegramBotAPI.git","developers":[{"id":"InsanusMokrassar","name":"Ovsiannikov Aleksei","eMail":"ovsyannikov.alexey95@gmail.com"}]},"type":"Multiplatform"} \ No newline at end of file diff --git a/TelegramBotAPI-extensions-utils/maven.publish.gradle b/TelegramBotAPI-extensions-utils/maven.publish.gradle index 1f2fd3ed2a..61f4b51a17 100644 --- a/TelegramBotAPI-extensions-utils/maven.publish.gradle +++ b/TelegramBotAPI-extensions-utils/maven.publish.gradle @@ -26,7 +26,7 @@ publishing { description "Util extensions for more useful work with updates and other things" name "Telegram Bot API Utility Extensions" - url "https://insanusmokrassar.github.io/TelegramBotAPI" + url "https://insanusmokrassar.github.io/TelegramBotAPI/TelegramBotAPI-extensions-utils" scm { developerConnection "scm:git:[fetch=]https://github.com/insanusmokrassar/TelegramBotAPI.git[push=]https://github.com/insanusmokrassar/TelegramBotAPI.git" diff --git a/TelegramBotAPI-extensions-utils/mpp_publish_template.json b/TelegramBotAPI-extensions-utils/mpp_publish_template.json index d5c147454d..c51f9e348d 100644 --- a/TelegramBotAPI-extensions-utils/mpp_publish_template.json +++ b/TelegramBotAPI-extensions-utils/mpp_publish_template.json @@ -1 +1 @@ -{"bintrayConfig":{"repo":"StandardRepository","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/TelegramBotAPI"},"licenses":[{"id":"Apache-2.0","title":"Apache Software License 2.0","url":"https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE"}],"mavenConfig":{"name":"Telegram Bot API Utility Extensions","description":"Util extensions for more useful work with updates and other things","url":"https://insanusmokrassar.github.io/TelegramBotAPI","vcsUrl":"https://github.com/insanusmokrassar/TelegramBotAPI.git","developers":[{"id":"InsanusMokrassar","name":"Ovsiannikov Aleksei","eMail":"ovsyannikov.alexey95@gmail.com"}]},"type":"Multiplatform"} \ No newline at end of file +{"bintrayConfig":{"repo":"StandardRepository","packageName":"${project.name}","packageVcs":"https://github.com/InsanusMokrassar/TelegramBotAPI"},"licenses":[{"id":"Apache-2.0","title":"Apache Software License 2.0","url":"https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/LICENSE"}],"mavenConfig":{"name":"Telegram Bot API Utility Extensions","description":"Util extensions for more useful work with updates and other things","url":"https://insanusmokrassar.github.io/TelegramBotAPI/TelegramBotAPI-extensions-utils","vcsUrl":"https://github.com/insanusmokrassar/TelegramBotAPI.git","developers":[{"id":"InsanusMokrassar","name":"Ovsiannikov Aleksei","eMail":"ovsyannikov.alexey95@gmail.com"}]},"type":"Multiplatform"} \ No newline at end of file From dfb22b0e8953ddc0255cc19b6417bb5d54dc5da4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 14 May 2020 14:26:42 +0600 Subject: [PATCH 16/46] update readme of all includer --- TelegramBotAPI-all/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/TelegramBotAPI-all/README.md b/TelegramBotAPI-all/README.md index a60c71722e..8ed70ed448 100644 --- a/TelegramBotAPI-all/README.md +++ b/TelegramBotAPI-all/README.md @@ -5,3 +5,12 @@ Concentration of all TelegramBotAPI libraries: * [TelegramBotAPI](TelegramBotAPI/README.md) * [TelegramBotAPI Extensions](TelegramBotAPI-extensions-api/README.md) * [TelegramBotAPI Util Extensions](TelegramBotAPI-extensions-utils/README.md) + +## Implementation + +```groovy +dependencies { + // ... + implementation "com.github.insanusmokrassar:TelegramBotAPI-all:tgBotAPIVersion" +} +``` From 7f51544bb9d3609949e2100dfe548467b87a36f3 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 14 May 2020 16:25:39 +0600 Subject: [PATCH 17/46] Fix links in TelegramBotAPI-all readme --- TelegramBotAPI-all/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/TelegramBotAPI-all/README.md b/TelegramBotAPI-all/README.md index 8ed70ed448..4fc918fb4e 100644 --- a/TelegramBotAPI-all/README.md +++ b/TelegramBotAPI-all/README.md @@ -2,9 +2,9 @@ Concentration of all TelegramBotAPI libraries: -* [TelegramBotAPI](TelegramBotAPI/README.md) -* [TelegramBotAPI Extensions](TelegramBotAPI-extensions-api/README.md) -* [TelegramBotAPI Util Extensions](TelegramBotAPI-extensions-utils/README.md) +* [TelegramBotAPI](../TelegramBotAPI/README.md) +* [TelegramBotAPI Extensions](../TelegramBotAPI-extensions-api/README.md) +* [TelegramBotAPI Util Extensions](../TelegramBotAPI-extensions-utils/README.md) ## Implementation From ea224fd7656924b3ddfbbb6ca2fdf2b04f8212dd Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 14 May 2020 21:18:33 +0600 Subject: [PATCH 18/46] add javax.activation dependency --- CHANGELOG.md | 2 +- TelegramBotAPI/build.gradle | 2 ++ gradle.properties | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9534633ac..f11d6eb60b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,7 +52,7 @@ ### 0.27.3 * `TelegramBotAPI`: - * Currently `UpdateDeserializationStrategy` is publicly available + * `UpdateDeserializationStrategy` is publicly available now * All `setWebhook` extensions was marked as deprecated and replaced into `TelegramBotAPI-extensions-utils` * Typealias `ExceptionHandler` was added - it will be used for `handleSafely` * `TelegramBotAPI-extensions-api`: diff --git a/TelegramBotAPI/build.gradle b/TelegramBotAPI/build.gradle index c475e31eed..583e4cf72d 100644 --- a/TelegramBotAPI/build.gradle +++ b/TelegramBotAPI/build.gradle @@ -64,6 +64,8 @@ kotlin { api "io.ktor:ktor-server-host-common:$ktor_version" api "io.ktor:ktor-client-cio:$ktor_version" + + api "javax.activation:activation:$javax_activation_version" } } jvmTest { diff --git a/gradle.properties b/gradle.properties index fa3887df82..6a07572a4d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,6 +6,8 @@ klock_version=1.11.1 uuid_version=0.1.0 ktor_version=1.3.2 +javax_activation_version=1.1.1 + library_group=com.github.insanusmokrassar library_version=0.27.3 From a1788e35b2b772f6520a16ead8e0b4ccf9a523c6 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 00:28:16 +0600 Subject: [PATCH 19/46] clean up in webhooks --- CHANGELOG.md | 6 +- .../utils/updates/retrieving/LongPolling.kt | 8 +- .../utils/updates/retrieving/Webhook.kt | 138 +++--------------- 3 files changed, 25 insertions(+), 127 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f11d6eb60b..262cbf573b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,7 +53,7 @@ * `TelegramBotAPI`: * `UpdateDeserializationStrategy` is publicly available now - * All `setWebhook` extensions was marked as deprecated and replaced into `TelegramBotAPI-extensions-utils` + * All `setWebhook` extensions was marked as deprecated, renamed and replaced into `TelegramBotAPI-extensions-utils` * Typealias `ExceptionHandler` was added - it will be used for `handleSafely` * `TelegramBotAPI-extensions-api`: * Long Polling extensions now are deprecated in this project. It was replaced into `TelegramBotAPI-extensions-utils` @@ -61,10 +61,10 @@ * Extension `toTelegramUpdate` was added * Long Polling extensions were added * Updates utils were added - * New extensions `setWebhook` and `includeWebhookInRoute` was added + * New extensions `startListenWebhooks`, `setWebhookInfoAndStartListenWebhooks` and `includeWebhookHandlingInRoute` was added * New extension `CoroutineScope#updateHandlerWithMediaGroupsAdaptation` was added * `TelegramBotAPI-all`: - * Project is created + * Project was created ### 0.27.2 diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/LongPolling.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/LongPolling.kt index cf946d4d60..10d9003d61 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/LongPolling.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/LongPolling.kt @@ -75,7 +75,7 @@ fun RequestsExecutor.startGettingOfUpdatesByLongPolling( fun RequestsExecutor.startGettingFlowsUpdatesByLongPolling( timeoutSeconds: Seconds = 30, scope: CoroutineScope = CoroutineScope(Dispatchers.Default), - exceptionsHandler: (suspend (Exception) -> Unit)? = null, + exceptionsHandler: ExceptionHandler? = null, flowsUpdatesFilterUpdatesKeeperCount: Int = 64, flowUpdatesPreset: FlowsUpdatesFilter.() -> Unit = {} ): FlowsUpdatesFilter = FlowsUpdatesFilter(flowsUpdatesFilterUpdatesKeeperCount).apply { @@ -86,7 +86,7 @@ fun RequestsExecutor.startGettingFlowsUpdatesByLongPolling( fun RequestsExecutor.startGettingOfUpdatesByLongPolling( updatesFilter: UpdatesFilter, timeoutSeconds: Seconds = 30, - exceptionsHandler: (suspend (Exception) -> Unit)? = null, + exceptionsHandler: ExceptionHandler? = null, scope: CoroutineScope = CoroutineScope(Dispatchers.Default) ): Job = startGettingOfUpdatesByLongPolling( timeoutSeconds, @@ -113,7 +113,7 @@ fun RequestsExecutor.startGettingOfUpdatesByLongPolling( pollCallback: UpdateReceiver? = null, pollAnswerCallback: UpdateReceiver? = null, timeoutSeconds: Seconds = 30, - exceptionsHandler: (suspend (Exception) -> Unit)? = null, + exceptionsHandler: ExceptionHandler? = null, scope: CoroutineScope = GlobalScope ): Job { return startGettingOfUpdatesByLongPolling( @@ -155,7 +155,7 @@ fun RequestsExecutor.startGettingOfUpdatesByLongPolling( pollCallback: UpdateReceiver? = null, pollAnswerCallback: UpdateReceiver? = null, timeoutSeconds: Seconds = 30, - exceptionsHandler: (suspend (Exception) -> Unit)? = null, + exceptionsHandler: ExceptionHandler? = null, scope: CoroutineScope = CoroutineScope(Dispatchers.Default) ): Job = startGettingOfUpdatesByLongPolling( messageCallback = messageCallback, diff --git a/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt b/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt index efd02eb960..e202c66ac7 100644 --- a/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt +++ b/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt @@ -2,14 +2,13 @@ package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.retr import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.nonstrictJsonFormat -import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.InputFile import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UpdateDeserializationStrategy 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.extensions.executeAsync +import com.github.insanusmokrassar.TelegramBotAPI.utils.ExceptionHandler import com.github.insanusmokrassar.TelegramBotAPI.utils.handleSafely import io.ktor.application.call import io.ktor.request.receiveText @@ -31,9 +30,9 @@ import java.util.concurrent.Executors * @see UpdatesFilter * @see UpdatesFilter.asUpdateReceiver */ -fun Route.includeWebhookInRoute( +fun Route.includeWebhookHandlingInRoute( scope: CoroutineScope, - exceptionsHandler: (suspend (Exception) -> Unit)? = null, + exceptionsHandler: ExceptionHandler? = null, block: UpdateReceiver ) { val transformer = scope.updateHandlerWithMediaGroupsAdaptation(block) @@ -56,7 +55,7 @@ fun Route.includeWebhookInRoute( /** * Setting up ktor server, set webhook info via [SetWebhook] request. * - * @param port port which will be listen by bot + * @param listenPort port which will be listen by bot * @param listenRoute address to listen by bot * @param scope Scope which will be used for * @param privateKeyConfig If configured - server will be created with [sslConnector]. [connector] will be used otherwise @@ -65,14 +64,14 @@ fun Route.includeWebhookInRoute( * @see UpdatesFilter * @see UpdatesFilter.asUpdateReceiver */ -fun setWebhook( - port: Int, +fun startListenWebhooks( + listenPort: Int, engineFactory: ApplicationEngineFactory<*, *>, + exceptionsHandler: ExceptionHandler, listenHost: String = "0.0.0.0", listenRoute: String = "/", privateKeyConfig: WebhookPrivateKeyConfig? = null, scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), - exceptionsHandler: (suspend (Exception) -> Unit)? = null, block: UpdateReceiver ): ApplicationEngine { lateinit var engine: ApplicationEngine @@ -81,7 +80,7 @@ fun setWebhook( module { routing { route(listenRoute) { - includeWebhookInRoute(scope, exceptionsHandler, block) + includeWebhookHandlingInRoute(scope, exceptionsHandler, block) } } } @@ -93,11 +92,11 @@ fun setWebhook( privateKeyConfig::aliasPassword ) { host = listenHost - this.port = port + this.port = listenPort } } ?: connector { host = listenHost - this.port = port + this.port = listenPort } } @@ -110,52 +109,29 @@ fun setWebhook( /** * Setting up ktor server, set webhook info via [SetWebhook] request. * - * @param url URL of webhook WITHOUT including of [port] - * @param port port which will be listen by bot + * @param listenPort port which will be listen by bot * @param listenRoute address to listen by bot - * @param certificate [com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartFile] or [com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId] - * which will be used by telegram to send encrypted messages * @param scope Scope which will be used for * * @see com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter * @see UpdatesFilter * @see UpdatesFilter.asUpdateReceiver */ -suspend fun RequestsExecutor.setWebhook( - url: String, - port: Int, +@Suppress("unused") +suspend fun RequestsExecutor.setWebhookInfoAndStartListenWebhooks( + listenPort: Int, engineFactory: ApplicationEngineFactory<*, *>, + setWebhookRequest: SetWebhook, + exceptionsHandler: ExceptionHandler = {}, listenHost: String = "0.0.0.0", listenRoute: String = "/", - certificate: InputFile? = null, privateKeyConfig: WebhookPrivateKeyConfig? = null, scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), - allowedUpdates: List? = null, - maxAllowedConnections: Int? = null, - exceptionsHandler: (suspend (Exception) -> Unit)? = null, block: UpdateReceiver ): Job { - val executeDeferred = certificate ?.let { - executeAsync( - SetWebhook( - url, - certificate, - maxAllowedConnections, - allowedUpdates - ) - ) - } ?: executeAsync( - SetWebhook( - url, - maxAllowedConnections, - allowedUpdates - ) - ) - - return try { - executeDeferred.await() - val engine = setWebhook(port, engineFactory, listenHost, listenRoute, privateKeyConfig, scope, exceptionsHandler, block) + execute(setWebhookRequest) + val engine = startListenWebhooks(listenPort, engineFactory, exceptionsHandler, listenHost, listenRoute, privateKeyConfig, scope, block) scope.launch { engine.environment.parentCoroutineContext[Job] ?.join() engine.stop(1000, 5000) @@ -164,81 +140,3 @@ suspend fun RequestsExecutor.setWebhook( throw e } } - -suspend fun RequestsExecutor.setWebhook( - url: String, - port: Int, - engineFactory: ApplicationEngineFactory<*, *>, - listenHost: String = "0.0.0.0", - listenRoute: String = "/", - certificate: InputFile? = null, - privateKeyConfig: WebhookPrivateKeyConfig? = null, - scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), - allowedUpdates: List? = null, - maxAllowedConnections: Int? = null, - block: UpdateReceiver -) = setWebhook( - url, - port, - engineFactory, - listenHost, - listenRoute, - certificate, - privateKeyConfig, - scope, - allowedUpdates, - maxAllowedConnections, - null, - block -) - -@Suppress("unused") -suspend fun RequestsExecutor.setWebhook( - url: String, - port: Int, - engineFactory: ApplicationEngineFactory<*, *>, - certificate: InputFile? = null, - privateKeyConfig: WebhookPrivateKeyConfig? = null, - scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), - allowedUpdates: List? = null, - maxAllowedConnections: Int? = null, - block: UpdateReceiver -) = setWebhook( - url, - port, - engineFactory, - certificate ?.let { "0.0.0.0" } ?: "localhost", - "/", - certificate, - privateKeyConfig, - scope, - allowedUpdates, - maxAllowedConnections, - block -) - -@Suppress("unused") -suspend fun RequestsExecutor.setWebhook( - url: String, - port: Int, - filter: UpdatesFilter, - engineFactory: ApplicationEngineFactory<*, *>, - certificate: InputFile? = null, - privateKeyConfig: WebhookPrivateKeyConfig? = null, - scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), - maxAllowedConnections: Int? = null, - listenHost: String = certificate ?.let { "0.0.0.0" } ?: "localhost", - listenRoute: String = "/" -): Job = setWebhook( - url, - port, - engineFactory, - listenHost, - listenRoute, - certificate, - privateKeyConfig, - scope, - filter.allowedUpdates, - maxAllowedConnections, - filter.asUpdateReceiver -) From dc173d752c1640c8d547b456ee00390500ea43aa Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 00:29:17 +0600 Subject: [PATCH 20/46] optimize imports --- .../TelegramBotAPI/extensions/api/BotBuilder.kt | 3 ++- .../TelegramBotAPI/extensions/api/utils/UpdatesHandling.kt | 3 ++- .../TelegramBotAPI/utils/extensions/Webhooks.kt | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotBuilder.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotBuilder.kt index 414f2a1d8d..083db9a41e 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotBuilder.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotBuilder.kt @@ -4,7 +4,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper import io.ktor.client.HttpClient import io.ktor.client.HttpClientConfig -import io.ktor.client.engine.* +import io.ktor.client.engine.HttpClientEngine +import io.ktor.client.engine.ProxyConfig /** * @param proxy Standard ktor [ProxyConfig] diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/utils/UpdatesHandling.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/utils/UpdatesHandling.kt index 37b3bf1ec0..376c88c02a 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/utils/UpdatesHandling.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/utils/UpdatesHandling.kt @@ -2,7 +2,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.utils import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils.convertWithMediaGroupUpdates import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage -import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.* +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.utils.extensions.accumulateByKey import kotlinx.coroutines.CoroutineScope 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 6a35242236..9f3865c94d 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 @@ -12,7 +12,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.utils.* import io.ktor.application.call import io.ktor.request.receiveText import io.ktor.response.respond -import io.ktor.routing.* +import io.ktor.routing.post +import io.ktor.routing.routing import io.ktor.server.engine.* import kotlinx.coroutines.* import kotlinx.coroutines.channels.Channel From 095c91bf39729889e9b27269b7470b8b6b7197db Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 00:48:34 +0600 Subject: [PATCH 21/46] update readmes with updates handling --- TelegramBotAPI-extensions-api/README.md | 4 ++ TelegramBotAPI-extensions-utils/README.md | 69 ++++++++++++++++++++++- TelegramBotAPI/README.md | 50 ---------------- 3 files changed, 72 insertions(+), 51 deletions(-) diff --git a/TelegramBotAPI-extensions-api/README.md b/TelegramBotAPI-extensions-api/README.md index eeac67ba38..d49094f4d5 100644 --- a/TelegramBotAPI-extensions-api/README.md +++ b/TelegramBotAPI-extensions-api/README.md @@ -78,6 +78,10 @@ In all examples supposed that you have created bot. ## Updates +**Currently, these paragraphs almost outdated due to the fact that extensions for listening of updates and webhooks were +replaced into `TelegramBotAPI-extensions-utils`. But, most part of information below is correct with small fixes and +adding of `TelegramBotAPI-extensions-utils` dependency.** + Usually, it is more comfortable to use filter object to get separated types of updates: ```kotlin diff --git a/TelegramBotAPI-extensions-utils/README.md b/TelegramBotAPI-extensions-utils/README.md index b4b1e5bb66..cfcaf41484 100644 --- a/TelegramBotAPI-extensions-utils/README.md +++ b/TelegramBotAPI-extensions-utils/README.md @@ -60,13 +60,80 @@ val filter = FlowsUpdatesFilter(64) Alternative way to use the things below: ```kotlin -val filter = bot.startGettingUpdates( +val filter = bot.startGettingFlowsUpdatesByLongPolling( scope = CoroutineScope(Dispatchers.Default) ) { // place code from examples here with replacing of `filter` by `this` } ``` +### Updates + +As mentioned in [Telegram Bot API reference](https://core.telegram.org/bots/api#getting-updates), there are two ways for +updates retrieving: + +* Webhooks +* Long Polling + +Both of them you could use in your project using [TelegramBotAPI](../TelegramBotAPI/README.md), but here there are +several useful extensions for both of them: + +#### Long polling + +The most simple way is Long Polling. The most simple way was mentioned above: + +```kotlin +val filter = bot.startGettingFlowsUpdatesByLongPolling( + scope = CoroutineScope(Dispatchers.Default) +) { + // place code from examples here with replacing of `filter` by `this` +} +``` + +Extension `startGettingFlowsUpdatesByLongPolling` was used in this example, but there are a lot of variations of +`startGettingOfUpdatesByLongPolling` and others for getting the same result. Usually, it is supposed that you already +have created `filter` object (or something like this) and will pass it into extension: + +```kotlin +val filter = FlowsUpdatesFilter(64) +bot.startGettingOfUpdatesByLongPolling( + filter +) +``` + +But also there are extensions which allow to pass lambdas directly: + +```kotlin +bot.startGettingOfUpdatesByLongPolling( + { + println("Received message update: $it") + } +) +``` + +Anyway, it is strictly recommended to pass your `CoroutineScope` object to this method at least for more comfortable +management of updates. + +#### WebHooks (currently JVM-only) + +For webhooks there are less number of functions and extensions than for Long Polling (but it is still fully automated): + +```kotlin +startListenWebhooks( + 8081, + CIO // require to implement this engine dependency +) { + // here will be all updates one by one in $it +} +``` + +Besides, there are two additional opportunities: + +* Extension `Route#includeWebhookHandlingInRoute`, which allow you to include webhook processing inside your ktor +application without creating of new one server (as it is happening in `startListenWebhooks`) +* Extension `RequestsExecutor#setWebhookInfoAndStartListenWebhooks`. It is allow to set up full server (in fact, with +`startListenWebhooks`), but also send `SetWebhook` request before and check that it was successful + ### Filters There are several filters for flows. diff --git a/TelegramBotAPI/README.md b/TelegramBotAPI/README.md index 87db0f0558..67536e6141 100644 --- a/TelegramBotAPI/README.md +++ b/TelegramBotAPI/README.md @@ -149,53 +149,3 @@ Here was used `okhttp` realisation of client, but there are several others engin available on ktor.io site for [client](https://ktor.io/clients/http-client/engines.html) and [server](https://ktor.io/quickstart/artifacts.html) engines. -## Getting updates - -In this library currently realised two ways to get updates from telegram: - -* Polling - in this case bot will request updates from time to time (you can set up delay between requests) -* Webhook via reverse proxy or something like this - -### Updates filters - -Currently webhook method contains `UpdatesFilter` as necessary argument for getting updates. -`UpdatesFilter` will sort updates and throw their into different callbacks. Currently supporting -separate getting updates for media groups - they are accumulating with debounce in one second -(for being sure that all objects of media group was received). - -Updates polling also support `UpdatesFilter` but it is not required to use it and you can get updates directly -in `UpdateReceiver`, which you will provide to `startGettingOfUpdates` method - -### Webhook set up - -If you wish to use webhook method, you will need: - -* White IP - your IP address or host, which available for calling. [TelegramBotAPI](https://core.telegram.org/bots/api#setwebhook) -recommend to use some unique address for each bot which you are using -* SSL certificate. Usually you can obtain the certificate using your domain provider, [Let'sEncrypt](https://letsencrypt.org/) or [create it](https://core.telegram.org/bots/self-signed) -* Nginx or something like this - -Template for Nginx server config you can find in [this gist](https://gist.github.com/InsanusMokrassar/fcc6e09cebd07e46e8f0fdec234750c4#file-nginxssl-conf). - -For webhook you can provide `File` with public part of certificate, `URL` where bot will be available and inner `PORT` which -will be used to start receiving of updates. Actually, you can skip passing of `File` when you have something like -nginx for proxy forwarding. - -In case of using `nginx` with reverse-proxy config, setting up of Webhook will look like: - -```kotlin -requestsExecutor.setWebhook( - WEBHOOK_URL, - INTERNAL_PORT, - filter, - ENGINE_FACTORY -) -``` - -Here: - -* `WEBHOOK_URL` - the url which will be used by Telegram system to send updates -* `INTERNAL_PORT` - the port which will be used in bot for listening of updates -* `filter` - instance of [UpdatesFilter](https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt), -which will be used to filter incoming updates -* `ENGINE_FACTORY` - used factory name, for example, `CIO` in case of usage `io.ktor:ktor-server-cio` as server engine From 1f20ae16aab1b20eb7453c414d37210e10cdbc66 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 00:54:00 +0600 Subject: [PATCH 22/46] Update README.md --- TelegramBotAPI-extensions-utils/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TelegramBotAPI-extensions-utils/README.md b/TelegramBotAPI-extensions-utils/README.md index cfcaf41484..c138af694b 100644 --- a/TelegramBotAPI-extensions-utils/README.md +++ b/TelegramBotAPI-extensions-utils/README.md @@ -80,7 +80,7 @@ several useful extensions for both of them: #### Long polling -The most simple way is Long Polling. The most simple way was mentioned above: +The most simple way is Long Polling and one of the usages was mentioned above: ```kotlin val filter = bot.startGettingFlowsUpdatesByLongPolling( From ea40474c47511893288c90ee99be3387fd2c5c13 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 16:53:21 +0600 Subject: [PATCH 23/46] add opportunity to set up media groups devounce time --- .../TelegramBotAPI/extensions/api/utils/UpdatesHandling.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/utils/UpdatesHandling.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/utils/UpdatesHandling.kt index 376c88c02a..98d3e67f4f 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/utils/UpdatesHandling.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/utils/UpdatesHandling.kt @@ -17,12 +17,13 @@ import kotlinx.coroutines.launch * @see UpdateReceiver */ fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation( - output: UpdateReceiver + output: UpdateReceiver, + mediaGroupsDebounceMillis: Long = 1000L ): UpdateReceiver { val updatesChannel = Channel(Channel.UNLIMITED) val mediaGroupChannel = Channel>(Channel.UNLIMITED) val mediaGroupAccumulatedChannel = mediaGroupChannel.accumulateByKey( - 1000L, + mediaGroupsDebounceMillis, scope = this ) From 49573607fb5136930c05cc24f9a8711ff6e25cc8 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 15:49:10 +0600 Subject: [PATCH 24/46] Create greetings.yml --- .github/workflows/greetings.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .github/workflows/greetings.yml diff --git a/.github/workflows/greetings.yml b/.github/workflows/greetings.yml new file mode 100644 index 0000000000..bdde1cca7b --- /dev/null +++ b/.github/workflows/greetings.yml @@ -0,0 +1,13 @@ +name: Greetings + +on: [pull_request, issues] + +jobs: + greeting: + runs-on: ubuntu-latest + steps: + - uses: actions/first-interaction@v1 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + issue-message: 'Welcome with your first issue' + pr-message: 'Welcome with your first PullRequest' From 8419b0ab6ad977be7607ac2e48326cdf38a14d40 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 17:02:33 +0600 Subject: [PATCH 25/46] Create label.yml --- .github/workflows/label.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 .github/workflows/label.yml diff --git a/.github/workflows/label.yml b/.github/workflows/label.yml new file mode 100644 index 0000000000..e90b599b9a --- /dev/null +++ b/.github/workflows/label.yml @@ -0,0 +1,19 @@ +# This workflow will triage pull requests and apply a label based on the +# paths that are modified in the pull request. +# +# To use this workflow, you will need to set up a .github/labeler.yml +# file with configuration. For more information, see: +# https://github.com/actions/labeler/blob/master/README.md + +name: Labeler +on: [pull_request] + +jobs: + label: + + runs-on: ubuntu-latest + + steps: + - uses: actions/labeler@v2 + with: + repo-token: "${{ secrets.GITHUB_TOKEN }}" From 45467e5bd715ad68ebde0f80dd8a051ce7068575 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 17:05:53 +0600 Subject: [PATCH 26/46] Create labeler.yml --- .github/labeler.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .github/labeler.yml diff --git a/.github/labeler.yml b/.github/labeler.yml new file mode 100644 index 0000000000..10e298e962 --- /dev/null +++ b/.github/labeler.yml @@ -0,0 +1,6 @@ +core: "./TelegramBotAPI/*" +api_extensions: "./TelegramBotAPI-extensions-api/*" +utils_extensions: "./TelegramBotAPI-extensions-utils/*" + +code: "./**/*.kt" +gradle: "./**/*.gradle" From 1a638fe0a5fe96e39e97dfb7e10ee8ceca90019b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 17:14:15 +0600 Subject: [PATCH 27/46] update labeler one more update of labeler --- .github/labeler.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/labeler.yml b/.github/labeler.yml index 10e298e962..41aaf94cad 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -1,6 +1,6 @@ -core: "./TelegramBotAPI/*" -api_extensions: "./TelegramBotAPI-extensions-api/*" -utils_extensions: "./TelegramBotAPI-extensions-utils/*" +core: ./TelegramBotAPI/**/* +api_extensions: ./TelegramBotAPI-extensions-api/**/* +utils_extensions: ./TelegramBotAPI-extensions-utils/**/* -code: "./**/*.kt" -gradle: "./**/*.gradle" +code: ./**/*.kt +gradle: ./**/*.gradle From e856dc4754575bb146d8574c436c11f01e2074f2 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 18:17:55 +0600 Subject: [PATCH 28/46] add flowsUdatesFilter --- CHANGELOG.md | 1 + TelegramBotAPI-extensions-utils/README.md | 12 ++++++++- .../utils/updates/FlowsUpdatesFactory.kt | 25 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/FlowsUpdatesFactory.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 262cbf573b..4f3f641986 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,7 @@ * Updates utils were added * New extensions `startListenWebhooks`, `setWebhookInfoAndStartListenWebhooks` and `includeWebhookHandlingInRoute` was added * New extension `CoroutineScope#updateHandlerWithMediaGroupsAdaptation` was added + * New extension `flowsUpdatesFilter` was added * `TelegramBotAPI-all`: * Project was created diff --git a/TelegramBotAPI-extensions-utils/README.md b/TelegramBotAPI-extensions-utils/README.md index c138af694b..8c9fa129ff 100644 --- a/TelegramBotAPI-extensions-utils/README.md +++ b/TelegramBotAPI-extensions-utils/README.md @@ -76,7 +76,17 @@ updates retrieving: * Long Polling Both of them you could use in your project using [TelegramBotAPI](../TelegramBotAPI/README.md), but here there are -several useful extensions for both of them: +several useful extensions for both of them. + +Anyway, in both of ways it will be useful to know that it is possible to create `UpdateReceiver` object using function +`flowsUpdatesFilter`: + +```kotlin +val internalChannelsSizes = 128 +flowsUpdatesFilter(internalChannelsSizes/* default is 64 */) { + /* ... */ +} +``` #### Long polling diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/FlowsUpdatesFactory.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/FlowsUpdatesFactory.kt new file mode 100644 index 0000000000..297706c5b7 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/FlowsUpdatesFactory.kt @@ -0,0 +1,25 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates + +import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter + +/** + * Non-suspendable function for easy-to-use creating of [FlowsUpdatesFilter] and applying the block to it + */ +fun flowsUpdatesFilter( + internalChannelsSizes: Int = 64, + block: FlowsUpdatesFilter.() -> Unit +): FlowsUpdatesFilter = FlowsUpdatesFilter(internalChannelsSizes).apply(block) + +/** + * Suspend variation for [flowsUpdatesFilter] function + * + * @see flowsUpdatesFilter + */ +suspend fun flowsUpdatesFilter( + internalChannelsSizes: Int = 64, + block: suspend FlowsUpdatesFilter.() -> Unit +): FlowsUpdatesFilter { + val filter = FlowsUpdatesFilter(internalChannelsSizes) + filter.block() + return filter +} From 735ed9fd868aa7d873878f7b8db944f796dc1c77 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 18:31:49 +0600 Subject: [PATCH 29/46] change flowsUpdatesFilter signature --- .../extensions/utils/updates/FlowsUpdatesFactory.kt | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/FlowsUpdatesFactory.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/FlowsUpdatesFactory.kt index 297706c5b7..08d3cb7b77 100644 --- a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/FlowsUpdatesFactory.kt +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/FlowsUpdatesFactory.kt @@ -4,20 +4,12 @@ import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFi /** * Non-suspendable function for easy-to-use creating of [FlowsUpdatesFilter] and applying the block to it - */ -fun flowsUpdatesFilter( - internalChannelsSizes: Int = 64, - block: FlowsUpdatesFilter.() -> Unit -): FlowsUpdatesFilter = FlowsUpdatesFilter(internalChannelsSizes).apply(block) - -/** - * Suspend variation for [flowsUpdatesFilter] function * * @see flowsUpdatesFilter */ -suspend fun flowsUpdatesFilter( +inline fun flowsUpdatesFilter( internalChannelsSizes: Int = 64, - block: suspend FlowsUpdatesFilter.() -> Unit + block: FlowsUpdatesFilter.() -> Unit ): FlowsUpdatesFilter { val filter = FlowsUpdatesFilter(internalChannelsSizes) filter.block() From efc2681da8b82eafd41342280e6a40c733e59251 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 18:35:16 +0600 Subject: [PATCH 30/46] add additional signatures for setWebhookInfoAndStartListenWebhooks --- CHANGELOG.md | 1 + .../utils/updates/retrieving/Webhook.kt | 83 ++++++++++++++++--- .../requests/webhook/SetWebhook.kt | 34 ++++---- 3 files changed, 92 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f3f641986..32d47cd227 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,7 @@ * `UpdateDeserializationStrategy` is publicly available now * All `setWebhook` extensions was marked as deprecated, renamed and replaced into `TelegramBotAPI-extensions-utils` * Typealias `ExceptionHandler` was added - it will be used for `handleSafely` + * `SetWebhook` factories signatures was changed (backward compatibility was not broken) * `TelegramBotAPI-extensions-api`: * Long Polling extensions now are deprecated in this project. It was replaced into `TelegramBotAPI-extensions-utils` * `TelegramBotAPI-extensions-utils`: diff --git a/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt b/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt index e202c66ac7..d6c33c77a0 100644 --- a/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt +++ b/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt @@ -2,6 +2,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.retr import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.nonstrictJsonFormat +import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartFile +import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request +import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base.MultipartRequestImpl import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UpdateDeserializationStrategy @@ -106,6 +109,29 @@ fun startListenWebhooks( return engine } +internal suspend fun RequestsExecutor.internalSetWebhookInfoAndStartListenWebhooks( + listenPort: Int, + engineFactory: ApplicationEngineFactory<*, *>, + setWebhookRequest: Request, + exceptionsHandler: ExceptionHandler = {}, + listenHost: String = "0.0.0.0", + listenRoute: String = "/", + privateKeyConfig: WebhookPrivateKeyConfig? = null, + scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), + block: UpdateReceiver +): Job { + return try { + execute(setWebhookRequest) + val engine = startListenWebhooks(listenPort, engineFactory, exceptionsHandler, listenHost, listenRoute, privateKeyConfig, scope, block) + scope.launch { + engine.environment.parentCoroutineContext[Job] ?.join() + engine.stop(1000, 5000) + } + } catch (e: Exception) { + throw e + } +} + /** * Setting up ktor server, set webhook info via [SetWebhook] request. * @@ -128,15 +154,48 @@ suspend fun RequestsExecutor.setWebhookInfoAndStartListenWebhooks( privateKeyConfig: WebhookPrivateKeyConfig? = null, scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), block: UpdateReceiver -): Job { - return try { - execute(setWebhookRequest) - val engine = startListenWebhooks(listenPort, engineFactory, exceptionsHandler, listenHost, listenRoute, privateKeyConfig, scope, block) - scope.launch { - engine.environment.parentCoroutineContext[Job] ?.join() - engine.stop(1000, 5000) - } - } catch (e: Exception) { - throw e - } -} +): Job = internalSetWebhookInfoAndStartListenWebhooks( + listenPort, + engineFactory, + setWebhookRequest as Request, + exceptionsHandler, + listenHost, + listenRoute, + privateKeyConfig, + scope, + block +) + +/** + * Setting up ktor server, set webhook info via [SetWebhook] request. + * + * @param listenPort port which will be listen by bot + * @param listenRoute address to listen by bot + * @param scope Scope which will be used for + * + * @see com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter + * @see UpdatesFilter + * @see UpdatesFilter.asUpdateReceiver + */ +@Suppress("unused") +suspend fun RequestsExecutor.setWebhookInfoAndStartListenWebhooks( + listenPort: Int, + engineFactory: ApplicationEngineFactory<*, *>, + setWebhookRequest: MultipartRequestImpl, Boolean>, + exceptionsHandler: ExceptionHandler = {}, + listenHost: String = "0.0.0.0", + listenRoute: String = "/", + privateKeyConfig: WebhookPrivateKeyConfig? = null, + scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), + block: UpdateReceiver +): Job = internalSetWebhookInfoAndStartListenWebhooks( + listenPort, + engineFactory, + setWebhookRequest as Request, + exceptionsHandler, + listenHost, + listenRoute, + privateKeyConfig, + scope, + block +) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt index 43c53b89aa..c0c872c66d 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt @@ -9,30 +9,36 @@ import kotlinx.serialization.builtins.serializer fun SetWebhook( url: String, - certificate: InputFile, + certificate: MultipartFile, maxAllowedConnections: Int? = null, allowedUpdates: List? = null -) : Request { - val data = SetWebhook( +): MultipartRequestImpl, Boolean> = MultipartRequestImpl( + SetWebhook( url, - (certificate as? FileId) ?.fileId, + null, maxAllowedConnections, allowedUpdates - ) - return when (certificate) { - is FileId -> data - is MultipartFile -> MultipartRequestImpl( - data, - mapOf(certificateField to certificate) - ) - } -} + ), + mapOf(certificateField to certificate) +) + +fun SetWebhook( + url: String, + certificate: FileId, + maxAllowedConnections: Int? = null, + allowedUpdates: List? = null +): SetWebhook = SetWebhook( + url, + certificate.fileId, + maxAllowedConnections, + allowedUpdates +) fun SetWebhook( url: String, maxAllowedConnections: Int? = null, allowedUpdates: List? = null -) : Request = SetWebhook( +) = SetWebhook( url, null, maxAllowedConnections, From 0532dbb1aec57eb1a4f21188213bf75f14597a16 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 18:39:12 +0600 Subject: [PATCH 31/46] fixes --- .../TelegramBotAPI/requests/webhook/SetWebhook.kt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt index c0c872c66d..84fd651e22 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt @@ -34,6 +34,16 @@ fun SetWebhook( allowedUpdates ) +fun SetWebhook( + url: String, + certificate: InputFile, + maxAllowedConnections: Int? = null, + allowedUpdates: List? = null +): Request = when (certificate) { + is MultipartFile -> SetWebhook(url, certificate as MultipartFile, maxAllowedConnections, allowedUpdates) + is FileId -> SetWebhook(url, certificate as FileId, maxAllowedConnections, allowedUpdates) +} + fun SetWebhook( url: String, maxAllowedConnections: Int? = null, From f3827f81a730c4b31384c78bce7c45f7a2ad835c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 18:59:42 +0600 Subject: [PATCH 32/46] change mechanism of executeUnsafe --- CHANGELOG.md | 1 + .../TelegramBotAPI/utils/extensions/Executes.kt | 16 ++++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32d47cd227..3716e7345c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,7 @@ * All `setWebhook` extensions was marked as deprecated, renamed and replaced into `TelegramBotAPI-extensions-utils` * Typealias `ExceptionHandler` was added - it will be used for `handleSafely` * `SetWebhook` factories signatures was changed (backward compatibility was not broken) + * `executeUnsafe` now working differently * `TelegramBotAPI-extensions-api`: * Long Polling extensions now are deprecated in this project. It was replaced into `TelegramBotAPI-extensions-utils` * `TelegramBotAPI-extensions-utils`: diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Executes.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Executes.kt index ebb3f462ae..0dc1f6f868 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Executes.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Executes.kt @@ -4,6 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions.RequestException import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request import com.github.insanusmokrassar.TelegramBotAPI.types.Response +import com.github.insanusmokrassar.TelegramBotAPI.utils.handleSafely import kotlinx.coroutines.* @@ -37,12 +38,15 @@ suspend fun RequestsExecutor.executeUnsafe( ): T? { var leftRetries = retries do { - try { - return execute(request) - } catch (e: RequestException) { - leftRetries-- - delay(retriesDelay) - } + handleSafely( + { + leftRetries-- + delay(retriesDelay) + null + } + ) { + execute(request) + } ?.let { return it } } while(leftRetries >= 0) return null } From db19b69ca026de4775c2ee92e066166bfa98e58e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 19:04:10 +0600 Subject: [PATCH 33/46] fails handler in executeUnsafe --- CHANGELOG.md | 1 + .../TelegramBotAPI/utils/extensions/Executes.kt | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3716e7345c..973cf0fa86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ * Typealias `ExceptionHandler` was added - it will be used for `handleSafely` * `SetWebhook` factories signatures was changed (backward compatibility was not broken) * `executeUnsafe` now working differently + * Now it is possible to pass exceptions handler into `executeUnsafe` * `TelegramBotAPI-extensions-api`: * Long Polling extensions now are deprecated in this project. It was replaced into `TelegramBotAPI-extensions-utils` * `TelegramBotAPI-extensions-utils`: diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Executes.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Executes.kt index 0dc1f6f868..6e4a4f50ef 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Executes.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Executes.kt @@ -34,19 +34,23 @@ fun RequestsExecutor.executeAsync( suspend fun RequestsExecutor.executeUnsafe( request: Request, retries: Int = 0, - retriesDelay: Long = 1000L + retriesDelay: Long = 1000L, + onAllFailed: (suspend (exceptions: Array) -> Unit)? = null ): T? { var leftRetries = retries + val exceptions = onAllFailed ?.let { mutableListOf() } do { handleSafely( { leftRetries-- delay(retriesDelay) + exceptions ?.add(it) null } ) { execute(request) } ?.let { return it } } while(leftRetries >= 0) + onAllFailed ?.invoke(exceptions ?.toTypedArray() ?: emptyArray()) return null } From 3032aa84747c3c50e5ad872e758ddf77a61f293b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 19:14:09 +0600 Subject: [PATCH 34/46] renaming of telegramBot functions --- CHANGELOG.md | 1 + .../TelegramBotAPI/extensions/api/BotBuilder.kt | 2 +- .../TelegramBotAPI/extensions/api/BotExtensions.kt | 12 ++++++------ 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 973cf0fa86..10c5550390 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,7 @@ * Now it is possible to pass exceptions handler into `executeUnsafe` * `TelegramBotAPI-extensions-api`: * Long Polling extensions now are deprecated in this project. It was replaced into `TelegramBotAPI-extensions-utils` + * Several `telegramBot` functions was renamed into `telegramBotWithCustomClientConfig` * `TelegramBotAPI-extensions-utils`: * Extension `toTelegramUpdate` was added * Long Polling extensions were added diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotBuilder.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotBuilder.kt index 083db9a41e..fc8a7f8856 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotBuilder.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotBuilder.kt @@ -33,7 +33,7 @@ data class BotBuilder internal constructor( } /** - * @return Created by [telegramBot] function [RequestsExecutor]. This executor will be preconfigured using [token] and + * @return Created by [telegramBotWithCustomClientConfig] function [RequestsExecutor]. This executor will be preconfigured using [token] and * [block] */ fun telegramBot( diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotExtensions.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotExtensions.kt index 44514ddeae..ef586bcae8 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotExtensions.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotExtensions.kt @@ -22,7 +22,7 @@ fun telegramBot( * Allows to create bot using bot [urlsKeeper] and specify [HttpClientEngine] by passing [clientEngine] param and optionally * configure [HttpClient] using [clientConfig] */ -fun telegramBot( +fun telegramBotWithCustomClientConfig( urlsKeeper: TelegramAPIUrlsKeeper, clientEngine: HttpClientEngine, clientConfig: HttpClientConfig<*>.() -> Unit = {} @@ -34,7 +34,7 @@ fun telegramBot( /** * Allows to create bot using bot [urlsKeeper] and optionally configure [HttpClient] using [clientConfig] */ -fun telegramBot( +fun telegramBotWithCustomClientConfig( urlsKeeper: TelegramAPIUrlsKeeper, clientConfig: HttpClientConfig<*>.() -> Unit = {} ): RequestsExecutor = telegramBot( @@ -47,7 +47,7 @@ fun telegramBot( */ fun telegramBot( token: String -): RequestsExecutor = telegramBot(TelegramAPIUrlsKeeper(token)) +): RequestsExecutor = telegramBotWithCustomClientConfig(TelegramAPIUrlsKeeper(token)) /** * Allows to create bot using bot [token] and already prepared [client] @@ -60,10 +60,10 @@ fun telegramBot( /** * Allows to create bot using bot [token] and configure [HttpClient] using [clientConfig] */ -fun telegramBot( +fun telegramBotWithCustomClientConfig( token: String, clientConfig: HttpClientConfig<*>.() -> Unit -): RequestsExecutor = telegramBot(TelegramAPIUrlsKeeper(token), clientConfig) +): RequestsExecutor = telegramBotWithCustomClientConfig(TelegramAPIUrlsKeeper(token), clientConfig) /** * Allows to create bot using bot [token] and specify [HttpClientEngine] by passing [clientEngine] param and optionally @@ -73,4 +73,4 @@ fun telegramBot( token: String, clientEngine: HttpClientEngine, clientConfig: HttpClientConfig<*>.() -> Unit = {} -): RequestsExecutor = telegramBot(TelegramAPIUrlsKeeper(token), clientEngine, clientConfig) +): RequestsExecutor = telegramBotWithCustomClientConfig(TelegramAPIUrlsKeeper(token), clientEngine, clientConfig) From 23dca3d307edeed0548fc53e1ea126f343eb4445 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 19:19:30 +0600 Subject: [PATCH 35/46] small grammar fix in WebhookInfo --- .../github/insanusmokrassar/TelegramBotAPI/types/WebhookInfo.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/WebhookInfo.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/WebhookInfo.kt index 7930f11d22..037e6e6815 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/WebhookInfo.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/WebhookInfo.kt @@ -7,7 +7,7 @@ data class WebhookInfo( @SerialName(urlField) val url: String, @SerialName(pendingUpdateCountField) - val awaitDeliery: Int, + val awaitDelivery: Int, @SerialName(maxAllowedConnectionsField) val maxConnections: Int = 40, // default count according to documentation @SerialName(hasCustomCertificateField) From 6394e1a52b5513534a24d658b3b59e48bbb1d4be Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 22:02:53 +0600 Subject: [PATCH 36/46] setWebhookInfo --- CHANGELOG.md | 1 + .../extensions/api/webhook/SetWebhookInfo.kt | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10c5550390..898141fe75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,6 +61,7 @@ * `TelegramBotAPI-extensions-api`: * Long Polling extensions now are deprecated in this project. It was replaced into `TelegramBotAPI-extensions-utils` * Several `telegramBot` functions was renamed into `telegramBotWithCustomClientConfig` + * Add one more `setWebhookInfo` realisation * `TelegramBotAPI-extensions-utils`: * Extension `toTelegramUpdate` was added * Long Polling extensions were added diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/webhook/SetWebhookInfo.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/webhook/SetWebhookInfo.kt index 73fb7c418e..9e4a80a11b 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/webhook/SetWebhookInfo.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/webhook/SetWebhookInfo.kt @@ -5,6 +5,19 @@ import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartFile import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook +/** + * Use this method to send information about webhook (like [url] and [certificate]) + */ +suspend fun RequestsExecutor.setWebhookInfo( + url: String, + maxAllowedConnections: Int? = null, + allowedUpdates: List? = null +) = execute( + SetWebhook( + url, maxAllowedConnections, allowedUpdates + ) +) + /** * Use this method to send information about webhook (like [url] and [certificate]) */ From d629aa206eeb7185c178ceb3851a0d9fa1fd79d4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 22:14:19 +0600 Subject: [PATCH 37/46] fixes --- .../extensions/utils/updates/retrieving/Webhook.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt b/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt index d6c33c77a0..7fa6001286 100644 --- a/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt +++ b/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt @@ -59,7 +59,7 @@ fun Route.includeWebhookHandlingInRoute( * Setting up ktor server, set webhook info via [SetWebhook] request. * * @param listenPort port which will be listen by bot - * @param listenRoute address to listen by bot + * @param listenRoute address to listen by bot. If null - will be set up in root of host * @param scope Scope which will be used for * @param privateKeyConfig If configured - server will be created with [sslConnector]. [connector] will be used otherwise * @@ -72,7 +72,7 @@ fun startListenWebhooks( engineFactory: ApplicationEngineFactory<*, *>, exceptionsHandler: ExceptionHandler, listenHost: String = "0.0.0.0", - listenRoute: String = "/", + listenRoute: String? = null, privateKeyConfig: WebhookPrivateKeyConfig? = null, scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), block: UpdateReceiver @@ -82,9 +82,11 @@ fun startListenWebhooks( module { routing { - route(listenRoute) { - includeWebhookHandlingInRoute(scope, exceptionsHandler, block) - } + listenRoute ?.also { + route(it) { + includeWebhookHandlingInRoute(scope, exceptionsHandler, block) + } + } ?: includeWebhookHandlingInRoute(scope, exceptionsHandler, block) } } privateKeyConfig ?.let { From 85317a510eafdbc26a548175c0a92c75d3a79989 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 16 May 2020 11:09:31 +0600 Subject: [PATCH 38/46] update docs --- .../TelegramBotAPI/extensions/api/webhook/SetWebhookInfo.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/webhook/SetWebhookInfo.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/webhook/SetWebhookInfo.kt index 9e4a80a11b..5878a01717 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/webhook/SetWebhookInfo.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/webhook/SetWebhookInfo.kt @@ -6,7 +6,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartFi import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook /** - * Use this method to send information about webhook (like [url] and [certificate]) + * Use this method to send information about webhook (like [url]) */ suspend fun RequestsExecutor.setWebhookInfo( url: String, From 7008f312dc12c9068500e2979afebd0760c787e9 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 16 May 2020 20:43:09 +0600 Subject: [PATCH 39/46] fixes --- .../utils/updates/retrieving/Webhook.kt | 12 +++++------- .../requests/webhook/SetWebhook.kt | 16 +++++++++++----- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt b/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt index 7fa6001286..08c4bffaa8 100644 --- a/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt +++ b/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt @@ -83,9 +83,7 @@ fun startListenWebhooks( module { routing { listenRoute ?.also { - route(it) { - includeWebhookHandlingInRoute(scope, exceptionsHandler, block) - } + createRouteFromPath(it).includeWebhookHandlingInRoute(scope, exceptionsHandler, block) } ?: includeWebhookHandlingInRoute(scope, exceptionsHandler, block) } } @@ -97,11 +95,11 @@ fun startListenWebhooks( privateKeyConfig::aliasPassword ) { host = listenHost - this.port = listenPort + port = listenPort } } ?: connector { host = listenHost - this.port = listenPort + port = listenPort } } @@ -117,7 +115,7 @@ internal suspend fun RequestsExecutor.internalSetWebhookInfoAndStartListenWebhoo setWebhookRequest: Request, exceptionsHandler: ExceptionHandler = {}, listenHost: String = "0.0.0.0", - listenRoute: String = "/", + listenRoute: String? = null, privateKeyConfig: WebhookPrivateKeyConfig? = null, scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), block: UpdateReceiver @@ -186,7 +184,7 @@ suspend fun RequestsExecutor.setWebhookInfoAndStartListenWebhooks( setWebhookRequest: MultipartRequestImpl, Boolean>, exceptionsHandler: ExceptionHandler = {}, listenHost: String = "0.0.0.0", - listenRoute: String = "/", + listenRoute: String? = null, privateKeyConfig: WebhookPrivateKeyConfig? = null, scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), block: UpdateReceiver diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt index 84fd651e22..703c6ac043 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt @@ -7,6 +7,12 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.* import kotlinx.serialization.* import kotlinx.serialization.builtins.serializer +private fun correctWebhookUrl(sourceUrl: String) = if (sourceUrl.contains("://")) { + sourceUrl +} else { + "https://$sourceUrl" +} + fun SetWebhook( url: String, certificate: MultipartFile, @@ -14,7 +20,7 @@ fun SetWebhook( allowedUpdates: List? = null ): MultipartRequestImpl, Boolean> = MultipartRequestImpl( SetWebhook( - url, + correctWebhookUrl(url), null, maxAllowedConnections, allowedUpdates @@ -28,7 +34,7 @@ fun SetWebhook( maxAllowedConnections: Int? = null, allowedUpdates: List? = null ): SetWebhook = SetWebhook( - url, + correctWebhookUrl(url), certificate.fileId, maxAllowedConnections, allowedUpdates @@ -40,8 +46,8 @@ fun SetWebhook( maxAllowedConnections: Int? = null, allowedUpdates: List? = null ): Request = when (certificate) { - is MultipartFile -> SetWebhook(url, certificate as MultipartFile, maxAllowedConnections, allowedUpdates) - is FileId -> SetWebhook(url, certificate as FileId, maxAllowedConnections, allowedUpdates) + is MultipartFile -> SetWebhook(correctWebhookUrl(url), certificate as MultipartFile, maxAllowedConnections, allowedUpdates) + is FileId -> SetWebhook(correctWebhookUrl(url), certificate as FileId, maxAllowedConnections, allowedUpdates) } fun SetWebhook( @@ -49,7 +55,7 @@ fun SetWebhook( maxAllowedConnections: Int? = null, allowedUpdates: List? = null ) = SetWebhook( - url, + correctWebhookUrl(url), null, maxAllowedConnections, allowedUpdates From 0ec18cbf0642751e63fc7813a1b735219d6a0474 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 16 May 2020 20:46:49 +0600 Subject: [PATCH 40/46] dice changes --- CHANGELOG.md | 3 +++ .../TelegramBotAPI/types/dice/DiceAnimationType.kt | 11 +++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 898141fe75..e4b61a23ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,9 @@ * `SetWebhook` factories signatures was changed (backward compatibility was not broken) * `executeUnsafe` now working differently * Now it is possible to pass exceptions handler into `executeUnsafe` + * `BasketballDiceAnimationType` was added + * `UnknownDiceAnimationType` now is deprecated due to renaming - currently it is typealias for `CustomDiceAnimationType` + * `CustomDiceAnimationType` now is `data` class instead of common class * `TelegramBotAPI-extensions-api`: * Long Polling extensions now are deprecated in this project. It was replaced into `TelegramBotAPI-extensions-utils` * Several `telegramBot` functions was renamed into `telegramBotWithCustomClientConfig` diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/dice/DiceAnimationType.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/dice/DiceAnimationType.kt index 28b4836d15..0c51776d74 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/dice/DiceAnimationType.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/dice/DiceAnimationType.kt @@ -15,9 +15,15 @@ object DartsDiceAnimationType : DiceAnimationType() { override val emoji: String = "\uD83C\uDFAF" } @Serializable(DiceAnimationTypeSerializer::class) -class UnknownDiceAnimationType( +object BasketballDiceAnimationType : DiceAnimationType() { + override val emoji: String = "\uD83C\uDFC0" +} +@Serializable(DiceAnimationTypeSerializer::class) +data class CustomDiceAnimationType( override val emoji: String ) : DiceAnimationType() +@Deprecated("Renamed", ReplaceWith("CustomDiceAnimationType")) +typealias UnknownDiceAnimationType = CustomDiceAnimationType @Serializer(DiceAnimationType::class) internal object DiceAnimationTypeSerializer : KSerializer { @@ -26,7 +32,8 @@ internal object DiceAnimationTypeSerializer : KSerializer { return when (val type = decoder.decodeString()) { CubeDiceAnimationType.emoji -> CubeDiceAnimationType DartsDiceAnimationType.emoji -> DartsDiceAnimationType - else -> UnknownDiceAnimationType(type) + BasketballDiceAnimationType.emoji -> BasketballDiceAnimationType + else -> CustomDiceAnimationType(type) } } From 4c8861ba79aa66740432a20b1559221f138c82d8 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 16 May 2020 20:57:57 +0600 Subject: [PATCH 41/46] change visibility of internalSetWebhookInfoAndStartListenWebhooks --- .../extensions/utils/updates/retrieving/Webhook.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt b/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt index 08c4bffaa8..ea95854ba7 100644 --- a/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt +++ b/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt @@ -109,7 +109,7 @@ fun startListenWebhooks( return engine } -internal suspend fun RequestsExecutor.internalSetWebhookInfoAndStartListenWebhooks( +private suspend fun RequestsExecutor.internalSetWebhookInfoAndStartListenWebhooks( listenPort: Int, engineFactory: ApplicationEngineFactory<*, *>, setWebhookRequest: Request, From a9fe584504c40f541a4477832bdb4ed2887b16da Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 16 May 2020 21:14:10 +0600 Subject: [PATCH 42/46] add TOC for TelegramBotAPI-extensions-utils --- TelegramBotAPI-extensions-utils/README.md | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/TelegramBotAPI-extensions-utils/README.md b/TelegramBotAPI-extensions-utils/README.md index 8c9fa129ff..e2078e7f8b 100644 --- a/TelegramBotAPI-extensions-utils/README.md +++ b/TelegramBotAPI-extensions-utils/README.md @@ -1,5 +1,23 @@ # TelegramBotAPI Util Extensions +- [TelegramBotAPI Util Extensions](#telegrambotapi-util--extensions) + * [What is it?](#what-is-it-) + * [How to implement library?](#how-to-implement-library-) + + [Maven](#maven) + + [Gradle](#gradle) + * [How to use?](#how-to-use-) + + [Updates](#updates) + - [Long polling](#long-polling) + - [WebHooks (currently JVM-only)](#webhooks--currently-jvm-only-) + + [Filters](#filters) + - [Sent messages](#sent-messages) + * [Common messages](#common-messages) + * [Chat actions](#chat-actions) + + [Shortcuts](#shortcuts) + - [ScheduledCloseInfo](#scheduledcloseinfo) + +Table of contents generated with markdown-toc + [![Download](https://api.bintray.com/packages/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-utils/images/download.svg) ](https://bintray.com/insanusmokrassar/StandardRepository/TelegramBotAPI-extensions-utils/_latestVersion) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-utils/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.insanusmokrassar/TelegramBotAPI-extensions-utils) @@ -213,11 +231,11 @@ filter.messageFlow.asChatEventsFlow().onlySupergroupEvents().onEach { ) ``` -## Shortcuts +### Shortcuts With shortcuts you are able to use simple factories for several things. -### ScheduledCloseInfo +#### ScheduledCloseInfo In case if you are creating some poll, you able to use next shortcuts. From 81aa3f2307c81b05e171f904974e8c35d8e8863f Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 16 May 2020 21:19:38 +0600 Subject: [PATCH 43/46] update labeler task --- .github/workflows/label.yml | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/.github/workflows/label.yml b/.github/workflows/label.yml index e90b599b9a..be1ee465e3 100644 --- a/.github/workflows/label.yml +++ b/.github/workflows/label.yml @@ -5,15 +5,14 @@ # file with configuration. For more information, see: # https://github.com/actions/labeler/blob/master/README.md -name: Labeler -on: [pull_request] +name: "Pull Request Labeler" +on: + - pull_request jobs: - label: - + triage: runs-on: ubuntu-latest - steps: - - uses: actions/labeler@v2 - with: - repo-token: "${{ secrets.GITHUB_TOKEN }}" + - uses: actions/labeler@v2 + with: + repo-token: "${{ secrets.GITHUB_TOKEN }}" From e7b5b9184d8b82ae8674b17a17d85d8df980c758 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 16 May 2020 21:22:37 +0600 Subject: [PATCH 44/46] update rules for labelers --- .github/labeler.yml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/labeler.yml b/.github/labeler.yml index 41aaf94cad..5e8e6a1da8 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -1,6 +1,7 @@ -core: ./TelegramBotAPI/**/* -api_extensions: ./TelegramBotAPI-extensions-api/**/* -utils_extensions: ./TelegramBotAPI-extensions-utils/**/* +core: TelegramBotAPI/**/* +api_extensions: TelegramBotAPI-extensions-api/**/* +utils_extensions: TelegramBotAPI-extensions-utils/**/* -code: ./**/*.kt -gradle: ./**/*.gradle +code_changes: ./**/*.kt +gradle_changes: ./**/*.gradle +markdown_changes: ./**/*.md From fa43a55f26417d924d3abf50ac9ce96966c6025b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 16 May 2020 21:26:54 +0600 Subject: [PATCH 45/46] one more update of labeler --- .github/labeler.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/labeler.yml b/.github/labeler.yml index 5e8e6a1da8..45ea34644b 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -1,7 +1,7 @@ -core: TelegramBotAPI/**/* -api_extensions: TelegramBotAPI-extensions-api/**/* -utils_extensions: TelegramBotAPI-extensions-utils/**/* +api: "TelegramBotAPI-extensions-api/**" +utils: "TelegramBotAPI-extensions-utils/**" +core: "TelegramBotAPI/**" # currently not work -code_changes: ./**/*.kt -gradle_changes: ./**/*.gradle -markdown_changes: ./**/*.md +code: "**/*.kt" +gradle: "**/*.gradle" +markdown: "**/*.md" From 0260e7bedc84491b975d24241926bc590faff503 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 16 May 2020 22:02:57 +0600 Subject: [PATCH 46/46] fixes of warnings --- CHANGELOG.md | 1 + .../TelegramBotAPI/requests/webhook/SetWebhook.kt | 1 + .../TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt | 7 +++---- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4b61a23ff..eabd6a44c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,6 +61,7 @@ * `BasketballDiceAnimationType` was added * `UnknownDiceAnimationType` now is deprecated due to renaming - currently it is typealias for `CustomDiceAnimationType` * `CustomDiceAnimationType` now is `data` class instead of common class + * `FlowsUpdatesFilter` will use size 64 by default for internal broadcast channels * `TelegramBotAPI-extensions-api`: * Long Polling extensions now are deprecated in this project. It was replaced into `TelegramBotAPI-extensions-utils` * Several `telegramBot` functions was renamed into `telegramBotWithCustomClientConfig` diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt index 703c6ac043..e662818d14 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/webhook/SetWebhook.kt @@ -40,6 +40,7 @@ fun SetWebhook( allowedUpdates ) +@Suppress("USELESS_CAST") fun SetWebhook( url: String, certificate: InputFile, 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 ffde4ccb35..052e1a8f03 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 @@ -3,17 +3,16 @@ 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.FlowPreview import kotlinx.coroutines.channels.BroadcastChannel -import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.asFlow +@Suppress("EXPERIMENTAL_API_USAGE") private fun BroadcastChannel.createUpdateReceiver(): UpdateReceiver = ::send -@FlowPreview +@Suppress("EXPERIMENTAL_API_USAGE", "unused") class FlowsUpdatesFilter( - broadcastChannelsSize: Int = Channel.CONFLATED + broadcastChannelsSize: Int = 64 ): UpdatesFilter { private val messageChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) private val messageMediaGroupChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize)