Skip to content

Commit 02ebc89

Browse files
committed
Merge branch 'master' into development
2 parents d8399a4 + 7466042 commit 02ebc89

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

examples/botexample.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
#
22+
23+
import web
24+
import json
25+
import requests
26+
from ciscosparkapi import CiscoSparkAPI
27+
28+
#
29+
# CHANGE this to your bot Access Token file
30+
#
31+
file_name="/home/brad/ciscosparkapiexamples/botat.txt"
32+
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'
95+
96+
97+
98+
if __name__ == '__main__':
99+
app.run() #starts listening for incoming webhooks
100+
101+
102+

examples/simple.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/python
2+
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.
6+
7+
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+
14+
room_name="TEST Hello World Room" #set the name of our test room - PLEASE NOTE this room can be delted 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
18+
19+
api = CiscoSparkAPI(access_token) #invoke the API
20+
21+
rooms = api.rooms.list() #grab the list of rooms where the user(from Access Token) is a member
22+
23+
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")
32+
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
40+
41+
42+
43+
44+

0 commit comments

Comments
 (0)