sticker-captcha-bot/pyromod/helpers/helpers.py

87 lines
2.0 KiB
Python
Raw Normal View History

2023-09-11 13:05:50 +00:00
from pyrogram.types import (
InlineKeyboardButton,
InlineKeyboardMarkup,
KeyboardButton,
ReplyKeyboardMarkup,
ForceReply,
)
2022-07-02 11:44:57 +00:00
def ikb(rows=None):
if rows is None:
rows = []
lines = []
for row in rows:
line = []
for button in row:
button = btn(*button) # InlineKeyboardButton
line.append(button)
lines.append(line)
return InlineKeyboardMarkup(inline_keyboard=lines)
# return {'inline_keyboard': lines}
2023-09-11 13:05:50 +00:00
def btn(text, value, type="callback_data"):
2022-07-02 11:44:57 +00:00
return InlineKeyboardButton(text, **{type: value})
# return {'text': text, type: value}
# The inverse of above
def bki(keyboard):
lines = []
for row in keyboard.inline_keyboard:
line = []
for button in row:
button = ntb(button) # btn() format
line.append(button)
lines.append(line)
return lines
# return ikb() format
def ntb(button):
2023-09-11 13:05:50 +00:00
for btn_type in [
"callback_data",
"url",
"switch_inline_query",
"switch_inline_query_current_chat",
"callback_game",
]:
2022-07-02 11:44:57 +00:00
value = getattr(button, btn_type)
if value:
break
button = [button.text, value]
2023-09-11 13:05:50 +00:00
if btn_type != "callback_data":
2022-07-02 11:44:57 +00:00
button.append(btn_type)
return button
# return {'text': text, type: value}
def kb(rows=None, **kwargs):
if rows is None:
rows = []
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)
line.append(button)
lines.append(line)
return ReplyKeyboardMarkup(keyboard=lines, **kwargs)
kbtn = KeyboardButton
def force_reply(selective=True):
return ForceReply(selective=selective)
def array_chunk(input_, size):
2023-09-11 13:05:50 +00:00
return [input_[i : i + size] for i in range(0, len(input_), size)]