Skip to content

Commit 06bc932

Browse files
committed
Update examples
Update the example code with Cisco Spark to Webex Teams name changes, copyright and license headers, and PEP8 fixes.
1 parent a902f2b commit 06bc932

File tree

12 files changed

+344
-149
lines changed

12 files changed

+344
-149
lines changed

examples/bot-example-flask.py

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,37 @@
1010
if your machine sits behind a firewall.
1111
1212
You must create a Spark webhook that points to the URL where this script is
13-
hosted. You can do this via the CiscoSparkAPI.webhooks.create() method.
13+
hosted. You can do this via the WebexTeamsAPI.webhooks.create() method.
1414
1515
Additional Spark webhook details can be found here:
16-
https://developer.ciscospark.com/webhooks-explained.html
16+
https://developer.webex.com/webhooks-explained.html
1717
1818
A bot must be created and pointed to this server in the My Apps section of
19-
https://developer.ciscospark.com. The bot's Access Token should be added as a
20-
'SPARK_ACCESS_TOKEN' environment variable on the web server hosting this
19+
https://developer.webex.com. The bot's Access Token should be added as a
20+
'WEBEX_TEAMS_ACCESS_TOKEN' environment variable on the web server hosting this
2121
script.
2222
2323
This script supports Python versions 2 and 3.
2424
25+
Copyright (c) 2016-2018 Cisco and/or its affiliates.
26+
27+
Permission is hereby granted, free of charge, to any person obtaining a copy
28+
of this software and associated documentation files (the "Software"), to deal
29+
in the Software without restriction, including without limitation the rights
30+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
31+
copies of the Software, and to permit persons to whom the Software is
32+
furnished to do so, subject to the following conditions:
33+
34+
The above copyright notice and this permission notice shall be included in all
35+
copies or substantial portions of the Software.
36+
37+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
43+
SOFTWARE.
2544
"""
2645

2746

@@ -53,8 +72,10 @@
5372

5473

5574
# Initialize the environment
56-
flask_app = Flask(__name__) # Create the web application instance
57-
spark_api = WebexTeamsAPI() # Create the Webex Teams API connection object
75+
# Create the web application instance
76+
flask_app = Flask(__name__)
77+
# Create the Webex Teams API connection object
78+
spark_api = WebexTeamsAPI()
5879

5980

6081
# Helper functions
@@ -72,7 +93,8 @@ def get_catfact():
7293

7394

7495
# Core bot functionality
75-
@flask_app.route('/sparkwebhook', methods=['GET', 'POST']) # Your Spark webhook should point to http://<serverip>:5000/sparkwebhook
96+
# Your Spark webhook should point to http://<serverip>:5000/sparkwebhook
97+
@flask_app.route('/sparkwebhook', methods=['GET', 'POST'])
7698
def sparkwebhook():
7799
"""Processes incoming requests to the '/sparkwebhook' URI."""
78100
if request.method == 'GET':
@@ -96,16 +118,21 @@ def sparkwebhook():
96118
elif request.method == 'POST':
97119
"""Respond to inbound webhook JSON HTTP POST from Webex Teams."""
98120

99-
json_data = request.json # Get the POST data sent from Webex Teams
121+
# Get the POST data sent from Webex Teams
122+
json_data = request.json
100123
print("\n")
101124
print("WEBHOOK POST RECEIVED:")
102125
print(json_data)
103126
print("\n")
104127

105-
webhook_obj = Webhook(json_data) # Create a Webhook object from the JSON data
106-
room = spark_api.rooms.get(webhook_obj.data.roomId) # Get the room details
107-
message = spark_api.messages.get(webhook_obj.data.id) # Get the message details
108-
person = spark_api.people.get(message.personId) # Get the sender's details
128+
# Create a Webhook object from the JSON data
129+
webhook_obj = Webhook(json_data)
130+
# Get the room details
131+
room = spark_api.rooms.get(webhook_obj.data.roomId)
132+
# Get the message details
133+
message = spark_api.messages.get(webhook_obj.data.id)
134+
# Get the sender's details
135+
person = spark_api.people.get(message.personId)
109136

110137
print("NEW MESSAGE IN ROOM '{}'".format(room.title))
111138
print("FROM '{}'".format(person.displayName))
@@ -123,9 +150,11 @@ def sparkwebhook():
123150
# Message was sent by someone else; parse message and respond.
124151
if "/CAT" in message.text:
125152
print("FOUND '/CAT'")
126-
cat_fact = get_catfact() # Get a cat fact
153+
# Get a cat fact
154+
cat_fact = get_catfact()
127155
print("SENDING CAT FACT '{}'".format(cat_fact))
128-
spark_api.messages.create(room.id, text=cat_fact) # Post the fact to the room where the request was received
156+
# Post the fact to the room where the request was received
157+
spark_api.messages.create(room.id, text=cat_fact)
129158
return 'OK'
130159

131160

examples/bot-example-webpy.py

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,39 @@
88
your server if you don't wish to expose your machine publicly to the Internet.
99
1010
You must create a Spark webhook that points to the URL where this script is
11-
hosted. You can do this via the CiscoSparkAPI.webhooks.create() method.
11+
hosted. You can do this via the WebexTeamsAPI.webhooks.create() method.
1212
1313
Additional Spark webhook details can be found here:
14-
https://developer.ciscospark.com/webhooks-explained.html
14+
https://developer.webex.com/webhooks-explained.html
1515
1616
A bot must be created and pointed to this server in the My Apps section of
17-
https://developer.ciscospark.com. The bot's Access Token should be added as a
18-
'SPARK_ACCESS_TOKEN' environment variable on the web server hosting this
17+
https://developer.webex.com. The bot's Access Token should be added as a
18+
'WEBEX_TEAMS_ACCESS_TOKEN' environment variable on the web server hosting this
1919
script.
2020
2121
NOTE: While this script is written to support Python versions 2 and 3, as of
2222
the time of this writing web.py (v0.38) only supports Python 2.
2323
Therefore this script only supports Python 2.
2424
25+
Copyright (c) 2016-2018 Cisco and/or its affiliates.
26+
27+
Permission is hereby granted, free of charge, to any person obtaining a copy
28+
of this software and associated documentation files (the "Software"), to deal
29+
in the Software without restriction, including without limitation the rights
30+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
31+
copies of the Software, and to permit persons to whom the Software is
32+
furnished to do so, subject to the following conditions:
33+
34+
The above copyright notice and this permission notice shall be included in all
35+
copies or substantial portions of the Software.
36+
37+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
43+
SOFTWARE.
2544
"""
2645

2746

@@ -53,9 +72,12 @@
5372

5473

5574
# Global variables
56-
urls = ('/sparkwebhook', 'webhook') # Your Spark webhook should point to http://<serverip>:8080/sparkwebhook
57-
app = web.application(urls, globals()) # Create the web application instance
58-
api = WebexTeamsAPI() # Create the Webex Teams API connection object
75+
# Your Spark webhook should point to http://<serverip>:8080/sparkwebhook
76+
urls = ('/sparkwebhook', 'webhook')
77+
# Create the web application instance
78+
app = web.application(urls, globals())
79+
# Create the Webex Teams API connection object
80+
api = WebexTeamsAPI()
5981

6082

6183
def get_catfact():
@@ -74,14 +96,19 @@ def get_catfact():
7496
class webhook(object):
7597
def POST(self):
7698
"""Respond to inbound webhook JSON HTTP POSTs from Webex Teams."""
77-
json_data = web.data() # Get the POST data sent from Spark
99+
# Get the POST data sent from Spark
100+
json_data = web.data()
78101
print("\nWEBHOOK POST RECEIVED:")
79102
print(json_data, "\n")
80103

81-
webhook_obj = Webhook(json_data) # Create a Webhook object from the JSON data
82-
room = api.rooms.get(webhook_obj.data.roomId) # Get the room details
83-
message = api.messages.get(webhook_obj.data.id) # Get the message details
84-
person = api.people.get(message.personId) # Get the sender's details
104+
# Create a Webhook object from the JSON data
105+
webhook_obj = Webhook(json_data)
106+
# Get the room details
107+
room = api.rooms.get(webhook_obj.data.roomId)
108+
# Get the message details
109+
message = api.messages.get(webhook_obj.data.id)
110+
# Get the sender's details
111+
person = api.people.get(message.personId)
85112

86113
print("NEW MESSAGE IN ROOM '{}'".format(room.title))
87114
print("FROM '{}'".format(person.displayName))
@@ -98,9 +125,11 @@ def POST(self):
98125
# Message was sent by someone else; parse message and respond.
99126
if "/CAT" in message.text:
100127
print("FOUND '/CAT'")
101-
cat_fact = get_catfact() # Get a cat fact
128+
# Get a cat fact
129+
cat_fact = get_catfact()
102130
print("SENDING CAT FACT '{}'".format(cat_fact))
103-
api.messages.create(room.id, text=cat_fact) # Post the fact to the room where the request was received
131+
# Post the fact to the room where the request was received
132+
api.messages.create(room.id, text=cat_fact)
104133
return 'OK'
105134

106135

examples/ngrokwebhook.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@
1313
To use script simply launch ngrok, and then launch this script. After ngrok is
1414
killed, run this script a second time to remove webhook from Webex Teams.
1515
16+
Copyright (c) 2016-2018 Cisco and/or its affiliates.
17+
18+
Permission is hereby granted, free of charge, to any person obtaining a copy
19+
of this software and associated documentation files (the "Software"), to deal
20+
in the Software without restriction, including without limitation the rights
21+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22+
copies of the Software, and to permit persons to whom the Software is
23+
furnished to do so, subject to the following conditions:
24+
25+
The above copyright notice and this permission notice shall be included in all
26+
copies or substantial portions of the Software.
27+
28+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34+
SOFTWARE.
1635
"""
1736

1837

examples/people.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,28 @@
33
""" Script to demonstrate the use of webexteamssdk for the people API
44
55
The package natively retrieves your Spark access token from the
6-
SPARK_ACCESS_TOKEN environment variable. You must have this environment
6+
WEBEX_TEAMS_ACCESS_TOKEN environment variable. You must have this environment
77
variable set to run this script.
88
9+
Copyright (c) 2016-2018 Cisco and/or its affiliates.
10+
11+
Permission is hereby granted, free of charge, to any person obtaining a copy
12+
of this software and associated documentation files (the "Software"), to deal
13+
in the Software without restriction, including without limitation the rights
14+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15+
copies of the Software, and to permit persons to whom the Software is
16+
furnished to do so, subject to the following conditions:
17+
18+
The above copyright notice and this permission notice shall be included in all
19+
copies or substantial portions of the Software.
20+
21+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27+
SOFTWARE.
928
"""
1029

1130

@@ -27,7 +46,9 @@
2746
from webexteamssdk import WebexTeamsAPI
2847

2948

30-
api = WebexTeamsAPI() # Create a CiscoSparkAPI connection object; uses your SPARK_ACCESS_TOKEN environment variable
49+
# Create a WebexTeamsAPI connection object; uses your WEBEX_TEAMS_ACCESS_TOKEN
50+
# environment variable
51+
api = WebexTeamsAPI()
3152

3253

3354
# Get my user information
@@ -44,6 +65,8 @@
4465

4566
# Get my user information using id
4667
print("Get the list of people I know...")
47-
people = api.people.list(displayName="Jose") # Creates a generator container (iterable) that lists the people I know
68+
# Creates a generator container (iterable) that lists the people I know
69+
people = api.people.list(displayName="Jose")
70+
# Return the displayName of every person found
4871
for person in people:
49-
print(person.displayName) # Return the displayName of every person found
72+
print(person.displayName)

examples/pyramidSparkBot/pyramidSparkBot/__init__.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

examples/pyramidSparkBot/setup.py

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
Documentation
22
=============
33

4-
A simple bot script, built on Pyramid using Cornice
4+
A simple bot script, built on Pyramid using Cornice.
55

66
This sample script leverages the Pyramid web framework https://trypyramid.com/
77
with Cornice https://cornice.readthedocs.io. By default the web server will be
88
reachable at port 6543 you can change this default if desired
9-
(see `pyramidSparkBot.ini`).
9+
(see `pyramidWebexTeamsBot.ini`).
1010

1111
ngrok (https://ngrok.com/) can be used to tunnel traffic back to your server
1212
if your machine sits behind a firewall.
1313

14-
You must create a Spark webhook that points to the URL where this script is
15-
hosted. You can do this via the CiscoSparkAPI.webhooks.create() method.
14+
You must create a Webex Teams webhook that points to the URL where this script is
15+
hosted. You can do this via the WebexTeamsAPI.webhooks.create() method.
1616

17-
Additional Spark webhook details can be found here:
18-
https://developer.ciscospark.com/webhooks-explained.html
17+
Additional Webex Teams webhook details can be found here:
18+
https://developer.webex.com/webhooks-explained.html
1919

2020
A bot must be created and pointed to this server in the My Apps section of
21-
https://developer.ciscospark.com. The bot's Access Token should be added as a
22-
'SPARK_ACCESS_TOKEN' environment variable on the web server hosting this
21+
https://developer.webex.com. The bot's Access Token should be added as a
22+
'WEBEX_TEAMS_ACCESS_TOKEN' environment variable on the web server hosting this
2323
script.
2424

2525
This script supports Python versions 2 and 3.
@@ -30,4 +30,4 @@ Running the bot
3030
In order to execute the bot, you need to
3131

3232
``python setup.py develop``
33-
``pserve --reload pyramidSparkBot``
33+
``pserve --reload pyramidWebexTeamsBot``

0 commit comments

Comments
 (0)