-
Notifications
You must be signed in to change notification settings - Fork 34
/
handlers.py
155 lines (117 loc) · 5.08 KB
/
handlers.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
import datetime
import sys
from telegram import Update
import job_storage
import termin_api
import utils
from printers import print_termin_type_message, print_main_message, print_quering_message, print_deadline_message, \
print_subscribe_message, print_stat_message
from states import MAIN, SELECTING_TERMIN_TYPE, QUERING_TERMINS, SELECT_DEADLINE, SELECT_INTERVAL
def remove_subscription_helper(update: Update, context):
chat_id = str(update.effective_chat.id)
job_storage.remove_subscription(chat_id)
return main_helper(update, context)
def query_termins_helper(update: Update, context):
print_quering_message(update, context)
return QUERING_TERMINS
def main_helper(update: Update, context):
print_main_message(update, context)
return MAIN
def main_handler(update: Update, context):
if update.callback_query:
if update.callback_query.data == '_REUSE':
# All data already stored in user_data and user asked to repeat
return query_termins_helper(update, context)
elif update.callback_query.data == '_STOP':
return remove_subscription_helper(update, context)
else:
# All options out, but data is here - it's the name of the department
try:
department = getattr(termin_api, update.callback_query.data)
except AttributeError:
return main_helper(update, context)
context.user_data['buro'] = department
try:
print_termin_type_message(update, context)
except NotImplementedError:
print_main_message(update, context)
return MAIN
return SELECTING_TERMIN_TYPE
else:
return main_helper(update, context)
def stat_handler(update: Update, context):
print_stat_message(update, context)
return main_helper(update, context)
def termin_type_handler(update: Update, context):
if update.callback_query:
department = context.user_data['buro']
try:
index = int(update.callback_query.data)
except:
return SELECTING_TERMIN_TYPE
if index < 0:
return SELECTING_TERMIN_TYPE
termin_type_str = department.get_available_appointment_types()[index]
context.user_data['termin_type'] = termin_type_str
return query_termins_helper(update, context)
else:
print_termin_type_message(update, context)
return SELECTING_TERMIN_TYPE
def quering_termins_handler(update: Update, context):
if update.callback_query:
if update.callback_query.data == 'return':
return main_helper(update, context)
elif update.callback_query.data == 'subscribe':
print_deadline_message(update, context)
return SELECT_DEADLINE
elif update.callback_query.data == '_STOP':
return remove_subscription_helper(update, context)
else:
return query_termins_helper(update, context)
def deadline_handler(update: Update, context):
msg = update.message
days = msg.text
valid_deadline = True
try:
if int(days) < 1 or int(days) > 365:
valid_deadline = False
except ValueError:
valid_deadline = False
if not valid_deadline:
msg.reply_text('Deadline must be a positive integer.')
return SELECT_DEADLINE
context.user_data['deadline'] = datetime.datetime.now() + datetime.timedelta(days=int(days))
print_subscribe_message(update, context)
return SELECT_INTERVAL
def interval_handler(update: Update, context):
"""
Schedules a job for user which will check available appointments by interval
"""
MIN_CHECK_INTERVAL_MINUTES = utils.get_min_interval()
msg = update.message
minutes = update.message.text
# check interval at least MIN_CHECK_INTERVAL mins
valid_interval = True
try:
if int(minutes) < MIN_CHECK_INTERVAL_MINUTES:
valid_interval = False
except ValueError:
valid_interval = False
if not valid_interval:
msg.reply_text(f'Interval should be greater or equals than {MIN_CHECK_INTERVAL_MINUTES} minutes')
return SELECT_INTERVAL
chat_id = str(update.effective_chat.id)
subsciption_present = job_storage.get_job(chat_id)
# User cannot have two or more subscriptions
if subsciption_present:
msg.reply_text(
'⚠️ You had some subscription already. In order to activate the new check, I have removed the old one.')
job_storage.remove_subscription(chat_id)
job_storage.add_subscription(update, context, interval=int(minutes))
friendly_deadline = context.user_data['deadline'].strftime("%d-%m-%Y")
msg.reply_text(f"Ok, I've started subscription with checking interval {minutes} minutes\n"
"I will notify you if something is available\n"
"Please note the subscription will be automatically removed after one month.\n"
f"Will notify only about termins on {friendly_deadline} or earlier, "
"if not cancelled manually before")
return main_helper(update, context)