v0.3.x · pykeyboard

Inline Keyboard

Build Telegram inline keyboards with automatic row management.InlineKeyboard wraps a list of InlineButton rows and serialises to a pyrogram.types.InlineKeyboardMarkup.

Basic Usage

from pykeyboard import InlineKeyboard, InlineButton

kb = InlineKeyboard(row_width=2)
kb.add(
    InlineButton("Button 1", callback_data="btn1"),
    InlineButton("Button 2", callback_data="btn2"),
    InlineButton("Button 3", callback_data="btn3"),
)

await message.reply("Pick one:", reply_markup=kb)

row_width=2 means each row holds at most 2 buttons. The third button automatically wraps to the next row.

InlineButton Types

# Callback data button
InlineButton("Click me", callback_data="action:123")

# URL button
InlineButton("Open link", url="https://example.com")

# Switch inline query
InlineButton("Search", switch_inline_query="query")

# Switch inline in current chat
InlineButton("Search here", switch_inline_query_current_chat="")

Row Control

kb = InlineKeyboard()

# add() groups buttons in rows of row_width
kb.add(btn1, btn2, btn3)

# row() forces each group to its own row
kb.row(
    InlineButton("Full-width", callback_data="wide")
)

# Insert at specific row index
kb.insert(InlineButton("Inserted", callback_data="ins"), row=0)

Dynamic Keyboards

items = [("Apple", "fruit:apple"), ("Banana", "fruit:banana"), ("Cherry", "fruit:cherry")]

kb = InlineKeyboard(row_width=2)
kb.add(*[InlineButton(label, callback_data=data) for label, data in items])
kb.row(InlineButton("✗ Cancel", callback_data="cancel"))

API Reference

MethodDescription
add(*buttons)Add buttons, wrapping at row_width
row(*buttons)Add buttons on a dedicated row
insert(button, row)Insert into a specific row index
keyboardProperty — returns raw 2D list