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_normal = False
resize = None
single_use = None
selective = None
rows = []
for row in buttons:
current = []
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
inline = custom.Button._is_inline(button)
@ -57,5 +69,6 @@ class ButtonMethods(UpdateMethods):
raise ValueError('You cannot mix inline with normal buttons')
elif is_inline:
return types.ReplyInlineMarkup(rows)
elif is_normal:
return types.ReplyKeyboardMarkup(rows)
# elif is_normal:
return types.ReplyKeyboardMarkup(
rows, resize=resize, single_use=single_use, selective=selective)

View File

@ -1,5 +1,4 @@
from .. import types
from .messagebutton import MessageButton
class Button:
@ -25,6 +24,7 @@ class Button:
You can use `text`, `request_location` and `request_phone`
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,
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
the 129.
"""
def __init__(self):
raise ValueError('Cannot create instances of this class; '
'use the static methods')
def __init__(self, button, *, resize, single_use, selective):
self.button = button
self.resize = resize
self.single_use = single_use
self.selective = selective
@staticmethod
def _is_inline(button):
@ -93,28 +95,52 @@ class Button:
"""
return types.KeyboardButtonUrl(text, url or text)
@staticmethod
def text(text):
@classmethod
def text(cls, text, *, resize=None, single_use=None, selective=None):
"""
Creates a new button with the given text.
"""
return types.KeyboardButton(text)
@staticmethod
def request_location(text):
Args:
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
the user's location upon being clicked.
"""
return types.KeyboardButtonRequestGeoLocation(text)
@staticmethod
def request_phone(text):
``resize``, ``single_use`` and ``selective`` are documented in `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
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
def clear():