joinToInlineKeyboard

fun <T> Collection<T>.joinToInlineKeyboard(nameSelector: T.() -> String, callbackSelector: T.() -> String, paginationCallbackPattern: String, currentPage: Int = 1, perPage: Int = 5, perLine: Int = 1): InlineKeyboardMarkup

Helper function to paginate over a collection.

Return

InlineKeyboardMarkup.

Example of Usage:

    @CommandHandler(["/start"])
suspend fun start(bot: TelegramBot, user: User) {
message("Hello").markup(
elements.joinToInlineKeyboard(
{ "button $name" },
{ "selector?id=$id" },
"/start?page=%P",
)
).sendAsync(user, bot).getOrNull()?.also {
bot.userData[user, "startPage"] = it.messageId
}
}

@CommandHandler.CallbackQuery(["/start"])
suspend fun paginator(bot: TelegramBot, user: User, page: Int) {
editMarkup(
bot.userData.get<Long>(user.id, "startPage")!!
).markup(
elements.joinToInlineKeyboard(
{ "button $name" },
{ "selector?id=$id" },
"/start?page=%P",
currentPage = page
)
).send(user, bot)
}

Parameters

T

generic for paginating data.

nameSelector

selector for button name.

callbackSelector

selector for callback data.

paginationCallbackPattern

pattern that will be used for pagination buttons. ex: /start?page=%P where %P will be replaced with page number.

currentPage

current page

perPage

elements per page.

perLine

elements per line.