-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook-listener.py
More file actions
79 lines (57 loc) · 2.36 KB
/
webhook-listener.py
File metadata and controls
79 lines (57 loc) · 2.36 KB
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
#!/usr/bin/env python
#from __future__ import print_function
from flask import Flask
from flask import request
import os, json
import requests
app = Flask(__name__)
#WEBEX API URL
url = "https://api.ciscospark.com/v1/messages"
#ENVIRONMENTAL VARIABLES - MUST BE DEFINED IN AWS LAMBDA, ADDITIONAL ENCRYPTION POSSIBLE
roomid = os.environ['ROOMID']
auth = os.environ['AUTH']
#DON'T FORGET TO CHANGE DEFAULT LAMBDA HANDLER IN AWS GUI. IT SHOULD BE "webhook-listener.lambda_handler"
def lambda_handler(dnac, event):
body = json.loads(dnac['body'])
"""
This is a subject of change. I have chosen only following parameters from the webhook which are posted in WebEx room.
"""
event_name = body['details']['Assurance Issue Name']
time = dnac['requestContext']['requestTime']
category = body['category']
details = body['details']['Assurance Issue Details']
if 'Location' in body['details']:
location = body['details']['Location']
else:
location = "N/A"
#This is only for testing purpose to see how the entire Webhook looks like in AWS CloudWatch - might be commented
#print(dnac)
message = "******** ALERT CISCO DNA CENTER ************\n\n" + "EVENT : " + event_name + "\n\n" + "TIME : " + time + "\n\n" + "CATEGORY : " + category + "\n\n" + "DETAILS : " + details + "\n\n" + "LOCATION : " + location
#Posting a message to WebEx room
post_message(message)
return { "statusCode": 200 }
def post_message(message):
#ENVIRONMENT VARIABLES USED HERE
if auth == "Bearer XXXX":
print("No WebexTeams Token")
return
payload = {"roomId" : roomid,"text" : message}
headers = {
'authorization': auth,
'content-type': "application/json"
}
response = requests.request("POST", url, data=json.dumps(payload), headers=headers)
@app.route('/', defaults={'path': ''}, methods=['GET','POST'])
@app.route('/<path:path>', methods=["GET","PUT","POST","DELETE"])
def get_all(path):
print("Method {}, URI {}".format(request.method,path))
if request.method == "POST":
print (request.headers)
print (request.json)
if request.json != {}:
lambda_handler(request.remote_addr, request.json)
else:
print("skipping - empty")
return ("OK")
if __name__ == '__main__':
app.run(host="0.0.0.0", port="9000", ssl_context='adhoc')