-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegramcalendar.py
More file actions
91 lines (81 loc) · 3.95 KB
/
Copy pathtelegramcalendar.py
File metadata and controls
91 lines (81 loc) · 3.95 KB
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
import datetime
import calendar
import json
def create_callback_data(action, year, month, day):
""" Create the callback data associated to each button"""
return ";".join([action, str(year), str(month), str(day)])
def separate_callback_data(data):
""" Separate the callback data"""
return data.split(";")
def create_calendar(year=None, month=None):
"""
Create an inline markup with the provided year and month
:param int year: Year to use in the calendar, if None the current year is used.
:param int month: Month to use in the calendar, if None the current month is used.
:return: Returns the InlineKeyboardMarkup object with the calendar.
"""
now = datetime.datetime.now()
year = now.year if year is None else year
month = now.month if month is None else month
data_ignore = create_callback_data("IGNORE", year, month, 0)
markup = {"inline_keyboard": []}
# First row - Month and Year
row = [{"text": calendar.month_name[month] + " " + str(year), "callback_data": data_ignore}]
markup["inline_keyboard"].append(row)
# Second row - Week Days
row = []
for day in ["Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun"]:
row.append({"text": day, "callback_data": data_ignore})
markup["inline_keyboard"].append(row)
my_calendar = calendar.monthcalendar(year, month)
for week in my_calendar:
row = []
for day in week:
if day == 0:
row.append({"text": " ", "callback_data": data_ignore})
else:
row.append({"text": "{}".format(day), "callback_data": create_callback_data("DAY", year, month, day)})
markup["inline_keyboard"].append(row)
# Last row - Buttons
row = [{"text": "< Prev", "callback_data": create_callback_data("PREV-MONTH", year, month, day)},
{"text": " ", "callback_data": data_ignore},
{"text": "Next >", "callback_data": create_callback_data("NEXT-MONTH", year, month, day)}]
markup["inline_keyboard"].append(row)
return json.dumps(markup)
def process_calendar_selection(bot, update):
"""
Process the callback_query. This method generates a new calendar if forward or
backward is pressed. This method should be called inside a CallbackQueryHandler.
:param telegram.Bot bot: The bot, as provided by the CallbackQueryHandler
:param telegram.Update update: The update, as provided by the CallbackQueryHandler
:return: Returns a tuple (Boolean,datetime.datetime), indicating if a date is selected
and returning the date if so.
"""
ret_data = (False, None)
query = update.callback_query
(action, year, month, day) = separate_callback_data(query.data)
curr = datetime.datetime(int(year), int(month), 1)
if action == "IGNORE":
bot.answer_callback_query(callback_query_id=query.id)
elif action == "DAY":
bot.edit_message_text(text=query.message.text,
chat_id=query.message.chat_id,
message_id=query.message.message_id
)
ret_data = True, datetime.datetime(int(year), int(month), int(day))
elif action == "PREV-MONTH":
pre = curr - datetime.timedelta(days=1)
bot.edit_message_text(text=query.message.text,
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=create_calendar(int(pre.year), int(pre.month)))
elif action == "NEXT-MONTH":
ne = curr + datetime.timedelta(days=31)
bot.edit_message_text(text=query.message.text,
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=create_calendar(int(ne.year), int(ne.month)))
else:
bot.answer_callback_query(callback_query_id=query.id, text="Something went wrong!")
# UNKNOWN
return ret_data