diff --git a/CHANGELOG.md b/CHANGELOG.md index ca6520b706..c796a42cba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,8 @@ * `CallbackGameInlineKeyboardButton` was added ([Issue-79](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/79), [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` ### 0.26.2 diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt index 11c7ca255b..875655bc1f 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt @@ -4,12 +4,19 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.games.CallbackGame import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import kotlinx.serialization.json.JsonObject @Serializable(InlineKeyboardButtonSerializer::class) sealed class InlineKeyboardButton { abstract val text: String } +@Serializable +data class UnknownInlineKeyboardButton internal constructor( + override val text: String, + val rawData: JsonObject +) : InlineKeyboardButton() + @Serializable data class PayInlineKeyboardButton( override val text: String, diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt index 05a604fea6..722c2fd07a 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt @@ -3,8 +3,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardB import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.utils.nonstrictJsonFormat import kotlinx.serialization.* -import kotlinx.serialization.json.JsonElementSerializer -import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.* internal object InlineKeyboardButtonSerializer : KSerializer { override val descriptor: SerialDescriptor = SerialDescriptor( @@ -12,7 +11,7 @@ internal object InlineKeyboardButtonSerializer : KSerializer { + private fun resolveSerializer(json: JsonObject): KSerializer? { return when { json[callbackDataField] != null -> CallbackDataInlineKeyboardButton.serializer() json[callbackGameField] != null -> CallbackGameInlineKeyboardButton.serializer() @@ -21,14 +20,16 @@ internal object InlineKeyboardButtonSerializer : KSerializer SwitchInlineQueryInlineKeyboardButton.serializer() json[switchInlineQueryCurrentChatField] != null -> SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer() json[urlField] != null -> URLInlineKeyboardButton.serializer() - else -> throw IllegalArgumentException("Can't find correct serializer for inline button serialized as $json") + else -> null } } override fun deserialize(decoder: Decoder): InlineKeyboardButton { - val json = JsonElementSerializer.deserialize(decoder) + val json = JsonObjectSerializer.deserialize(decoder) - return nonstrictJsonFormat.fromJson(resolveSerializer(json.jsonObject), json) + return resolveSerializer(json) ?.let { + nonstrictJsonFormat.fromJson(it, json) + } ?: UnknownInlineKeyboardButton(json[textField] ?.contentOrNull ?: "", json) } override fun serialize(encoder: Encoder, value: InlineKeyboardButton) {