Skip to content

Commit 38b2fbb

Browse files
committed
Merge branch 'master' into development
2 parents 5cd318c + 8586284 commit 38b2fbb

File tree

7 files changed

+228
-14
lines changed

7 files changed

+228
-14
lines changed

ciscosparkapi/api/people.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,7 @@ def __init__(self, session):
131131

132132
@generator_container
133133
def list(self, email=None, displayName=None, max=None):
134-
"""List people by email or displayName.
135-
136-
An email address or displayName must be provided.
134+
"""List people
137135
138136
This method supports Cisco Spark's implementation of RFC5988 Web
139137
Linking to provide pagination support. It returns a generator
@@ -158,8 +156,6 @@ def list(self, email=None, displayName=None, max=None):
158156
159157
Raises:
160158
AssertionError: If the parameter types are incorrect.
161-
ciscosparkapiException: If neither an email or displayName argument
162-
is specified.
163159
SparkApiError: If the Cisco Spark cloud returns an error.
164160
165161
"""
@@ -172,10 +168,6 @@ def list(self, email=None, displayName=None, max=None):
172168
params['email'] = email
173169
elif displayName:
174170
params['displayName'] = displayName
175-
else:
176-
error_message = "An email or displayName argument must be " \
177-
"specified."
178-
raise ciscosparkapiException(error_message)
179171
if max:
180172
params['max'] = max
181173
# API request - get items

examples/bot-example-flask.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
# -*- coding: utf-8 -*-
33
"""A simple bot script, built on Flask.
44
5-
This sample script leverages the Flask web service micro-framework
6-
(see http://flask.pocoo.org/). By default the web server will be reachable at
7-
port 5000 you can change this default if desired (see `flask_app.run(...)`).
5+
This sample script leverages the Flask web service micro-framework
6+
(see http://flask.pocoo.org/). By default the web server will be reachable at
7+
port 5000 you can change this default if desired (see `flask_app.run(...)`).
88
9-
ngrok (https://ngrok.com/) can be used to tunnel traffic back to your server
9+
ngrok (https://ngrok.com/) can be used to tunnel traffic back to your server
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
@@ -88,7 +88,7 @@ def sparkwebhook():
8888
elif request.method == 'POST':
8989
"""Respond to inbound webhook JSON HTTP POST from Cisco Spark."""
9090

91-
json_data = request.data # Get the POST data sent from Cisco Spark
91+
json_data = request.json # Get the POST data sent from Cisco Spark
9292
print("\n")
9393
print("WEBHOOK POST RECEIVED:")
9494
print(json_data)

examples/pyramidSparkBot/README.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Documentation
2+
=============
3+
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``
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
[app:main]
2+
use = egg:pyramidSparkBot
3+
4+
pyramid.reload_templates = true
5+
pyramid.debug_authorization = false
6+
pyramid.debug_notfound = false
7+
pyramid.debug_routematch = false
8+
pyramid.debug_templates = true
9+
pyramid.default_locale_name = en
10+
11+
[server:main]
12+
use = egg:waitress#main
13+
host = 0.0.0.0
14+
port = 6543
15+
16+
# Begin logging configuration
17+
18+
[loggers]
19+
keys = root, pyramidSparkBot
20+
21+
[handlers]
22+
keys = console
23+
24+
[formatters]
25+
keys = generic
26+
27+
[logger_root]
28+
level = INFO
29+
handlers = console
30+
31+
[logger_pyramidSparkBot]
32+
level = DEBUG
33+
handlers =
34+
qualname = pyramidSparkBot
35+
36+
[handler_console]
37+
class = StreamHandler
38+
args = (sys.stderr,)
39+
level = NOTSET
40+
formatter = generic
41+
42+
[formatter_generic]
43+
format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s
44+
45+
# End logging configuration
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""Main entry point
2+
"""
3+
from pyramid.config import Configurator
4+
5+
6+
def main(global_config, **settings):
7+
config = Configurator(settings=settings)
8+
config.include("cornice")
9+
config.scan("pyramidSparkBot.views")
10+
return config.make_wsgi_app()
11+
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""A simple bot script, built on Pyramid using Cornice
2+
3+
This sample script leverages the Pyramid web framework (https://trypyramid.com/) with
4+
Cornice (https://cornice.readthedocs.io). By default the web server will be reachable at
5+
port 6543 you can change this default if desired (see `pyramidSparkBot.ini`).
6+
7+
ngrok (https://ngrok.com/) can be used to tunnel traffic back to your server
8+
if your machine sits behind a firewall.
9+
10+
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.
12+
13+
Additional Spark webhook details can be found here:
14+
https://developer.ciscospark.com/webhooks-explained.html
15+
16+
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
19+
script.
20+
21+
This script supports Python versions 2 and 3.
22+
"""
23+
from __future__ import (absolute_import, division,
24+
print_function, unicode_literals)
25+
from cornice import Service
26+
27+
from builtins import *
28+
29+
import json
30+
31+
import requests
32+
33+
from ciscosparkapi import CiscoSparkAPI, Webhook
34+
35+
import logging
36+
log = logging.getLogger(__name__)
37+
38+
39+
# Module constants
40+
CAT_FACT_URL = 'http://catfacts-api.appspot.com/api/facts?number=1'
41+
42+
43+
# Initialize the environment
44+
spark_api = CiscoSparkAPI() # Create the Cisco Spark API connection object
45+
46+
47+
# Helper functions
48+
def get_catfact():
49+
"""Get a cat fact from catfacts-api.appspot.com and return it as a string.
50+
Functions for Soundhound, Google, IBM Watson, or other APIs can be added
51+
to create the desired functionality into this bot.
52+
"""
53+
response = requests.get(CAT_FACT_URL, verify=False)
54+
response_dict = json.loads(response.text)
55+
return response_dict['facts'][0]
56+
57+
58+
sparkwebhook = Service(name='sparkwebhook', path='/sparkwebhook', description="Spark Webhook")
59+
60+
61+
@sparkwebhook.get()
62+
def get_sparkwebhook(request):
63+
log.info(get_catfact())
64+
return {"fact": get_catfact()}
65+
66+
@sparkwebhook.post()
67+
# Your Spark webhook should point to http://<serverip>:6543/sparkwebhook
68+
def post_sparkwebhook(request):
69+
"""Respond to inbound webhook JSON HTTP POST from Cisco Spark."""
70+
71+
json_data = request.json # Get the POST data sent from Cisco Spark
72+
log.info("\n")
73+
log.info("WEBHOOK POST RECEIVED:")
74+
log.info(json_data)
75+
log.info("\n")
76+
77+
webhook_obj = Webhook(json_data) # Create a Webhook object from the JSON data
78+
room = spark_api.rooms.get(webhook_obj.data.roomId) # Get the room details
79+
message = spark_api.messages.get(webhook_obj.data.id) # Get the message details
80+
person = spark_api.people.get(message.personId) # Get the sender's details
81+
82+
log.info("NEW MESSAGE IN ROOM '{}'".format(room.title))
83+
log.info("FROM '{}'".format(person.displayName))
84+
log.info("MESSAGE '{}'\n".format(message.text))
85+
86+
# This is a VERY IMPORTANT loop prevention control step.
87+
# If you respond to all messages... You will respond to the messages
88+
# that the bot posts and thereby create a loop condition.
89+
me = spark_api.people.me()
90+
if message.personId == me.id:
91+
# Message was sent by me (bot); do not respond.
92+
return {'Message': 'OK'}
93+
94+
else:
95+
# Message was sent by someone else; parse message and respond.
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
101+
return {'Message': 'OK'}
102+

examples/pyramidSparkBot/setup.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import os
2+
from setuptools import setup, find_packages
3+
4+
here = os.path.abspath(os.path.dirname(__file__))
5+
6+
with open(os.path.join(here, 'README.rst')) as f:
7+
README = f.read()
8+
9+
10+
setup(name='pyramidSparkBot',
11+
version=0.1,
12+
description='Pyramid Spark Bot application',
13+
long_description=README,
14+
classifiers=[
15+
"Programming Language :: Python",
16+
"Framework :: Pylons",
17+
"Topic :: Internet :: WWW/HTTP",
18+
"Topic :: Internet :: WWW/HTTP :: WSGI :: Application"
19+
],
20+
keywords="web services",
21+
author='',
22+
author_email='',
23+
url='',
24+
packages=find_packages(),
25+
include_package_data=True,
26+
zip_safe=False,
27+
install_requires=['cornice', 'waitress', 'ciscosparkapi'],
28+
entry_points="""\
29+
[paste.app_factory]
30+
main=pyramidSparkBot:main
31+
""",
32+
paster_plugins=['pyramid'])

0 commit comments

Comments
 (0)