diff --git a/CHANGELOG.md b/CHANGELOG.md index 60358d2838..a25cf9aaa0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ `SerializationException` or `NotImplemented` error * `CallbackGame` currently is an object * It is possible to use `CallbackGame` for now + * Now it is possible to pass exception handler in webhook ### 0.26.3 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 fd995cde3d..b79136f8fc 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 @@ -11,8 +11,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update 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.convertWithMediaGroupUpdates -import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat +import com.github.insanusmokrassar.TelegramBotAPI.utils.* import io.ktor.application.call import io.ktor.request.receiveText import io.ktor.response.respond @@ -45,6 +44,7 @@ suspend fun RequestsExecutor.setWebhook( 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 { @@ -74,12 +74,18 @@ suspend fun RequestsExecutor.setWebhook( module { routing { post(listenRoute) { - val asJson = nonstrictJsonFormat.parseJson(call.receiveText()) - val update = nonstrictJsonFormat.fromJson( - RawUpdate.serializer(), - asJson - ) - updatesChannel.send(update.asUpdate(asJson)) + handleSafely( + { + exceptionsHandler ?.invoke(it) + } + ) { + val asJson = nonstrictJsonFormat.parseJson(call.receiveText()) + val update = nonstrictJsonFormat.fromJson( + RawUpdate.serializer(), + asJson + ) + updatesChannel.send(update.asUpdate(asJson)) + } call.respond("Ok") } } @@ -113,11 +119,6 @@ suspend fun RequestsExecutor.setWebhook( launch { for (update in updatesChannel) { val data = update.data - if (data is MediaGroupUpdate) { - - } else { - - } when (data) { is MediaGroupMessage -> mediaGroupChannel.send("${data.mediaGroupId}${update::class.simpleName}" to update as BaseMessageUpdate) else -> block(update) @@ -139,6 +140,33 @@ suspend fun RequestsExecutor.setWebhook( } } +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 +) + suspend fun RequestsExecutor.setWebhook( url: String, port: Int,