Inline Keyboard

Build inline keyboards with automatic row layout, callback data, URL buttons, and Pyrogram integration.

Basic Usage

Creating an inline keyboard
from pykeyboard import InlineKeyboard, InlineButton

# Create with 2 buttons per row
kb = InlineKeyboard(row_width=2)
kb.add(
    InlineButton(text="✅ Yes", callback_data="confirm"),
    InlineButton(text="❌ No", callback_data="cancel"),
    InlineButton(text="ℹ️ Info", callback_data="info"),
)
# Layout: [Yes][No]
#         [Info]

await message.reply("Confirm?", reply_markup=kb)

Constructor

ParameterTypeDefaultDescription
row_widthint3Max buttons per row (1–8)

Button Types

Each InlineButton can be one of several types. Only one action parameter should be set per button.

ParameterTypeDescription
textstrButton label (required, non-empty)
callback_datastrData sent to callback query handler
urlstrURL to open when tapped
web_appWebAppInfoWeb App to open
login_urlLoginUrlLogin URL for seamless auth
switch_inline_querystrSwitch to inline mode in current chat
switch_inline_query_current_chatstrSwitch to inline in same chat
payboolPayment button (first button only)
copy_textstrText to copy to clipboard

URL Buttons

kb = InlineKeyboard()
kb.add(
    InlineButton(text="📖 Documentation", url="https://example.com/docs"),
    InlineButton(text="💻 GitHub", url="https://github.com/example"),
)

Serialization

Inline keyboards can be serialized to JSON and restored — useful for caching or database storage.

# Serialize
json_str = kb.to_json()

# Deserialize
restored = InlineKeyboard.from_json(json_str)

Methods

MethodReturnsDescription
add(*buttons)selfAdd buttons, auto-wrapping into rows
row(*buttons)selfAdd a single row of buttons (ignoring row_width)
paginate(...)selfAdd pagination controls (see Pagination page)
languages(...)selfAdd language selection buttons (see Languages page)
to_json()strSerialize keyboard to JSON string
from_json(data)InlineKeyboardClass method: restore from JSON