-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboards.py
59 lines (49 loc) · 2.13 KB
/
keyboards.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import typing
from dataclasses import dataclass
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton, ReplyKeyboardMarkup, KeyboardButton
@dataclass
class ListOfButtons:
text: typing.List
callback: typing.List = None
align: typing.List[int] = None
"""
Использование:
ListOfButtons(text=["Кнопка", "Кнопка", "Кнопка", "Кнопка"],
callback=["callback1", "callback2", "callback3", "callback4"],
align=[1, 2, 1]).keyboard
row_sizes - количество кнопок в ряде
"""
@property
def inline_keyboard(self):
return generate_inline_keyboard(self)
@property
def reply_keyboard(self):
return generate_reply_keyboard(self)
def generate_inline_keyboard(args: ListOfButtons) -> InlineKeyboardMarkup:
keyboard = InlineKeyboardMarkup()
if args.text and args.callback and not (len(args.text) == len(args.callback)):
raise IndexError("Все списки должны быть одной длины!")
if not args.align:
for num, button in enumerate(args.text):
keyboard.add(InlineKeyboardButton(text=str(button),
callback_data=str(args.callback[num])))
else:
count = 0
for row_size in args.align:
keyboard.row(*[InlineKeyboardButton(text=str(text), callback_data=str(callback_data))
for text, callback_data in
tuple(zip(args.text, args.callback))[count:count + row_size]])
count += row_size
return keyboard
#callback_data=str(args.callback[num])
def generate_reply_keyboard(args: ListOfButtons) -> ReplyKeyboardMarkup:
keyboard = ReplyKeyboardMarkup(resize_keyboard=True)
if not args.align:
for num, button in enumerate(args.text):
keyboard.add(KeyboardButton(text=str(button)))
else:
count = 0
for row_size in args.align:
keyboard.row(*[KeyboardButton(text=str(text)) for text in args.text[count:count + row_size]])
count += row_size
return keyboard