Keyboards and callbacks
Typed inline buttons
Section titled “Typed inline buttons”use jingzhe::{CallbackData, InlineButton, InlineKeyboard};
#[derive(CallbackData)]enum Action { ShowHelp, OpenPage { page: u32 },}
fn keyboard() -> InlineKeyboard { InlineKeyboard::new().row([ InlineButton::callback("Help", Action::ShowHelp), InlineButton::callback("Next page", Action::OpenPage { page: 2 }), InlineButton::callback("Raw payload", "raw-help"), ])}By default, the derive macro uses Type:1:Variant:JSONTuple, with null for variants without fields. For example, Action:1:OpenPage:[2].
Pin the wire protocol with a namespace and version:
#[derive(CallbackData)]#[callback_data(namespace = "pages", version = 2)]enum PageAction { Open { page: u32 },}Increase the version when changing field order or meaning so old buttons are not interpreted incorrectly. A namespace and version pair can be registered only once.
Handle callbacks
Section titled “Handle callbacks”async fn handle_action( context: Context<AppState>, callback: Callback<Action>,) -> anyhow::Result<()> { callback.answer("Working").await?;
match callback.data() { Action::ShowHelp => context.reply("Help text").await?, Action::OpenPage { page } => context.reply(format!("Page: {page}")).await?, }
Ok(())}Register it with .callback::<Action>(handle_action). callback.data() returns the typed payload, while query() returns the original query.
Inline callbacks may not have a chat target. You can still answer the callback, but you must not assume reply() is available. A fallback handler using Callback<Option<String>> preserves the original optional string.
Payload limits and authorization
Section titled “Payload limits and authorization”- Callback payloads are limited to 1–64 UTF-8 bytes.
- Construction stores invalid payload errors and returns them when encoding is explicitly requested. It does not panic or send invalid requests.
- Callback data is user input, not an authorization credential. Validate the acting user’s permissions in your application.
- Unknown callbacks are not answered automatically. Register a fallback and tell the user to refresh the keyboard.
Reply keyboards
Section titled “Reply keyboards”.reply_markup(...) on SendMessage, SendPhoto, SendDocument, and SendAnimation accepts the shared ReplyMarkup type:
use jingzhe::{KeyboardButton, ReplyKeyboardMarkup, SendMessage};
fn menu(chat_id: i64) -> SendMessage { let keyboard = ReplyKeyboardMarkup::new() .row(["Help", "Settings"]) .row([ KeyboardButton::request_contact("Share contact"), KeyboardButton::request_location("Share location"), ]) .resize_keyboard(true) .one_time_keyboard(true) .input_field_placeholder("Choose an action");
SendMessage::new(chat_id, "Choose an action").reply_markup(keyboard)}Reply keyboard text buttons send ordinary Message updates received by .on_message(...); they do not trigger .callback::<Action>(...). Special buttons are available only in private chats, and their returned data still requires application validation.
Remove the reply keyboard completely:
SendMessage::new(chat_id, "Keyboard removed") .reply_markup(ReplyKeyboardRemove::new())Use ForceReply::new() to ask the client to display a reply interface. It does not create a persistent conversation session.