1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-01 07:25:23 +00:00
tgbotapi/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/types/buttons/ReplyKeyboardBuilder.kt

411 lines
11 KiB
Kotlin
Raw Normal View History

2021-10-01 13:38:22 +00:00
package dev.inmo.tgbotapi.extensions.utils.types.buttons
import dev.inmo.tgbotapi.types.buttons.*
import dev.inmo.tgbotapi.types.buttons.reply.requestChatReplyButton
import dev.inmo.tgbotapi.types.buttons.reply.requestUserReplyButton
2024-01-10 17:05:44 +00:00
import dev.inmo.tgbotapi.types.buttons.reply.requestUsersReplyButton
import dev.inmo.tgbotapi.types.chat.member.ChatCommonAdministratorRights
2024-01-09 12:21:20 +00:00
import dev.inmo.tgbotapi.types.keyboardButtonRequestUserLimit
import dev.inmo.tgbotapi.types.request.RequestId
2022-04-17 14:18:09 +00:00
import dev.inmo.tgbotapi.types.webapps.WebAppInfo
import dev.inmo.tgbotapi.utils.*
2021-10-01 13:38:22 +00:00
/**
* Core DSL part of Keyboard DSL. Can accept only [KeyboardButton] and returns ready to use
* [ReplyKeyboardMarkup] via [build] method
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
* @see ReplyKeyboardRowBuilder
*/
2022-09-28 14:38:16 +00:00
typealias ReplyKeyboardBuilder = MatrixBuilder<KeyboardButton>
/**
* Creates [InlineKeyboardMarkup] using internal [matrix]
*/
fun ReplyKeyboardBuilder.build(
resizeKeyboard: Boolean? = null,
oneTimeKeyboard: Boolean? = null,
inputFieldPlaceholder: String? = null,
selective: Boolean? = null,
2022-12-30 13:38:26 +00:00
persistent: Boolean? = null,
) = ReplyKeyboardMarkup(matrix, resizeKeyboard, oneTimeKeyboard, inputFieldPlaceholder, selective, persistent)
2021-10-01 13:38:22 +00:00
/**
* Row builder of [KeyboardButton]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
2022-09-28 14:38:16 +00:00
typealias ReplyKeyboardRowBuilder = RowBuilder<KeyboardButton>
2021-10-01 13:38:22 +00:00
/**
* Factory-function for [ReplyKeyboardBuilder]. It will [apply] [block] to internally created [ReplyKeyboardMarkup]
* and [ReplyKeyboardBuilder.build] [ReplyKeyboardMarkup] then
*
* @see ReplyKeyboardBuilder.row
*/
inline fun replyKeyboard(
resizeKeyboard: Boolean? = null,
oneTimeKeyboard: Boolean? = null,
inputFieldPlaceholder: String? = null,
selective: Boolean? = null,
2022-12-30 13:38:26 +00:00
persistent: Boolean? = null,
block: ReplyKeyboardBuilder.() -> Unit
2022-12-30 13:38:26 +00:00
) = ReplyKeyboardBuilder().apply(block).build(resizeKeyboard, oneTimeKeyboard, inputFieldPlaceholder, selective, persistent)
2021-10-01 13:38:22 +00:00
/**
* Factory-function for [ReplyKeyboardBuilder], but in difference with [replyKeyboard] this method will create single-row
* keyboard
*/
inline fun flatReplyKeyboard(
resizeKeyboard: Boolean? = null,
oneTimeKeyboard: Boolean? = null,
inputFieldPlaceholder: String? = null,
selective: Boolean? = null,
2022-12-30 13:38:26 +00:00
persistent: Boolean? = null,
block: ReplyKeyboardRowBuilder.() -> Unit
2022-12-30 13:38:26 +00:00
) = replyKeyboard(resizeKeyboard, oneTimeKeyboard, inputFieldPlaceholder, selective, persistent) {
row<KeyboardButton>(block)
}
2021-10-01 13:38:22 +00:00
/**
* Creates and put [SimpleKeyboardButton]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.simpleButton(
text: String
) = add(SimpleKeyboardButton(text))
/**
* Creates and put [RequestContactKeyboardButton]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.requestContactButton(
text: String
) = add(RequestContactKeyboardButton(text))
/**
* Creates and put [RequestLocationKeyboardButton]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.requestLocationButton(
text: String
) = add(RequestLocationKeyboardButton(text))
/**
* Creates and put [RequestPollKeyboardButton]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.requestPollButton(
text: String,
pollType: KeyboardButtonPollType
) = add(RequestPollKeyboardButton(text, pollType))
2022-04-17 14:18:09 +00:00
/**
* Creates and put [WebAppKeyboardButton]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.webAppButton(
text: String,
webApp: WebAppInfo
) = add(WebAppKeyboardButton(text, webApp))
/**
* Creates and put [WebAppKeyboardButton]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.webAppButton(
text: String,
url: String
) = webAppButton(text, WebAppInfo(url))
/**
* Creates and put [RequestUserKeyboardButton]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
2024-01-09 12:21:20 +00:00
inline fun ReplyKeyboardRowBuilder.requestUsersButton(
text: String,
2024-01-03 10:28:56 +00:00
requestUser: KeyboardButtonRequestUsers
) = add(
2024-01-10 17:05:44 +00:00
requestUsersReplyButton(
text,
requestUser
)
)
2024-01-09 12:21:20 +00:00
/**
* Creates and put [RequestUserKeyboardButton] with [KeyboardButtonRequestUsers.Bot]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.requestBotsButton(
text: String,
requestId: RequestId,
2024-04-19 13:34:27 +00:00
maxCount: Int = keyboardButtonRequestUserLimit.first,
requestName: Boolean? = null,
requestUsername: Boolean? = null,
requestPhoto: Boolean? = null,
2024-01-09 12:21:20 +00:00
) = requestUsersButton(
text,
2024-04-19 13:34:27 +00:00
KeyboardButtonRequestUsers.Bot(
requestId = requestId,
maxCount = maxCount,
requestName = requestName,
requestUsername = requestUsername,
requestPhoto = requestPhoto
)
2024-01-09 12:21:20 +00:00
)
/**
2024-01-03 10:28:56 +00:00
* Creates and put [RequestUserKeyboardButton] with [KeyboardButtonRequestUsers.Bot]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.requestBotButton(
text: String,
2024-04-19 13:34:27 +00:00
requestId: RequestId,
requestName: Boolean? = null,
requestUsername: Boolean? = null,
requestPhoto: Boolean? = null,
2024-01-09 12:21:20 +00:00
) = requestBotsButton(
text,
2024-01-09 12:21:20 +00:00
requestId,
2024-04-19 13:34:27 +00:00
maxCount = keyboardButtonRequestUserLimit.first,
requestName = requestName,
requestUsername = requestUsername,
requestPhoto = requestPhoto
2024-01-09 12:21:20 +00:00
)
/**
* Creates and put [RequestUserKeyboardButton] with [KeyboardButtonRequestUsers.Common]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.requestUsersButton(
text: String,
requestId: RequestId,
premiumUser: Boolean? = null,
2024-04-19 13:34:27 +00:00
maxCount: Int = keyboardButtonRequestUserLimit.first,
requestName: Boolean? = null,
requestUsername: Boolean? = null,
requestPhoto: Boolean? = null,
2024-01-09 12:21:20 +00:00
) = requestUsersButton(
text,
2024-04-19 13:34:27 +00:00
KeyboardButtonRequestUsers.Common(
requestId = requestId,
isPremium = premiumUser,
maxCount = maxCount,
requestName = requestName,
requestUsername = requestUsername,
requestPhoto = requestPhoto
)
)
/**
2024-01-03 10:28:56 +00:00
* Creates and put [RequestUserKeyboardButton] with [KeyboardButtonRequestUsers.Common]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.requestUserButton(
text: String,
requestId: RequestId,
2024-04-19 13:34:27 +00:00
premiumUser: Boolean? = null,
requestName: Boolean? = null,
requestUsername: Boolean? = null,
requestPhoto: Boolean? = null,
) = requestUsersButton(
text = text,
requestId = requestId,
premiumUser = premiumUser,
maxCount = keyboardButtonRequestUserLimit.first,
requestName = requestName,
requestUsername = requestUsername,
requestPhoto = requestPhoto
)
2024-01-09 12:21:20 +00:00
/**
* Creates and put [RequestUserKeyboardButton] with [KeyboardButtonRequestUsers.Any]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.requestUsersOrBotsButton(
text: String,
requestId: RequestId,
2024-01-10 17:05:44 +00:00
premiumUser: Boolean? = null,
2024-04-19 13:34:27 +00:00
maxCount: Int = keyboardButtonRequestUserLimit.first,
requestName: Boolean? = null,
requestUsername: Boolean? = null,
requestPhoto: Boolean? = null,
2024-01-09 12:21:20 +00:00
) = requestUsersButton(
text,
2024-04-19 13:34:27 +00:00
KeyboardButtonRequestUsers.Any(
requestId = requestId,
isPremium = premiumUser,
maxCount = maxCount,
requestName = requestName,
requestUsername = requestUsername,
requestPhoto = requestPhoto
)
)
/**
2024-01-03 10:28:56 +00:00
* Creates and put [RequestUserKeyboardButton] with [KeyboardButtonRequestUsers.Any]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.requestUserOrBotButton(
text: String,
2024-04-19 13:34:27 +00:00
requestId: RequestId,
requestName: Boolean? = null,
requestUsername: Boolean? = null,
requestPhoto: Boolean? = null,
2024-01-09 12:21:20 +00:00
) = requestUsersOrBotsButton(
2024-04-19 13:34:27 +00:00
text = text,
requestId = requestId,
maxCount = keyboardButtonRequestUserLimit.first,
requestName = requestName,
requestUsername = requestUsername,
requestPhoto = requestPhoto,
)
/**
* 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,
2023-02-05 17:26:41 +00:00
isPublic: Boolean? = null,
isOwnedBy: Boolean? = null,
userRightsInChat: ChatCommonAdministratorRights? = null,
botRightsInChat: ChatCommonAdministratorRights? = null,
2024-04-19 13:34:27 +00:00
botIsMember: Boolean? = null,
requestTitle: Boolean? = null,
requestUsername: Boolean? = null,
requestPhoto: Boolean? = null,
) = requestChatButton(
text,
KeyboardButtonRequestChat(
2023-02-05 17:26:41 +00:00
requestId = requestId,
isChannel = isChannel,
isForum = isForum,
isPublic = isPublic,
isOwnedBy = isOwnedBy,
userRightsInChat = userRightsInChat,
botRightsInChat = botRightsInChat,
2024-04-19 13:34:27 +00:00
botIsMember = botIsMember,
requestTitle = requestTitle,
requestUsername = requestUsername,
requestPhoto = requestPhoto,
)
)
/**
* Creates and put [RequestChatKeyboardButton] with [KeyboardButtonRequestChat.Channel]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.requestChannelButton(
text: String,
requestId: RequestId,
isPublic: Boolean? = null,
isOwnedBy: Boolean? = null,
userRightsInChat: ChatCommonAdministratorRights? = null,
botRightsInChat: ChatCommonAdministratorRights? = null,
2024-04-19 13:34:27 +00:00
botIsMember: Boolean? = null,
requestTitle: Boolean? = null,
requestUsername: Boolean? = null,
requestPhoto: Boolean? = null,
) = requestChatButton(
text,
KeyboardButtonRequestChat.Channel(
requestId = requestId,
isPublic = isPublic,
isOwnedBy = isOwnedBy,
userRightsInChat = userRightsInChat,
botRightsInChat = botRightsInChat,
2024-04-19 13:34:27 +00:00
botIsMember = botIsMember,
requestTitle = requestTitle,
requestUsername = requestUsername,
requestPhoto = requestPhoto,
)
)
/**
* Creates and put [RequestChatKeyboardButton] with [KeyboardButtonRequestChat.Group]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
inline fun ReplyKeyboardRowBuilder.requestGroupButton(
text: String,
requestId: RequestId,
isForum: Boolean? = null,
isPublic: Boolean? = null,
isOwnedBy: Boolean? = null,
userRightsInChat: ChatCommonAdministratorRights? = null,
botRightsInChat: ChatCommonAdministratorRights? = null,
2024-04-19 13:34:27 +00:00
botIsMember: Boolean? = null,
requestTitle: Boolean? = null,
requestUsername: Boolean? = null,
requestPhoto: Boolean? = null,
) = requestChatButton(
text,
KeyboardButtonRequestChat.Group(
requestId = requestId,
isForum = isForum,
isPublic = isPublic,
isOwnedBy = isOwnedBy,
userRightsInChat = userRightsInChat,
botRightsInChat = botRightsInChat,
2024-04-19 13:34:27 +00:00
botIsMember = botIsMember,
requestTitle = requestTitle,
requestUsername = requestUsername,
requestPhoto = requestPhoto,
)
)