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

fixes in keyboards

This commit is contained in:
InsanusMokrassar 2023-02-05 21:47:15 +06:00
parent 687f9e95fa
commit ae8ef0dd3c
7 changed files with 250 additions and 252 deletions

View File

@ -2,6 +2,9 @@ package dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons
import dev.inmo.tgbotapi.utils.internal.ClassCastsIncluded import dev.inmo.tgbotapi.utils.internal.ClassCastsIncluded
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.buttons.KeyboardButton
import dev.inmo.tgbotapi.types.buttons.KeyboardButtonRequestChat
import dev.inmo.tgbotapi.types.buttons.KeyboardButtonRequestUser
import dev.inmo.tgbotapi.types.games.CallbackGame import dev.inmo.tgbotapi.types.games.CallbackGame
import dev.inmo.tgbotapi.types.webapps.WebAppInfo import dev.inmo.tgbotapi.types.webapps.WebAppInfo
import kotlinx.serialization.* import kotlinx.serialization.*
@ -134,3 +137,39 @@ data class WebAppInlineKeyboardButton(
@SerialName(webAppField) @SerialName(webAppField)
val webApp: WebAppInfo val webApp: WebAppInfo
) : InlineKeyboardButton ) : InlineKeyboardButton
/**
* Private chats only. When user will tap on this button, he will be asked for the chat with [requestChat] options. You
* will be able to catch this [ChatId] in updates and data using
* [dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onUserShared] in case you are using Behaviour
* Builder OR with [dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter.messagesFlow]
* and [kotlinx.coroutines.flow.filterIsInstance].
*
* In case you will use [dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onUserShared] it is
* recommended to use [kotlinx.coroutines.flow.Flow] [kotlinx.coroutines.flow.filter] with checking of incoming
* [dev.inmo.tgbotapi.types.request.UserShared.requestId]
*/
@Serializable
data class RequestUserInlineKeyboardButton(
override val text: String,
@SerialName(requestUserField)
val requestUser: KeyboardButtonRequestUser
) : InlineKeyboardButton
/**
* Private chats only. When user will tap on this button, he will be asked for the chat with [requestChat] options. You
* will be able to catch this [ChatId] in updates and data using
* [dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChatShared] in case you are using Behaviour
* Builder OR with [dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter.messagesFlow]
* and [kotlinx.coroutines.flow.filterIsInstance].
*
* In case you will use [dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChatShared] it is
* recommended to use [kotlinx.coroutines.flow.Flow] [kotlinx.coroutines.flow.filter] with checking of incoming
* [dev.inmo.tgbotapi.types.request.ChatShared.requestId]
*/
@Serializable
data class RequestChatInlineKeyboardButton(
override val text: String,
@SerialName(requestChatField)
val requestChat: KeyboardButtonRequestChat
) : InlineKeyboardButton

View File

@ -28,6 +28,8 @@ object InlineKeyboardButtonSerializer : KSerializer<InlineKeyboardButton> {
json[switchInlineQueryField] != null -> SwitchInlineQueryInlineKeyboardButton.serializer() json[switchInlineQueryField] != null -> SwitchInlineQueryInlineKeyboardButton.serializer()
json[switchInlineQueryCurrentChatField] != null -> SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer() json[switchInlineQueryCurrentChatField] != null -> SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer()
json[urlField] != null -> URLInlineKeyboardButton.serializer() json[urlField] != null -> URLInlineKeyboardButton.serializer()
json[requestUserField] != null -> RequestUserInlineKeyboardButton.serializer()
json[requestChatField] != null -> RequestChatInlineKeyboardButton.serializer()
else -> null else -> null
} }
} }
@ -50,6 +52,8 @@ object InlineKeyboardButtonSerializer : KSerializer<InlineKeyboardButton> {
is URLInlineKeyboardButton -> URLInlineKeyboardButton.serializer().serialize(encoder, value) is URLInlineKeyboardButton -> URLInlineKeyboardButton.serializer().serialize(encoder, value)
is WebAppInlineKeyboardButton -> WebAppInlineKeyboardButton.serializer().serialize(encoder, value) is WebAppInlineKeyboardButton -> WebAppInlineKeyboardButton.serializer().serialize(encoder, value)
is CallbackGameInlineKeyboardButton -> CallbackGameInlineKeyboardButton.serializer().serialize(encoder, value) is CallbackGameInlineKeyboardButton -> CallbackGameInlineKeyboardButton.serializer().serialize(encoder, value)
is RequestUserInlineKeyboardButton -> RequestUserInlineKeyboardButton.serializer().serialize(encoder, value)
is RequestChatInlineKeyboardButton -> RequestChatInlineKeyboardButton.serializer().serialize(encoder, value)
is UnknownInlineKeyboardButton -> JsonElement.serializer().serialize(encoder, value.rawData) is UnknownInlineKeyboardButton -> JsonElement.serializer().serialize(encoder, value.rawData)
} }
} }

