Add Bot API 10.3 Reply markup support

This commit is contained in:
InsanusMokrassar committed 2026-08-25 12:33:45 +06:00
1 parent 752689447b
commit efd4667ffa
15 files changed
+247 -36

No files matched your search

@@ -776,6 +776,7 @@ const val disableEditMessageField = "disable_edit_message"
const val scoreField = "score"
const val forceField = "force"
const val forceReplyField = "force_reply"
const val disabledField = "disabled"
const val regularPollType = "regular"
const val quizPollType = "quiz"
@@ -24,6 +24,34 @@ sealed interface InlineKeyboardButton {
val iconCustomEmojiId: CustomEmojiId?
}
/**
* A disabled inline keyboard button action. Telegram Bot API currently defines no fields for this object.
*
* @see DisabledInlineKeyboardButton
* @see https://core.telegram.org/bots/api#disabledbutton
*/
@Serializable
data object DisabledButton
/**
* An inline keyboard button which does nothing when pressed.
*
* @see https://core.telegram.org/bots/api#inlinekeyboardbutton
*/
@Serializable
data class DisabledInlineKeyboardButton(
@SerialName(textField)
override val text: String,
@SerialName(iconCustomEmojiIdField)
override val iconCustomEmojiId: CustomEmojiId? = null,
@SerialName(styleField)
override val style: KeyboardButtonStyle? = null
) : InlineKeyboardButton {
@SerialName(disabledField)
@EncodeDefault
private val disabled = DisabledButton
}
@Serializable
data class UnknownInlineKeyboardButton (
val rawData: JsonElement
@@ -24,6 +24,7 @@ object InlineKeyboardButtonSerializer : KSerializer<InlineKeyboardButton> {
private fun resolveSerializer(json: JsonObject): KSerializer<out InlineKeyboardButton>? {
return when {
json[disabledField] != null -> DisabledInlineKeyboardButton.serializer()
json[callbackDataField] != null -> CallbackDataInlineKeyboardButton.serializer()
json[callbackGameField] != null -> CallbackGameInlineKeyboardButton.serializer()
json[loginUrlField] != null -> LoginURLInlineKeyboardButton.serializer()
@@ -47,6 +48,7 @@ object InlineKeyboardButtonSerializer : KSerializer<InlineKeyboardButton> {
override fun serialize(encoder: Encoder, value: InlineKeyboardButton) {
when (value) {
is DisabledInlineKeyboardButton -> DisabledInlineKeyboardButton.serializer().serialize(encoder, value)
is CallbackDataInlineKeyboardButton -> CallbackDataInlineKeyboardButton.serializer().serialize(encoder, value)
is LoginURLInlineKeyboardButton -> LoginURLInlineKeyboardButton.serializer().serialize(encoder, value)
is PayInlineKeyboardButton -> PayInlineKeyboardButton.serializer().serialize(encoder, value)
@@ -2,6 +2,7 @@ package dev.inmo.tgbotapi.types.buttons
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.InlineKeyboardButton
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.PayInlineKeyboardButton
import dev.inmo.tgbotapi.types.forceReplyField
import dev.inmo.tgbotapi.types.inlineKeyboardField
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@@ -9,7 +10,13 @@ import kotlinx.serialization.Serializable
@Serializable
data class InlineKeyboardMarkup(
@SerialName(inlineKeyboardField)
val keyboard: Matrix<InlineKeyboardButton>
val keyboard: Matrix<InlineKeyboardButton>,
/**
* Requests a reply interface for the message. Telegram does not allow changing this value when editing the inline
* keyboard.
*/
@SerialName(forceReplyField)
val forceReply: Boolean? = null
) : KeyboardMarkup {
init {
val isTherePayButton = keyboard.any { it ->
@@ -28,20 +35,22 @@ data class InlineKeyboardMarkup(
operator fun plus(other: InlineKeyboardMarkup): InlineKeyboardMarkup {
return InlineKeyboardMarkup(
keyboard + other.keyboard
keyboard = keyboard + other.keyboard,
forceReply = forceReply ?.or(other.forceReply ?: false) ?: other.forceReply
)
}
operator fun minus(other: InlineKeyboardMarkup): InlineKeyboardMarkup {
val otherButtons = other.keyboard.flatten()
return InlineKeyboardMarkup(
keyboard.mapNotNull { row ->
keyboard = keyboard.mapNotNull { row ->
row.filter { button ->
button !in otherButtons
}.takeIf {
it.isNotEmpty()
}
}
},
forceReply = forceReply
)
}
}
@@ -15,7 +15,12 @@ data class ReplyKeyboardMarkup(
val inputFieldPlaceholder: String? = null,
val selective: Boolean? = null,
@SerialName(isPersistentField)
val persistent: Boolean? = null
val persistent: Boolean? = null,
/**
* Requests a reply interface for the message.
*/
@SerialName(forceReplyField)
val forceReply: Boolean? = null
) : KeyboardMarkup {
init {
if (inputFieldPlaceholder != null && inputFieldPlaceholder.length !in inputFieldPlaceholderLimit) {
@@ -31,6 +36,7 @@ data class ReplyKeyboardMarkup(
inputFieldPlaceholder = inputFieldPlaceholder ?.plus(other.inputFieldPlaceholder ?.let { placeholderDelimiter + it } ?: "") ?: other.inputFieldPlaceholder,
selective = selective ?.or(other.selective ?: false) ?: other.selective,
persistent = persistent ?.or(other.persistent ?: false) ?: other.persistent,
forceReply = forceReply ?.or(other.forceReply ?: false) ?: other.forceReply,
)
}
@@ -7,6 +7,15 @@ import dev.inmo.tgbotapi.types.buttons.KeyboardButtonStyle
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.*
import dev.inmo.tgbotapi.types.webapps.WebAppInfo
/**
* Creates [DisabledInlineKeyboardButton].
*/
fun disabledInlineButton(
text: String,
iconCustomEmojiId: CustomEmojiId? = null,
style: KeyboardButtonStyle? = null
) = DisabledInlineKeyboardButton(text, iconCustomEmojiId, style)
/**
* Creates [PayInlineKeyboardButton]
*/
@@ -0,0 +1,59 @@
package dev.inmo.tgbotapi.types
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.DisabledInlineKeyboardButton
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.InlineKeyboardButton
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
import dev.inmo.tgbotapi.types.buttons.ReplyKeyboardMarkup
import dev.inmo.tgbotapi.types.buttons.SimpleKeyboardButton
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonObject
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class ReplyMarkupSerializationTest {
private val json = Json { encodeDefaults = false }
@Test
fun disabledInlineKeyboardButtonUsesEmptyDisabledObjectAndRoundTrips() {
val value: InlineKeyboardButton = DisabledInlineKeyboardButton("Unavailable")
val encoded = json.encodeToString(value)
val encodedObject = json.parseToJsonElement(encoded).jsonObject
assertEquals("{}", encodedObject.getValue(disabledField).toString())
assertEquals(value, json.decodeFromString<InlineKeyboardButton>(encoded))
}
@Test
fun inlineKeyboardMarkupSerializesForceReplyAndPreservesItWhenCombined() {
val value = InlineKeyboardMarkup(
keyboard = listOf(listOf(DisabledInlineKeyboardButton("Unavailable"))),
forceReply = true
)
val encodedObject = json.parseToJsonElement(json.encodeToString(value)).jsonObject
assertEquals(true, encodedObject.getValue(forceReplyField).toString().toBoolean())
assertEquals(value, json.decodeFromString<InlineKeyboardMarkup>(encodedObject.toString()))
assertTrue((value + InlineKeyboardMarkup(listOf(listOf(DisabledInlineKeyboardButton("Other"))))).forceReply == true)
assertTrue((value - InlineKeyboardMarkup(listOf(listOf(DisabledInlineKeyboardButton("Unavailable"))))).forceReply == true)
}
@Test
fun replyKeyboardMarkupSerializesForceReplyAndPreservesItWhenCombined() {
val value = ReplyKeyboardMarkup(
keyboard = listOf(listOf(SimpleKeyboardButton("Reply"))),
forceReply = true
)
val encodedObject = json.parseToJsonElement(json.encodeToString(value)).jsonObject
assertEquals(true, encodedObject.getValue(forceReplyField).toString().toBoolean())
assertEquals(value, json.decodeFromString<ReplyKeyboardMarkup>(encodedObject.toString()))
assertTrue((value + ReplyKeyboardMarkup(listOf(listOf(SimpleKeyboardButton("Other"))))).forceReply == true)
assertTrue((value - ReplyKeyboardMarkup(listOf(listOf(SimpleKeyboardButton("Reply"))))).forceReply == true)
}
}