Skip to content

Commit 977bf20

Browse files
committed
Update examples
Update examples to leverage new SPARK_ACCESS_TOKEN, and Python 2/3 support where possible. web.py does not support Python 3.
1 parent e46c3af commit 977bf20

File tree

2 files changed

+117
-122
lines changed

2 files changed

+117
-122
lines changed

examples/botexample.py

Lines changed: 82 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,92 @@
1-
#!/usr/bin/python
2-
3-
# This is a simple bot script. You must create a Spark webhook that points to the server where this script runs
4-
# ngrok can be used to tunnel traffic back to this server if you don't wish to expose your test machine publicly to the Internet
5-
# By default this server will be reachable at port 8080 - append a different port when launching the script if desired
6-
# Additional Spark webhook details can be found here - https://developer.ciscospark.com/webhooks-explained.html
7-
#
8-
# A bot must be created and pointed to this server in the My Apps section of https://developer.ciscospark.com
9-
# The bot's Access Token should be placed in a directory on the system running this script and the file_name should be updated below
10-
#
11-
# Sample Webhook:
12-
# {
13-
# "id": "Y2lzY29..........mE4U1XXW3323",
14-
# "name": "Example Webhook",
15-
# "targetUrl": "http://<yourserverurl>.com:56811/sparkwebhook",
16-
# "resource": "messages",
17-
# "event": "created",
18-
# "secret": "supersecret",
19-
# "created": "2016-09-20T18:29:06.920Z"
20-
# }
21-
#
1+
#!/usr/bin/env python2
2+
# -*- coding: utf-8 -*-
3+
"""A simple bot script.
4+
5+
This sample script leverages web.py (see http://webpy.org/). By default the
6+
web server will be reachable at port 8080 - append a different port when
7+
launching the script if desired. ngrok can be used to tunnel traffic back to
8+
your server if you don't wish to expose your machine publicly to the Internet.
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+
"""
22+
23+
24+
from __future__ import print_function
25+
from builtins import object
2226

23-
import web
2427
import json
28+
29+
import web
2530
import requests
26-
from ciscosparkapi import CiscoSparkAPI
2731

28-
#
29-
# CHANGE this to your bot Access Token file
30-
#
31-
file_name="/home/brad/ciscosparkapiexamples/botat.txt"
32+
from ciscosparkapi import CiscoSparkAPI, Webhook
3233

33-
#this function gets a cat fact from appspot.com and returns it as a string
34-
#functions for Soundhound, Google, IBM Watson, or other APIs can be added to build desired functionality into this bot
35-
def get_catfact():
36-
URL = 'http://catfacts-api.appspot.com/api/facts?number=1'
37-
resp = requests.get(URL, verify=False)
38-
resp_dict = json.loads(resp.text)
39-
return resp_dict["facts"][0]
40-
41-
42-
fat=open (file_name,"r+") #open the file with the token
43-
44-
access_token=fat.readline().rstrip() #strip whitespace, newline, etc.
45-
46-
fat.close
47-
48-
49-
50-
urls = ('/sparkwebhook', 'webhook') #webhook should point to http://<serverip>:8080/sparkwebhook
51-
52-
app = web.application(urls, globals()) #create a web application instance
53-
54-
api = CiscoSparkAPI(access_token) #invoke the API
55-
56-
# This section defines what to do when a webhook is received from Spark
57-
class webhook:
58-
def POST(self): #webhook are received via HTTP POST
59-
data = web.data() #grabs the data sent from Spark
60-
61-
print
62-
print 'DATA RECEIVED:'
63-
print data
64-
print
65-
66-
json_data = json.loads(data) #loads the data as JSON format
67-
room_id = json_data["data"]["roomId"] #grabs the roomId
68-
message_id = json_data["data"]["id"] #grabs the messageId
69-
70-
print room_id
71-
print message_id
72-
73-
message=api.messages.get(message_id) #grabs the actual message detail
74-
75-
print message
76-
77-
message_text = message.text #grabs the message text
78-
79-
print message_text
80-
81-
#THIS IS VERY IMPORTANT LOOP CONTROL
82-
#if you respond to all messages you will respond to the bot and create a loop condition
83-
#Alternatively you could look at the posting room member and filter your bot's messages
84-
85-
if ((message_text.find("\CAT", 10, 20)) > -1): #looks for \CAT between position 10 and 20 in message string
86-
87-
print "Found \CAT"
88-
89-
cat_fact=get_catfact() #grabs a cat fact
90-
message_post = api.messages.create(room_id,None,None,cat_fact)#posts the fact in the room where request received
91-
92-
print message_post
93-
94-
return 'OK'
9534

