8.6.7 Lab - Construct A Python Script To Manage Webex Teams
8.6.7 Lab - Construct A Python Script To Manage Webex Teams
Objectives
Part 1: Launch the DEVASC VM
Part 2: Get Your Webex Teams Access Token
Part 3: Test Your Access Token
Part 4: Manage People in Webex Teams
Part 5: Manage Rooms in Webex Teams
Part 6: Manage Memberships in Webex Teams
Part 7: Manage Messages in Webex Teams
Background / Scenario
In this lab, you will use Webex Teams APIs to authenticate, manage people, manage rooms, manage
memberships to rooms, and send a message.
Required Resources
• 1 PC with operating system of your choice with Webex Teams Installed
• Virtual Box or VMWare
• DEVASC Virtual Machine
Instructions
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 1 of 10 www.netacad.com
Lab - Construct a Python Script to Manage Webex Teams
b. Webex has many APIs that you can use in your applications. Click the API Reference to expand its
submenu
c. Explore all the varieties of API calls. In this lab, you will use API documentation for People, Rooms,
Membership, and Message.
access_token = 'your_token_here'
url = 'https://webexapis.com/v1/people/me'
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 2 of 10 www.netacad.com
Lab - Construct a Python Script to Manage Webex Teams
headers = {
'Authorization': 'Bearer {}'.format(access_token)
}
res = requests.get(url, headers=headers)
print(json.dumps(res.json(), indent=4))
d. Save and run the file. You should get the same output your saw in the OpenAPI documentation.
Note: The values for some of the keys have been truncated in the output below.
devasc@labvm:~/labs/devnet-src/webex-teams$ python3 authentication.py
{
"id": "Y2lz...UyM2U",
"emails": [
"[email protected]"
],
"phoneNumbers": [],
"displayName": "Your-First-Name Your-Last-Name",
"nickName": "Your-Nick-Name",
"firstName": " Your-First-Name",
"lastName": "Your-Last-Name",
"avatar": "https://9643-417f-9974...6baa4~1600",
"orgId": "Y2lzY2...UxMGY",
"created": "2012-06-15T20:23:12.529Z",
"lastActivity": "2020-06-02T20:16:52.111Z",
"status": "active",
"type": "person"
}
devasc@labvm:~/labs/devnet-src/webex-teams$
Step 1: Locate the API documentation for listing details of a registered Webex Teams user.
a. Return to the developer.webex.com website. Under API Reference > People, click the Method for List
People.
b. Under Query Parameters, find the email parameter. This is the parameter you will use to find a specific
user in your organization. Alternatively, you could use the displayName parameter, if you know the exact
name. You can use the Try it feature.
Step 2: Use a Python script to list details of a registered Webex Teams user.
a. In VS Code, click the list-people.py file.
b. Place the following code into the file. Be sure to replace your_token_here with your personal access
token and [email protected] with an actual registered Webex Team user in your organization.
import requests
import json
access_token = 'your_token_here'
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 3 of 10 www.netacad.com
Lab - Construct a Python Script to Manage Webex Teams
url = 'https://webexapis.com/v1/people'
headers = {
'Authorization': 'Bearer {}'.format(access_token),
'Content-Type': 'application/json'
}
params = {
'email': '[email protected]'
}
res = requests.get(url, headers=headers, params=params)
print(json.dumps(res.json(), indent=4))
c. Save and run the script. You should get the same output similar to the following. If you get a message
like, {'message': 'Invalid email address.'..., it means that you did not replace the empty email parameter
with a legitimate email address for a registered Webex Teams user. The value for the id key will be used
in the next API call.
Note: The values for some of the keys have been truncated in the output below.
devasc@labvm:~/labs/devnet-src/webex-teams$ python3 list-people.py
{
"notFoundIds": null,
"items": [
{
"id": "Y2l...2I", # You will use this value in the next step
"emails": [
"[email protected]"
],
"phoneNumbers": [
{
"type": "mobile",
"value": "+1234567690"
}
],
"displayName": "displayName",
"nickName": "nickName",
"firstName": "firstName",
"lastName": "lastName",
"avatar": "https://9643-417f-9974...6baa4~1600",
"orgId": "Y2lzY...UxMGY",
"created": "2012-06-15T20:39:19.726Z",
"lastActivity": "2020-06-04T13:57:01.688Z",
"status": "active",
"type": "person"
}
]
}
devasc@labvm:~/labs/devnet-src/webex-teams$
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 4 of 10 www.netacad.com
Lab - Construct a Python Script to Manage Webex Teams
b. Save the file and run it. As a non-administrator, you will get information that is very similar to the previous
step.
devasc@labvm:~/labs/devnet-src/webex-teams$ python3 list-people.py
{
<first API call output omitted>
}
{
"id": "Y2l...2I",
"emails": [
"[email protected]"
],
"phoneNumbers": [
{
"type": "mobile",
"value": "+1234567890"
}
],
"displayName": "displayName",
"nickName": "nickName",
"firstName": "firstName",
"lastName": "lastName",
"avatar": "https://9643-417f-9974...6baa4~1600",
"orgId": "Y2l...MGY",
"created": "2012-06-15T20:39:19.726Z",
"lastActivity": "2020-06-04T14:39:36.535Z",
"status": "active",
"type": "person"
}
devasc@labvm:~/labs/devnet-src/webex-teams$
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 5 of 10 www.netacad.com
Lab - Construct a Python Script to Manage Webex Teams
b. Explore the various API calls you can make with the Rooms API.
c. Click the GET request for List Rooms and explore the Query Parameters.
Step 2: Use a Python script to list all the rooms for an authenticated user.
a. For this step, you will need to be a member of at least one room. A conversation with one other person is
considered a room in Webex Teams.
b. In VS Code, click the list-rooms.py file.
c. Place the following code into the file. Be sure to replace your_token_here with your personal access
token.
import requests
access_token = 'your_token_here'
url = 'https://webexapis.com/v1/rooms'
headers = {
'Authorization': 'Bearer {}'.format(access_token),
'Content-Type': 'application/json'
}
params={'max': '100'}
res = requests.get(url, headers=headers, params=params)
print(res.json())
d. Save and run the file. Your output will be different than the following. Only one room is listed here. ID
values have been truncated.
devasc@labvm:~/labs/devnet-src/webex-teams$ python3 list-rooms.py
{'items': [{'id': 'Y2l...ZTE0', 'title': 'User Name', 'type': 'direct', 'isLocked':
False, 'lastActivity': '2020-06-01T16:34:56.536Z', 'creatorId': 'Y2lz...yM2U',
'created': '2020-06-01T16:30:21.816Z', 'ownerId': 'Y2lz...xMGY'}
# additional rooms displayed up to 'max' value.
]}
devasc@labvm:~/labs/devnet-src/webex-teams$
Step 3: Locate and investigate the API documentation for posting to the Rooms API.
a. Return to the developer.webex.com website. Under API Reference, click Rooms, if necessary.
b. The Rooms API has one POST method for Create a Room. Click the link see Query Parameters that
are available. In your script, you will use the required title parameter.
access_token = 'your_token_here'
url = 'https://webexapis.com/v1/rooms'
headers = {
'Authorization': 'Bearer {}'.format(access_token),
'Content-Type': 'application/json'
}
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 6 of 10 www.netacad.com
Lab - Construct a Python Script to Manage Webex Teams
c. Save and run the file. You should get a response similar to the following. ID values have been truncated.
The room id and title are highlighted. Copy the room ID and save it in a text file for use in the rest of this
lab.
devasc@labvm:~/labs/devnet-src/webex-teams$ python3 create-rooms.py
{'id': 'Y2l...GNm', 'title': 'DevNet Associate Training!', 'type': 'group',
'isLocked': False, 'lastActivity': '2020-06-04T16:50:19.371Z', 'creatorId':
'Y2l...M2U', 'created': '2020-06-04T16:50:19.371Z', 'ownerId': 'Y2l...MGY'}
devasc@labvm:~/labs/devnet-src/webex-teams$
d. In your Webex Teams application, verify you now see the DevNet Associate Training! room. You are
currently the only member.
access_token = 'your_token_here'
room_id = 'your_room_id'
url = 'https://webexapis.com/v1/rooms/{}/meetingInfo'.format(room_id)
headers = {
'Authorization': 'Bearer {}'.format(access_token),
'Content-Type': 'application/json'
}
res = requests.get(url, headers=headers)
print(res.json())
c. Save and run the file. You should get a response similar to the following. Values have been truncated.
devasc@labvm:~/labs/devnet-src/webex-teams$ python3 get-room-details.py
{'roomId': 'Y2l...GNm', 'meetingLink': 'https://cisco.webex.com/m/3272...a837',
'sipAddress': '[email protected]', 'meetingNumber': '162...0468',
'callInTollFreeNumber': '+1-866-...-9903', 'callInTollNumber': '+1-408-...-6800'}
devasc@labvm:~/labs/devnet-src/webex-teams$
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 7 of 10 www.netacad.com
Lab - Construct a Python Script to Manage Webex Teams
access_token = 'your_token_here'
room_id = 'your_room_id'
url = 'https://webexapis.com/v1/memberships'
headers = {
'Authorization': 'Bearer {}'.format(access_token),
'Content-Type': 'application/json'
}
params = {'roomId': room_id}
res = requests.get(url, headers=headers, params=params)
print(res.json())
c. Save and run the file. You should get a response similar to the following. You should be the only member
unless you have added someone in the Webex Teams application. ID values have been truncated.
devasc@labvm:~/labs/devnet-src/webex-teams$ python3 list-memberships.py
{'items': [{'id': 'Y2l...RjZg', 'roomId': 'Y2l...GNm', 'personId': 'Y2l...M2U',
'personEmail': '[email protected]', 'personDisplayName': 'personDisplayName',
'personOrgId': 'Y2l...MGY', 'isModerator': False, 'isMonitor': False, 'created':
'2020-06-04T16:50:19.819Z'}]}
devasc@labvm:~/labs/devnet-src/webex-teams$
Step 3: Locate and investigate the API documentation for posting to the Memberships API.
a. Return to the developer.webex.com website. Under API Reference, click Memberships, if necessary.
b. The Memberships API has one POST method for Create a Membership. Click the link see Query
Parameters that are available. In your script, you will use the required roomID and personEmail
parameters.
Step 4: Use a Python script to create a membership, adding someone to the room.
For this step, you will need the email of someone else who is a registered Webex Teams user in your
organization. You can use the same email you used previously to list details about a person.
a. In VS Code, click the create-membership.py file.
b. Place the following code into the file. Replace your_token_here with your personal access token.
Replace your_room_id with the value from you got in the previous part. Replace new-
[email protected] with the email of the person you want to add to the room.
import requests
access_token = 'your_token_here'
room_id = 'your_room_id'
person_email = '[email protected]'
url = 'https://webexapis.com/v1/memberships'
headers = {
'Authorization': 'Bearer {}'.format(access_token),
'Content-Type': 'application/json'
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 8 of 10 www.netacad.com
Lab - Construct a Python Script to Manage Webex Teams
}
params = {'roomId': room_id, 'personEmail': person_email}
res = requests.post(url, headers=headers, json=params)
print(res.json())
c. Save and run the file. You should get a response similar to the following. You should be the only member
unless you have added someone in the Webex Teams application. ID values have been truncated.
devasc@labvm:~/labs/devnet-src/webex-teams$ python3 list-memberships.py
{'items': [{'id': 'Y2l...RjZg', 'roomId': 'Y2l...GNm', 'personId': 'Y2l...M2U',
'personEmail': '[email protected]', 'personDisplayName': 'personDisplayName',
'personOrgId': 'Y2l...MGY', 'isModerator': False, 'isMonitor': False, 'created':
'2020-06-04T16:50:19.819Z'}]}
devasc@labvm:~/labs/devnet-src/webex-teams$
access_token = 'your_token_here'
room_id = 'your_room_id'
message = 'Hello **DevNet Associates**!!'
url = 'https://webexapis.com/v1/messages'
headers = {
'Authorization': 'Bearer {}'.format(access_token),
'Content-Type': 'application/json'
}
params = {'roomId': room_id, 'markdown': message}
res = requests.post(url, headers=headers, json=params)
print(res.json())
c. Save and run the file. You should get a response similar to the following. Notice that the Markdown was
converted to HTML. ID values have been truncated.
devasc@labvm:~/labs/devnet-src/webex-teams$ python3 create-markdown-
message.py
{'id': 'Y2l...RjZg', 'roomId': 'Y2l...GNm', 'roomType': 'group', 'text': 'Hello DevNet
Associates!!', 'personId': 'Y2l...M2U', 'personEmail': '[email protected]', 'markdown':
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 9 of 10 www.netacad.com
Lab - Construct a Python Script to Manage Webex Teams
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 10 of 10 www.netacad.com