-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathclassFeedbackAutomation.py
More file actions
262 lines (217 loc) · 11.4 KB
/
Copy pathclassFeedbackAutomation.py
File metadata and controls
262 lines (217 loc) · 11.4 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
########################## Asmbly Class Feedback Automation ###########################
# Neon API docs - https://developer.neoncrm.com/api-v2/ #
# Gmail API docs - https://developers.google.com/gmail/api/reference/rest #
# Google Drive API docs - https://developers.google.com/drive/api/reference/rest/v3 #
# Google Forms API docs - https://developers.google.com/forms/api/reference/rest #
#######################################################################################
#######################################################################################
# This script pulls the previous day's events from Neon and gathers instructor, class #
# and registrant information. It references a JSON file containing Asmbly teachers #
# and their classes. If the file does not exist, it creates a new empty dict that #
# will be populated automatically. When a new survey needs to be created, the Google #
# Drive API is called to copy a master survey template and rename the copied file #
# to the class name in Neon (e.g. Woodshop Safety w/ Maz). The Forms API is used to #
# get the survey response URL for the survey. The link is then copied to the #
# surveyLinks dict, which will ultimately be written back to surveyLinks.json. Each #
# succesful registrant for the event is emailed a link to the survey. #
# #
# When the surveys need to be refreshed periodically (e.g. quarterly), the old #
# surveys should be moved to a new folder in Drive, and the surveyLinks.json file #
# should be deleted. #
#######################################################################################
# Run daily as cronjob on AWS EC2 instance
import json
import base64
import datetime
import smtplib
import ssl
import logging
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import helpers.neon as neon
from google.oauth2.service_account import Credentials
from googleapiclient.discovery import build
if os.environ.get("USER") == "ec2-user" or os.environ.get("LAMBDA_TASK_ROOT"):
from aws_ssm import G_user, G_password
else:
from config import G_user, G_password
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%H:%S",
)
#################################################################################
# Send a MIME email object to its recipient using GMail
#################################################################################
def sendMIMEmessage(MIMEmessage):
# if not "@" in MIMEmessage['To']:
# raise ValueError("Message doesn't have a sane destination address")
MIMEmessage["From"] = "Asmbly Education Team"
logging.debug(
f"""Sending email subject "{MIMEmessage['Subject']}" to {MIMEmessage['To']}"""
)
context = ssl.create_default_context()
try:
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
server.login(G_user, G_password)
server.sendmail(
G_user,
[MIMEmessage["To"], "classes@asmbly.org"],
MIMEmessage.as_string(),
)
logging.info(f"Sent survey email to {MIMEmessage['To']}")
except:
logging.exception(
f"""Failed sending email subject "{MIMEmessage['Subject']}" to {MIMEmessage['To']}"""
)
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
server.login(G_user, G_password)
MIMEmessage["To"] = "classes@asmbly.org"
MIMEmessage["Subject"] = "Feedback Request Failure"
server.sendmail(G_user, MIMEmessage["To"], MIMEmessage.as_string())
# Define Google OAuth2 scopes needed. See https://developers.google.com/identity/protocols/oauth2/scopes
SCOPES = [
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/forms",
]
# File containing private key for the service account used to implement automation
# Edit service account in Google Cloud Console
# This script requires domain-wide delegation to run. See https://support.google.com/a/answer/162106?hl=en
SERVICE_ACCOUNT_FILE = "classFeedbackServiceAccountKey.json"
# User email that the service account will impersonate
USER_EMAIL = "admin@asmbly.org"
# Check if instructor-class combo already has an active survey in the feedback folder. If not, create new survey
def getSurveyLink(eventName, driveService, formsService):
# FileId of the folder where surveys will go. Find using driveService.files().get() on a test file in that folder
# and copying the parents field
parentFolderId = "17aM-fE8bBZqDZA1NhnWupAFan87Tpdsd"
# Apostrophes in query params break the URL encoding using the service's "q" search queries, so remove them
if "'" in eventName:
eventName = eventName.replace("'", "")
# Check to see if the event already has a survey in Drive in case the survey links JSON files was accidentally deleted
surveyFileCheck = (
driveService.files()
.list(
q=f"mimeType='application/vnd.google-apps.form' and trashed=false and '{parentFolderId}' in parents and name='{eventName}'",
includeItemsFromAllDrives=True,
supportsAllDrives=True,
corpora="drive",
driveId="0ADuGDgrEXJMJUk9PVA",
)
.execute()
)
# check if the search returned a result for the teacher-class combo already in drive.
# If so, get the response URL for the survey
if len(surveyFileCheck.get("files")) > 0:
surveyId = surveyFileCheck["files"][0]["id"]
formResponseUrl = (
formsService.forms().get(formId=surveyId).execute().get("responderUri")
)
# if no hits, create a new survey for the teacher-class combo from the master survey template
# and return the response URL
else:
# originFileId = "1TG7_qzC728qaDcqZhAZ9NO4B6EtCPuPJd21qh6BemGA" # OLD master survey template fileId
# Google Forms was updated to use new permission settings for responders
originFileId = "1zCmHpktgblR8auKWMO6eNdTBt2frGj3Q6ExFTP9rHKI" # NEW master survey template fileId
copiedFileParams = {"name": eventName, "parents": [f"{parentFolderId}"]}
newSurvey = (
driveService.files()
.copy(fileId=originFileId, body=copiedFileParams, supportsAllDrives=True)
.execute()
)
newSurveyId = newSurvey.get("id")
formResponseUrl = (
formsService.forms().get(formId=newSurveyId).execute().get("responderUri")
)
return formResponseUrl
def main():
# Build OAuth credentials
credentials = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
creds = credentials.with_subject(USER_EMAIL)
# Create the API services using built credential tokens
driveService = build("drive", "v3", credentials=creds)
formsService = build("forms", "v1", credentials=creds)
# Import json file with survey links for each teacher-class combo
# When surveys need to be reset (e.g. quarterly), delete this file and move all current surveys to an archive folder
surveyLinkFile = "surveyLinks.json"
try:
with open(surveyLinkFile, encoding="utf-8") as f:
surveyLinks = json.load(f)
except (
FileNotFoundError
): # if survey link file has been deleted to reset links, start over with fresh dict.
# Dict will contain nested dict for each teacher.
surveyLinks = {}
dryRun = False
TODAY = datetime.date.today()
YESTERDAY = (TODAY - datetime.timedelta(days=1)).isoformat()
# Search Neon for all active events that ended yesterday
searchFields = [
{"field": "Event End Date", "operator": "EQUAL", "value": YESTERDAY},
{"field": "Event Archived", "operator": "EQUAL", "value": "No"},
]
outputFields = ["Event Name", "Event Topic", "Event ID"]
responseEvents = neon.postEventSearch(searchFields, outputFields)["searchResults"]
logging.info("\nBeginning survey emails for %s\n", YESTERDAY)
for event in responseEvents:
eventName = event["Event Name"]
instructor = event["Event Topic"]
logging.info("%s:", eventName)
# Try to find teacher-class combo survey link in the imported survey links file/surveyLinks dict. If not present,
# create using Drive API and update the dict with the survey response URL
instructorDict = surveyLinks.get(instructor)
if instructorDict:
surveyLink = instructorDict.get(eventName)
if not surveyLink:
surveyLink = getSurveyLink(eventName, driveService, formsService)
instructorDict.update({eventName: surveyLink})
elif not instructorDict:
surveyLink = getSurveyLink(eventName, driveService, formsService)
surveyLinks[instructor] = {eventName: surveyLink}
# Get all registrants for the event with a Succeeded registration and populate dict with name and email
registrants = neon.getEventRegistrants(event["Event ID"])["eventRegistrations"]
eventDict = {}
if type(registrants) is not type(None):
for registrant in registrants:
if (
registrant["tickets"][0]["attendees"][0]["registrationStatus"]
== "SUCCEEDED"
):
firstName = registrant["tickets"][0]["attendees"][0]["firstName"]
lastName = registrant["tickets"][0]["attendees"][0]["lastName"]
neonId = registrant["registrantAccountId"]
fullName = firstName + " " + lastName
email = neon.getAccountIndividual(neonId)["individualAccount"][
"primaryContact"
]["email1"]
eventDict[neonId] = [firstName, email]
for k, v in eventDict.items():
emailMsg = f"""
<html>
<body>
<p>Hi {v[0]},</p>
<p>We hope you enjoyed {eventName} yesterday!</p>
<p>
Asmbly is always looking to improve our class offerings. As part of that goal, we hope you will take a minute to respond to a very brief survey about your class.
</p>
<p><a href={surveyLink} target="_blank">This link</a> will take you to the survey.</p>
<p>We really appreciate your help in improving Asmbly!</p>
<p>Best, <br>Asmbly Education Team</p>
</body>
</html>
"""
mimeMessage = MIMEMultipart()
mimeMessage["Subject"] = f"Your Feedback On {eventName} is Requested"
mimeMessage.attach(MIMEText(emailMsg, "html"))
raw_string = base64.urlsafe_b64encode(mimeMessage.as_bytes()).decode()
if not dryRun:
mimeMessage["To"] = f"{v[1]}"
else:
mimeMessage["to"] = "matthew.miller@asmbly.org"
sendMIMEmessage(mimeMessage)
# Write surveyLinks dict back to surveyLinks.json to persist any created survey response URLs
with open(surveyLinkFile, "w", encoding="utf-8") as f:
json.dump(surveyLinks, f)
if __name__ == '__main__':
main()