From 961fa65415a05283c3c58c5b70f9336d24fdf7b9 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 23 Jan 2020 03:31:56 +0600 Subject: [PATCH] UnknownUpdate --- CHANGELOG.md | 1 + .../TelegramBotAPI/types/update/RawUpdate.kt | 12 +++++++++--- .../TelegramBotAPI/types/update/UnknownUpdate.kt | 9 +++++++++ .../TelegramBotAPI/types/update/abstracts/Update.kt | 10 +++++++++- .../TelegramBotAPI/utils/extensions/Webhooks.kt | 2 +- 5 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/UnknownUpdate.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 407d5dcb52..a2d4458644 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -80,6 +80,7 @@ while they can work incorrectly ### 0.22.2 CashTag and independent updates handling * `cashtag` entity type was added +* New type of updates was added: `UnknownUpdate`. It will be used in cases when type of update is unknown in system ## 0.21.0 TelegramBotAPI 4.5 diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/RawUpdate.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/RawUpdate.kt index cbebc8f29f..78c533c805 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/RawUpdate.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/RawUpdate.kt @@ -33,8 +33,12 @@ internal data class RawUpdate constructor( private val pre_checkout_query: PreCheckoutQuery? = null, private val poll: Poll? = null ) { - val asUpdate: Update by lazy { - when { + private var initedUpdate: Update? = null + /** + * @return One of children of [Update] interface or null in case of unknown type of update + */ + fun asUpdate(raw: String): 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) @@ -46,7 +50,9 @@ internal data class RawUpdate constructor( shipping_query != null -> ShippingQueryUpdate(updateId, shipping_query) pre_checkout_query != null -> PreCheckoutQueryUpdate(updateId, pre_checkout_query) poll != null -> PollUpdate(updateId, poll) - else -> throw IllegalArgumentException("Unsupported type of update") + else -> UnknownUpdate(updateId, raw) + }.also { + initedUpdate = it } } } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/UnknownUpdate.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/UnknownUpdate.kt new file mode 100644 index 0000000000..6e4c166313 --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/UnknownUpdate.kt @@ -0,0 +1,9 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.update + +import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update + +data class UnknownUpdate( + override val updateId: UpdateIdentifier, + override val data: String +) : Update diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt index 46f998d759..b7bbbf5085 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt @@ -4,6 +4,8 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.update.RawUpdate import kotlinx.serialization.* import kotlinx.serialization.internal.StringDescriptor +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonElementSerializer interface Update { val updateId: UpdateIdentifier @@ -24,6 +26,12 @@ internal object UpdateDeserializationStrategy : DeserializationStrategy override fun patch(decoder: Decoder, old: Update): Update = throw UpdateNotSupportedException(descriptor.name) override fun deserialize(decoder: Decoder): Update { - return RawUpdate.serializer().deserialize(decoder).asUpdate + val asJson = JsonElementSerializer.deserialize(decoder) + return Json.nonstrict.fromJson( + RawUpdate.serializer(), + asJson + ).asUpdate( + asJson.toString() + ) } } diff --git a/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt b/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt index 856cbaf3cc..7ab7f2f320 100644 --- a/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt +++ b/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/Webhooks.kt @@ -78,7 +78,7 @@ suspend fun RequestsExecutor.setWebhook( RawUpdate.serializer(), deserialized ) - updatesChannel.send(update.asUpdate) + updatesChannel.send(update.asUpdate(deserialized)) call.respond("Ok") } }