1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-01 23:45:25 +00:00
tgbotapi/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/RawUpdate.kt

100 lines
5.0 KiB
Kotlin
Raw Normal View History

2020-10-04 11:06:30 +00:00
package dev.inmo.tgbotapi.types.update
2018-12-26 08:07:24 +00:00
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.queries.callback.RawCallbackQuery
2020-10-04 11:06:30 +00:00
import dev.inmo.tgbotapi.types.InlineQueries.ChosenInlineResult.RawChosenInlineResult
import dev.inmo.tgbotapi.types.InlineQueries.query.RawInlineQuery
2024-01-04 05:38:30 +00:00
import dev.inmo.tgbotapi.types.boosts.ChatBoostRemoved
import dev.inmo.tgbotapi.types.boosts.ChatBoostUpdated
2022-04-21 18:16:41 +00:00
import dev.inmo.tgbotapi.types.chat.ChatJoinRequest
2023-12-31 10:16:26 +00:00
import dev.inmo.tgbotapi.types.chat.ChatMessageReactionUpdated
import dev.inmo.tgbotapi.types.chat.ChatMessageReactionsCountUpdated
2022-04-21 18:16:41 +00:00
import dev.inmo.tgbotapi.types.chat.member.ChatMemberUpdated
2020-10-04 11:06:30 +00:00
import dev.inmo.tgbotapi.types.message.abstracts.*
import dev.inmo.tgbotapi.types.payments.PreCheckoutQuery
import dev.inmo.tgbotapi.types.payments.ShippingQuery
import dev.inmo.tgbotapi.types.polls.Poll
import dev.inmo.tgbotapi.types.polls.PollAnswer
import dev.inmo.tgbotapi.types.update.abstracts.UnknownUpdate
import dev.inmo.tgbotapi.types.update.abstracts.Update
import kotlinx.serialization.*
2020-01-22 21:59:59 +00:00
import kotlinx.serialization.json.JsonElement
2020-08-18 06:50:11 +00:00
import kotlinx.serialization.json.jsonObject
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: CommonMessage<*>? = null,
@Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class)
private val message: Message? = null,
@Serializable(TelegramBotAPIMessageDeserializeOnlySerializer::class)
private val edited_channel_post: CommonMessage<*>? = 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,
private val my_chat_member: ChatMemberUpdated? = null,
2021-11-08 12:21:55 +00:00
private val chat_member: ChatMemberUpdated? = null,
2023-12-31 10:16:26 +00:00
private val chat_join_request: ChatJoinRequest? = null,
private val message_reaction: ChatMessageReactionUpdated? = null,
2024-01-04 05:38:30 +00:00
private val message_reaction_count: ChatMessageReactionsCountUpdated? = null,
private val chat_boost: ChatBoostUpdated? = null,
private val removed_chat_boost: ChatBoostRemoved? = 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 {
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)
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)
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)
my_chat_member != null -> MyChatMemberUpdatedUpdate(updateId, my_chat_member)
chat_member != null -> CommonChatMemberUpdatedUpdate(updateId, chat_member)
2021-11-08 12:21:55 +00:00
chat_join_request != null -> ChatJoinRequestUpdate(updateId, chat_join_request)
2023-12-31 10:16:26 +00:00
message_reaction != null -> ChatMessageReactionUpdatedUpdate(updateId, message_reaction)
message_reaction_count != null -> ChatMessageReactionsCountUpdatedUpdate(updateId, message_reaction_count)
2024-01-04 05:38:30 +00:00
chat_boost != null -> ChatBoostUpdatedUpdate(updateId, chat_boost)
removed_chat_boost != null -> ChatBoostRemovedUpdate(updateId, removed_chat_boost)
2020-06-27 03:43:24 +00:00
else -> UnknownUpdate(
updateId,
raw
)
}
} catch (e: NotImplementedError) {
UnknownUpdate(
updateId,
raw
)
} catch (e: SerializationException) {
UnknownUpdate(
updateId,
raw
)
2020-01-22 21:31:56 +00:00
}.also {
initedUpdate = it
2018-12-26 08:07:24 +00:00
}
}
}