From e9ff93cde1db088c22c126749d1302bc2d70cd85 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 14 May 2022 01:35:38 +0600 Subject: [PATCH] improvements in webhooks --- CHANGELOG.md | 3 ++ .../utils/updates/retrieving/Webhook.kt | 35 +++++++++++-------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd90241faa..6ddc013835 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 1.1.0 +* `Utils`: + * New parameter `additionalApplicationEngineEnvironmentConfigurator` in `RequestsExecutor#setWebhookInfoAndStartListenWebhooks` and `startListenWebhooks` + ## 1.0.1 * `Versions`: diff --git a/tgbotapi.utils/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/Webhook.kt b/tgbotapi.utils/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/Webhook.kt index 04d39ed179..013ddccbc4 100644 --- a/tgbotapi.utils/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/Webhook.kt +++ b/tgbotapi.utils/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/utils/updates/retrieving/Webhook.kt @@ -1,7 +1,6 @@ package dev.inmo.tgbotapi.extensions.utils.updates.retrieving -import dev.inmo.micro_utils.coroutines.ExceptionHandler -import dev.inmo.micro_utils.coroutines.safely +import dev.inmo.micro_utils.coroutines.* import dev.inmo.tgbotapi.bot.RequestsExecutor import dev.inmo.tgbotapi.extensions.utils.nonstrictJsonFormat import dev.inmo.tgbotapi.extensions.utils.updates.flowsUpdatesFilter @@ -10,6 +9,7 @@ import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.types.update.abstracts.UpdateDeserializationStrategy import dev.inmo.tgbotapi.updateshandlers.* import dev.inmo.tgbotapi.updateshandlers.webhook.WebhookPrivateKeyConfig +import io.ktor.http.HttpStatusCode import io.ktor.server.application.call import io.ktor.server.engine.* import io.ktor.server.request.receiveText @@ -39,18 +39,21 @@ fun Route.includeWebhookHandlingInRoute( ) { val transformer = scope.updateHandlerWithMediaGroupsAdaptation(block, mediaGroupsDebounceTimeMillis) post { - safely( - exceptionsHandler ?: {} - ) { - val asJson = - nonstrictJsonFormat.parseToJsonElement(call.receiveText()) - val update = nonstrictJsonFormat.decodeFromJsonElement( - UpdateDeserializationStrategy, - asJson - ) - transformer(update) + try { + runCatchingSafely { + val asJson = nonstrictJsonFormat.parseToJsonElement(call.receiveText()) + val update = nonstrictJsonFormat.decodeFromJsonElement( + UpdateDeserializationStrategy, + asJson + ) + transformer(update) + }.onSuccess { + call.respond(HttpStatusCode.OK) + } + } catch (e: Throwable) { + exceptionsHandler ?.invoke(e) + call.respond(HttpStatusCode.InternalServerError) } - call.respond("Ok") } } @@ -87,6 +90,7 @@ fun startListenWebhooks( privateKeyConfig: WebhookPrivateKeyConfig? = null, scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), mediaGroupsDebounceTimeMillis: Long = 1000L, + additionalApplicationEngineEnvironmentConfigurator: ApplicationEngineEnvironmentBuilder.() -> Unit = {}, block: UpdateReceiver ): ApplicationEngine { val env = applicationEngineEnvironment { @@ -98,6 +102,7 @@ fun startListenWebhooks( } ?: includeWebhookHandlingInRoute(scope, exceptionsHandler, mediaGroupsDebounceTimeMillis, block) } } + privateKeyConfig ?.let { sslConnector( privateKeyConfig.keyStore, @@ -113,6 +118,7 @@ fun startListenWebhooks( port = listenPort } + additionalApplicationEngineEnvironmentConfigurator() } return embeddedServer(engineFactory, env).also { @@ -142,10 +148,11 @@ suspend fun RequestsExecutor.setWebhookInfoAndStartListenWebhooks( privateKeyConfig: WebhookPrivateKeyConfig? = null, scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), mediaGroupsDebounceTimeMillis: Long = 1000L, + additionalApplicationEngineEnvironmentConfigurator: ApplicationEngineEnvironmentBuilder.() -> Unit = {}, block: UpdateReceiver ): ApplicationEngine = try { execute(setWebhookRequest) - startListenWebhooks(listenPort, engineFactory, exceptionsHandler, listenHost, listenRoute, privateKeyConfig, scope, mediaGroupsDebounceTimeMillis, block) + startListenWebhooks(listenPort, engineFactory, exceptionsHandler, listenHost, listenRoute, privateKeyConfig, scope, mediaGroupsDebounceTimeMillis, additionalApplicationEngineEnvironmentConfigurator, block) } catch (e: Exception) { throw e }