-
Notifications
You must be signed in to change notification settings - Fork 0
/
teachable_webhooks.py
393 lines (334 loc) · 10.2 KB
/
teachable_webhooks.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# Import packages
from settings import *
from mailchimp import (
add_student,
)
from google_sheets import (
get_data_from_s3,
update_csv,
)
from slack import (
post_to_slack,
create_slack_message,
)
'''
Method: course_enrollment()
Summary: Runs all the code for new course enrollments
'''
def course_enrollment(
data,
):
# Get pertinent data from webhook
course_enrollment_data = parse_enrollment_data(
data
)
# Post message in Slack
message_data = {
'Name': 'student_name',
'Email': 'student_email',
'Course': 'course_name',
'Amount': 'payment',
}
message = create_slack_message(
'COURSE ENROLLMENT',
course_enrollment_data,
message_data,
)
# Post message to slack
post_to_slack(message)
# Get the info for the first lecture the student needs to watch
course_enrollment_data = get_first_lecture_info(
data,
course_enrollment_data,
)
# Update csv
update_csv(
'Sign Ups',
course_enrollment_data,
)
# Add tags to lecture_completion_data
course_enrollment_data['tags'] = [
{
'name': course_enrollment_data['course_name'],
'status': 'active',
},
{
'name': 'Not Started',
'status': 'active',
},
{
'name': 'New Sign Up',
'status': 'active',
},
]
# Create / Update student in Mailchimp
add_student(
course_enrollment_data,
)
return course_enrollment_data
'''
Method: course_completion()
Summary: Runs all the code for course completions
'''
def course_completion(
data,
):
# Get pertinent data from webhook
course_enrollment_data = parse_enrollment_data(data)
# Post message in Slack
message_data = {
'Name': 'student_name',
'Email': 'student_email',
'Course': 'course_name',
}
message = create_slack_message(
'COURSE COMPLETION',
course_enrollment_data,
message_data,
)
# Post message to slack
post_to_slack(message)
# Get the info for the first lecture the student needs to watch
course_enrollment_data = get_first_lecture_info(
data,
course_enrollment_data,
)
# Update csv
update_csv(
'Completed Courses',
course_enrollment_data,
)
# Add tags to lecture_completion_data
course_enrollment_data['tags'] = [
{
'name': course_enrollment_data['course_name'],
'status': 'active',
},
{
'name': 'Completed Course',
'status': 'active',
},
{
'name': 'In Progress',
'status': 'inactive',
},
]
# Create / Update student in Mailchimp
add_student(
course_enrollment_data,
)
return course_enrollment_data
'''
Method: lecture_completion()
Summary: Runs all the code for lecture completions
'''
def lecture_completion(
data,
):
# Get pertinent data from webhook
lecture_completion_data = parse_lecture_completion_data(data)
# Post message in Slack
message_data = {
'Name': 'student_name',
'Email': 'student_email',
'Course': 'course_name',
'Lecture': 'lecture_name',
'Percentage Complete': 'percent_complete',
}
message = create_slack_message(
'LECTURE COMPLETION',
lecture_completion_data,
message_data,
)
# Post message to slack
post_to_slack(message)
# Get the info for the first lecture the student needs to watch
lecture_completion_data = get_first_lecture_info(
data,
lecture_completion_data,
)
# Update csv
update_csv(
'Completed Lectures',
lecture_completion_data,
)
# Add tags to lecture_completion_data
lecture_completion_data['tags'] = [
{
'name': lecture_completion_data['course_name'],
'status': 'active',
},
{
'name': 'In Progress',
'status': 'active',
},
{
'name': 'Not Started',
'status': 'inactive',
},
{
'name': 'New Sign Up',
'status': 'inactive',
},
]
# Create / Update student in Mailchimp
add_student(
lecture_completion_data,
)
return lecture_completion_data
'''
Method: parse_enrollment_data()
Summary: Gets enrollment information from the Teachable webhook
'''
def parse_enrollment_data(
data
):
student_name = data['object']['user']['name']
split_name_info = split_name(student_name)
first_name = split_name_info['first_name']
last_name = split_name_info['last_name']
student_email = data['object']['user']['email']
course_id = data['object']['course']['id']
course_name = data['object']['course']['name']
payment = data['object']['user']['transactions_gross'] / 100
promo_code = data['object']['coupon']
signup_datetime = data['created']
return {
'student_name': student_name,
'student_first_name': first_name,
'student_last_name': last_name,
'student_email': student_email,
'course_id': course_id,
'course_name': course_name,
'payment': payment,
'promo_code': promo_code,
'signup_datetime': signup_datetime,
'last_lecture_date': dt.datetime.now().strftime('%m/%d/%Y')
}
'''
Method: parse_lecture_completion_data()
Summary: Gets lecture completion information from the Teachable webhook
'''
def parse_lecture_completion_data(
data
):
student_name = data['object']['user']['name']
split_name_info = split_name(student_name)
first_name = split_name_info['first_name']
last_name = split_name_info['last_name']
student_email = data['object']['user']['email']
course_id = data['object']['course']['id']
course_name = data['object']['course']['name']
lecture_name = data["object"]["lecture"]["name"]
percent_complete = data["object"]["percent_complete"]
timestamp = data["created"]
return {
'student_name': student_name,
'student_first_name': first_name,
'student_last_name': last_name,
'student_email': student_email,
'course_id': course_id,
'course_name': course_name,
'lecture_name': lecture_name,
'percent_complete': percent_complete,
'timestamp': dt.datetime.strptime(timestamp[:10], '%Y-%m-%d').strftime('%m/%d/%Y'),
'last_lecture_date': dt.datetime.now().strftime('%m/%d/%Y')
}
'''
Method: get_first_lecture_info()
Summary: Gets information about the first lecture the student has to take
'''
def get_first_lecture_info(
data,
course_enrollment_data,
):
# Get the data about all the lectures
lecture_df = get_data_from_s3(
os.environ['liaf_s3_bucket'],
'Course Videos',
)
# Filter the lecture_df to the course the student is taking
current_course_id = course_enrollment_data['course_id']
lecture_df = lecture_df[lecture_df['Course Code'] == current_course_id]
# Get the information about the next lecture's description, minutes, and link
next_lecture_description = lecture_df.loc[
lecture_df['Course Video'] == 1,
'Video About',
].values[0]
next_lecture_minutes = int(
lecture_df.loc[
lecture_df['Course Video'] == 1,
'Video Minutes',
].values[0]
)
next_lecture_link = lecture_df.loc[
lecture_df['Course Video'] == 1,
'Lecture Link'
].values[0]
# Add next_lecture_description, next_lecture_minutes, and next_lecture_link to course_enrollment_data
course_enrollment_data['next_lecture_description'] = next_lecture_description
course_enrollment_data['next_lecture_minutes'] = next_lecture_minutes
course_enrollment_data['next_lecture_link'] = next_lecture_link
# Return course_enrollment_data
return course_enrollment_data
'''
Method: get_next_lecture_info()
Summary: Gets information about the next lecture the student has to take
'''
def get_next_lecture_info(
data,
lecture_completion_data,
):
# If the student completed the course, return empty strings
if data['object']['percent_complete'] == 100:
lecture_completion_data['next_lecture_description'] = 'Congrats on finishing the course!'
lecture_completion_data['next_lecture_minutes'] = 0
lecture_completion_data['next_lecture_link'] = 'likeiamfive.teachable.com'
return lecture_completion_data
# Get the data about all the lectures
lecture_df = get_csv_from_s3(
os.environ['liaf_s3_bucket'],
'Course Videos',
)
# Filter the lecture_df to the course the student is taking
current_course_id = data['object']['course_id']
current_lecture_id = data['object']['lecture_id']
lecture_df = lecture_df[lecture_df['Course Code'] == current_course_id]
# Get the video # for the current lecture
current_lecture_number = lecture_df.loc[
lecture_df['Lecture Code'] == current_lecture_id,
'Course Video'
].values[0]
next_lecture_number = current_lecture_number + 1
# Get the information about the next lecture's description, minutes, and link
next_lecture_description = lecture_df.loc[
lecture_df['Course Video'] == next_lecture_number,
'Video About',
].values[0]
next_lecture_minutes = int(
lecture_df.loc[
lecture_df['Course Video'] == next_lecture_number,
'Video Minutes',
].values[0]
)
next_lecture_link = lecture_df.loc[
lecture_df['Course Video'] == next_lecture_number,
'Lecture Link'
].values[0]
# Add next_lecture_description, next_lecture_minutes, and next_lecture_link to lecture_completion_data
lecture_completion_data['next_lecture_description'] = next_lecture_description
lecture_completion_data['next_lecture_minutes'] = next_lecture_minutes
lecture_completion_data['next_lecture_link'] = next_lecture_link
# Return lecture_completion_data
return lecture_completion_data
'''
Method: split_name()
Summary: Splits name into first and last
'''
def split_name(name):
first_name = name.split(" ")[0]
last_name = name.split(" ")[1] if len(name.split(" ")) > 1 else ''
return {
'first_name': first_name,
'last_name': last_name,
}