forked from Peter-Nhan/Flask_webhook_receiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_rx_web_view.py
69 lines (56 loc) · 2.76 KB
/
flask_rx_web_view.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
import urllib3
import json
import os
import time
import cryptography
from flask import Flask, render_template, request, send_file
from flask_basicauth import BasicAuth
from urllib3.exceptions import InsecureRequestWarning # for insecure https warnings
urllib3.disable_warnings(InsecureRequestWarning) # disable insecure https warnings
from config import WEBHOOK_USERNAME, WEBHOOK_PASSWORD
save_webhook_output_file = "all_webhooks_detailed.json"
app = Flask(__name__)
app.config['BASIC_AUTH_USERNAME'] = WEBHOOK_USERNAME
app.config['BASIC_AUTH_PASSWORD'] = WEBHOOK_PASSWORD
# If true, then site wide authentication is needed
# otherwise if false - authentication is used only where @basic_auth.required configured
# only used for app.route is "/" and "webhook"
app.config['BASIC_AUTH_FORCE'] = False
basic_auth = BasicAuth(app)
@app.route("/") # create a route for / - just to test server is up.
@basic_auth.required # Authenticate this request
def index():
return '<h1>Flask Receiver App is Up!</h1>', 200
# View the logs via the Web page
@app.route("/log", methods=['GET']) # create a route for /log, method GET
def log():
with open(save_webhook_output_file, "r") as f:
content_of_file = f.read()
return render_template('bootstrap.html', content_var = content_of_file, filename_var = save_webhook_output_file)
# Download the logs file
@app.route("/download", methods=['GET']) # create a route for /download, method GET
def download():
return send_file(save_webhook_output_file, as_attachment=True)
@app.route('/webhook', methods=['POST']) # create a route for /webhook, method POST
@basic_auth.required # Authenticate this request
def webhook():
if request.method == 'POST':
print('Webhook Received')
request_json = request.json
# print the received notification
print('Payload: ')
# Change from original - remove the need for function to print
print(json.dumps(request_json,indent=4))
# save as a file, create new file if not existing, append to existing file
# full details of each notification to file 'all_webhooks_detailed.json'
# Change above save_webhook_output_file to a different filename
with open(save_webhook_output_file, 'a') as filehandle:
# Change from original - we output to file so that the we page works better with the newlines.
filehandle.write('%s\n' % json.dumps(request_json,indent=4))
filehandle.write('= - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - \n')
return 'Webhook notification received', 202
else:
return 'POST Method not supported', 405
if __name__ == '__main__':
app.run(ssl_context='adhoc', host='0.0.0.0', port=5443, debug=True)
# Set to use HTTPS with port 5443