1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-29 13:38:43 +00:00

add support of menu buttons

This commit is contained in:
InsanusMokrassar 2022-04-17 19:13:43 +06:00
parent 63bcc4bcf8
commit 7bac4a3a32
13 changed files with 284 additions and 3 deletions

View File

@ -0,0 +1,15 @@
package dev.inmo.tgbotapi.extensions.api.chat.get
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.chat.get.GetChatMenuButton
import dev.inmo.tgbotapi.requests.chat.modify.*
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.chat.abstracts.PrivateChat
suspend fun TelegramBot.getChatMenuButton(
chatId: ChatId
) = execute(GetChatMenuButton(chatId))
suspend fun TelegramBot.getChatMenuButton(
chat: PrivateChat
) = getChatMenuButton(chat.id)

View File

@ -0,0 +1,8 @@
package dev.inmo.tgbotapi.extensions.api.chat.get
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.chat.get.GetDefaultChatMenuButton
import dev.inmo.tgbotapi.requests.chat.modify.SetDefaultChatMenuButton
import dev.inmo.tgbotapi.types.MenuButton
suspend fun TelegramBot.getDefaultChatMenuButton() = execute(GetDefaultChatMenuButton)

View File

@ -0,0 +1,16 @@
package dev.inmo.tgbotapi.extensions.api.chat.modify
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.chat.modify.*
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.chat.abstracts.PrivateChat
suspend fun TelegramBot.setChatMenuButton(
chatId: ChatId,
menuButton: MenuButton
) = execute(SetChatMenuButton(chatId, menuButton))
suspend fun TelegramBot.setChatMenuButton(
chat: PrivateChat,
menuButton: MenuButton
) = setChatMenuButton(chat.id, menuButton)

View File

@ -0,0 +1,9 @@
package dev.inmo.tgbotapi.extensions.api.chat.modify
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.chat.modify.SetDefaultChatMenuButton
import dev.inmo.tgbotapi.types.MenuButton
suspend fun TelegramBot.setDefaultChatMenuButton(
menuButton: MenuButton
) = execute(SetDefaultChatMenuButton(menuButton))

View File

@ -2,6 +2,6 @@ package dev.inmo.tgbotapi.CommonAbstracts.types
import dev.inmo.tgbotapi.types.ChatIdentifier import dev.inmo.tgbotapi.types.ChatIdentifier
interface ChatRequest { interface ChatRequest : OptionalChatRequest {
val chatId: ChatIdentifier override val chatId: ChatIdentifier
} }

View File

@ -0,0 +1,7 @@
package dev.inmo.tgbotapi.CommonAbstracts.types
import dev.inmo.tgbotapi.types.ChatIdentifier
interface OptionalChatRequest {
val chatId: ChatIdentifier?
}

View File

@ -0,0 +1,21 @@
package dev.inmo.tgbotapi.requests.chat.get
import dev.inmo.tgbotapi.CommonAbstracts.types.ChatRequest
import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
@Serializable
data class GetChatMenuButton(
@SerialName(chatIdField)
override val chatId: ChatIdentifier
) : ChatRequest, SimpleRequest<MenuButton> {
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
override fun method(): String = GetDefaultChatMenuButton.method()
override val resultDeserializer: DeserializationStrategy<MenuButton>
get() = MenuButtonSerializer
}

View File

@ -0,0 +1,21 @@
package dev.inmo.tgbotapi.requests.chat.get
import dev.inmo.tgbotapi.CommonAbstracts.types.ChatRequest
import dev.inmo.tgbotapi.CommonAbstracts.types.OptionalChatRequest
import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
@Serializable
object GetDefaultChatMenuButton : OptionalChatRequest, SimpleRequest<MenuButton> {
override val chatId: ChatIdentifier?
get() = null
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
override fun method(): String = "getChatMenuButton"
override val resultDeserializer: DeserializationStrategy<MenuButton>
get() = MenuButtonSerializer
}

View File

@ -0,0 +1,24 @@
package dev.inmo.tgbotapi.requests.chat.modify
import dev.inmo.tgbotapi.CommonAbstracts.types.ChatRequest
import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
@Serializable
data class SetChatMenuButton(
@SerialName(chatIdField)
override val chatId: ChatIdentifier,
@Serializable(MenuButtonSerializer::class)
@SerialName(menuButtonField)
val menuButton: MenuButton
) : ChatRequest, SimpleRequest<Boolean> {
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
override fun method(): String = SetDefaultChatMenuButton.method()
override val resultDeserializer: DeserializationStrategy<Boolean>
get() = Boolean.serializer()
}

