-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsending_conversations_sample.py
46 lines (34 loc) · 1.36 KB
/
sending_conversations_sample.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
# -*- coding: utf-8 -*-
"""
@author: Jeremy H. & Victor S.
"""
import requests, pandas as pd, getpass
#Canvas instance URL
url = "https://canvas.ubc.ca"
if __name__ == "__main__":
#importing Excel file and parsing into a dataframe
the_file = pd.read_csv('key.csv')
token = getpass.getpass("Enter your Canvas Token: ")
course = input("Enter your Canvas course ID: ")
print("Sending your messages...")
for index, row in the_file.iterrows():
recipient = str(row['User ID'])
key = str(row['key'])
#payload/contents of your API request
payload = {'recipients[]': [recipient],
'subject': "title of email",
'context_code': 'course_{}'.format(course),
'body': """
Dear ....
Body text
Key 1: {}\n
Body text
\n
""".format(key)}
#Post the request
r = requests.post('{}/api/v1/conversations'.format(url),
params = payload,
headers= {'Authorization': 'Bearer ' + token})
if not r.ok:
print(r.text)
print("Failed to send message for student ID: {}. {}".format(recipient, r))