-
Notifications
You must be signed in to change notification settings - Fork 4
/
webhook-v2.py
53 lines (42 loc) · 2.01 KB
/
webhook-v2.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
from flask import Flask, request, jsonify
from ciscosparkapi import CiscoSparkAPI, SparkApiError
import json
import os
import time
import datetime
import pytz
bearer_token = os.environ.get("bearer_token")
room_id = os.environ.get("room_id")
if bearer_token is None or room_id is None:
print("\nWebex Teams Authorization and roomId details must be set via environment variables using below commands on macOS or Ubuntu workstation")
print("export bearer_token=<authorization bearer token>")
print("export room_id=<webex teams room-id>")
print("\nWebex Teams Authorization and roomId details must be set via environment variables using below commands on Windows workstation")
print("set bearer_token=<authorization bearer token>")
print("set room_id=<webex teams room-id>")
exit()
app = Flask(__name__)
@app.route('/',methods=['POST'])
def alarms():
try:
PDT = pytz.timezone('America/Los_Angeles')
data = json.loads(request.data)
print(data)
message = '''Team, Alarm event : **''' + data['rule_name_display'] + '''** is recieved from vManage and here are the complete details <br><br>'''
temp_time = datetime.datetime.utcfromtimestamp(data['entry_time']/1000.)
temp_time = pytz.UTC.localize(temp_time)
message = message + '**Alarm Date & Time:** ' + temp_time.astimezone(PDT).strftime('%c') + ' PDT'
temp = data['values_short_display']
for item in temp:
for key, value in item.items():
message = message + '<br> **' + key + ':** ' + value
message = message + '<br> **' + 'Details:' + '** ' + "https://sdwanlab.cisco.com:10000/#/app/monitor/alarms/details/" + data['uuid']
api = CiscoSparkAPI(access_token=bearer_token)
res=api.messages.create(roomId=room_id, markdown=message)
print(res)
except Exception as exc:
print(exc)
return jsonify(str(exc)), 500
return jsonify("Message sent to Webex Teams"), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001, debug=True)