-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallback_keyboards.py
100 lines (78 loc) · 3.24 KB
/
callback_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import json
import random
from datetime import datetime, timedelta
from typing import List, Dict, Union
from telegram import InlineKeyboardMarkup, InlineKeyboardButton
from main import CallbackType
# CONSTANTS
KEY_KEYBOARD_BUTTON_ID_SHOW_ALL_DATES = "keyboard_action_show_all"
def german_weekday_name(date: datetime):
translations = {'Mon': 'Mo', 'Tue': 'Di', 'Wed': 'Mi', 'Thu': 'Do', 'Fri': 'Fr'}
name = date.strftime('%a')
if name in translations.keys():
return translations[name]
else:
return name
def get_select_dates_keyboard(days: Dict, show_all=False, max_selections=4) -> InlineKeyboardMarkup:
count = 0
delta_t = 0
data = []
action_text = []
timestamps = list(days.keys())
while count < max_selections or show_all:
ts_i = datetime.now() + timedelta(days=delta_t)
ts_i_str = (datetime.now() + timedelta(days=delta_t)).strftime('%d.%m.%Y')
if ts_i > datetime.strptime(timestamps[-1], '%d.%m.%Y'):
# the current timestamp is larger than the last provided by the api.
break
if ts_i_str in timestamps:
count += 1
if show_all:
# when showing all, always use the dates
name = ts_i.strftime('%d.%m')
elif delta_t == 0:
name = 'Heute'
elif delta_t == 1:
name = 'Morgen'
elif delta_t < 7:
name = german_weekday_name(ts_i)
else:
name = ts_i.strftime('%d.%m')
data.append(ts_i_str)
action_text.append(name)
delta_t += 1
if not show_all:
data.append(KEY_KEYBOARD_BUTTON_ID_SHOW_ALL_DATES)
action_text.append(random.choices(['🔎', '🐮', '🐼', '💯', '🔑'], weights=(1000, 10, 10, 10, 1), k=1)[0])
return get_callback_keyboard(callback_type=CallbackType.selected_date,
data=data,
action_text=action_text,
max_per_row=3 if show_all else None,
)
def get_callback_keyboard(callback_type: Union[CallbackType, List[CallbackType]], data: List, action_text: List,
one_per_row=False, max_per_row=None, ):
keyboard = []
is_type_list = isinstance(callback_type, list) or isinstance(callback_type, tuple)
for index in range(0, len(action_text)):
callback_type_str = callback_type[index].value if is_type_list else callback_type.value
callback_data = {
'type': callback_type_str,
'data': data[index],
}
keyboard.append(
InlineKeyboardButton(action_text[index],
callback_data=json.dumps(callback_data)),
)
if one_per_row:
return InlineKeyboardMarkup([[item] for item in keyboard])
elif max_per_row is not None:
# reshape keyboard to 2d with max_per_row items per row
count = 0
out = []
for b in keyboard:
index = count // max_per_row
(out[index].append(b)) if len(out) > index else (out.append([b]))
count += 1
return InlineKeyboardMarkup(out)
else:
return InlineKeyboardMarkup([keyboard])