-
Notifications
You must be signed in to change notification settings - Fork 0
/
join.py
184 lines (125 loc) · 5 KB
/
join.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
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import CallbackContext
from datetime import datetime, timedelta
from bson.objectid import ObjectId
from pymongo import *
from credentials import *
def join_date(update, context):
context.chat_data["state"] = "join_date"
keyboard = [[]]
col = 0
row = -1
for i in range(7):
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)
date = curr_day + "/" + curr_month + "/" + curr_year
if valid_date(update, context, date):
keyboard[row].append(InlineKeyboardButton(
date, callback_data="join_date_"+str(i)))
col += 1
col %= 3
if col == 0:
keyboard.append([])
row += 1
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 no_sessions(update, context):
context.chat_data["state"] = "no_sessions"
keyboard = [
[InlineKeyboardButton("Initiate", callback_data="initiate")]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.message.reply_text(
"There are no study sessions available at the moment, would you like to initiate one?", reply_markup=reply_markup)
return
def join_sessions(update, context):
keyboard = []
sessions = available_sessions(update, context)
for session in sessions:
session_details = session[0]
session_id = session[1]
keyboard.append([InlineKeyboardButton(session_details,
callback_data="join_session_" + str(session_id))])
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.message.reply_text(
"Which session would you like to join?", reply_markup=reply_markup)
return
def prompt_contact(update, context):
contact = context.chat_data["initiator_telegram_handle"]
update.callback_query.message.reply_text(
"Please kindly contact your initiator @" + contact)
return
###
# helper functions
###
def valid_date(update, context, date):
cursor = db.dates.find_one({"date": date})
if cursor == None:
return False
sessions = cursor["sessions"]
for session in sessions:
if valid_session(update, context, session):
return True
return False
def valid_session(update, context, session):
cursor = db.sessions.find_one({"_id": session})
if context.chat_data["user_id"] in cursor["user_id_array"]:
return False
pax = len(cursor["user_id_array"])
total_pax = int(cursor["pax"][-1])
if pax == total_pax:
return False
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 cursor["date"] == date and int(cursor["time"]) <= curr_time:
return False
return True
def available_sessions(update, context):
date = context.chat_data["join_date"]
cursor = db.dates.find_one({"date": date})
sessions = cursor["sessions"]
valid_sessions = []
for session in sessions:
if valid_session(update, context, session):
cursor = db.sessions.find_one({"_id": session})
# year
if cursor["year"] == "year_one":
year = "Y1"
elif cursor["year"] == "year_two":
year = "Y2"
elif cursor["year"] == "year_three":
year = "Y3"
elif cursor["year"] == "year_four":
year = "Y4"
elif cursor["year"] == "year_five":
year = "Y5"
# course
course = cursor["course"].split("_")[1]
# gender
gender = cursor["gender"]
# time
time = cursor["time"]
# location
location = cursor["location"]
# current pax
current_pax = len(cursor["user_id_array"])
# pax
pax = cursor["pax"][-1]
# remarks
remarks = cursor["remarks"]
if gender == "gender_null":
session_details = [str(year) + " " + str(course) + ", " + str(date) + " " + str(time) + "H @" + str(
location) + "\n" + " (Remarks: " + str(remarks) + ") (" + str(current_pax) + "/" + str(pax) + " pax)", session]
else:
session_details = [str(year) + " " + str(course) + ", " + str(gender).capitalize() + ", " + str(date) + " " + str(
time) + "H @" + str(location) + "\n" + " (Remarks: " + str(remarks) + ") (" + str(current_pax) + "/" + str(pax) + " pax)", session]
valid_sessions.append(session_details)
return valid_sessions