1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-12-17 19:55:49 +00:00

new dsls for keyboards #472

This commit is contained in:
2021-10-01 19:38:22 +06:00
parent 5b98c5f821
commit 7f9faa69c8
42 changed files with 1028 additions and 49 deletions

View File

@@ -0,0 +1,130 @@
package dev.inmo.tgbotapi.extensions.utils.types.buttons
import dev.inmo.tgbotapi.types.LoginURL
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.*
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
import dev.inmo.tgbotapi.utils.MatrixBuilder
import dev.inmo.tgbotapi.utils.RowBuilder
/**
* Core DSL part of Inline Keyboard DSL. Can accept only [InlineKeyboardButton] and returns ready to use
* [InlineKeyboardMarkup] via [build] method
*
* @see inlineKeyboard
* @see InlineKeyboardBuilder.row
* @see InlineKeyboardRowBuilder
*/
class InlineKeyboardBuilder : MatrixBuilder<InlineKeyboardButton>() {
/**
* Creates [InlineKeyboardMarkup] using internal [matrix]
*/
fun build() = InlineKeyboardMarkup(matrix)
}
/**
* Row builder of [InlineKeyboardBuilder]
*
* @see inlineKeyboard
* @see InlineKeyboardBuilder.row
*/
class InlineKeyboardRowBuilder : RowBuilder<InlineKeyboardButton>()
/**
* Factory-function for [InlineKeyboardBuilder]. It will [apply] [block] to internally created [InlineKeyboardMarkup]
* and [InlineKeyboardBuilder.build] [InlineKeyboardMarkup] then
*
* @see InlineKeyboardBuilder.row
*/
inline fun inlineKeyboard(
crossinline block: InlineKeyboardBuilder.() -> Unit
) = InlineKeyboardBuilder().apply(block).build()
/**
* Creates an [InlineKeyboardRowBuilder] and [apply] [block] with this builder
*
* @see payButton
* @see dataButton
* @see gameButton
* @see loginButton
* @see inlineQueryInCurrentChatButton
* @see inlineQueryButton
* @see urlButton
*/
inline fun InlineKeyboardBuilder.row(
crossinline block: InlineKeyboardRowBuilder.() -> Unit
) = add(InlineKeyboardRowBuilder().apply(block).row)
/**
* Creates and put [PayInlineKeyboardButton]
*
* @see inlineKeyboard
* @see InlineKeyboardBuilder.row
*/
inline fun InlineKeyboardRowBuilder.payButton(
text: String
) = add(PayInlineKeyboardButton(text))
/**
* Creates and put [CallbackDataInlineKeyboardButton]
*
* @see inlineKeyboard
* @see InlineKeyboardBuilder.row
*/
inline fun InlineKeyboardRowBuilder.dataButton(
text: String,
data: String
) = add(CallbackDataInlineKeyboardButton(text, data))
/**
* Creates and put [CallbackGameInlineKeyboardButton]
*
* @see inlineKeyboard
* @see InlineKeyboardBuilder.row
*/
inline fun InlineKeyboardRowBuilder.gameButton(
text: String
) = add(CallbackGameInlineKeyboardButton(text))
/**
* Creates and put [LoginURLInlineKeyboardButton]
*
* @see inlineKeyboard
* @see InlineKeyboardBuilder.row
*/
inline fun InlineKeyboardRowBuilder.loginButton(
text: String,
loginUrl: LoginURL
) = add(LoginURLInlineKeyboardButton(text, loginUrl))
/**
* Creates and put [SwitchInlineQueryCurrentChatInlineKeyboardButton]
*
* @see inlineKeyboard
* @see InlineKeyboardBuilder.row
*/
inline fun InlineKeyboardRowBuilder.inlineQueryInCurrentChatButton(
text: String,
data: String
) = add(SwitchInlineQueryCurrentChatInlineKeyboardButton(text, data))
/**
* Creates and put [SwitchInlineQueryInlineKeyboardButton]
*
* @see inlineKeyboard
* @see InlineKeyboardBuilder.row
*/
inline fun InlineKeyboardRowBuilder.inlineQueryButton(
text: String,
data: String
) = add(SwitchInlineQueryInlineKeyboardButton(text, data))
/**
* Creates and put [URLInlineKeyboardButton]
*
* @see inlineKeyboard
* @see InlineKeyboardBuilder.row
*/
inline fun InlineKeyboardRowBuilder.urlButton(
text: String,
url: String
) = add(URLInlineKeyboardButton(text, url))

View File

@@ -0,0 +1,102 @@
package dev.inmo.tgbotapi.extensions.utils.types.buttons
import dev.inmo.tgbotapi.types.buttons.*
import dev.inmo.tgbotapi.types.inputFieldPlaceholderField
import dev.inmo.tgbotapi.utils.MatrixBuilder
import dev.inmo.tgbotapi.utils.RowBuilder
import kotlinx.serialization.SerialName
/**
* 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
*/
class ReplyKeyboardBuilder : MatrixBuilder<KeyboardButton>() {
/**
* Creates [InlineKeyboardMarkup] using internal [matrix]
*/
fun build(
resizeKeyboard: Boolean? = null,
oneTimeKeyboard: Boolean? = null,
inputFieldPlaceholder: String? = null,
selective: Boolean? = null,
) = ReplyKeyboardMarkup(matrix, resizeKeyboard, oneTimeKeyboard, inputFieldPlaceholder, selective)
}
/**
* Row builder of [KeyboardButton]
*
* @see replyKeyboard
* @see ReplyKeyboardBuilder.row
*/
class ReplyKeyboardRowBuilder : RowBuilder<KeyboardButton>()
/**
* 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,
crossinline block: ReplyKeyboardBuilder.() -> Unit
) = ReplyKeyboardBuilder().apply(block).build(resizeKeyboard, oneTimeKeyboard, inputFieldPlaceholder, selective)
/**
* Creates an [ReplyKeyboardRowBuilder] and [apply] [block] with this builder
*
* @see simpleButton
* @see requestContactButton
* @see requestLocationButton
* @see requestPollButton
*/
inline fun ReplyKeyboardBuilder.row(
crossinline block: ReplyKeyboardRowBuilder.() -> Unit
) = add(ReplyKeyboardRowBuilder().apply(block).row)
/**
* 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))