35+
# Module constants
36+
CAT_FACTS_URL = 'http://catfacts-api.appspot.com/api/facts?number=1'
9637

97-
98-
if __name__ == '__main__':
99-
app.run() #starts listening for incoming webhooks
10038

39+
# Global variables
40+
urls = ('/sparkwebhook', 'webhook') # Your Spark webhook should point to http://<serverip>:8080/sparkwebhook
41+
app = web.application(urls, globals()) # Create the web application instance
42+
api = CiscoSparkAPI() # Create the Cisco Spark API connection object
10143

10244

45+
def get_catfact():
46+
"""Get a cat fact from appspot.com and return it as a string.
47+
48+
Functions for Soundhound, Google, IBM Watson, or other APIs can be added
49+
to create the desired functionality into this bot.
50+
51+
"""
52+
response = requests.get(CAT_FACTS_URL, verify=False)
53+
response_dict = json.loads(response.text)
54+
return response_dict['facts'][0]
55+
56+
57+
class webhook(object):
58+
def POST(self):
59+
"""Respond to inbound webhook JSON HTTP POSTs from Cisco Spark."""
60+
json_data = web.data() # Get the POST data sent from Spark
61+
print("\nWEBHOOK POST RECEIVED:")
62+
print(json_data, "\n")
63+
64+
webhook_obj = Webhook(json_data) # Create a Webhook object from the JSON data
65+
room = api.rooms.get(webhook_obj.data.roomId) # Get the room details
66+
message = api.messages.get(webhook_obj.data.id) # Get the message details
67+
person = api.people.get(message.personId) # Get the sender's details
68+
69+
print("NEW MESSAGE IN ROOM '{}'".format(room.title))
70+
print("FROM '{}'".format(person.displayName))
71+
print("MESSAGE '{}'\n".format(message.text))
72+
73+
# This is a VERY IMPORTANT loop prevention control step.
74+
# If you respond to all messages... You will respond to the messages
75+
# that the bot posts and thereby create a loop condition.
76+
me = api.people.me()
77+
if message.personId == me.id:
78+
# Message was sent by me (bot); do not respond.
79+
return 'OK'
80+
else:
81+
# Mesage was sent by someone else; parse message and respond.
82+
if "/CAT" in message.text:
83+
print("FOUND '/CAT'")
84+
cat_fact = get_catfact() # Get a cat fact
85+
print("SENDING CAT FACT '{}'".format(cat_fact))
86+
response_message = api.messages.create(room.id, text=cat_fact) # Post the fact to the room where the request was received
87+
return 'OK'
88+
89+
90+
if __name__ == '__main__':
91+
# Start the web.py web server
92+
app.run()

