-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitiate.py
More file actions
501 lines (384 loc) · 14.5 KB
/
initiate.py
File metadata and controls
501 lines (384 loc) · 14.5 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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import CallbackContext, ConversationHandler
from datetime import datetime, timedelta
from pymongo import *
from credentials import *
def initiate_date(update, context):
context.chat_data["state"] = "date"
keyboard = []
col = 0
row = -1
for i in range(7):
if col == 0:
keyboard.append([])
row += 1
curr_date = datetime.now() + timedelta(days=i)
curr_day = str(curr_date.day)
curr_month = str(curr_date.month)
curr_year = str(curr_date.year)
keyboard[row].append(InlineKeyboardButton(
curr_day + "/" + curr_month + "/" + curr_year, callback_data="date_"+str(i + 1)))
col += 1
col %= 3
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.message.reply_text(
"Please select your desired date for your study session", reply_markup=reply_markup)
return
def initiate_time(update, context):
context.chat_data["state"] = "time"
try:
update.message.reply_text(
"What time would you like to have your study session? (Please key in a time in 24 hour format e.g. 1400)")
except:
update.callback_query.message.reply_text(
"What time would you like to have your study session? (Please key in a time in 24 hour format e.g. 1400)")
return
def invalid_initiate_time(update, context, reason):
context.chat_data["state"] = "invalid_initiate_time"
try:
update.message.reply_text(
"Sorry, you keyed in an invalid time. (Reason: " + reason + ")")
except:
update.callback_query.message.reply_text(
"Sorry, you keyed in an invalid time. (Reason: " + reason + ")")
initiate_time(update, context)
return
def update_data(update, context):
context.chat_data["state"] = "update_data"
stored_data = context.chat_data["stored_data"]
keyboard = []
for data in stored_data:
keyboard.append(
[
InlineKeyboardButton(
data.capitalize(), callback_data="update_" + data)
]
)
keyboard.append([InlineKeyboardButton(
"Done", callback_data="update_done")])
reply_markup = InlineKeyboardMarkup(keyboard)
try:
update.message.reply_text(
"We noted that you have stored the following data previously. Would you like to update them?", reply_markup=reply_markup)
except:
update.callback_query.message.reply_text(
"We noted that you have stored the following data previously. Would you like to update them?", reply_markup=reply_markup)
return
def edit_update_data(update, context):
context.chat_data["state"] = "edit_update_data"
stored_data = context.chat_data["stored_data"]
keyboard = []
for data in stored_data:
keyboard.append(
[
InlineKeyboardButton(
data.capitalize(), callback_data="update_" + data)
]
)
keyboard.append([InlineKeyboardButton(
"Done", callback_data="update_done")])
reply_markup = InlineKeyboardMarkup(keyboard)
query = update.callback_query
context.bot.edit_message_reply_markup(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=reply_markup
)
return
def year(update, context):
context.chat_data["state"] = "year"
keyboard = [
[
InlineKeyboardButton("Year One", callback_data="year_one"),
InlineKeyboardButton("Year Two", callback_data="year_two")
],
[
InlineKeyboardButton("Year Three", callback_data="year_three"),
InlineKeyboardButton("Year Four", callback_data="year_four")
],
[
InlineKeyboardButton("Year Five", callback_data="year_five")
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
try:
update.message.reply_text(
"What year are you in?", reply_markup=reply_markup)
except:
update.callback_query.message.reply_text(
"What year are you in?", reply_markup=reply_markup)
return
def course(update, context):
context.chat_data["state"] = "course"
keyboard = [
[
InlineKeyboardButton(
"Electrical Engineering (EE)", callback_data="CDE_EE")
],
[
InlineKeyboardButton(
"Computer Engineering (CEG)", callback_data="CDE_CEG")
]
# [
# InlineKeyboardButton("Architecture (ARCH)",
# callback_data="CDE_ARCH")
# ],
# [
# InlineKeyboardButton(
# "Biomedical Engineering (BME)", callback_data="CDE_BME")
# ],
# [
# InlineKeyboardButton(
# "Chemical Engineering (CHBE)", callback_data="CDE_CHBE")
# ],
# [
# InlineKeyboardButton("Civil Engineering (CEE)",
# callback_data="CDE_CEE")
# ],
# [
# InlineKeyboardButton(
# "Engineering Science Programme (ESP)", callback_data="CDE_ESP")
# ],
# [
# InlineKeyboardButton(
# "Environmental Engineering (ENV)", callback_data="CDE_ENV")
# ],
# [
# InlineKeyboardButton("Industrial Design (DID)",
# callback_data="CDE_DID")
# ],
# [
# InlineKeyboardButton(
# "Industrial Systems Engineering and Management (ISEM)", callback_data="CDE_ISEM")
# ],
# [
# InlineKeyboardButton(
# "Infrastructure and project management (IPM)", callback_data="CDE_IPM")
# ],
# [
# InlineKeyboardButton(
# "Landscape architecture (LRCH)", callback_data="CDE_LRCH")
# ],
# [
# InlineKeyboardButton(
# "Material Science and Engineering (MSE)", callback_data="CDE_MSE")
# ],
# [
# InlineKeyboardButton(
# "Mechanical Engineering (ME)", callback_data="CDE_ME")
# ]
]
reply_markup = InlineKeyboardMarkup(keyboard)
try:
update.message.reply_text(
"What is your course?", reply_markup=reply_markup)
except:
update.callback_query.message.reply_text(
"What is your course?", reply_markup=reply_markup)
return
def gender(update, context):
context.chat_data["state"] = "gender"
keyboard = [
[
InlineKeyboardButton("Male", callback_data="male"),
InlineKeyboardButton("Female", callback_data="female"),
InlineKeyboardButton("Prefer not to say",
callback_data="gender_null")
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
try:
update.message.reply_text(
"What is your gender? (Note that you will not have a say in the gender of your study buddies if you choose ‘prefer not to say’)", reply_markup=reply_markup)
except:
update.callback_query.message.reply_text(
"What is your gender? (Note that you will not have a say in the gender of your study buddies if you choose ‘prefer not to say’)", reply_markup=reply_markup)
return
def location(update, context):
context.chat_data["state"] = "location"
try:
update.message.reply_text("Where would you like to study?")
except:
update.callback_query.message.reply_text(
"Where would you like to study?")
return
def pax(update, context):
context.chat_data["state"] = "pax"
keyboard = [
[
InlineKeyboardButton("2", callback_data="pax_2")
],
[
InlineKeyboardButton("3", callback_data="pax_3")
],
[
InlineKeyboardButton("4", callback_data="pax_4")
],
[
InlineKeyboardButton("5", callback_data="pax_5")
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
try:
update.message.reply_text(
"How many people would you like in your study session?", reply_markup=reply_markup)
except:
update.callback_query.message.reply_text(
"How many people would you like in your study session?", reply_markup=reply_markup)
return
def remark(update, context):
context.chat_data["state"] = "remark"
keyboard = [
[
InlineKeyboardButton("Yes", callback_data="remark_yes")
],
[
InlineKeyboardButton("No", callback_data="remark_no")
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
try:
update.message.reply_text(
"Any additional remarks?", reply_markup=reply_markup)
except:
update.callback_query.message.reply_text(
"Any additional remarks?", reply_markup=reply_markup)
return
def add_remark(update, context):
context.chat_data["state"] = "add_remark"
update.callback_query.message.reply_text(
"What remark would you like to add?")
return
def store_data(update, context):
keyboard = [
[
InlineKeyboardButton("Yes", callback_data="store_data_yes"),
InlineKeyboardButton("No", callback_data="store_data_no")
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
try:
update.message.reply_text(
"Would you like your data to be stored?", reply_markup=reply_markup)
except:
update.callback_query.message.reply_text(
"Would you like your data to be stored?", reply_markup=reply_markup)
context.chat_data["state"] = "store_data"
return
def which_data(update, context):
context.chat_data["state"] = "which_data"
stored_data = context.chat_data["stored_data"]
keyboard = []
for data in stored_data:
if data in context.chat_data:
keyboard.append([InlineKeyboardButton(
data.capitalize(), callback_data=data)])
keyboard.append([InlineKeyboardButton("Done", callback_data="store_done")])
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.message.reply_text(
"Which data would you like to store?", reply_markup=reply_markup)
return
def edit_which_data(update, context):
context.chat_data["state"] = "edit_which_data"
stored_data = context.chat_data["stored_data"]
keyboard = []
for data in stored_data:
if data in context.chat_data:
keyboard.append([InlineKeyboardButton(
data.capitalize(), callback_data=data)])
keyboard.append([InlineKeyboardButton("Done", callback_data="store_done")])
reply_markup = InlineKeyboardMarkup(keyboard)
query = update.callback_query
context.bot.edit_message_reply_markup(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=reply_markup
)
return
def end(update, context):
context.chat_data["state"] = "end"
create_study_session(update, context)
purge_data(update, context)
update.callback_query.message.reply_text(
"Your study session has been posted successfully! We will update you when someone joined your session")
return ConversationHandler.END
###
# helper functions
###
def valid_time(update, context, text):
curr_date = datetime.now()
curr_day = str(curr_date.day)
curr_month = str(curr_date.month)
curr_year = str(curr_date.year)
curr_time = curr_date.hour * 100 + curr_date.minute
date = curr_day + "/" + curr_month + "/" + curr_year
if context.chat_data["initiate_date"] == date and int(text) <= curr_time:
return False
return True
def check_data(update, context):
cursor = db.users.find_one({"user_id": context.chat_data["user_id"]})
stored_data = []
data = ["year", "course", "gender", "location", "pax", "remarks"]
for d in data:
if cursor[d] != "":
stored_data.append(d)
context.chat_data["stored_data"] = stored_data
return stored_data
def next_data(update, context):
data = ["year", "course", "gender", "location", "pax", "remarks"]
for d in data:
if d not in context.chat_data["stored_data"]:
if d == "year":
year(update, context)
context.chat_data["stored_data"].append("year")
elif d == "course":
course(update, context)
context.chat_data["stored_data"].append("course")
elif d == "gender":
gender(update, context)
context.chat_data["stored_data"].append("gender")
elif d == "location":
location(update, context)
context.chat_data["stored_data"].append("location")
elif d == "pax":
pax(update, context)
context.chat_data["stored_data"].append("pax")
elif d == "remarks":
remark(update, context)
context.chat_data["stored_data"].append("remarks")
return
store_data(update, context)
return
def create_study_session(update, context):
# sessions
session_id = db.sessions.insert_one(
{"user_id_array": [context.chat_data["user_id"]]}).inserted_id
filter_con = {"_id": session_id}
new_con = {"$set": {"date": context.chat_data["initiate_date"]}}
db.sessions.update_one(filter_con, new_con)
new_con = {"$set": {"time": context.chat_data["initiate_time"]}}
db.sessions.update_one(filter_con, new_con)
data = ["year", "course", "gender", "location", "pax", "remarks"]
for d in data:
if d in context.chat_data:
new_con = {"$set": {d: context.chat_data[d]}}
else:
cursor = db.users.find_one(
{"user_id": context.chat_data["user_id"]})
new_con = {"$set": {d: cursor[d]}}
db.sessions.update_one(filter_con, new_con)
# dates
cursor = db.dates.find({"date": context.chat_data["initiate_date"]})
if list(cursor) == []:
date_id = db.dates.insert_one(
{"date": context.chat_data["initiate_date"]}).inserted_id
filter_con = {"_id": date_id}
new_con = {"$set": {"sessions": [session_id]}}
db.dates.update_one(filter_con, new_con)
else:
filter_con = {"date": context.chat_data["initiate_date"]}
new_con = {"$push": {"sessions": session_id}}
db.dates.update_one(filter_con, new_con)
def purge_data(update, context):
context.chat_data.clear()
return