TradingView-Webhook-Bot/main.py

47 wiersze
1.5 KiB
Python

2020-07-04 07:38:38 +00:00
# ----------------------------------------------- #
# Plugin Name : TradingView-Webhook-Bot #
2021-03-14 08:24:07 +00:00
# Author Name : fabston #
2020-07-04 07:38:38 +00:00
# File Name : main.py #
# ----------------------------------------------- #
2024-05-01 01:38:17 +00:00
from handler import send_alert
2021-06-22 12:34:31 +00:00
import config
2024-05-01 01:38:17 +00:00
import time
from flask import Flask, request, jsonify
2020-04-21 14:29:00 +00:00
app = Flask(__name__)
2021-03-27 07:20:00 +00:00
def get_timestamp():
timestamp = time.strftime("%Y-%m-%d %X")
return timestamp
2021-03-27 07:20:00 +00:00
2021-06-22 12:34:31 +00:00
@app.route("/webhook", methods=["POST"])
2020-04-21 14:29:00 +00:00
def webhook():
2024-05-01 01:38:17 +00:00
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:
2021-06-22 12:34:31 +00:00
if request.method == "POST":
data = request.get_json()
2024-05-01 01:38:17 +00:00
if data["key"] == config.sec_key:
2021-06-22 12:34:31 +00:00
print(get_timestamp(), "Alert Received & Sent!")
send_alert(data)
2024-05-01 01:38:17 +00:00
return jsonify({'message': 'Webhook received successfully'}), 200
else:
2021-06-22 12:34:31 +00:00
print("[X]", get_timestamp(), "Alert Received & Refused! (Wrong Key)")
2024-05-01 01:38:17 +00:00
return jsonify({'message': 'Unauthorized'}), 401
2021-03-27 07:20:00 +00:00
except Exception as e:
2021-06-22 12:34:31 +00:00
print("[X]", get_timestamp(), "Error:\n>", e)
2024-05-01 01:38:17 +00:00
return jsonify({'message': 'Error'}), 400
2020-04-21 14:29:00 +00:00
2021-03-27 07:20:00 +00:00
2021-06-22 12:34:31 +00:00
if __name__ == "__main__":
2020-04-21 14:29:00 +00:00
from waitress import serve
2021-03-27 07:20:00 +00:00
2024-05-01 01:38:17 +00:00
serve(app, host="0.0.0.0", port=8080)