Skip to content
↑ ↓ move↵ openEvery page also as Markdown: add .md

Guide

Keyboards

Keyboards

Buttons under a message (inline) or in place of the user’s keyboard (reply), and the callback query a button sends back when it is pressed.

Example

use vaegram::prelude::*;

let menu = InlineKeyboardMarkup::default()
    .callback("Yes", "yes")
    .callback("No", "no")
    .row()
    .button(InlineKeyboardButton::callback("Delete", "delete").style(ButtonStyle::Danger))
    .url("Docs", "https://docs.rs/vaegram");
assert_eq!(menu.rows.len(), 2); // Yes, No / Delete, Docs

let keyboard = ReplyKeyboardMarkup::default()
    .text("Yes")
    .text("No")
    .row()
    .button(KeyboardButton::new("Share my number").request_contact(true))
    .one_time(true)
    .placeholder("Pick one");
assert_eq!(keyboard.rows.len(), 2);
assert!(keyboard.resize); // on unless turned off
See it run
Your botbot
  1. A message sent with .reply_markup(menu)
    Docs

What ran

  1. Press a button.

How it works

Both keyboards are built the same way: each button joins the last row, and .row() starts the next one. .row() does nothing while the last row is empty, so a keyboard is never written with an empty row, though the empty row still counts in rows.

Inline keyboards

InlineKeyboardMarkup::default() is an empty keyboard. Add buttons with .callback(text, data), .url(text, url), .button(InlineKeyboardButton) and .buttons(iter). The button constructors cover what a callback cannot: InlineKeyboardButton::copy_text, ::web_app, ::login_url, ::switch_inline_query_current_chat, ::disabled, and more; each has a short description in the API docs. .style(ButtonStyle::Primary) colors a button; a plain button is the default.

For a list whose length you don’t know, build the rows yourself; new takes them:

let buttons = vec![InlineKeyboardButton::callback("A", "a"), InlineKeyboardButton::callback("B", "b")];
let keyboard = InlineKeyboardMarkup::new(buttons.chunks(2).map(<[_]>::to_vec));
assert_eq!(keyboard.rows.len(), 1);

Attach it with .reply_markup(..) on any send, and on edit_message_text. A keyboard with no buttons is still sent as an empty keyboard; to remove one, pass None to edit_reply_markup.

Reply keyboards

ReplyKeyboardMarkup::default() starts with resize on, which fits the keyboard to its buttons. .text(..) adds a plain button; .button(KeyboardButton::new(..).request_contact(true)) adds one that asks for a phone number, location, user, chat or poll instead of sending text. A button sends one request kind, so the with_* constructors build it directly: KeyboardButton::with_contact("Share my number"), with_location, with_users, with_chat, with_poll, with_managed_bot and with_web_app. .one_time(true) hides the keyboard after one press, .placeholder(..) sets the hint in the input field, persistent and selective behave as Telegram documents them.

Take a reply keyboard away with ReplyKeyboardRemove::new(), and ask for a reply with ForceReply::new().

use vaegram::prelude::*;

async fn ask(msg: &Message) -> Result<()> {
    let keyboard = ReplyKeyboardMarkup::default().text("Yes").text("No").resize(true);
    msg.answer("Use the keyboard below.").reply_markup(keyboard).await?;
    msg.answer("Keyboard removed.").reply_markup(ReplyKeyboardRemove::new()).await?;
    Ok(())
}

Handling a press

A pressed button arrives as a CallbackQuery carrying data and the chat and message it is on. Register a handler with on_callback_query and a filter; filters::data("yes") matches one button, filters::all() every press. Answer the query with query.answer(), then edit the message the button was on:

use vaegram::prelude::*;

async fn setup(app: &Client) -> Result<()> {
    app.on_message(filters::command("menu"), |msg| async move {
        let keyboard = InlineKeyboardMarkup::default().callback("Yes", "yes").callback("No", "no");
        msg.answer("Pick one:").reply_markup(keyboard).await?;
        Ok(())
    });

    app.on_callback_query(filters::data("yes"), |query| async move {
        query.answer().text("Saved").alert().await?;
        query.edit_message_text("You picked <b>Yes</b>.").html().await?;
        Ok(())
    });

    app.run().await
}
See it run
Your botbot
  1. /menu
  2. Pick one:

What ran

  1. Press Yes or No.

query.edit_message_text(..) works on regular, inline and ephemeral messages; awaiting it gives (). query.answer().text(..) shows a toast, .alert() makes it a modal alert, .url(..) opens a link. Answer every query: the answer is all the user sees from the press.

For the message itself, query.message().await? returns it (None if it was deleted), and then the usual methods work on it: message.edit_text(..), message.edit_caption(..) and message.edit_reply_markup(None) to remove the keyboard. message.wait_for_click() waits for the next press on that message’s buttons.

use vaegram::prelude::*;

async fn edit(msg: &Message) -> Result<()> {
    let edited = msg.edit_text("Updated").await?;
    edited.edit_reply_markup(None).await?;
    Ok(())
}

Common mistakes

  • Expecting .row() to make a row of its own: it only opens one, and buttons land in the last row, so a trailing .row() leaves an empty row in rows.
  • Removing a keyboard with an empty markup. Send ReplyKeyboardRemove for a reply keyboard, and None to edit_reply_markup for an inline one.
  • A callback button whose data no filter matches: filters::all() takes every press, while a wrong filters::data(..) ignores it silently.
  • Reading query.data as the pressed message: it is the button’s payload, set by you. The message is query.message().await?.
  • Long callback data: Telegram caps it (64 bytes in the Bot API, https://core.telegram.org/bots/api#inlinekeyboardbutton). Keep an id or a short code there, not a whole state.

See also