From 9c91980d5d729fd4e9eabb0f8af5589bce80f94e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 10 Nov 2020 20:44:39 +0600 Subject: [PATCH] webhook fixes --- CHANGELOG.md | 7 ++ .../tgbotapi/requests/webhook/SetWebhook.kt | 30 +++++-- .../utils/updates/retrieving/Webhook.kt | 81 +++---------------- 3 files changed, 40 insertions(+), 78 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f68ab68bfc..52a59c7c5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ ## 0.30.2 +* `Core`: + * New sealed class `SetWebhookRequest` which can be used in `SetWebhook` requests +* `Utils`: + * Extensions `setWebhookInfoAndStartListenWebhooks` has been united in one extension with `SetWebhookRequest` + incoming parameter + + ## 0.30.1 * `Common`: diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/webhook/SetWebhook.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/webhook/SetWebhook.kt index 9a2c5a3a0f..3e54a9c6ad 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/webhook/SetWebhook.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/webhook/SetWebhook.kt @@ -1,8 +1,7 @@ package dev.inmo.tgbotapi.requests.webhook import dev.inmo.tgbotapi.requests.abstracts.* -import dev.inmo.tgbotapi.requests.send.media.base.DataRequest -import dev.inmo.tgbotapi.requests.send.media.base.MultipartRequestImpl +import dev.inmo.tgbotapi.requests.send.media.base.* import dev.inmo.tgbotapi.types.* import kotlinx.serialization.* import kotlinx.serialization.builtins.serializer @@ -13,14 +12,15 @@ private fun correctWebhookUrl(sourceUrl: String) = if (sourceUrl.contains("://") "https://$sourceUrl" } -fun SetWebhook( +sealed class SetWebhookRequest : Request +class MultipartSetWebhookRequest( url: String, certificate: MultipartFile, ipAddress: String? = null, maxAllowedConnections: Int? = null, allowedUpdates: List? = null, dropPendingUpdates: Boolean? = null -): MultipartRequestImpl, Boolean> = MultipartRequestImpl( +) : SetWebhookRequest(), MultipartRequest by MultipartRequestImpl( SetWebhook( correctWebhookUrl(url), null as String?, @@ -32,6 +32,22 @@ fun SetWebhook( mapOf(certificateField to certificate) ) +fun SetWebhook( + url: String, + certificate: MultipartFile, + ipAddress: String? = null, + maxAllowedConnections: Int? = null, + allowedUpdates: List? = null, + dropPendingUpdates: Boolean? = null +): MultipartSetWebhookRequest = MultipartSetWebhookRequest( + correctWebhookUrl(url), + certificate, + ipAddress, + maxAllowedConnections, + allowedUpdates, + dropPendingUpdates +) + fun SetWebhook( url: String, certificate: FileId, @@ -63,7 +79,7 @@ fun SetWebhook( maxAllowedConnections: Int? = null, allowedUpdates: List? = null, dropPendingUpdates: Boolean? = null -): Request = when (certificate) { +) = when (certificate) { is MultipartFile -> SetWebhook(correctWebhookUrl(url), certificate as MultipartFile, ipAddress, maxAllowedConnections, allowedUpdates, dropPendingUpdates) is FileId -> SetWebhook(correctWebhookUrl(url), certificate as FileId, ipAddress, maxAllowedConnections, allowedUpdates, dropPendingUpdates) } @@ -82,7 +98,7 @@ fun SetWebhook( maxAllowedConnections: Int? = null, allowedUpdates: List? = null, dropPendingUpdates: Boolean? = null -): Request = SetWebhook( +) = SetWebhook( correctWebhookUrl(url), null, ipAddress, @@ -112,7 +128,7 @@ data class SetWebhook internal constructor( val allowedUpdates: List? = null, @SerialName(dropPendingUpdatesField) val dropPendingUpdates: Boolean? = null -) : DataRequest { +) : SetWebhookRequest(), DataRequest { override fun method(): String = "setWebhook" override val resultDeserializer: DeserializationStrategy get() = Boolean.serializer() diff --git a/tgbotapi.extensions.utils/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/Webhook.kt b/tgbotapi.extensions.utils/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/Webhook.kt index 92c50173d4..fcb780754d 100644 --- a/tgbotapi.extensions.utils/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/Webhook.kt +++ b/tgbotapi.extensions.utils/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/Webhook.kt @@ -5,10 +5,7 @@ import dev.inmo.micro_utils.coroutines.safely import dev.inmo.tgbotapi.bot.RequestsExecutor import dev.inmo.tgbotapi.extensions.utils.nonstrictJsonFormat import dev.inmo.tgbotapi.extensions.utils.updates.flowsUpdatesFilter -import dev.inmo.tgbotapi.requests.abstracts.MultipartFile -import dev.inmo.tgbotapi.requests.abstracts.Request -import dev.inmo.tgbotapi.requests.send.media.base.MultipartRequestImpl -import dev.inmo.tgbotapi.requests.webhook.SetWebhook +import dev.inmo.tgbotapi.requests.webhook.SetWebhookRequest import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.types.update.abstracts.UpdateDeserializationStrategy import dev.inmo.tgbotapi.updateshandlers.* @@ -67,7 +64,7 @@ fun Route.includeWebhookHandlingInRouteWithFlows( ) /** - * Setting up ktor server, set webhook info via [SetWebhook] request. + * Setting up ktor server * * @param listenPort port which will be listen by bot * @param listenRoute address to listen by bot. If null - will be set up in root of host @@ -119,27 +116,8 @@ fun startListenWebhooks( } } -private suspend fun RequestsExecutor.internalSetWebhookInfoAndStartListenWebhooks( - listenPort: Int, - engineFactory: ApplicationEngineFactory<*, *>, - setWebhookRequest: Request, - exceptionsHandler: ExceptionHandler = {}, - listenHost: String = "0.0.0.0", - listenRoute: String? = null, - privateKeyConfig: WebhookPrivateKeyConfig? = null, - scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), - block: UpdateReceiver -): ApplicationEngine { - return try { - execute(setWebhookRequest) - startListenWebhooks(listenPort, engineFactory, exceptionsHandler, listenHost, listenRoute, privateKeyConfig, scope, block) - } catch (e: Exception) { - throw e - } -} - /** - * Setting up ktor server, set webhook info via [SetWebhook] request. + * Setting up ktor server, set webhook info via [SetWebhookRequest] request. * * @param listenPort port which will be listen by bot * @param listenRoute address to listen by bot @@ -153,55 +131,16 @@ private suspend fun RequestsExecutor.internalSetWebhookInfoAndStartListenWebhook suspend fun RequestsExecutor.setWebhookInfoAndStartListenWebhooks( listenPort: Int, engineFactory: ApplicationEngineFactory<*, *>, - setWebhookRequest: SetWebhook, + setWebhookRequest: SetWebhookRequest, exceptionsHandler: ExceptionHandler = {}, listenHost: String = "0.0.0.0", listenRoute: String = "/", privateKeyConfig: WebhookPrivateKeyConfig? = null, scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), block: UpdateReceiver -): ApplicationEngine = 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 dev.inmo.tgbotapi.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? = null, - privateKeyConfig: WebhookPrivateKeyConfig? = null, - scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), - block: UpdateReceiver -): ApplicationEngine = internalSetWebhookInfoAndStartListenWebhooks( - listenPort, - engineFactory, - setWebhookRequest as Request, - exceptionsHandler, - listenHost, - listenRoute, - privateKeyConfig, - scope, - block -) +): ApplicationEngine = try { + execute(setWebhookRequest) + startListenWebhooks(listenPort, engineFactory, exceptionsHandler, listenHost, listenRoute, privateKeyConfig, scope, block) +} catch (e: Exception) { + throw e +}