2019-05-03 22:37:27 +03:00
|
|
|
import typing
|
|
|
|
|
2021-09-12 13:16:02 +03:00
|
|
|
from .. import utils, hints, _tl
|
2019-05-03 22:37:27 +03:00
|
|
|
|
2018-07-10 13:42:57 +03:00
|
|
|
|
2021-09-11 14:33:27 +03:00
|
|
|
def build_reply_markup(
|
|
|
|
buttons: 'typing.Optional[hints.MarkupLike]',
|
2021-09-12 13:16:02 +03:00
|
|
|
inline_only: bool = False) -> 'typing.Optional[_tl.TypeReplyMarkup]':
|
2021-09-11 14:33:27 +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]]
|
|
|
|
elif not buttons or not utils.is_list_like(buttons[0]):
|
|
|
|
buttons = [buttons]
|
|
|
|
|
|
|
|
is_inline = False
|
|
|
|
is_normal = False
|
|
|
|
resize = None
|
|
|
|
single_use = None
|
|
|
|
selective = None
|
|
|
|
|
|
|
|
rows = []
|
|
|
|
for row in buttons:
|
|
|
|
current = []
|
|
|
|
for button in row:
|
2021-09-12 13:16:02 +03:00
|
|
|
if isinstance(button, _tl.custom.Button):
|
2021-09-11 14:33:27 +03:00
|
|
|
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
|
2021-09-12 13:16:02 +03:00
|
|
|
elif isinstance(button, _tl.custom.MessageButton):
|
2021-09-11 14:33:27 +03:00
|
|
|
button = button.button
|
|
|
|
|
2021-09-12 13:16:02 +03:00
|
|
|
inline = _tl.custom.Button._is_inline(button)
|
2021-09-11 14:33:27 +03:00
|
|
|
is_inline |= inline
|
|
|
|
is_normal |= not inline
|
|
|
|
|
|
|
|
if button.SUBCLASS_OF_ID == 0xbad74a3:
|
|
|
|
# 0xbad74a3 == crc32(b'KeyboardButton')
|
|
|
|
current.append(button)
|
|
|
|
|
|
|
|
if current:
|
2021-09-12 13:16:02 +03:00
|
|
|
rows.append(_tl.KeyboardButtonRow(current))
|
2021-09-11 14:33:27 +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:
|
|
|
|
raise ValueError('You cannot mix inline with normal buttons')
|
|
|
|
elif is_inline:
|
2021-09-12 13:16:02 +03:00
|
|
|
return _tl.ReplyInlineMarkup(rows)
|
2021-09-11 14:33:27 +03:00
|
|
|
# elif is_normal:
|
2021-09-12 13:16:02 +03:00
|
|
|
return _tl.ReplyKeyboardMarkup(
|
2021-09-11 14:33:27 +03:00
|
|
|
rows, resize=resize, single_use=single_use, selective=selective)
|