-
Notifications
You must be signed in to change notification settings - Fork 394
/
main.py
46 lines (35 loc) · 1.47 KB
/
main.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
# ----------------------------------------------- #
# Plugin Name : TradingView-Webhook-Bot #
# Author Name : fabston #
# File Name : main.py #
# ----------------------------------------------- #
from handler import send_alert
import config
import time
from flask import Flask, request, jsonify
app = Flask(__name__)
def get_timestamp():
timestamp = time.strftime("%Y-%m-%d %X")
return timestamp
@app.route("/webhook", methods=["POST"])
def webhook():
whitelisted_ips = ['52.89.214.238', '34.212.75.30', '54.218.53.128', '52.32.178.7']
client_ip = request.headers.get('X-Forwarded-For', request.remote_addr)
if client_ip not in whitelisted_ips:
return jsonify({'message': 'Unauthorized'}), 401
try:
if request.method == "POST":
data = request.get_json()
if data["key"] == config.sec_key:
print(get_timestamp(), "Alert Received & Sent!")
send_alert(data)
return jsonify({'message': 'Webhook received successfully'}), 200
else:
print("[X]", get_timestamp(), "Alert Received & Refused! (Wrong Key)")
return jsonify({'message': 'Unauthorized'}), 401
except Exception as e:
print("[X]", get_timestamp(), "Error:\n>", e)
return jsonify({'message': 'Error'}), 400
if __name__ == "__main__":
from waitress import serve
serve(app, host="0.0.0.0", port=8080)