1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-09-14 21:00:15 +00:00

add support of WebAppInfo

This commit is contained in:
2022-04-17 20:18:09 +06:00
parent 2c71377058
commit c19bccc7d1
6 changed files with 69 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ package dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.games.CallbackGame
import dev.inmo.tgbotapi.types.webapps.WebAppInfo
import kotlinx.serialization.*
import kotlinx.serialization.json.*
@@ -120,3 +121,14 @@ data class URLInlineKeyboardButton(
@SerialName(urlField)
val url: String
) : InlineKeyboardButton
/**
* Button with [WebAppInfo]. Web App will be launched when the button is pressed. The Web App will be able to send a
* `web_app_data` service message. **Available in private chats only**.
*/
@Serializable
data class WebAppInlineKeyboardButton(
override val text: String,
@SerialName(webAppField)
val webApp: WebAppInfo
) : InlineKeyboardButton

View File

@@ -48,6 +48,7 @@ object InlineKeyboardButtonSerializer : KSerializer<InlineKeyboardButton> {
is SwitchInlineQueryInlineKeyboardButton -> SwitchInlineQueryInlineKeyboardButton.serializer().serialize(encoder, value)
is SwitchInlineQueryCurrentChatInlineKeyboardButton -> SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer().serialize(encoder, value)
is URLInlineKeyboardButton -> URLInlineKeyboardButton.serializer().serialize(encoder, value)
is WebAppInlineKeyboardButton -> WebAppInlineKeyboardButton.serializer().serialize(encoder, value)
is CallbackGameInlineKeyboardButton -> CallbackGameInlineKeyboardButton.serializer().serialize(encoder, value)
is UnknownInlineKeyboardButton -> JsonElement.serializer().serialize(encoder, value.rawData)
}

View File

@@ -1,6 +1,7 @@
package dev.inmo.tgbotapi.types.buttons
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.webapps.WebAppInfo
import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.nonstrictJsonFormat
import kotlinx.serialization.*
@@ -63,10 +64,22 @@ data class RequestLocationKeyboardButton(
override val text: String
) : KeyboardButton {
@SerialName(requestLocationField)
@EncodeDefault
@Required
val requestLocation: Boolean = true
}
/**
* Private chats only. Description of the Web App that will be launched when the user presses the button. The Web App
* will be able to send an arbitrary message on behalf of the user using the method `answerWebAppQuery`. Available only
* in private chats between a user and the bot.
*/
@Serializable
data class WebAppKeyboardButton(
override val text: String,
@SerialName(webAppField)
val webApp: WebAppInfo
) : KeyboardButton
/**
* Private chats only. When user will tap on this button, he will be asked for the poll with [requestPoll] options. You will be able
* to catch this poll in updates and data using [dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onPoll] in
@@ -97,6 +110,13 @@ object KeyboardButtonSerializer : KSerializer<KeyboardButton> {
asJson is JsonObject && asJson[requestLocationField] != null -> RequestLocationKeyboardButton(
asJson[textField]!!.jsonPrimitive.content
)
asJson is JsonObject && asJson[webAppField] != null -> WebAppKeyboardButton(
asJson[textField]!!.jsonPrimitive.content,
nonstrictJsonFormat.decodeFromJsonElement(
WebAppInfo.serializer(),
asJson[webAppField]!!
)
)
asJson is JsonObject && asJson[requestPollField] != null -> RequestPollKeyboardButton(
asJson[textField]!!.jsonPrimitive.content,
nonstrictJsonFormat.decodeFromJsonElement(
@@ -119,6 +139,7 @@ object KeyboardButtonSerializer : KSerializer<KeyboardButton> {
when (value) {
is RequestContactKeyboardButton -> RequestContactKeyboardButton.serializer().serialize(encoder, value)
is RequestLocationKeyboardButton -> RequestLocationKeyboardButton.serializer().serialize(encoder, value)
is WebAppKeyboardButton -> WebAppKeyboardButton.serializer().serialize(encoder, value)
is RequestPollKeyboardButton -> RequestPollKeyboardButton.serializer().serialize(encoder, value)
is SimpleKeyboardButton -> encoder.encodeString(value.text)
is UnknownKeyboardButton -> JsonElement.serializer().serialize(encoder, nonstrictJsonFormat.parseToJsonElement(value.raw))