2019-05-03 22:37:27 +03:00
|
|
|
import typing
|
|
|
|
|
|
|
|
from .. import utils, hints
|
2018-07-10 13:42:57 +03:00
|
|
|
from ..tl import types, custom
|
2019-05-03 22:37:27 +03:00
|
|
|
|
2018-07-10 13:42:57 +03:00
|
|
|
|
2019-06-24 18:48:46 +03:00
|
|
|
class ButtonMethods:
|
2019-05-06 12:38:26 +03:00
|
|
|
@staticmethod
|
2019-05-03 22:37:27 +03:00
|
|
|
def build_reply_markup(
|
2019-05-08 18:16:09 +03:00
|
|
|
buttons: 'typing.Optional[hints.MarkupLike]',
|
|
|
|
inline_only: bool = False) -> 'typing.Optional[types.TypeReplyMarkup]':
|
2018-07-21 14:54:36 +03:00
|
|
|
"""
|
2019-05-06 12:38:26 +03:00
|
|
|
Builds a :tl:`ReplyInlineMarkup` or :tl:`ReplyKeyboardMarkup` for
|
|
|
|
the given buttons.
|
|
|
|
|
|
|
|
Does nothing if either no buttons are provided or the provided
|
|
|
|
argument is already a reply markup.
|
|
|
|
|
|
|
|
You should consider using this method if you are going to reuse
|
|
|
|
the markup very often. Otherwise, it is not necessary.
|
|
|
|
|
|
|
|
This method is **not** asynchronous (don't use ``await`` on it).
|
2019-05-20 12:38:26 +03:00
|
|
|
|
|
|
|
Arguments
|
|
|
|
buttons (`hints.MarkupLike`):
|
|
|
|
The button, list of buttons, array of buttons or markup
|
|
|
|
to convert into a markup.
|
|
|
|
|
|
|
|
inline_only (`bool`, optional):
|
|
|
|
Whether the buttons **must** be inline buttons only or not.
|
|
|
|
|
|
|
|
Example
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from telethon import Button
|
|
|
|
|
|
|
|
markup = client.build_reply_markup(Button.inline('hi'))
|
2020-01-19 15:25:58 +03:00
|
|
|
# later
|
|
|
|
await client.send_message(chat, 'click me', buttons=markup)
|
2018-07-21 14:54:36 +03:00
|
|
|
"""
|
2018-07-10 13:42:57 +03:00
|
|
|
if buttons is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
|
|
|
if buttons.SUBCLASS_OF_ID == 0xe2e10ef2:
|
|
|
|
return buttons # crc32(b'ReplyMarkup'):
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if not utils.is_list_like(buttons):
|
|
|
|
buttons = [[buttons]]
|
2019-12-23 16:49:40 +03:00
|
|
|
elif not buttons or not utils.is_list_like(buttons[0]):
|
2018-07-10 13:42:57 +03:00
|
|
|
buttons = [buttons]
|
|
|
|
|
|
|
|
is_inline = False
|
|
|
|
is_normal = False
|
2019-01-03 21:03:47 +03:00
|
|
|
resize = None
|
|
|
|
single_use = None
|
|
|
|
selective = None
|
2018-07-10 13:42:57 +03:00
|
|
|
|
|
|
|
rows = []
|
|
|
|
for row in buttons:
|
|
|
|
current = []
|
|
|
|
for button in row:
|
2019-01-03 21:03:47 +03:00
|
|
|
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):
|
2018-10-05 21:53:32 +03:00
|
|
|
button = button.button
|
|
|
|
|
2018-07-10 13:42:57 +03:00
|
|
|
inline = custom.Button._is_inline(button)
|
|
|
|
is_inline |= inline
|
|
|
|
is_normal |= not inline
|
|
|
|
|
|
|
|
if button.SUBCLASS_OF_ID == 0xbad74a3:
|
|
|
|
# 0xbad74a3 == crc32(b'KeyboardButton')
|
|
|
|
current.append(button)
|
|
|
|
|
|
|
|
if current:
|
|
|
|
rows.append(types.KeyboardButtonRow(current))
|
|
|
|
|
2018-07-15 12:31:14 +03:00
|
|
|
if inline_only and is_normal:
|
|
|
|
raise ValueError('You cannot use non-inline buttons here')
|
|
|
|
elif is_inline == is_normal and is_normal:
|
2018-07-10 13:42:57 +03:00
|
|
|
raise ValueError('You cannot mix inline with normal buttons')
|
|
|
|
elif is_inline:
|
|
|
|
return types.ReplyInlineMarkup(rows)
|
2019-01-03 21:03:47 +03:00
|
|
|
# elif is_normal:
|
|
|
|
return types.ReplyKeyboardMarkup(
|
|
|
|
rows, resize=resize, single_use=single_use, selective=selective)
|