examples/simple.py

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,49 @@
1-
#!/usr/bin/python
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""Simple ciscosparkapi demonstration script.
24
3-
from ciscosparkapi import CiscoSparkAPI
4-
5-
# VERY simple script to create a room, post a message, and post a file. Alternatively, if the room already exists it will delete the room.
5+
Very simple script to create a demo room, post a message, and post a file.
6+
If one or more rooms with the name of the demo room already exist, it will
7+
delete the previously existing rooms.
68
9+
The package natively retrieves your Spark access token from the
10+
SPARK_ACCESS_TOKEN environment variable. You must have this environment
11+
variable set to run this script.
712
8-
#storing the Authentication token in a file in the OS vs. leaving in script
9-
# see https://developer.ciscospark.com/getting-started.html to copy your token and simply place in a text file
10-
fat=open ("/home/brad/brad_at.txt","r+") #open the file with the token
11-
access_token=fat.readline().rstrip() #strip whitespace, newline, etc.
12-
fat.close #close file
13+
"""
1314

14-
room_name="TEST Hello World Room" #set the name of our test room - PLEASE NOTE this room can be deleted by script so don't use a current name
15-
room_count=0
16-
test_message="Hello World" #set the test message we will post
17-
test_url=["https://upload.wikimedia.org/wikipedia/commons/thumb/6/64/Cisco_logo.svg/375px-Cisco_logo.svg.png"] #set the location of a file we will post
1815

19-
api = CiscoSparkAPI(access_token) #invoke the API
16+
from __future__ import print_function
17+
from ciscosparkapi import CiscoSparkAPI
2018

21-
rooms = api.rooms.list() #grab the list of rooms where the user(from Access Token) is a member
2219

20+
DEMO_ROOM_NAME = "ciscosparkapi Demo Room"
21+
DEMO_PEOPLE = ["test01@cmlccie.com", "test02@cmlccie.com"]
22+
DEMO_MESSAGE = u"Cisco Spark rocks! \ud83d\ude0e"
23+
DEMO_FILE_URL = "https://developer.ciscospark.com/images/logo_spark_lg@256.png"
2324

24-
for room in rooms: #iterate through list of rooms
25-
if room.title == room_name: #look for our room
26-
room_count += 1 #if we find a room with the same name increment a counter
27-
print (room.title)
28-
room_id = room.id #grab the room id
29-
print (room_id)
30-
api.rooms.delete(room_id) #delete the room
31-
print ("Room Deleted")
3225

33-
if (room_count == 0): #Otherwise create the room, post a message, add a user, and attachment
34-
room = api.rooms.create(room_name) #create a room with room_name - room.id will be the id of this new room
35-
print room #prints returned JSON
36-
message = api.messages.create(room.id,None,None,test_message) #create a message in the new room
37-
print message
38-
message = api.messages.create(room.id,None,None,None,None,test_url) #Post a file in the new room from test_url
39-
print message
26+
api = CiscoSparkAPI() # Create a CiscoSparkAPI connection object; uses your SPARK_ACCESS_TOKEN
4027

4128

29+
# Clean up previous demo rooms
30+
print("Searching for existing demo rooms...")
31+
rooms = api.rooms.list() # Creates a generator container (iterable) that lists the rooms where you are a member
32+
existing_demo_rooms = [room for room in rooms if room.title == DEMO_ROOM_NAME] # Builds a list of rooms with the name DEMO_ROOM_NAME
33+
if existing_demo_rooms:
34+
print("Found {} existing room(s); "
35+
"deleting them.".format(len(existing_demo_rooms)))
36+
for room in existing_demo_rooms:
37+
api.rooms.delete(room.id) # Delete the room
38+
print ("Room '{}' deleted.".format(room.id))
4239

4340

4441

42+
demo_room = api.rooms.create(DEMO_ROOM_NAME) # Create a new demo room
43+
print(demo_room) # Print the room details (formatted JSON)
44+
for person_email in DEMO_PEOPLE:
45+
api.memberships.create(demo_room.id, personEmail=person_email) # Add people to the room
46+
message = api.messages.create(demo_room.id,text=DEMO_MESSAGE) # Create a message in the new room
47+
print(message) # Print the message details (formatted JSON)
48+
message = api.messages.create(demo_room.id,files=[DEMO_FILE_URL]) # Post a file in the new room from test_url
49+
print(message) # Print the message details (formatted JSON)

0 commit comments

Comments
 (0)