|
| 1 | +import json |
| 2 | +import requests |
| 3 | + |
| 4 | +ACCESS_TOKEN = "" #put your access token between the quotes |
| 5 | +ROOM_NAME = "" #give the room you will create a name |
| 6 | +YOUR_MESSAGE = "" #put the message that you will post to the room |
| 7 | + |
| 8 | +#sets the header to be used for authentication and data format to be sent. |
| 9 | +def setHeaders(): |
| 10 | + accessToken_hdr = 'Bearer ' + ACCESS_TOKEN |
| 11 | + spark_header = {'Authorization': accessToken_hdr, 'Content-Type': 'application/json; charset=utf-8'} |
| 12 | + return (spark_header) |
| 13 | + |
| 14 | + |
| 15 | +# creates a new room and returns the room id. |
| 16 | +# Mission: Add code to parse and return the room id |
| 17 | +def createRoom(the_header,room_name): |
| 18 | + roomInfo = '{"title" :"' + room_name + '"}' |
| 19 | + uri = 'https://api.ciscospark.com/v1/rooms' |
| 20 | + resp = requests.post(uri, data=roomInfo, headers=the_header) |
| 21 | + var = resp.json() |
| 22 | + print("createRoom JSON: ", var) |
| 23 | + #add code here to parse and return the room id. |
| 24 | + |
| 25 | + |
| 26 | +# adds a new member to the room. Member e-mail is test@test.com |
| 27 | +def addMembers(the_header,roomId): |
| 28 | + member = '{"roomId":"' + roomId + '","personEmail": "test@test.com", "isModerator": false}' |
| 29 | + uri = 'https://api.ciscospark.com/v1/memberships' |
| 30 | + resp = requests.post(uri, data=member, headers=the_header) |
| 31 | + print("addMembers JSON: ", resp.json()) |
| 32 | + |
| 33 | +#posts a message to the room |
| 34 | +def postMsg(the_header,roomId,message): |
| 35 | + message = '{"roomId":"' + roomId + '","text":"'+message+'"}' |
| 36 | + uri = 'https://api.ciscospark.com/v1/messages' |
| 37 | + resp = requests.post(uri, data=message, headers=the_header) |
| 38 | + print("postMsg JSON: ", resp.json()) |
| 39 | + |
| 40 | +#Mission write code to retrieve and display about the room |
| 41 | +def getRoomInfo(the_header,roomId): |
| 42 | + print("In function getRoomInfo") |
| 43 | + |
| 44 | + |
| 45 | +if __name__ == '__main__': |
| 46 | + header=setHeaders() |
| 47 | + #passing the ROOM_NAME for the room to be created |
| 48 | + room_id=createRoom(header,ROOM_NAME) |
| 49 | + #passing roomId to members function here to add member to the room. |
| 50 | + addMembers(header,room_id) |
| 51 | + #passing roomId to message function here to Post Message to a room. |
| 52 | + postMsg(header,room_id,YOUR_MESSAGE) |
| 53 | + |
| 54 | + |
0 commit comments