-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
79 lines (53 loc) · 1.94 KB
/
app.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
from flask import Flask, request, make_response
from flask_restful import Resource, Api
import os
from spark.rooms import Room
from spark.messages import Message
from spark.session import Session
from spark.webhooks import Webhook
app = Flask(__name__)
api = Api(app)
app.secret_key = 'CHANGEME'
REGISTERED = False
class Status(Resource):
def get(self):
return {"status": "ok"}
class SparkWebHook(Resource):
"""
A Resource template for our Spark Webhook
"""
# Create your responses here as a dictionary
response_dict = dict()
# This will be used for health checks at '/
def get(self):
return {'info': 'SparkWebhook',
'detail': self.response_dict,
'registered': REGISTERED}
# Receive a post request
def post(self):
# Extract the data key from the POST request
# Determine the message ID that triggered the webhook
# Retrieve the message based on ID
# https://github.com/imapex/spark-python/blob/master/spark/messages.py
# Conditional to test if we need to respond (if it's in our response_map)
# Determine Room the message was received in
# Send Message to room
# Send response code to the caller
return {'status': 'processed'}
class RegisterWebhook(Resource):
def post(self):
# Register our webhook
session = Session('https://api.ciscospark.com', os.getenv("SPARK_TOKEN"))
webhook = Webhook()
webhook.targetUrl = request.url_root + '/api/spark'
webhook.resource = 'messages'
webhook.event = 'created'
resp = webhook.create(session)
if resp.ok:
REGISTERED = True
# Register our resource classes as endpoints to our API
api.add_resource(Status, '/')
api.add_resource(SparkWebHook, '/api/spark')
api.add_resource(RegisterWebhook, '/api/spark/register')
if __name__ == "__main__":
app.run(debug=True,host='0.0.0.0')