kopia lustrzana https://github.com/fabston/TradingView-Webhook-Bot
Clean up and beautify code
rodzic
997420fb59
commit
cb431288d7
28
config.py
28
config.py
|
@ -13,31 +13,31 @@ sec_key = '' # Can be anything. Has to match with "key" in your TradingView ale
|
||||||
|
|
||||||
# Telegram Settings
|
# Telegram Settings
|
||||||
send_telegram_alerts = False
|
send_telegram_alerts = False
|
||||||
tg_token = '' # Bot token. Get it from @Botfather
|
tg_token = '' # Bot token. Get it from @Botfather
|
||||||
channel = 0 # Channel ID (ex. -1001487568087)
|
channel = 0 # Channel ID (ex. -1001487568087)
|
||||||
|
|
||||||
# Discord Settings
|
# Discord Settings
|
||||||
send_discord_alerts = False
|
send_discord_alerts = False
|
||||||
discord_webhook = '' # Discord Webhook URL (https://support.discordapp.com/hc/de/articles/228383668-Webhooks-verwenden)
|
discord_webhook = '' # Discord Webhook URL (https://support.discordapp.com/hc/de/articles/228383668-Webhooks-verwenden)
|
||||||
|
|
||||||
# Slack Settings
|
# Slack Settings
|
||||||
send_slack_alerts = False
|
send_slack_alerts = False
|
||||||
slack_webhook = '' # Slack Webhook URL (https://api.slack.com/messaging/webhooks)
|
slack_webhook = '' # Slack Webhook URL (https://api.slack.com/messaging/webhooks)
|
||||||
|
|
||||||
#Twitter Settings
|
# Twitter Settings
|
||||||
send_twitter_alerts = False
|
send_twitter_alerts = False
|
||||||
tw_ckey = ''
|
tw_ckey = ''
|
||||||
tw_csecret = ''
|
tw_csecret = ''
|
||||||
tw_atoken = ''
|
tw_atoken = ''
|
||||||
tw_asecret = ''
|
tw_asecret = ''
|
||||||
|
|
||||||
# Email Settings
|
# Email Settings
|
||||||
send_email_alerts = False
|
send_email_alerts = False
|
||||||
email_sender = '' # Your email address
|
email_sender = '' # Your email address
|
||||||
email_receivers = ['', ''] # Receivers, can be multiple
|
email_receivers = ['', ''] # Receivers, can be multiple
|
||||||
email_subject = 'Trade Alert!'
|
email_subject = 'Trade Alert!'
|
||||||
|
|
||||||
email_port = 465 # SMTP SSL Port (ex. 465)
|
email_port = 465 # SMTP SSL Port (ex. 465)
|
||||||
email_host = '' # SMTP host (ex. smtp.gmail.com)
|
email_host = '' # SMTP host (ex. smtp.gmail.com)
|
||||||
email_user = '' # SMTP Login credentials
|
email_user = '' # SMTP Login credentials
|
||||||
email_password = '' # SMTP Login credentials
|
email_password = '' # SMTP Login credentials
|
||||||
|
|
25
handler.py
25
handler.py
|
@ -12,16 +12,21 @@ import tweepy
|
||||||
import smtplib, ssl
|
import smtplib, ssl
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
|
|
||||||
|
|
||||||
def send_alert(data):
|
def send_alert(data):
|
||||||
if config.send_telegram_alerts:
|
if config.send_telegram_alerts:
|
||||||
tg_bot = Bot(token=config.tg_token)
|
tg_bot = Bot(token=config.tg_token)
|
||||||
try:
|
try:
|
||||||
tg_bot.sendMessage(data['telegram'], data['msg'].encode('latin-1','backslashreplace').decode('unicode_escape'), parse_mode='MARKDOWN')
|
tg_bot.sendMessage(data['telegram'],
|
||||||
|
data['msg'].encode('latin-1', 'backslashreplace').decode('unicode_escape'),
|
||||||
|
parse_mode='MARKDOWN')
|
||||||
except KeyError:
|
except KeyError:
|
||||||
tg_bot.sendMessage(config.channel, data['msg'].encode('latin-1','backslashreplace').decode('unicode_escape'), parse_mode='MARKDOWN')
|
tg_bot.sendMessage(config.channel,
|
||||||
except Exception as e:
|
data['msg'].encode('latin-1', 'backslashreplace').decode('unicode_escape'),
|
||||||
|
parse_mode='MARKDOWN')
|
||||||
|
except Exception as e:
|
||||||
print('[X] Telegram Error:\n>', e)
|
print('[X] Telegram Error:\n>', e)
|
||||||
|
|
||||||
if config.send_discord_alerts:
|
if config.send_discord_alerts:
|
||||||
try:
|
try:
|
||||||
webhook = DiscordWebhook(url="https://discord.com/api/webhooks/" + data['discord'])
|
webhook = DiscordWebhook(url="https://discord.com/api/webhooks/" + data['discord'])
|
||||||
|
@ -33,7 +38,7 @@ def send_alert(data):
|
||||||
embed = DiscordEmbed(title=data['msg'])
|
embed = DiscordEmbed(title=data['msg'])
|
||||||
webhook.add_embed(embed)
|
webhook.add_embed(embed)
|
||||||
response = webhook.execute()
|
response = webhook.execute()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('[X] Discord Error:\n>', e)
|
print('[X] Discord Error:\n>', e)
|
||||||
|
|
||||||
if config.send_slack_alerts:
|
if config.send_slack_alerts:
|
||||||
|
@ -43,7 +48,7 @@ def send_alert(data):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
slack = Slack(url='https://hooks.slack.com/services/' + config.slack_webhook)
|
slack = Slack(url='https://hooks.slack.com/services/' + config.slack_webhook)
|
||||||
slack.post(text=data['msg'])
|
slack.post(text=data['msg'])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('[X] Slack Error:\n>', e)
|
print('[X] Slack Error:\n>', e)
|
||||||
|
|
||||||
if config.send_twitter_alerts:
|
if config.send_twitter_alerts:
|
||||||
|
@ -54,17 +59,17 @@ def send_alert(data):
|
||||||
tw_api.update_status(status=data)
|
tw_api.update_status(status=data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('[X] Twitter Error:\n>', e)
|
print('[X] Twitter Error:\n>', e)
|
||||||
|
|
||||||
if config.send_email_alerts:
|
if config.send_email_alerts:
|
||||||
try:
|
try:
|
||||||
email_msg = MIMEText(data)
|
email_msg = MIMEText(data)
|
||||||
email_msg['Subject'] = config.email_subject
|
email_msg['Subject'] = config.email_subject
|
||||||
email_msg['From'] = config.email_sender
|
email_msg['From'] = config.email_sender
|
||||||
email_msg['To'] = config.email_sender
|
email_msg['To'] = config.email_sender
|
||||||
context = ssl.create_default_context()
|
context = ssl.create_default_context()
|
||||||
with smtplib.SMTP_SSL(config.email_host, config.email_port, context=context) as server:
|
with smtplib.SMTP_SSL(config.email_host, config.email_port, context=context) as server:
|
||||||
server.login(config.email_user, config.email_password)
|
server.login(config.email_user, config.email_password)
|
||||||
server.sendmail(config.email_sender, config.email_receivers, email_msg.as_string())
|
server.sendmail(config.email_sender, config.email_receivers, email_msg.as_string())
|
||||||
server.quit()
|
server.quit()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('[X] Email Error:\n>', e)
|
print('[X] Email Error:\n>', e)
|
||||||
|
|
12
main.py
12
main.py
|
@ -12,11 +12,13 @@ from handler import *
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
|
||||||
def get_timestamp():
|
def get_timestamp():
|
||||||
timestamp = time.strftime("%Y-%m-%d %X")
|
timestamp = time.strftime("%Y-%m-%d %X")
|
||||||
return timestamp
|
return timestamp
|
||||||
|
|
||||||
@app.route('/webhook', methods=['POST'])
|
|
||||||
|
@app.route('/webhook', methods=['POST'])
|
||||||
def webhook():
|
def webhook():
|
||||||
try:
|
try:
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
|
@ -30,11 +32,13 @@ def webhook():
|
||||||
else:
|
else:
|
||||||
print('[X]', get_timestamp(), 'Alert Received & Refused! (Wrong Key)')
|
print('[X]', get_timestamp(), 'Alert Received & Refused! (Wrong Key)')
|
||||||
return 'Refused alert', 400
|
return 'Refused alert', 400
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('[X]', get_timestamp(), 'Error:\n>', e)
|
print('[X]', get_timestamp(), 'Error:\n>', e)
|
||||||
return 'Error', 400
|
return 'Error', 400
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
from waitress import serve
|
from waitress import serve
|
||||||
serve(app, host='0.0.0.0', port=80)
|
|
||||||
|
serve(app, host='0.0.0.0', port=80)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
flask
|
flask
|
||||||
waitress
|
waitress
|
||||||
python-telegram-bot
|
python-telegram-bot
|
||||||
discord-webhook
|
discord-webhook
|
||||||
slack-webhook
|
slack-webhook
|
||||||
tweepy
|
tweepy
|
Ładowanie…
Reference in New Issue