1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-03 00:15:27 +00:00
tgbotapi/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/RawUpdate.kt

70 lines
3.7 KiB
Kotlin
Raw Normal View History

2018-12-26 08:07:24 +00:00
package com.github.insanusmokrassar.TelegramBotAPI.types.update
import com.github.insanusmokrassar.TelegramBotAPI.types.CallbackQuery.RawCallbackQuery
import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.ChosenInlineResult.RawChosenInlineResult
import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.query.RawInlineQuery
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializeOnlySerializer
2018-12-26 08:07:24 +00:00
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.PreCheckoutQuery
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.ShippingQuery
2019-04-16 07:56:04 +00:00
import com.github.insanusmokrassar.TelegramBotAPI.types.polls.Poll
2020-01-23 11:29:09 +00:00
import com.github.insanusmokrassar.TelegramBotAPI.types.polls.PollAnswer
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UnknownUpdateType
2018-12-26 08:07:24 +00:00
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.types.updateIdField
2019-12-06 10:46:38 +00:00
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
2020-01-22 21:59:59 +00:00
import kotlinx.serialization.json.JsonElement
2018-12-26 08:07:24 +00:00
@Serializable
2019-08-17 16:08:36 +00:00
internal data class RawUpdate constructor(
2018-12-26 08:07:24 +00:00
@SerialName(updateIdField)
val updateId: UpdateIdentifier,
@Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class)
private val edited_message: Message? = null,
@Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class)
private val message: Message? = null,
@Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class)
private val edited_channel_post: Message? = null,
@Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class)
private val channel_post: Message? = null,
2019-04-13 02:16:40 +00:00
private val inline_query: RawInlineQuery? = null,
private val chosen_inline_result: RawChosenInlineResult? = null,
private val callback_query: RawCallbackQuery? = null,
private val shipping_query: ShippingQuery? = null,
2019-04-16 07:56:04 +00:00
private val pre_checkout_query: PreCheckoutQuery? = null,
2020-01-23 11:29:09 +00:00
private val poll: Poll? = null,
private val poll_answer: PollAnswer? = null
2018-12-26 08:07:24 +00:00
) {
2020-01-22 21:31:56 +00:00
private var initedUpdate: Update? = null
/**
* @return One of children of [Update] interface or null in case of unknown type of update
*/
2020-01-22 21:59:59 +00:00
fun asUpdate(raw: JsonElement): Update {
2020-01-22 21:31:56 +00:00
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)
2018-12-26 08:07:24 +00:00
chosen_inline_result != null -> ChosenInlineResultUpdate(updateId, chosen_inline_result.asChosenInlineResult)
inline_query != null -> InlineQueryUpdate(updateId, inline_query.asInlineQuery)
2020-01-22 21:59:59 +00:00
callback_query != null -> CallbackQueryUpdate(
updateId,
callback_query.asCallbackQuery(raw.jsonObject["callback_query"].toString())
)
2018-12-26 08:07:24 +00:00
shipping_query != null -> ShippingQueryUpdate(updateId, shipping_query)
pre_checkout_query != null -> PreCheckoutQueryUpdate(updateId, pre_checkout_query)
2019-04-16 07:56:04 +00:00
poll != null -> PollUpdate(updateId, poll)
2020-01-23 11:29:09 +00:00
poll_answer != null -> PollAnswerUpdate(updateId, poll_answer)
else -> UnknownUpdateType(
2020-01-22 21:35:56 +00:00
updateId,
2020-01-22 21:59:59 +00:00
raw.toString()
2020-01-22 21:35:56 +00:00
)
2020-01-22 21:31:56 +00:00
}.also {
initedUpdate = it
2018-12-26 08:07:24 +00:00
}
}
}