Support configuring the reply markup through buttons

This commit is contained in:
Lonami Exo 2019-01-03 19:03:47 +01:00
parent 2631144702
commit eda8d0dbc8
2 changed files with 57 additions and 18 deletions

View File

@ -32,12 +32,24 @@ class ButtonMethods(UpdateMethods):
is_inline = False is_inline = False
is_normal = False is_normal = False
resize = None
single_use = None
selective = None
rows = [] rows = []
for row in buttons: for row in buttons:
current = [] current = []
for button in row: for button in row:
if isinstance(button, custom.MessageButton): if isinstance(button, custom.Button):
if button.resize is not None:
resize = button.resize
if button.single_use is not None:
single_use = button.single_use
if button.selective is not None:
selective = button.selective
button = button.button
elif isinstance(button, custom.MessageButton):
button = button.button button = button.button
inline = custom.Button._is_inline(button) inline = custom.Button._is_inline(button)
@ -57,5 +69,6 @@ class ButtonMethods(UpdateMethods):
raise ValueError('You cannot mix inline with normal buttons') raise ValueError('You cannot mix inline with normal buttons')
elif is_inline: elif is_inline:
return types.ReplyInlineMarkup(rows) return types.ReplyInlineMarkup(rows)
elif is_normal: # elif is_normal:
return types.ReplyKeyboardMarkup(rows) return types.ReplyKeyboardMarkup(
rows, resize=resize, single_use=single_use, selective=selective)

View File

@ -1,5 +1,4 @@
from .. import types from .. import types
from .messagebutton import MessageButton
class Button: class Button:
@ -25,6 +24,7 @@ class Button:
You can use `text`, `request_location` and `request_phone` You can use `text`, `request_location` and `request_phone`
together to create a reply markup (replaces the user keyboard). together to create a reply markup (replaces the user keyboard).
You can also configure the aspect of the reply with these.
You **cannot** mix the two type of buttons together, You **cannot** mix the two type of buttons together,
and it will error if you try to do so. and it will error if you try to do so.
@ -34,9 +34,11 @@ class Button:
to 128 characters and add the ellipsis () character as to 128 characters and add the ellipsis () character as
the 129. the 129.
""" """
def __init__(self): def __init__(self, button, *, resize, single_use, selective):
raise ValueError('Cannot create instances of this class; ' self.button = button
'use the static methods') self.resize = resize
self.single_use = single_use
self.selective = selective
@staticmethod @staticmethod
def _is_inline(button): def _is_inline(button):
@ -93,28 +95,52 @@ class Button:
""" """
return types.KeyboardButtonUrl(text, url or text) return types.KeyboardButtonUrl(text, url or text)
@staticmethod @classmethod
def text(text): def text(cls, text, *, resize=None, single_use=None, selective=None):
""" """
Creates a new button with the given text. Creates a new button with the given text.
"""
return types.KeyboardButton(text)
@staticmethod Args:
def request_location(text): resize (`bool`):
If present, the entire keyboard will be reconfigured to
be resized and be smaller if there are not many buttons.
single_use (`bool`):
If present, the entire keyboard will be reconfigured to
be usable only once before it hides itself.
selective (`bool`):
If present, the entire keyboard will be reconfigured to
be "selective". The keyboard will be shown only to specific
users. It will target users that are @mentioned in the text
of the message or to the sender of the message you reply to.
"""
return cls(types.KeyboardButton(text),
resize=resize, single_use=single_use, selective=selective)
@classmethod
def request_location(cls, text, *,
resize=None, single_use=None, selective=None):
""" """
Creates a new button that will request Creates a new button that will request
the user's location upon being clicked. the user's location upon being clicked.
"""
return types.KeyboardButtonRequestGeoLocation(text)
@staticmethod ``resize``, ``single_use`` and ``selective`` are documented in `text`.
def request_phone(text): """
return cls(types.KeyboardButtonRequestGeoLocation(text),
resize=resize, single_use=single_use, selective=selective)
@classmethod
def request_phone(cls, text, *,
resize=None, single_use=None, selective=None):
""" """
Creates a new button that will request Creates a new button that will request
the user's phone number upon being clicked. the user's phone number upon being clicked.
``resize``, ``single_use`` and ``selective`` are documented in `text`.
""" """
return types.KeyboardButtonRequestPhone(text) return cls(types.KeyboardButtonRequestPhone(text),
resize=resize, single_use=single_use, selective=selective)
@staticmethod @staticmethod
def clear(): def clear():