Keyboard shortcuts

Press โ† or โ†’ to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Inline Keyboards & Reply Markup

ferogram ships with high-level keyboard builders: InlineKeyboard and ReplyKeyboard, so you never have to construct raw TL types by hand.

As of Telegram layer 229, inline-keyboard buttons and reply-keyboard buttons are two separate TL type trees (they used to share one KeyboardButton schema). ferogram mirrors that split: Button builds inline-keyboard buttons, ReplyButton builds reply-keyboard buttons. They are not interchangeable - InlineKeyboard::row() takes Button, ReplyKeyboard::row() takes ReplyButton.

Both builders are in ferogram::keyboard and re-exported at the crate root:

#![allow(unused)]
fn main() {
use ferogram::keyboard::{Button, InlineKeyboard, ReplyButton, ReplyKeyboard};
}

InlineKeyboard: buttons attached to a message

Inline keyboards appear below a message and trigger Update::CallbackQuery when tapped.

#![allow(unused)]
fn main() {
use ferogram::keyboard::{Button, InlineKeyboard};
use ferogram::InputMessage;

let kb = InlineKeyboard::new()
    .row([
        Button::callback("โœ… Yes", b"confirm:yes"),
        Button::callback("โŒ No",  b"confirm:no"),
    ])
    .row([
        Button::url("๐Ÿ“– Docs", "https://docs.rs/ferogram"),
    ]);

client.send_message(peer.clone(), InputMessage::text("Do you want to proceed?").keyboard(kb)).await?;
}

InlineKeyboard methods

MethodDescription
InlineKeyboard::new()Create an empty keyboard
.row(buttons)Append a row; accepts any IntoIterator<Item = Button>
.into_markup()Convert to tl::enums::ReplyMarkup

InlineKeyboard implements Into<tl::enums::ReplyMarkup>, so you can also pass it directly to InputMessage::reply_markup().


Button: inline-keyboard button types

#![allow(unused)]
fn main() {
// Sends data to your bot as Update::CallbackQuery (max 64 bytes)
Button::callback("โœ… Confirm", b"action:confirm")

// Opens URL in a browser
Button::url("๐ŸŒ Website", "https://example.com")

// Copy text to clipboard (Telegram 10.3+)
Button::copy_text("๐Ÿ“‹ Copy code", "PROMO2024")

// Login-widget: authenticates user before opening URL
Button::url_auth("๐Ÿ” Login", "https://example.com/auth", None, bot_input_user)

// Opens bot inline mode in the current chat with query pre-filled
Button::switch_inline("๐Ÿ” Search here", "default query")

// Chat picker so user can choose which chat to use inline mode in
Button::switch_elsewhere("๐Ÿ“ค Share", "")

// Telegram Mini App with full JS bridge
Button::mini_app("๐Ÿš€ Open App", "https://myapp.example.com")

// Launch a Telegram game (bots only)
Button::game("๐ŸŽฎ Play")

// Payment buy button (bots only, used with invoice)
Button::buy("๐Ÿ’ณ Pay $4.99")
}

There is no inline โ€œsimple webviewโ€ (no-JS-bridge) button as of layer 229 - Telegram moved that variant to reply keyboards only. See ReplyButton::mini_app_simple below.

Escape hatch

#![allow(unused)]
fn main() {
// Get the underlying tl::enums::KeyboardInlineButton
let raw = Button::callback("x", b"x").into_raw();
}

ReplyButton: reply-keyboard button types

#![allow(unused)]
fn main() {
use ferogram::keyboard::ReplyButton;

// Plain text
ReplyButton::text("๐Ÿ“ธ Send photo")

// Shares user's phone number on tap
ReplyButton::request_phone("๐Ÿ“ž Share my number")

// Shares user's location on tap
ReplyButton::request_geo("๐Ÿ“ Share location")

// Opens poll creation interface
ReplyButton::request_poll("๐Ÿ“Š Create poll")

// Forces quiz mode in poll creator
ReplyButton::request_quiz("๐Ÿง  Create quiz")

// Simple webview without JS bridge (reply-keyboard only, layer 229+)
ReplyButton::mini_app_simple("โ„น๏ธ Info", "https://info.example.com")
}

Escape hatch

#![allow(unused)]
fn main() {
// Get the underlying tl::enums::KeyboardButton
let raw = ReplyButton::text("x").into_raw();
}

ReplyKeyboard: replacement keyboard

A reply keyboard replaces the userโ€™s text input keyboard until dismissed. The userโ€™s tap arrives as a plain-text Update::NewMessage.

