forked from CiscoDevNet/DesignThinking-SparkBot-TensorFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ciscospark.py
199 lines (147 loc) · 6.18 KB
/
ciscospark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# import the requests library so we can use it to make REST calls
import requests
import sys
import config
# globally disable warnings about using certificate verification
requests.packages.urllib3.disable_warnings()
# create_room function
def create_room():
# add authorization to the header
header = {"Authorization": "Bearer %s" % config.token}
# create request url
rooms_api = "https://api.ciscospark.com/v1/rooms"
# create request body
payload = {
"title": "%s with Spark Bot" % config.email
}
# send POST request and do not verify SSL certificate for simplicity
api_response = requests.post(rooms_api, json=payload, headers=header, verify=False)
# get the response status code
response_status = api_response.status_code
if response_status == 200:
# parse the response in json
response_json = api_response.json()
# print the response
print("room %s created" % response_json['id'])
return response_json['id']
else:
print("%d: error creating room, please check your access token" % response_status)
sys.exit(0)
# create_membership function
def create_membership(room_id):
# add authorization to the header
header = {"Authorization": "Bearer %s" % config.token}
# create request url
memberships_api = "https://api.ciscospark.com/v1/memberships"
# create request body
payload = {
"roomId": room_id,
"personEmail": config.email
}
# send POST request and do not verify SSL certificate for simplicity
api_response = requests.post(memberships_api, json=payload, headers=header, verify=False)
# get the response status code
response_status = api_response.status_code
if response_status == 200:
# print the response
print("%s joined room %s" % (config.email, room_id))
else:
print("%d: error adding you to room, please check your email address" % response_status)
sys.exit(0)
# create_webhook function
def create_webhook(room_id):
# add authorization to the header
header = {"Authorization": "Bearer %s" % config.token}
# create request url
webhooks_api = "https://api.ciscospark.com/v1/webhooks"
# create request body
payload = {
"resource": "messages",
"event": "created",
"filter": "roomId=%s" % room_id,
"targetUrl": "%s/webhook" % config.ngrok_url,
"name": "tf_workshop"
}
# send POST request and do not verify SSL certificate for simplicity
api_response = requests.post(webhooks_api, json=payload, headers=header, verify=False)
# get the response status code
response_status = api_response.status_code
if response_status == 200:
# print the response
print("%s registered" % config.ngrok_url)
else:
print("%d: error registering webhook to cisco spark, please check your ngrok_url" % response_status)
sys.exit(0)
# get_message function
def get_message(message_id):
header = {"Authorization": "Bearer %s" % config.token}
# create request url using message ID
messages_api = "https://api.ciscospark.com/v1/messages/"
# send the GET request and do not verify SSL certificate for simplicity of this example
api_response = requests.get(messages_api + message_id, headers=header, verify=False)
# get the response status code
response_status = api_response.status_code
if response_status == 200:
# parse the response in json
response_json = api_response.json()
# get the text value from the response
text = response_json["text"]
# return the text value
return text
else:
print("%d: error retrieve message, please check your message_id: %s" % (response_status, message_id))
sys.exit(0)
# post_message function
def post_message(text):
# add authorization to the header
header = {"Authorization": "Bearer %s" % config.token}
# create request url
messages_api = "https://api.ciscospark.com/v1/messages"
# create message in Spark room
payload = {
"toPersonEmail": config.email,
"text": text
}
# create POST request do not verify SSL certificate for simplicity of this example
api_response = requests.post(messages_api, json=payload, headers=header, verify=False)
# get the response status code
response_status = api_response.status_code
if response_status == 200:
# parse the response in json
response_json = api_response.json()
room_id = response_json['roomId']
# print the response
print("message [%s] sent in room %s" % (text, room_id))
return room_id
else:
print("%d: error sending message please check your network connection" % response_status)
sys.exit(0)
# is_image_attachment function
def is_image_attachment(attachment):
# add authorization to the header
header = {"Authorization": "Bearer %s" % config.token}
# send the head request and do not verify SSL certificate for simplicity of this example
api_response = requests.head(attachment, headers=header, verify=False)
return api_response.headers['Content-Type'].startswith("image/"), api_response.headers['content-disposition']
# download_attachment function
def download_attachment(attachment, target):
# add authorization to the header
header = {"Authorization": "Bearer %s" % config.token}
# send the head request and do not verify SSL certificate for simplicity of this example
api_response = requests.get(attachment, headers=header, verify=False)
with open(target, 'wb') as f:
f.write(api_response.content)
# check_webhook function
def check_webhook():
# send the GET request and do not verify SSL certificate for simplicity of this example
api_response = requests.get(config.ngrok_url, verify=False)
# get the response status code
response_status = api_response.status_code
if response_status == 404:
print("%d: error hitting webhook, please check your ngrok instance is up and running: %s" % (
response_status, config.ngrok_url))
sys.exit(0)
if __name__ == '__main__':
check_webhook()
room_id = post_message("Welcome")
create_webhook(room_id)