From b8ef523b2c35755866ce726d60113b6f2604bda0 Mon Sep 17 00:00:00 2001 From: fabston Date: Wed, 1 May 2024 08:38:17 +0700 Subject: [PATCH] Update main.py --- .github/FUNDING.yml | 1 - README.md | 48 +++++++++++++++++++++++++++++++++------------ main.py | 24 +++++++++++------------ 3 files changed, 47 insertions(+), 26 deletions(-) delete mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml deleted file mode 100644 index 355b55d..0000000 --- a/.github/FUNDING.yml +++ /dev/null @@ -1 +0,0 @@ -liberapay: fabston \ No newline at end of file diff --git a/README.md b/README.md index 33736b8..b3884da 100644 --- a/README.md +++ b/README.md @@ -26,20 +26,21 @@ The **TradingView Webhook Bot** ⚙️ listens to [TradingView](https://tradingview.com) alerts via [webhooks](https://www.tradingview.com/support/solutions/43000529348-i-want-to-know-more-about-webhooks/) using [flask](https://flask.palletsprojects.com/en/1.1.x/). All alerts can be instantly sent to Telegram, Discord, Twitter and/or Email. -## Features -- Telegram Support using the [Python Telegram](https://github.com/python-telegram-bot/python-telegram-bot) libary -- Discord Support using [webhooks](https://support.discord.com/hc/de/articles/228383668-Webhooks-verwenden) -- Slack Support using [webhooks](https://api.slack.com/messaging/webhooks) -- Twitter Support using the [tweepy](https://github.com/tweepy/tweepy) libary -- Email Support using [smtplib](https://docs.python.org/3/library/smtplib.html) -- Alert channels can be enabled or disabled in [`config.py`](https://github.com/fabston/TradingView-Webhook-Bot/blob/master/config.py) -- Dynamically send alerts to different Telegram and/or Discord channels -- TradingView `{{close}}`, `{{exchange}}` etc. variables support. Read more [here](https://www.tradingview.com/blog/en/introducing-variables-in-alerts-14880/) +> 📊 If you are looking for an exchange to trade on, I can recommend [Bybit](https://partner.bybit.com/b/20882). +> Sign up now and receive up to $30,000 in Deposit Rewards! -> 💡 Got a feature idea? Open an [issue](https://github.com/fabston/TradingView-Webhook-Bot/issues/new?assignees=&labels=enhancement&template=feature-request---.md) and I might implement it. +## Features +- Telegram Support using the [Python Telegram](https://github.com/python-telegram-bot/python-telegram-bot) libary. +- Discord Support using [webhooks](https://support.discord.com/hc/de/articles/228383668-Webhooks-verwenden). +- Slack Support using [webhooks](https://api.slack.com/messaging/webhooks). +- Twitter Support using the [tweepy](https://github.com/tweepy/tweepy) libary. +- Email Support using [smtplib](https://docs.python.org/3/library/smtplib.html). +- Alert channels can be enabled or disabled in [`config.py`](https://github.com/fabston/TradingView-Webhook-Bot/blob/master/config.py). +- Dynamically send alerts to different Telegram and/or Discord channels. +- TradingView `{{close}}`, `{{exchange}}` etc. variables support. Read more [here](https://www.tradingview.com/blog/en/introducing-variables-in-alerts-14880/). ## Installation -> ⚠️ Best to run the bot on a VPS. I can recommend Hetzner's CX11 VPS for 2.89€/month. [Sign up](https://hetzner.cloud/?ref=tQ1NdT8zbfNY) now and receive **€20 free** credits. +> ⚠️ Best to run the bot on a VPS. I can recommend Hetzner's CX11 VPS for 3.79€/month. [Sign up](https://hetzner.cloud/?ref=tQ1NdT8zbfNY) now and receive **€20 free** credits. 1. Clone this repository `git clone https://github.com/fabston/TradingView-Webhook-Bot.git` 1. Create your virtual environment `python3 -m venv TradingView-Webhook-Bot` 1. Activate it `source TradingView-Webhook-Bot/bin/activate && cd TradingView-Webhook-Bot` @@ -64,14 +65,35 @@ All alerts can be instantly sent to Telegram, Discord, Twitter and/or Email. 1. Run the bot with `python main.py` 1. [PM2](https://github.com/fabston/TradingView-Webhook-Bot/issues/28#issuecomment-766301062) can help you in running the app in the background / on system boot. +### Forward Port 80 to 8080 using NGINX + +*It is recommended to run flask on a different port like 8080. It is then necessary to forward port 80 to 8080.* + +1. Edit the NGINX configuration file `sudo nano /etc/nginx/sites-enabled/tv_webhook` +1. Add the following content: + ```nginx + server { + listen 80; + + server_name ; + + location / { + proxy_pass http://127.0.0.1:8080; # Forward traffic to port 8080 + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # Pass client's IP address + proxy_set_header X-Forwarded-Proto $scheme; + } + } + ``` +1. Restart NGINX `sudo service nginx restart` + ### Docker 1. Clone this repository `git clone https://github.com/fabston/TradingView-Webhook-Bot.git` 1. Edit and update [`config.py`](https://github.com/fabston/TradingView-Webhook-Bot/blob/master/config.py) 1. `docker-compose build` 1. `docker-compose up` -*It is recommended to run flask on a different port like 8080. It is then necessary to forward port 80 to 8080.* - ## Images ![Webhook Bot](https://i.imgur.com/vZA42cc.png) diff --git a/main.py b/main.py index 318516f..ed899bd 100644 --- a/main.py +++ b/main.py @@ -4,13 +4,10 @@ # File Name : main.py # # ----------------------------------------------- # -import json -import time - -from flask import Flask, request - +from handler import send_alert import config -from handler import * +import time +from flask import Flask, request, jsonify app = Flask(__name__) @@ -22,25 +19,28 @@ def get_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() - key = data["key"] - if key == config.sec_key: + if data["key"] == config.sec_key: print(get_timestamp(), "Alert Received & Sent!") send_alert(data) - return "Sent alert", 200 + return jsonify({'message': 'Webhook received successfully'}), 200 else: print("[X]", get_timestamp(), "Alert Received & Refused! (Wrong Key)") - return "Refused alert", 400 + return jsonify({'message': 'Unauthorized'}), 401 except Exception as e: print("[X]", get_timestamp(), "Error:\n>", e) - return "Error", 400 + return jsonify({'message': 'Error'}), 400 if __name__ == "__main__": from waitress import serve - serve(app, host="0.0.0.0", port=80) + serve(app, host="0.0.0.0", port=8080)