#![allow(unused)]
fn main() {
use ferogram::keyboard::{ReplyButton, ReplyKeyboard};

let kb = ReplyKeyboard::new()
    .row([
        ReplyButton::text("๐Ÿ“ธ Photo"),
        ReplyButton::text("๐Ÿ“„ Document"),
    ])
    .row([ReplyButton::text("โŒ Cancel")])
    .resize()      // shrink to fit content (recommended)
    .single_use(); // hide after one press

client.send_message(peer.clone(), InputMessage::text("Choose file type:").keyboard(kb))
    .await?;
}

ReplyKeyboard methods

MethodDescription
ReplyKeyboard::new()Create an empty keyboard
.row(buttons)Append a row of buttons
.resize()Shrink keyboard to fit button count
.single_use()Dismiss after one tap
.selective()Show only to mentioned/replied users
.into_markup()Convert to tl::enums::ReplyMarkup

Remove keyboard

#![allow(unused)]
fn main() {
use ferogram_tl_types as tl;

let remove = tl::enums::ReplyMarkup::ReplyKeyboardHide(
    tl::types::ReplyKeyboardHide { selective: false }
);
client
    .send_message(peer.clone(), InputMessage::text("Done.").reply_markup(remove))
    .await?;
}

Answer callback queries

Always answer every CallbackQuery: Telegram shows a loading spinner until you do.

#![allow(unused)]
fn main() {
Update::CallbackQuery(cb) => {
    let data = cb.data().unwrap_or(b"");
    match data {
        b"confirm:yes" => client.answer_callback_query(cb.query_id, Some("โœ… Done!"), false).await?,
        b"confirm:no"  => client.answer_callback_query(cb.query_id, Some("โŒ Cancelled"), false).await?,
        _              => client.answer_callback_query(cb.query_id, None, false).await?,
    }
}
}

Pass alert: true to show a popup alert instead of a toast:

#![allow(unused)]
fn main() {
client.answer_callback_query(cb.query_id, Some("โ›” Access denied"), true).await?;
}

Legacy raw TL pattern (still works)

If you prefer constructing TL types directly. Note the two-level shape as of layer 229: the buttonโ€™s text lives on the outer KeyboardInlineButton, while type-specific data (like callback data) lives on the inner InlineButtonType variant.

#![allow(unused)]
fn main() {
use ferogram_tl_types as tl;

fn inline_kb(rows: Vec<Vec<tl::enums::KeyboardInlineButton>>) -> tl::enums::ReplyMarkup {
    tl::enums::ReplyMarkup::ReplyInlineMarkup(tl::types::ReplyInlineMarkup {
        force_reply: false,
        rows: rows.into_iter().map(|buttons|
            tl::enums::KeyboardInlineButtonRow::KeyboardInlineButtonRow(
                tl::types::KeyboardInlineButtonRow { buttons }
            )
        ).collect(),
    })
}

fn btn_cb(text: &str, data: &str) -> tl::enums::KeyboardInlineButton {
    tl::enums::KeyboardInlineButton::KeyboardInlineButton(tl::types::KeyboardInlineButton {
        style: None,
        text:  text.into(),
        r#type: tl::enums::InlineButtonType::Callback(tl::types::InlineButtonTypeCallback {
            requires_password: false,
            data: data.as_bytes().to_vec(),
        }),
    })
}

fn btn_url(text: &str, url: &str) -> tl::enums::KeyboardInlineButton {
    tl::enums::KeyboardInlineButton::KeyboardInlineButton(tl::types::KeyboardInlineButton {
        style: None,
        text:  text.into(),
        r#type: tl::enums::InlineButtonType::Url(tl::types::InlineButtonTypeUrl {
            url: url.into(),
        }),
    })
}
}

Send with keyboard

#![allow(unused)]
fn main() {
let kb = inline_kb(vec![
    vec![btn_cb("โœ… Yes", "confirm:yes"), btn_cb("โŒ No", "confirm:no")],
    vec![btn_url("๐ŸŒ Docs", "https://github.com/ankit-chaubey/ferogram")],
]);

let (text, entities) = parse_markdown("**Do you want to proceed?**");
let msg = InputMessage::text(text)
    .entities(entities)
    .reply_markup(kb);

client.send_message(peer, msg).await?;
}

All button types (raw TL, layer 229)

Inline keyboard (InlineButtonType, wrapped in KeyboardInlineButton)

TypeConstructorDescription
CallbackInlineButtonTypeCallbackTriggers CallbackQuery with custom data
URLInlineButtonTypeUrlOpens a URL in the browser
URL AuthInputInlineButtonTypeUrlAuthLogin-widget style authenticated URL
WebViewInlineButtonTypeWebViewOpens a Telegram Mini App (JS bridge)
Switch InlineInlineButtonTypeSwitchInlineOpens inline mode with a query
User ProfileInputInlineButtonTypeUserProfileOpens a userโ€™s profile
GameInlineButtonTypeGameOpens a Telegram game
BuyInlineButtonTypeBuyPurchase button for payments
CopyInlineButtonTypeCopyCopies text to clipboard
DisabledInlineButtonTypeDisabledRenders greyed out, not tappable

Reply keyboard (ButtonType, wrapped in KeyboardButton)

TypeConstructorDescription
TextButtonTypeDefaultPlain text button
Request PhoneButtonTypeRequestPhoneRequests the userโ€™s phone number
Request LocationButtonTypeRequestGeoLocationRequests location
Request PollButtonTypeRequestPollOpens poll creator
Request PeerInputButtonTypeRequestPeerRequests peer selection
Simple WebViewButtonTypeSimpleWebViewOpens a webview without a JS bridge

Switch Inline button (raw)

Opens the botโ€™s inline mode in the current or another chat:

#![allow(unused)]
fn main() {
tl::enums::KeyboardInlineButton::KeyboardInlineButton(tl::types::KeyboardInlineButton {
    style: None,
    text:  "๐Ÿ” Search with me".into(),
    r#type: tl::enums::InlineButtonType::SwitchInline(tl::types::InlineButtonTypeSwitchInline {
        same_peer:  false, // false = let user pick any chat
        query:      "default query".into(),
        peer_types: None,
    }),
})
}

Simple WebView button (raw, reply-keyboard only)

#![allow(unused)]
fn main() {
tl::enums::KeyboardButton::KeyboardButton(tl::types::KeyboardButton {
    style: None,
    text:  "Open App".into(),
    r#type: tl::enums::ButtonType::SimpleWebView(tl::types::ButtonTypeSimpleWebView {
        url: "https://myapp.example.com".into(),
    }),
})
}

Reply keyboard (replaces userโ€™s keyboard)

#![allow(unused)]
fn main() {
let reply_kb = tl::enums::ReplyMarkup::ReplyKeyboardMarkup(
    tl::types::ReplyKeyboardMarkup {
        resize:      true,       // shrink to fit buttons
        single_use:  true,       // hide after one tap
        selective:   false,      // show to everyone
        persistent:  false,      // don't keep after message
        force_reply: false,      // layer 229; not covered elsewhere in this doc
        placeholder: Some("Choose an optionโ€ฆ".into()),
        rows: vec![
            tl::enums::KeyboardButtonRow::KeyboardButtonRow(
                tl::types::KeyboardButtonRow {
                    buttons: vec![
                        tl::enums::KeyboardButton::KeyboardButton(tl::types::KeyboardButton {
                            style: None,
                            text: "๐Ÿ• Pizza".into(),
                            r#type: tl::enums::ButtonType::Default(tl::types::ButtonTypeDefault {}),
                        }),
                        tl::enums::KeyboardButton::KeyboardButton(tl::types::KeyboardButton {
                            style: None,
                            text: "๐Ÿ” Burger".into(),
                            r#type: tl::enums::ButtonType::Default(tl::types::ButtonTypeDefault {}),
                        }),
                    ]
                }
            ),
            tl::enums::KeyboardButtonRow::KeyboardButtonRow(
                tl::types::KeyboardButtonRow {
                    buttons: vec![
                        tl::enums::KeyboardButton::KeyboardButton(tl::types::KeyboardButton {
                            style: None,
                            text: "โŒ Cancel".into(),
                            r#type: tl::enums::ButtonType::Default(tl::types::ButtonTypeDefault {}),
                        }),
                    ]
                }
            ),
        ],
    }
);
}

The userโ€™s choices arrive as plain text NewMessage updates.

Remove keyboard

#![allow(unused)]
fn main() {
let remove = tl::enums::ReplyMarkup::ReplyKeyboardHide(
    tl::types::ReplyKeyboardHide { selective: false }
);
let msg = InputMessage::text("Keyboard removed.").reply_markup(remove);
}

Button data format

Telegram limits callback button data to 64 bytes. Use compact, parseable formats:

#![allow(unused)]
fn main() {
// Good: structured, compact
"vote:yes"
"page:3"
"item:42:delete"
"menu:settings:notifications"

// Bad: verbose
"user_clicked_the_settings_button"
}