2022-05-23 12:40:30 +00:00
|
|
|
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, ReplyKeyboardMarkup, ForceReply
|
|
|
|
|
2022-06-26 12:59:36 +00:00
|
|
|
|
|
|
|
def ikb(rows=None):
|
|
|
|
if rows is None:
|
|
|
|
rows = []
|
2022-05-23 12:40:30 +00:00
|
|
|
lines = []
|
|
|
|
for row in rows:
|
|
|
|
line = []
|
|
|
|
for button in row:
|
2022-06-26 12:59:36 +00:00
|
|
|
button = btn(*button) # InlineKeyboardButton
|
2022-05-23 12:40:30 +00:00
|
|
|
line.append(button)
|
|
|
|
lines.append(line)
|
|
|
|
return InlineKeyboardMarkup(inline_keyboard=lines)
|
2022-06-26 12:59:36 +00:00
|
|
|
# return {'inline_keyboard': lines}
|
|
|
|
|
2022-05-23 12:40:30 +00:00
|
|
|
|
2022-06-26 12:59:36 +00:00
|
|
|
def btn(text, value, type='callback_data'):
|
2022-05-23 12:40:30 +00:00
|
|
|
return InlineKeyboardButton(text, **{type: value})
|
2022-06-26 12:59:36 +00:00
|
|
|
# return {'text': text, type: value}
|
|
|
|
|
2022-05-23 12:40:30 +00:00
|
|
|
|
|
|
|
# The inverse of above
|
|
|
|
def bki(keyboard):
|
|
|
|
lines = []
|
|
|
|
for row in keyboard.inline_keyboard:
|
|
|
|
line = []
|
|
|
|
for button in row:
|
2022-06-26 12:59:36 +00:00
|
|
|
button = ntb(button) # btn() format
|
2022-05-23 12:40:30 +00:00
|
|
|
line.append(button)
|
|
|
|
lines.append(line)
|
|
|
|
return lines
|
2022-06-26 12:59:36 +00:00
|
|
|
# return ikb() format
|
|
|
|
|
2022-05-23 12:40:30 +00:00
|
|
|
|
|
|
|
def ntb(button):
|
2022-06-26 12:59:36 +00:00
|
|
|
for btn_type in ['callback_data', 'url', 'switch_inline_query', 'switch_inline_query_current_chat',
|
|
|
|
'callback_game']:
|
2022-05-23 12:40:30 +00:00
|
|
|
value = getattr(button, btn_type)
|
|
|
|
if value:
|
|
|
|
break
|
|
|
|
button = [button.text, value]
|
|
|
|
if btn_type != 'callback_data':
|
|
|
|
button.append(btn_type)
|
|
|
|
return button
|
2022-06-26 12:59:36 +00:00
|
|
|
# return {'text': text, type: value}
|
2022-05-23 12:40:30 +00:00
|
|
|
|
2022-06-26 12:59:36 +00:00
|
|
|
|
|
|
|
def kb(rows=None, **kwargs):
|
|
|
|
if rows is None:
|
|
|
|
rows = []
|
2022-05-23 12:40:30 +00:00
|
|
|
lines = []
|
|
|
|
for row in rows:
|
|
|
|
line = []
|
|
|
|
for button in row:
|
|
|
|
button_type = type(button)
|
|
|
|
if button_type == str:
|
|
|
|
button = KeyboardButton(button)
|
|
|
|
elif button_type == dict:
|
|
|
|
button = KeyboardButton(**button)
|
2022-06-26 12:59:36 +00:00
|
|
|
|
2022-05-23 12:40:30 +00:00
|
|
|
line.append(button)
|
|
|
|
lines.append(line)
|
|
|
|
return ReplyKeyboardMarkup(keyboard=lines, **kwargs)
|
|
|
|
|
2022-06-26 12:59:36 +00:00
|
|
|
|
2022-05-23 12:40:30 +00:00
|
|
|
kbtn = KeyboardButton
|
|
|
|
|
2022-06-26 12:59:36 +00:00
|
|
|
|
2022-05-23 12:40:30 +00:00
|
|
|
def force_reply(selective=True):
|
|
|
|
return ForceReply(selective=selective)
|
|
|
|
|
|
|
|
|
2022-06-26 12:59:36 +00:00
|
|
|
def array_chunk(input_, size):
|
|
|
|
return [input_[i:i + size] for i in range(0, len(input_), size)]
|