View File

@ -0,0 +1,25 @@
package dev.inmo.tgbotapi.requests.chat.modify
import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
@Serializable
data class SetDefaultChatMenuButton(
@Serializable(MenuButtonSerializer::class)
@SerialName(menuButtonField)
val menuButton: MenuButton
) : SimpleRequest<Boolean> {
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
override fun method(): String = Companion.method()
override val resultDeserializer: DeserializationStrategy<Boolean>
get() = Boolean.serializer()
companion object {
fun method() = "setChatMenuButton"
}
}

View File

@ -439,3 +439,5 @@ const val passportRegistrationField = "passport_registration"
const val temporaryRegistrationField = "temporary_registration" const val temporaryRegistrationField = "temporary_registration"
const val buttonTextField = "button_text" const val buttonTextField = "button_text"
const val webAppField = "web_app"
const val menuButtonField = "menu_button"

View File

@ -0,0 +1,122 @@
package dev.inmo.tgbotapi.types
import dev.inmo.tgbotapi.types.webapps.WebAppInfo
import dev.inmo.tgbotapi.utils.RiskFeature
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.*
@Serializable(MenuButtonSerializer::class)
sealed interface MenuButton {
@Required
val type: String
@Serializable
object Commands : MenuButton {
@Required
override val type: String
get() = "commands"
}
@Serializable
data class WebApp(
val text: String,
@SerialName(webAppField)
val webApp: WebAppInfo
) : MenuButton {
@Required
override val type: String
get() = Companion.type
companion object {
val type: String
get() = "web_app"
}
}
@Serializable
object Default : MenuButton {
@Required
override val type: String
get() = "default"
}
@Serializable
@RiskFeature
data class Unknown (
override val type: String,
val rawJson: JsonElement
) : MenuButton
companion object {
fun serializer(): KSerializer<MenuButton> = MenuButtonSerializer
}
}
@Serializable
internal data class MenuButtonSurrogate(
val type: String,
val text: String? = null,
@SerialName(webAppField)
val webApp: WebAppInfo? = null,
val srcJsonElement: JsonElement? = null
)
@Serializer(MenuButton::class)
object MenuButtonSerializer : KSerializer<MenuButton> {
override val descriptor: SerialDescriptor
get() = MenuButtonSurrogate.serializer().descriptor
override fun deserialize(decoder: Decoder): MenuButton {
val surrogate = if (decoder is JsonDecoder) {
val json = JsonElement.serializer().deserialize(decoder)
runCatching {
decoder.json.decodeFromJsonElement(MenuButtonSurrogate.serializer(), json)
}.onFailure {
return MenuButton.Unknown(
runCatching { json.jsonObject[typeField] ?.jsonPrimitive ?.content }.getOrNull() ?: "",
json
)
}.getOrThrow().copy(
srcJsonElement = json
)
} else {
MenuButtonSurrogate.serializer().deserialize(decoder)
}
return when (surrogate.type) {
MenuButton.Commands.type -> MenuButton.Commands
MenuButton.Default.type -> MenuButton.Default
MenuButton.WebApp.type -> if (surrogate.text != null && surrogate.webApp != null) {
MenuButton.WebApp(surrogate.text, surrogate.webApp)
} else {
null
}
else -> null
} ?: MenuButton.Unknown(
surrogate.type,
surrogate.srcJsonElement ?: buildJsonObject { }
)
}
override fun serialize(encoder: Encoder, value: MenuButton) {
encoder.encodeSerializableValue(
MenuButtonSurrogate.serializer(),
when (value) {
MenuButton.Default,
MenuButton.Commands -> MenuButtonSurrogate(value.type)
is MenuButton.WebApp -> MenuButtonSurrogate(value.type, value.text, value.webApp)
is MenuButton.Unknown -> {
encoder.encodeSerializableValue(
JsonElement.serializer(),
value.rawJson
)
return
}
}
)
}
}

View File

@ -0,0 +1,11 @@
package dev.inmo.tgbotapi.types.webapps
import dev.inmo.tgbotapi.types.urlField
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class WebAppInfo(
@SerialName(urlField)
val url: String
)