UnknownUpdate

This commit is contained in:
InsanusMokrassar 2020-01-23 03:31:56 +06:00
parent 4182d66f6e
commit 961fa65415
5 changed files with 29 additions and 5 deletions

View File

@ -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

View File

@ -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
}
}
}

View File

@ -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

View File

@ -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<Update>
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()
)
}
}

View File

@ -78,7 +78,7 @@ suspend fun RequestsExecutor.setWebhook(
RawUpdate.serializer(),
deserialized
)
updatesChannel.send(update.asUpdate)
updatesChannel.send(update.asUpdate(deserialized))
call.respond("Ok")
}
}