diff --git a/CHANGELOG.md b/CHANGELOG.md index c796a42cba..d3e2cb02d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,9 @@ [PR-80](https://github.com/InsanusMokrassar/TelegramBotAPI/pull/80)) * `UnknownInlineKeyboardButton` was added. It is unavailable for creating, but you can receive it, for example, in `InlineQueryResult` + * `Update` now will be created even if was `SerializationException` inside of creating the update instance - in this + case will be created `UnknownUpdateType` + * `UnknownUpdateType$rawJson` value now is included (`JsonElement`) ### 0.26.2 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 5ccd666095..1c77a0e4ca 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 @@ -13,8 +13,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.polls.PollAnswer import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UnknownUpdateType import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.types.updateIdField -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable +import kotlinx.serialization.* import kotlinx.serialization.json.JsonElement @Serializable @@ -42,25 +41,34 @@ internal data class RawUpdate constructor( * @return One of children of [Update] interface or null in case of unknown type of update */ fun asUpdate(raw: JsonElement): Update { - return initedUpdate ?: when { - edited_message != null -> EditMessageUpdate(updateId, edited_message) - message != null -> MessageUpdate(updateId, message) - edited_channel_post != null -> EditChannelPostUpdate(updateId, edited_channel_post) - channel_post != null -> ChannelPostUpdate(updateId, channel_post) + return initedUpdate ?: try { + when { + edited_message != null -> EditMessageUpdate(updateId, edited_message) + message != null -> MessageUpdate(updateId, message) + edited_channel_post != null -> EditChannelPostUpdate(updateId, edited_channel_post) + channel_post != null -> ChannelPostUpdate(updateId, channel_post) - chosen_inline_result != null -> ChosenInlineResultUpdate(updateId, chosen_inline_result.asChosenInlineResult) - inline_query != null -> InlineQueryUpdate(updateId, inline_query.asInlineQuery) - callback_query != null -> CallbackQueryUpdate( + chosen_inline_result != null -> ChosenInlineResultUpdate(updateId, chosen_inline_result.asChosenInlineResult) + inline_query != null -> InlineQueryUpdate(updateId, inline_query.asInlineQuery) + callback_query != null -> CallbackQueryUpdate( + updateId, + callback_query.asCallbackQuery(raw.jsonObject["callback_query"].toString()) + ) + shipping_query != null -> ShippingQueryUpdate(updateId, shipping_query) + pre_checkout_query != null -> PreCheckoutQueryUpdate(updateId, pre_checkout_query) + poll != null -> PollUpdate(updateId, poll) + poll_answer != null -> PollAnswerUpdate(updateId, poll_answer) + else -> UnknownUpdateType( + updateId, + raw.toString(), + raw + ) + } + } catch (e: SerializationException) { + UnknownUpdateType( updateId, - callback_query.asCallbackQuery(raw.jsonObject["callback_query"].toString()) - ) - shipping_query != null -> ShippingQueryUpdate(updateId, shipping_query) - pre_checkout_query != null -> PreCheckoutQueryUpdate(updateId, pre_checkout_query) - poll != null -> PollUpdate(updateId, poll) - poll_answer != null -> PollAnswerUpdate(updateId, poll_answer) - else -> UnknownUpdateType( - updateId, - raw.toString() + raw.toString(), + raw ) }.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 536fd33827..d361301b31 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 @@ -4,6 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.update.RawUpdate import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat import kotlinx.serialization.* +import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonElementSerializer interface Update { @@ -13,7 +14,8 @@ interface Update { data class UnknownUpdateType( override val updateId: UpdateIdentifier, - override val data: String + override val data: String, + val rawJson: JsonElement ) : Update internal object UpdateSerializerWithoutDeserialization : KSerializer {