Skip to content

Commit 7466042

Browse files
committed
Added Bot script example
1 parent 7498857 commit 7466042

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-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+

0 commit comments

Comments
 (0)