mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
InlineKeyboardMarkup: port 'add', 'row' and 'insert' functions
This commit is contained in:
parent
de3c5c1a8d
commit
34cac79a14
1 changed files with 45 additions and 1 deletions
|
|
@ -18,5 +18,49 @@ class InlineKeyboardMarkup(MutableTelegramObject):
|
|||
Source: https://core.telegram.org/bots/api#inlinekeyboardmarkup
|
||||
"""
|
||||
|
||||
inline_keyboard: List[List[InlineKeyboardButton]]
|
||||
row_width: int = 3
|
||||
inline_keyboard: List[List[InlineKeyboardButton]] = []
|
||||
"""Array of button rows, each represented by an Array of InlineKeyboardButton objects"""
|
||||
|
||||
def add(self, *args: InlineKeyboardButton) -> InlineKeyboardMarkup:
|
||||
"""
|
||||
Add buttons
|
||||
:param args:
|
||||
:return: self
|
||||
:rtype: :obj:`types.InlineKeyboardMarkup`
|
||||
"""
|
||||
row = []
|
||||
for index, button in enumerate(args, start=1):
|
||||
row.append(button)
|
||||
if index % self.row_width == 0:
|
||||
self.inline_keyboard.append(row)
|
||||
row = []
|
||||
if len(row) > 0:
|
||||
self.inline_keyboard.append(row)
|
||||
return self
|
||||
|
||||
def row(self, *args: InlineKeyboardButton) -> InlineKeyboardMarkup:
|
||||
"""
|
||||
Add row
|
||||
:param args:
|
||||
:return: self
|
||||
:rtype: :obj:`types.InlineKeyboardMarkup`
|
||||
"""
|
||||
btn_array = []
|
||||
for button in args:
|
||||
btn_array.append(button)
|
||||
self.inline_keyboard.append(btn_array)
|
||||
return self
|
||||
|
||||
def insert(self, button: InlineKeyboardButton) -> InlineKeyboardMarkup:
|
||||
"""
|
||||
Insert button to last row
|
||||
:param button:
|
||||
:return: self
|
||||
:rtype: :obj:`types.InlineKeyboardMarkup`
|
||||
"""
|
||||
if self.inline_keyboard and len(self.inline_keyboard[-1]) < self.row_width:
|
||||
self.inline_keyboard[-1].append(button)
|
||||
else:
|
||||
self.add(button)
|
||||
return self
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue