Skip to content

Commit f4280a4

Browse files
authored
Merge pull request #37 from jbogarin/master
Changed the pyramid bot from Chuck Norris to Cat facts
2 parents 077ec28 + 7c20460 commit f4280a4

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

examples/pyramidSparkBot/README.rst

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
11
Documentation
22
=============
33

4-
Put a brief description of 'Pyramid Spark Bot application'.
4+
A simple bot script, built on Pyramid using Cornice
5+
6+
This sample script leverages the Pyramid web framework (https://trypyramid.com/) with
7+
Cornice (https://cornice.readthedocs.io). By default the web server will be reachable at
8+
port 6543 you can change this default if desired (see `pyramidSparkBot.ini`).
9+
10+
ngrok (https://ngrok.com/) can be used to tunnel traffic back to your server
11+
if your machine sits behind a firewall.
12+
13+
You must create a Spark webhook that points to the URL where this script is
14+
hosted. You can do this via the CiscoSparkAPI.webhooks.create() method.
15+
16+
Additional Spark webhook details can be found here:
17+
https://developer.ciscospark.com/webhooks-explained.html
18+
19+
A bot must be created and pointed to this server in the My Apps section of
20+
https://developer.ciscospark.com. The bot's Access Token should be added as a
21+
'SPARK_ACCESS_TOKEN' environment variable on the web server hosting this
22+
script.
23+
24+
This script supports Python versions 2 and 3.
25+
26+
Running the bot
27+
-------------------
28+
29+
In order to execute the bot, you need to
30+
31+
``python setup.py develop``
32+
``pserve --reload pyramidSparkBot``

examples/pyramidSparkBot/pyramidSparkBot/views.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,34 @@
3737

3838

3939
# Module constants
40-
CHUCK_NORRIS_JOKES_URL = 'http://api.icndb.com/jokes/random'
40+
CAT_FACT_URL = 'http://catfacts-api.appspot.com/api/facts?number=1'
4141

4242

4343
# Initialize the environment
4444
spark_api = CiscoSparkAPI() # Create the Cisco Spark API connection object
4545

4646

4747
# Helper functions
48-
def get_chuck_norris_joke():
49-
"""Get a Chuck Norris Joke from api.icndb.com and return it as a string.
48+
def get_catfact():
49+
"""Get a cat fact from catfacts-api.appspot.com and return it as a string.
5050
Functions for Soundhound, Google, IBM Watson, or other APIs can be added
5151
to create the desired functionality into this bot.
5252
"""
53-
response = requests.get(CHUCK_NORRIS_JOKES_URL, verify=False)
53+
response = requests.get(CAT_FACT_URL, verify=False)
5454
response_dict = json.loads(response.text)
55-
return response_dict['value']['joke']
55+
return response_dict['facts'][0]
5656

5757

5858
sparkwebhook = Service(name='sparkwebhook', path='/sparkwebhook', description="Spark Webhook")
5959

6060

6161
@sparkwebhook.get()
6262
def get_sparkwebhook(request):
63-
log.info(get_chuck_norris_joke())
64-
return {"joke": get_chuck_norris_joke()}
63+
log.info(get_catfact())
64+
return {"fact": get_catfact()}
6565

6666
@sparkwebhook.post()
67-
# Your Spark webhook should point to http://<serverip>:5000/sparkwebhook
67+
# Your Spark webhook should point to http://<serverip>:6543/sparkwebhook
6868
def post_sparkwebhook(request):
6969
"""Respond to inbound webhook JSON HTTP POST from Cisco Spark."""
7070

@@ -93,10 +93,10 @@ def post_sparkwebhook(request):
9393

9494
else:
9595
# Message was sent by someone else; parse message and respond.
96-
if "/CHUCKNORRIS" in message.text:
97-
log.info("FOUND '/CHUCKNORRIS'")
98-
chuck_norris_joke = get_chuck_norris_joke() # Get a Chuck Norris Joke
99-
log.info("SENDING CHUCK NORRIS JOKE '{}'".format(chuck_norris_joke))
100-
spark_api.messages.create(room.id, text=chuck_norris_joke) # Post the fact to the room where the request was received
96+
if "/CAT" in message.text:
97+
log.info("FOUND '/CAT'")
98+
catfact = get_catfact() # Get a cat fact
99+
log.info("SENDING CAT FACT'{}'".format(catfact))
100+
spark_api.messages.create(room.id, text=catfact) # Post the fact to the room where the request was received
101101
return {'Message': 'OK'}
102102

examples/pyramidSparkBot/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
packages=find_packages(),
2525
include_package_data=True,
2626
zip_safe=False,
27-
install_requires=['cornice', 'waitress'],
27+
install_requires=['cornice', 'waitress', 'ciscosparkapi'],
2828
entry_points="""\
2929
[paste.app_factory]
3030
main=pyramidSparkBot:main

0 commit comments

Comments
 (0)