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
| Parameter | Type | Default | Description |
|---|---|---|---|
row_width | int | 3 | Max buttons per row (1–8) |
Button Types
Each InlineButton can be one of several types. Only one action parameter should be set per button.
| Parameter | Type | Description |
|---|---|---|
text | str | Button label (required, non-empty) |
callback_data | str | Data sent to callback query handler |
url | str | URL to open when tapped |
web_app | WebAppInfo | Web App to open |
login_url | LoginUrl | Login URL for seamless auth |
switch_inline_query | str | Switch to inline mode in current chat |
switch_inline_query_current_chat | str | Switch to inline in same chat |
pay | bool | Payment button (first button only) |
copy_text | str | Text 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
| Method | Returns | Description |
|---|---|---|
add(*buttons) | self | Add buttons, auto-wrapping into rows |
row(*buttons) | self | Add a single row of buttons (ignoring row_width) |
paginate(...) | self | Add pagination controls (see Pagination page) |
languages(...) | self | Add language selection buttons (see Languages page) |
to_json() | str | Serialize keyboard to JSON string |
from_json(data) | InlineKeyboard | Class method: restore from JSON |