mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
UPG: Refactoring InlineKeyboardMarkup
This commit is contained in:
parent
df640966c2
commit
1824637617
@ -855,17 +855,27 @@ class KeyboardButton(Dictionaryable, JsonSerializable):
|
|||||||
|
|
||||||
class InlineKeyboardMarkup(Dictionaryable, JsonSerializable):
|
class InlineKeyboardMarkup(Dictionaryable, JsonSerializable):
|
||||||
def __init__(self, row_width=3):
|
def __init__(self, row_width=3):
|
||||||
|
"""
|
||||||
|
This object represents an inline keyboard that appears
|
||||||
|
right next to the message it belongs to.
|
||||||
|
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
self.row_width = row_width
|
self.row_width = row_width
|
||||||
self.keyboard = []
|
self.keyboard = []
|
||||||
|
|
||||||
def add(self, *args):
|
def add(self, *args):
|
||||||
"""
|
"""
|
||||||
This function adds strings to the keyboard, while not exceeding row_width.
|
This method adds buttons to the keyboard without exceeding row_width.
|
||||||
E.g. ReplyKeyboardMarkup#add("A", "B", "C") yields the json result {keyboard: [["A"], ["B"], ["C"]]}
|
|
||||||
|
E.g. InlineKeyboardMarkup#add("A", "B", "C") yields the json result:
|
||||||
|
{keyboard: [["A"], ["B"], ["C"]]}
|
||||||
when row_width is set to 1.
|
when row_width is set to 1.
|
||||||
When row_width is set to 2, the following is the result of this function: {keyboard: [["A", "B"], ["C"]]}
|
When row_width is set to 2, the result:
|
||||||
See https://core.telegram.org/bots/api#replykeyboardmarkup
|
{keyboard: [["A", "B"], ["C"]]}
|
||||||
:param args: KeyboardButton to append to the keyboard
|
See https://core.telegram.org/bots/api#inlinekeyboardmarkup
|
||||||
|
|
||||||
|
:param args: Array of InlineKeyboardButton to append to the keyboard
|
||||||
"""
|
"""
|
||||||
i = 1
|
i = 1
|
||||||
row = []
|
row = []
|
||||||
@ -880,26 +890,28 @@ class InlineKeyboardMarkup(Dictionaryable, JsonSerializable):
|
|||||||
|
|
||||||
def row(self, *args):
|
def row(self, *args):
|
||||||
"""
|
"""
|
||||||
Adds a list of KeyboardButton to the keyboard. This function does not consider row_width.
|
Adds a list of InlineKeyboardButton to the keyboard.
|
||||||
ReplyKeyboardMarkup#row("A")#row("B", "C")#to_json() outputs '{keyboard: [["A"], ["B", "C"]]}'
|
This metod does not consider row_width.
|
||||||
|
|
||||||
|
InlineKeyboardMarkup.row("A").row("B", "C").to_json() outputs:
|
||||||
|
'{keyboard: [["A"], ["B", "C"]]}'
|
||||||
See https://core.telegram.org/bots/api#inlinekeyboardmarkup
|
See https://core.telegram.org/bots/api#inlinekeyboardmarkup
|
||||||
:param args: strings
|
|
||||||
|
:param args: Array of InlineKeyboardButton to append to the keyboard
|
||||||
:return: self, to allow function chaining.
|
:return: self, to allow function chaining.
|
||||||
"""
|
"""
|
||||||
btn_array = []
|
button_array = [button.to_dict() for button in args]
|
||||||
for button in args:
|
self.keyboard.append(button_array)
|
||||||
btn_array.append(button.to_dict())
|
|
||||||
self.keyboard.append(btn_array)
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def to_json(self):
|
def to_json(self):
|
||||||
"""
|
"""
|
||||||
Converts this object to its json representation following the Telegram API guidelines described here:
|
Converts this object to its json representation
|
||||||
|
following the Telegram API guidelines described here:
|
||||||
https://core.telegram.org/bots/api#inlinekeyboardmarkup
|
https://core.telegram.org/bots/api#inlinekeyboardmarkup
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
json_dict = {'inline_keyboard': self.keyboard}
|
return json.dumps(self.to_dict())
|
||||||
return json.dumps(json_dict)
|
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
json_dict = {'inline_keyboard': self.keyboard}
|
json_dict = {'inline_keyboard': self.keyboard}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user