1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 16:23:48 +00:00

InlineKeyboardButton as sealed class

This commit is contained in:
InsanusMokrassar 2019-06-02 22:02:12 +08:00
parent 85b3f86746
commit 5e8bf82358
9 changed files with 90 additions and 68 deletions

View File

@ -5,6 +5,7 @@
* `LoginURL` and `LoginURLInlineKeyboardButton` has been added * `LoginURL` and `LoginURLInlineKeyboardButton` has been added
* `replyMarkup` field was added to the `CommonMessage` objects via `AbleToBeMarkedUp` interface * `replyMarkup` field was added to the `CommonMessage` objects via `AbleToBeMarkedUp` interface
* `SwitchInlineQueryCurrentChatInlineKeyboardButton#switchInlineQueryCurrentChat` field fixed * `SwitchInlineQueryCurrentChatInlineKeyboardButton#switchInlineQueryCurrentChat` field fixed
* `InlineKeyboardButton` now is sealed class and all its possible realisations are inside of its class file
## 0.15.0 ## 0.15.0

View File

@ -1,16 +1,8 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons package com.github.insanusmokrassar.TelegramBotAPI.types
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import kotlinx.serialization.SerialName import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable
data class LoginURLInlineKeyboardButton(
override val text: String,
@SerialName(loginUrlField)
val loginUrl: LoginURL
) : InlineKeyboardButton
@Serializable @Serializable
data class LoginURL( data class LoginURL(
@SerialName(urlField) @SerialName(urlField)

View File

@ -1,14 +0,0 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons
import com.github.insanusmokrassar.TelegramBotAPI.types.callbackDataField
import com.github.insanusmokrassar.TelegramBotAPI.types.textField
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class CallbackDataInlineKeyboardButton(
@SerialName(textField)
override val text: String,
@SerialName(callbackDataField)
val callbackData: String
) : InlineKeyboardButton

View File

@ -1,10 +1,51 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import kotlinx.serialization.* import kotlinx.serialization.*
@Serializable(InlineKeyboardButtonSerializer::class) @Serializable(InlineKeyboardButtonSerializer::class)
interface InlineKeyboardButton { sealed class InlineKeyboardButton {
val text: String abstract val text: String
} }
object InlineKeyboardButtonSerializer : KSerializer<InlineKeyboardButton> by ContextSerializer(InlineKeyboardButton::class) //TODO:: add check that this button first in a row (it MUST be first in a row)
@Serializable
data class PayInlineKeyboardButton(
override val text: String,
val pay: Boolean
) : InlineKeyboardButton()
@Serializable
data class CallbackDataInlineKeyboardButton(
@SerialName(textField)
override val text: String,
@SerialName(callbackDataField)
val callbackData: String
) : InlineKeyboardButton()
@Serializable
data class LoginURLInlineKeyboardButton(
override val text: String,
@SerialName(loginUrlField)
val loginUrl: LoginURL
) : InlineKeyboardButton()
@Serializable
data class SwitchInlineQueryCurrentChatInlineKeyboardButton(
override val text: String,
@SerialName(switchInlineQueryCurrentChatField)
val switchInlineQueryCurrentChat: String
) : InlineKeyboardButton()
@Serializable
data class SwitchInlineQueryInlineKeyboardButton(
override val text: String,
@SerialName("switch_inline_query")
val switchInlineQuery: String
) : InlineKeyboardButton()
@Serializable
data class URLInlineKeyboardButton(
override val text: String,
val url: String
) : InlineKeyboardButton()

View File

@ -0,0 +1,44 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons
import kotlinx.serialization.*
import kotlinx.serialization.internal.StringDescriptor
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElementSerializer
object InlineKeyboardButtonSerializer : KSerializer<InlineKeyboardButton> {
override val descriptor: SerialDescriptor = StringDescriptor.withName("com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons.InlineKeyboardButton")
private val serializers = listOf(
CallbackDataInlineKeyboardButton.serializer(),
LoginURLInlineKeyboardButton.serializer(),
PayInlineKeyboardButton.serializer(),
SwitchInlineQueryInlineKeyboardButton.serializer(),
SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer(),
URLInlineKeyboardButton.serializer()
)
override fun deserialize(decoder: Decoder): InlineKeyboardButton {
val json = JsonElementSerializer.deserialize(decoder)
serializers.forEach {
try {
return Json.nonstrict.fromJson(it, json)
} catch (e: SerializationException) {
e
}
}
throw IllegalArgumentException("There is no known type of serializer for \"$json\" as inline button")
}
override fun serialize(encoder: Encoder, obj: InlineKeyboardButton) {
when (obj) {
is CallbackDataInlineKeyboardButton -> CallbackDataInlineKeyboardButton.serializer().serialize(encoder, obj)
is LoginURLInlineKeyboardButton -> LoginURLInlineKeyboardButton.serializer().serialize(encoder, obj)
is PayInlineKeyboardButton -> PayInlineKeyboardButton.serializer().serialize(encoder, obj)
is SwitchInlineQueryInlineKeyboardButton -> SwitchInlineQueryInlineKeyboardButton.serializer().serialize(encoder, obj)
is SwitchInlineQueryCurrentChatInlineKeyboardButton -> SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer().serialize(encoder, obj)
is URLInlineKeyboardButton -> URLInlineKeyboardButton.serializer().serialize(encoder, obj)
}
}
}

View File

@ -1,10 +0,0 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons
import kotlinx.serialization.Serializable
//TODO:: add check that this button first in a row (it MUST be first in a row)
@Serializable
data class PayInlineKeyboardButton(
override val text: String,
val pay: Boolean
) : InlineKeyboardButton

View File

@ -1,12 +0,0 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons
import com.github.insanusmokrassar.TelegramBotAPI.types.switchInlineQueryCurrentChatField
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class SwitchInlineQueryCurrentChatInlineKeyboardButton(
override val text: String,
@SerialName(switchInlineQueryCurrentChatField)
val switchInlineQueryCurrentChat: String
) : InlineKeyboardButton

View File

@ -1,11 +0,0 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class SwitchInlineQueryInlineKeyboardButton(
override val text: String,
@SerialName("switch_inline_query")
val switchInlineQuery: String
) : InlineKeyboardButton

View File

@ -1,9 +0,0 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons
import kotlinx.serialization.Serializable
@Serializable
data class URLInlineKeyboardButton(
override val text: String,
val url: String
) : InlineKeyboardButton