From b40cc0c1eaa2afdeaa7efaaabe2ccaa4f637634e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 14 May 2020 13:11:46 +0600 Subject: [PATCH] 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 {