Keyboard builder#
Keyboard builder helps to dynamically generate markup.
Note
Note that if you have static markup, it’s best to define it explicitly rather than using builder, but if you have dynamic markup configuration, feel free to use builder as you wish.
Usage example#
builder = InlineKeyboardBuilder()
for index in range(1, 11):
builder.button(text=f"Set {index}", callback_data=f"set:{index}")
builder.adjust(3, 2)
await message.answer("Some text here", reply_markup=builder.as_markup())
Base builder#
- class aiogram.utils.keyboard.ReplyKeyboardBuilder(markup: Optional[List[List[KeyboardButton]]] = None)[source]#
Reply keyboard builder inherits all methods from generic builder
- __init__(markup: Optional[List[List[KeyboardButton]]] = None) None [source]#
- add(*buttons: ButtonType) KeyboardBuilder[ButtonType] #
Add one or many buttons to markup.
- Parameters:
buttons –
- Returns:
- adjust(*sizes: int, repeat: bool = False) KeyboardBuilder[ButtonType] #
Adjust previously added buttons to specific row sizes.
By default, when the sum of passed sizes is lower than buttons count the last one size will be used for tail of the markup. If repeat=True is passed - all sizes will be cycled when available more buttons count than all sizes
- Parameters:
sizes –
repeat –
- Returns:
- as_markup(**kwargs: Any) Union[InlineKeyboardMarkup, ReplyKeyboardMarkup] [source]#
- property buttons: Generator[ButtonType, None, None]#
Get flatten set of all buttons
- Returns:
- copy() ReplyKeyboardBuilder [source]#
Make full copy of current builder with markup
- Returns:
- export() List[List[ButtonType]] #
Export configured markup as list of lists of buttons
>>> builder = KeyboardBuilder(button_type=InlineKeyboardButton) >>> ... # Add buttons to builder >>> markup = InlineKeyboardMarkup(inline_keyboard=builder.export())
- Returns:
- row(*buttons: ButtonType, width: int = 8) KeyboardBuilder[ButtonType] #
Add row to markup
When too much buttons is passed it will be separated to many rows
- Parameters:
buttons –
width –
- Returns:
Inline Keyboard#
- class aiogram.utils.keyboard.InlineKeyboardBuilder(markup: Optional[List[List[InlineKeyboardButton]]] = None)[source]
Inline keyboard builder inherits all methods from generic builder
- button(text: str, url: Optional[str] = None, login_url: Optional[LoginUrl] = None, callback_data: Optional[Union[str, CallbackData]] = None, switch_inline_query: Optional[str] = None, switch_inline_query_current_chat: Optional[str] = None, callback_game: Optional[CallbackGame] = None, pay: Optional[bool] = None, **kwargs: Any) aiogram.utils.keyboard.InlineKeyboardBuilder [source]
Add new inline button to markup
- as_markup() aiogram.types.inline_keyboard_markup.InlineKeyboardMarkup [source]
Construct an InlineKeyboardMarkup