diff --git a/CHANGELOG.md b/CHANGELOG.md index ae543f5dec..ccdc4a2965 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,16 @@ and size of retrieved updates is equal to 100 (max count of retrieved updates) * Extensions `getUpdates` now will receive only not nullable `limit` parameter +### 0.26.4 + +* `TelegramBotAPI`: + * Now any getting of updates will return `UnknownUpdateType` when inside of deserialization will be + `SerializationException` or `NotImplemented` error + * `CallbackGame` currently is an object + * It is possible to use `CallbackGame` for now + * `CallbackGameInlineKeyboardButton` now will not accept `callbackGame` as income object + * Now it is possible to pass exception handler in webhook + ### 0.26.3 * `TelegramBotAPI`: diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt index 3169c63936..bb682b4063 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetUpdates.kt @@ -3,12 +3,12 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update -import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UpdateSerializerWithoutDeserialization +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UpdateSerializerWithoutSerialization import kotlinx.serialization.* import kotlinx.serialization.builtins.ListSerializer private val updatesListSerializer = ListSerializer( - UpdateSerializerWithoutDeserialization + UpdateSerializerWithoutSerialization ) @Serializable diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt index 0e5c18508e..c8ea32297b 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt @@ -36,10 +36,11 @@ data class CallbackDataInlineKeyboardButton( @Serializable data class CallbackGameInlineKeyboardButton( @SerialName(textField) - override val text: String, + override val text: String +) : InlineKeyboardButton() { @SerialName(callbackGameField) - val callbackGame: CallbackGame -) : InlineKeyboardButton() + private val callbackGame = CallbackGame +} @Serializable data class LoginURLInlineKeyboardButton( diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/CallbackGame.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/CallbackGame.kt index 1d74fd18ff..32da936cf7 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/CallbackGame.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/CallbackGame.kt @@ -3,8 +3,4 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.games import kotlinx.serialization.Serializable @Serializable -class CallbackGame { - init { - TODO() - } -} +object CallbackGame diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/RawUpdate.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/RawUpdate.kt index f96d60bdb2..e27b840986 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/RawUpdate.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/RawUpdate.kt @@ -64,12 +64,16 @@ internal data class RawUpdate constructor( raw ) } - } catch (e: SerializationException) { - UnknownUpdateType( - updateId, - raw.toString(), - raw - ) + } catch (e: Error) { + when (e) { + is SerializationException, + is NotImplementedError -> UnknownUpdateType( + updateId, + raw.toString(), + raw + ) + else -> throw e + } }.also { initedUpdate = it } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt index d361301b31..fa2129f854 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt @@ -18,7 +18,7 @@ data class UnknownUpdateType( val rawJson: JsonElement ) : Update -internal object UpdateSerializerWithoutDeserialization : KSerializer { +internal object UpdateSerializerWithoutSerialization : KSerializer { override val descriptor: SerialDescriptor = JsonElementSerializer.descriptor override fun deserialize(decoder: Decoder): Update = UpdateDeserializationStrategy.deserialize(decoder) 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..64aaf9caf8 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 @@ -4,15 +4,11 @@ import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.InputFile import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage -import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.MediaGroupUpdate -import com.github.insanusmokrassar.TelegramBotAPI.types.update.RawUpdate -import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate -import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.* 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 +41,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 +71,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( + UpdateDeserializationStrategy, + asJson + ) + updatesChannel.send(update) + } call.respond("Ok") } } @@ -113,11 +116,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 +137,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, diff --git a/gradle.properties b/gradle.properties index 019b89b8dd..991a298c71 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,6 +7,6 @@ uuid_version=0.1.0 ktor_version=1.3.2 library_group=com.github.insanusmokrassar -library_version=0.26.3 +library_version=0.26.4 gradle_bintray_plugin_version=1.8.4