From efc2681da8b82eafd41342280e6a40c733e59251 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 May 2020 18:35:16 +0600 Subject: [PATCH] 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,