-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtelebot_calendar.py
371 lines (313 loc) · 10.7 KB
/
telebot_calendar.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import datetime
import calendar
import typing
from dataclasses import dataclass
from telebot import TeleBot
from telebot.types import InlineKeyboardButton, InlineKeyboardMarkup, CallbackQuery
@dataclass
class Language:
days: tuple
months: tuple
ENGLISH_LANGUAGE = Language(
days=("Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"),
months=(
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
),
)
RUSSIAN_LANGUAGE = Language(
days=("Пн", "Вт", "Ср", "Чт", "Пт", "Сб", "Вс"),
months=(
"Январь",
"Февраль",
"Март",
"Апрель",
"Май",
"Июнь",
"Июль",
"Август",
"Сентябрь",
"Октябрь",
"Ноябрь",
"Декабрь",
),
)
class Calendar:
"""
Calendar data factory
"""
__lang: Language
def __init__(self, language: Language = ENGLISH_LANGUAGE):
self.__lang = language
def create_calendar(
self,
name: str = "calendar",
year: int = None,
month: int = None,
) -> InlineKeyboardMarkup:
"""
Create a built in inline keyboard with calendar
:param name:
:param year: Year to use in the calendar if you are not using the current year.
:param month: Month to use in the calendar if you are not using the current month.
:return: Returns an InlineKeyboardMarkup object with a calendar.
"""
now_day = datetime.datetime.now()
if year is None:
year = now_day.year
if month is None:
month = now_day.month
calendar_callback = CallbackData(name, "action", "year", "month", "day")
data_ignore = calendar_callback.new("IGNORE", year, month, "!")
data_months = calendar_callback.new("MONTHS", year, month, "!")
keyboard = InlineKeyboardMarkup(row_width=7)
keyboard.add(
InlineKeyboardButton(
self.__lang.months[month - 1] + " " + str(year),
callback_data=data_months,
)
)
keyboard.add(
*[
InlineKeyboardButton(day, callback_data=data_ignore)
for day in self.__lang.days
]
)
for week in calendar.monthcalendar(year, month):
row = list()
for day in week:
if day == 0:
row.append(InlineKeyboardButton(" ", callback_data=data_ignore))
elif (
f"{now_day.day}.{now_day.month}.{now_day.year}"
== f"{day}.{month}.{year}"
):
row.append(
InlineKeyboardButton(
f"({day})",
callback_data=calendar_callback.new(
"DAY", year, month, day
),
)
)
else:
row.append(
InlineKeyboardButton(
str(day),
callback_data=calendar_callback.new(
"DAY", year, month, day
),
)
)
keyboard.add(*row)
keyboard.add(
InlineKeyboardButton(
"<",
callback_data=calendar_callback.new("PREVIOUS-MONTH", year, month, "!"),
),
InlineKeyboardButton(
"Cancel",
callback_data=calendar_callback.new("CANCEL", year, month, "!"),
),
InlineKeyboardButton(
">", callback_data=calendar_callback.new("NEXT-MONTH", year, month, "!")
),
)
return keyboard
def create_months_calendar(
self, name: str = "calendar", year: int = None
) -> InlineKeyboardMarkup:
"""
Creates a calendar with month selection
:param name:
:param year:
:return:
"""
if year is None:
year = datetime.datetime.now().year
calendar_callback = CallbackData(name, "action", "year", "month", "day")
keyboard = InlineKeyboardMarkup()
for i, month in enumerate(
zip(self.__lang.months[0::2], self.__lang.months[1::2])
):
keyboard.add(
InlineKeyboardButton(
month[0],
callback_data=calendar_callback.new("MONTH", year, 2 * i + 1, "!"),
),
InlineKeyboardButton(
month[1],
callback_data=calendar_callback.new(
"MONTH", year, (i + 1) * 2, "!"
),
),
)
return keyboard
def calendar_query_handler(
self,
bot: TeleBot,
call: CallbackQuery,
name: str,
action: str,
year: int,
month: int,
day: int,
) -> None or datetime.datetime:
"""
The method creates a new calendar if the forward or backward button is pressed
This method should be called inside CallbackQueryHandler.
:param bot: The object of the bot CallbackQueryHandler
:param call: CallbackQueryHandler data
:param day:
:param month:
:param year:
:param action:
:param name:
:return: Returns a tuple
"""
current = datetime.datetime(int(year), int(month), 1)
if action == "IGNORE":
bot.answer_callback_query(callback_query_id=call.id)
return False, None
elif action == "DAY":
bot.delete_message(
chat_id=call.message.chat.id, message_id=call.message.message_id
)
return datetime.datetime(int(year), int(month), int(day))
elif action == "PREVIOUS-MONTH":
preview_month = current - datetime.timedelta(days=1)
bot.edit_message_text(
text=call.message.text,
chat_id=call.message.chat.id,
message_id=call.message.message_id,
reply_markup=self.create_calendar(
name=name,
year=int(preview_month.year),
month=int(preview_month.month),
),
)
return None
elif action == "NEXT-MONTH":
next_month = current + datetime.timedelta(days=31)
bot.edit_message_text(
text=call.message.text,
chat_id=call.message.chat.id,
message_id=call.message.message_id,
reply_markup=self.create_calendar(
name=name, year=int(next_month.year), month=int(next_month.month)
),
)
return None
elif action == "MONTHS":
bot.edit_message_text(
text=call.message.text,
chat_id=call.message.chat.id,
message_id=call.message.message_id,
reply_markup=self.create_months_calendar(name=name, year=current.year),
)
return None
elif action == "MONTH":
bot.edit_message_text(
text=call.message.text,
chat_id=call.message.chat.id,
message_id=call.message.message_id,
reply_markup=self.create_calendar(
name=name, year=int(year), month=int(month)
),
)
return None
elif action == "CANCEL":
bot.delete_message(
chat_id=call.message.chat.id, message_id=call.message.message_id
)
return "CANCEL", None
else:
bot.answer_callback_query(callback_query_id=call.id, text="ERROR!")
bot.delete_message(
chat_id=call.message.chat.id, message_id=call.message.message_id
)
return None
class CallbackData:
"""
Callback data factory
"""
def __init__(self, prefix, *parts, sep=":"):
if not isinstance(prefix, str):
raise TypeError(
f"Prefix must be instance of str not {type(prefix).__name__}"
)
if not prefix:
raise ValueError("Prefix can't be empty")
if sep in prefix:
raise ValueError(f"Separator {sep!r} can't be used in prefix")
if not parts:
raise TypeError("Parts were not passed!")
self.prefix = prefix
self.sep = sep
self._part_names = parts
def new(self, *args, **kwargs) -> str:
"""
Generate callback data
:param args:
:param kwargs:
:return:
"""
args = list(args)
data = [self.prefix]
for part in self._part_names:
value = kwargs.pop(part, None)
if value is None:
if args:
value = args.pop(0)
else:
raise ValueError(f"Value for {part!r} was not passed!")
if value is not None and not isinstance(value, str):
value = str(value)
if not value:
raise ValueError(f"Value for part {part!r} can't be empty!'")
if self.sep in value:
raise ValueError(
f"Symbol {self.sep!r} is defined as the separator and can't be used in parts' values"
)
data.append(value)
if args or kwargs:
raise TypeError("Too many arguments were passed!")
callback_data = self.sep.join(data)
if len(callback_data) > 64:
raise ValueError("Resulted callback data is too long!")
return callback_data
def parse(self, callback_data: str) -> typing.Dict[str, str]:
"""
Parse data from the callback data
:param callback_data:
:return:
"""
prefix, *parts = callback_data.split(self.sep)
if prefix != self.prefix:
raise ValueError("Passed callback data can't be parsed with that prefix.")
elif len(parts) != len(self._part_names):
raise ValueError("Invalid parts count!")
result = {"@": prefix}
result.update(zip(self._part_names, parts))
return result
def filter(self, **config):
"""
Generate filter
:param config:
:return:
"""
print(config, self._part_names)
for key in config.keys():
if key not in self._part_names:
return False
return True