View File

@ -94,42 +94,6 @@ data class RequestPollKeyboardButton(
val requestPoll: KeyboardButtonPollType val requestPoll: KeyboardButtonPollType
) : KeyboardButton ) : KeyboardButton
/**
* Private chats only. When user will tap on this button, he will be asked for the chat with [requestChat] options. You
* will be able to catch this [ChatId] in updates and data using
* [dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onUserShared] in case you are using Behaviour
* Builder OR with [dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter.messagesFlow]
* and [kotlinx.coroutines.flow.filterIsInstance].
*
* In case you will use [dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onUserShared] it is
* recommended to use [kotlinx.coroutines.flow.Flow] [kotlinx.coroutines.flow.filter] with checking of incoming
* [dev.inmo.tgbotapi.types.request.UserShared.requestId]
*/
@Serializable
data class RequestUserKeyboardButton(
override val text: String,
@SerialName(requestUserField)
val requestUser: KeyboardButtonRequestUser
) : KeyboardButton
/**
* Private chats only. When user will tap on this button, he will be asked for the chat with [requestChat] options. You
* will be able to catch this [ChatId] in updates and data using
* [dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChatShared] in case you are using Behaviour
* Builder OR with [dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter.messagesFlow]
* and [kotlinx.coroutines.flow.filterIsInstance].
*
* In case you will use [dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChatShared] it is
* recommended to use [kotlinx.coroutines.flow.Flow] [kotlinx.coroutines.flow.filter] with checking of incoming
* [dev.inmo.tgbotapi.types.request.ChatShared.requestId]
*/
@Serializable
data class RequestChatKeyboardButton(
override val text: String,
@SerialName(requestChatField)
val requestChat: KeyboardButtonRequestChat
) : KeyboardButton
@RiskFeature @RiskFeature
object KeyboardButtonSerializer : KSerializer<KeyboardButton> { object KeyboardButtonSerializer : KSerializer<KeyboardButton> {
private val internalSerializer = JsonElement.serializer() private val internalSerializer = JsonElement.serializer()
@ -160,20 +124,6 @@ object KeyboardButtonSerializer : KSerializer<KeyboardButton> {
asJson[requestPollField] ?.jsonObject ?: buildJsonObject { } asJson[requestPollField] ?.jsonObject ?: buildJsonObject { }
) )
) )
asJson is JsonObject && asJson[requestUserField] != null -> RequestUserKeyboardButton(
asJson[textField]!!.jsonPrimitive.content,
nonstrictJsonFormat.decodeFromJsonElement(
KeyboardButtonRequestUser.serializer(),
asJson[requestUserField] ?.jsonObject ?: buildJsonObject { }
)
)
asJson is JsonObject && asJson[requestChatField] != null -> RequestChatKeyboardButton(
asJson[textField]!!.jsonPrimitive.content,
nonstrictJsonFormat.decodeFromJsonElement(
KeyboardButtonRequestChat.serializer(),
asJson[requestChatField] ?.jsonObject ?: buildJsonObject { }
)
)
else -> UnknownKeyboardButton( else -> UnknownKeyboardButton(
when (asJson) { when (asJson) {
is JsonObject -> asJson[textField]!!.jsonPrimitive.content is JsonObject -> asJson[textField]!!.jsonPrimitive.content
@ -192,8 +142,6 @@ object KeyboardButtonSerializer : KSerializer<KeyboardButton> {
is WebAppKeyboardButton -> WebAppKeyboardButton.serializer().serialize(encoder, value) is WebAppKeyboardButton -> WebAppKeyboardButton.serializer().serialize(encoder, value)
is RequestPollKeyboardButton -> RequestPollKeyboardButton.serializer().serialize(encoder, value) is RequestPollKeyboardButton -> RequestPollKeyboardButton.serializer().serialize(encoder, value)
is SimpleKeyboardButton -> encoder.encodeString(value.text) is SimpleKeyboardButton -> encoder.encodeString(value.text)
is RequestUserKeyboardButton -> RequestUserKeyboardButton.serializer().serialize(encoder, value)
is RequestChatKeyboardButton -> RequestChatKeyboardButton.serializer().serialize(encoder, value)
is UnknownKeyboardButton -> JsonElement.serializer().serialize(encoder, nonstrictJsonFormat.parseToJsonElement(value.raw)) is UnknownKeyboardButton -> JsonElement.serializer().serialize(encoder, nonstrictJsonFormat.parseToJsonElement(value.raw))
} }
} }

View File

@ -2,6 +2,10 @@ package dev.inmo.tgbotapi.types.buttons.inline
import dev.inmo.tgbotapi.types.LoginURL import dev.inmo.tgbotapi.types.LoginURL
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.* import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.*
import dev.inmo.tgbotapi.types.buttons.KeyboardButtonRequestChat
import dev.inmo.tgbotapi.types.buttons.KeyboardButtonRequestUser
import dev.inmo.tgbotapi.types.chat.member.ChatAdministratorRights
import dev.inmo.tgbotapi.types.request.RequestId
import dev.inmo.tgbotapi.types.webapps.WebAppInfo import dev.inmo.tgbotapi.types.webapps.WebAppInfo
/** /**
@ -100,3 +104,100 @@ inline fun webAppInlineButton(
text: String, text: String,
url: String url: String
) = webAppInlineButton(text, WebAppInfo(url)) ) = webAppInlineButton(text, WebAppInfo(url))
/**
* Creates and put [RequestUserKeyboardButton]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun requestUserInlineButton(
text: String,
requestUser: KeyboardButtonRequestUser
) = RequestUserInlineKeyboardButton(
text,
requestUser
)
/**
* Creates and put [RequestUserKeyboardButton] with [KeyboardButtonRequestUser.Bot]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun requestBotInlineButton(
text: String,
requestId: RequestId
) = requestUserInlineButton(
text,
KeyboardButtonRequestUser.Bot(requestId)
)
/**
* Creates and put [RequestUserKeyboardButton] with [KeyboardButtonRequestUser.Common]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun requestUserInlineButton(
text: String,
requestId: RequestId,
premiumUser: Boolean? = null
) = requestUserInlineButton(
text,
KeyboardButtonRequestUser.Common(requestId, premiumUser)
)
/**
* Creates and put [RequestUserKeyboardButton] with [KeyboardButtonRequestUser.Any]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun requestUserOrBotInlineButton(
text: String,
requestId: RequestId
) = requestUserInlineButton(
text,
KeyboardButtonRequestUser.Any(requestId)
)
/**
* Creates and put [RequestChatKeyboardButton]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun requestChatInlineButton(
text: String,
requestChat: KeyboardButtonRequestChat
) = RequestChatInlineKeyboardButton(
text,
requestChat
)
/**
* Creates and put [RequestChatKeyboardButton] with [KeyboardButtonRequestChat]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun requestChatInlineButton(
text: String,
requestId: RequestId,
isChannel: Boolean? = null,
isForum: Boolean? = null,
withUsername: Boolean? = null,
ownedBy: Boolean? = null,
userRightsInChat: ChatAdministratorRights? = null,
botRightsInChat: ChatAdministratorRights? = null,
botIsMember: Boolean = false
) = requestChatInlineButton(
text,
KeyboardButtonRequestChat(
requestId, isChannel, isForum, withUsername, ownedBy, userRightsInChat, botRightsInChat, botIsMember
)
)

View File

@ -69,99 +69,3 @@ inline fun webAppReplyButton(
text: String, text: String,
url: String url: String
) = webAppReplyButton(text, WebAppInfo(url)) ) = webAppReplyButton(text, WebAppInfo(url))
/**
* Creates and put [RequestUserKeyboardButton]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun requestUserReplyButton(
text: String,
requestUser: KeyboardButtonRequestUser
) = RequestUserKeyboardButton(
text,
requestUser
)
/**
* Creates and put [RequestUserKeyboardButton] with [KeyboardButtonRequestUser.Bot]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun requestBotReplyButton(
text: String,
requestId: RequestId
) = requestUserReplyButton(
text,
KeyboardButtonRequestUser.Bot(requestId)
)
/**
* Creates and put [RequestUserKeyboardButton] with [KeyboardButtonRequestUser.Common]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun requestUserReplyButton(
text: String,
requestId: RequestId,
premiumUser: Boolean? = null
) = requestUserReplyButton(
text,
KeyboardButtonRequestUser.Common(requestId, premiumUser)
)
/**
* Creates and put [RequestUserKeyboardButton] with [KeyboardButtonRequestUser.Any]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun requestUserOrBotReplyButton(
text: String,
requestId: RequestId
) = requestUserReplyButton(
text,
KeyboardButtonRequestUser.Any(requestId)
)
/**
* Creates and put [RequestChatKeyboardButton]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun requestChatReplyButton(
text: String,
requestChat: KeyboardButtonRequestChat
) = RequestChatKeyboardButton(
text,
requestChat
)
/**
* Creates and put [RequestChatKeyboardButton] with [KeyboardButtonRequestChat]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun requestChatReplyButton(
text: String,
requestId: RequestId,
isChannel: Boolean? = null,
isForum: Boolean? = null,
withUsername: Boolean? = null,
ownedBy: Boolean? = null,
userRightsInChat: ChatAdministratorRights? = null,
botRightsInChat: ChatAdministratorRights? = null,
botIsMember: Boolean = false
) = requestChatReplyButton(
text,
KeyboardButtonRequestChat(
requestId, isChannel, isForum, withUsername, ownedBy, userRightsInChat, botRightsInChat, botIsMember
)
)

View File

@ -3,6 +3,12 @@ package dev.inmo.tgbotapi.extensions.utils.types.buttons
import dev.inmo.tgbotapi.types.LoginURL import dev.inmo.tgbotapi.types.LoginURL
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.* import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.*
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
import dev.inmo.tgbotapi.types.buttons.KeyboardButtonRequestChat
import dev.inmo.tgbotapi.types.buttons.KeyboardButtonRequestUser
import dev.inmo.tgbotapi.types.buttons.inline.requestChatInlineButton
import dev.inmo.tgbotapi.types.buttons.inline.requestUserInlineButton
import dev.inmo.tgbotapi.types.chat.member.ChatAdministratorRights
import dev.inmo.tgbotapi.types.request.RequestId
import dev.inmo.tgbotapi.types.webapps.WebAppInfo import dev.inmo.tgbotapi.types.webapps.WebAppInfo
import dev.inmo.tgbotapi.utils.* import dev.inmo.tgbotapi.utils.*
@ -161,3 +167,103 @@ inline fun InlineKeyboardRowBuilder.webAppButton(
text: String, text: String,
url: String url: String
) = webAppButton(text, WebAppInfo(url)) ) = webAppButton(text, WebAppInfo(url))
/**
* Creates and put [RequestUserKeyboardButton]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun InlineKeyboardRowBuilder.requestUserButton(
text: String,
requestUser: KeyboardButtonRequestUser
) = add(
requestUserInlineButton(
text,
requestUser
)
)
/**
* Creates and put [RequestUserKeyboardButton] with [KeyboardButtonRequestUser.Bot]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun InlineKeyboardRowBuilder.requestBotButton(
text: String,
requestId: RequestId
) = requestUserButton(
text,
KeyboardButtonRequestUser.Bot(requestId)
)
/**
* Creates and put [RequestUserKeyboardButton] with [KeyboardButtonRequestUser.Common]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun InlineKeyboardRowBuilder.requestUserButton(
text: String,
requestId: RequestId,
premiumUser: Boolean? = null
) = requestUserButton(
text,
KeyboardButtonRequestUser.Common(requestId, premiumUser)
)
/**
* Creates and put [RequestUserKeyboardButton] with [KeyboardButtonRequestUser.Any]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun InlineKeyboardRowBuilder.requestUserOrBotButton(
text: String,
requestId: RequestId
) = requestUserButton(
text,
KeyboardButtonRequestUser.Any(requestId)
)
/**
* Creates and put [RequestChatKeyboardButton]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun InlineKeyboardRowBuilder.requestChatButton(
text: String,
requestChat: KeyboardButtonRequestChat
) = add(
requestChatInlineButton(
text,
requestChat
)
)
/**
* Creates and put [RequestChatKeyboardButton] with [KeyboardButtonRequestChat]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun InlineKeyboardRowBuilder.requestChatButton(
text: String,
requestId: RequestId,
isChannel: Boolean? = null,
isForum: Boolean? = null,
withUsername: Boolean? = null,
ownedBy: Boolean? = null,
userRightsInChat: ChatAdministratorRights? = null,
botRightsInChat: ChatAdministratorRights? = null,
botIsMember: Boolean = false
) = requestChatButton(
text,
KeyboardButtonRequestChat(
requestId, isChannel, isForum, withUsername, ownedBy, userRightsInChat, botRightsInChat, botIsMember
)
)

View File

@ -1,10 +1,6 @@
package dev.inmo.tgbotapi.extensions.utils.types.buttons package dev.inmo.tgbotapi.extensions.utils.types.buttons
import dev.inmo.tgbotapi.types.buttons.* import dev.inmo.tgbotapi.types.buttons.*
import dev.inmo.tgbotapi.types.buttons.reply.requestChatReplyButton
import dev.inmo.tgbotapi.types.buttons.reply.requestUserReplyButton
import dev.inmo.tgbotapi.types.chat.member.ChatAdministratorRights
import dev.inmo.tgbotapi.types.request.RequestId
import dev.inmo.tgbotapi.types.webapps.WebAppInfo import dev.inmo.tgbotapi.types.webapps.WebAppInfo
import dev.inmo.tgbotapi.utils.* import dev.inmo.tgbotapi.utils.*
@ -142,103 +138,3 @@ inline fun ReplyKeyboardRowBuilder.webAppButton(
text: String, text: String,
url: String url: String
) = webAppButton(text, WebAppInfo(url)) ) = webAppButton(text, WebAppInfo(url))
/**
* Creates and put [RequestUserKeyboardButton]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.requestUserButton(
text: String,
requestUser: KeyboardButtonRequestUser
) = add(
requestUserReplyButton(
text,
requestUser
)
)
/**
* Creates and put [RequestUserKeyboardButton] with [KeyboardButtonRequestUser.Bot]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.requestBotButton(
text: String,
requestId: RequestId
) = requestUserButton(
text,
KeyboardButtonRequestUser.Bot(requestId)
)
/**
* Creates and put [RequestUserKeyboardButton] with [KeyboardButtonRequestUser.Common]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.requestUserButton(
text: String,
requestId: RequestId,
premiumUser: Boolean? = null
) = requestUserButton(
text,
KeyboardButtonRequestUser.Common(requestId, premiumUser)
)
/**
* Creates and put [RequestUserKeyboardButton] with [KeyboardButtonRequestUser.Any]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.requestUserOrBotButton(
text: String,
requestId: RequestId
) = requestUserButton(
text,
KeyboardButtonRequestUser.Any(requestId)
)
/**
* Creates and put [RequestChatKeyboardButton]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.requestChatButton(
text: String,
requestChat: KeyboardButtonRequestChat
) = add(
requestChatReplyButton(
text,
requestChat
)
)
/**
* Creates and put [RequestChatKeyboardButton] with [KeyboardButtonRequestChat]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.requestChatButton(
text: String,
requestId: RequestId,
isChannel: Boolean? = null,
isForum: Boolean? = null,
withUsername: Boolean? = null,
ownedBy: Boolean? = null,
userRightsInChat: ChatAdministratorRights? = null,
botRightsInChat: ChatAdministratorRights? = null,
botIsMember: Boolean = false
) = requestChatButton(
text,
KeyboardButtonRequestChat(
requestId, isChannel, isForum, withUsername, ownedBy, userRightsInChat, botRightsInChat, botIsMember
)
)