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
|
||||
send_telegram_alerts = False
|
||||
tg_token = '' # Bot token. Get it from @Botfather
|
||||
channel = 0 # Channel ID (ex. -1001487568087)
|
||||
tg_token = '' # Bot token. Get it from @Botfather
|
||||
channel = 0 # Channel ID (ex. -1001487568087)
|
||||
|
||||
# Discord Settings
|
||||
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
|
||||
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
|
||||
tw_ckey = ''
|
||||
tw_ckey = ''
|
||||
tw_csecret = ''
|
||||
tw_atoken = ''
|
||||
tw_atoken = ''
|
||||
tw_asecret = ''
|
||||
|
||||
# Email Settings
|
||||
send_email_alerts = False
|
||||
email_sender = '' # Your email address
|
||||
email_receivers = ['', ''] # Receivers, can be multiple
|
||||
email_subject = 'Trade Alert!'
|
||||
email_sender = '' # Your email address
|
||||
email_receivers = ['', ''] # Receivers, can be multiple
|
||||
email_subject = 'Trade Alert!'
|
||||
|
||||
email_port = 465 # SMTP SSL Port (ex. 465)
|
||||
email_host = '' # SMTP host (ex. smtp.gmail.com)
|
||||
email_user = '' # SMTP Login credentials
|
||||
email_password = '' # SMTP Login credentials
|
||||
email_port = 465 # SMTP SSL Port (ex. 465)
|
||||
email_host = '' # SMTP host (ex. smtp.gmail.com)
|
||||
email_user = '' # SMTP Login credentials
|
||||
email_password = '' # SMTP Login credentials
|
||||
|
|
25
handler.py
25
handler.py
|
@ -12,16 +12,21 @@ import tweepy
|
|||
import smtplib, ssl
|
||||
from email.mime.text import MIMEText
|
||||
|
||||
|
||||
def send_alert(data):
|
||||
if config.send_telegram_alerts:
|
||||
tg_bot = Bot(token=config.tg_token)
|
||||
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:
|
||||
tg_bot.sendMessage(config.channel, data['msg'].encode('latin-1','backslashreplace').decode('unicode_escape'), parse_mode='MARKDOWN')
|
||||
except Exception as e:
|
||||
tg_bot.sendMessage(config.channel,
|
||||
data['msg'].encode('latin-1', 'backslashreplace').decode('unicode_escape'),
|
||||
parse_mode='MARKDOWN')
|
||||
except Exception as e:
|
||||
print('[X] Telegram Error:\n>', e)
|
||||
|
||||
|
||||
if config.send_discord_alerts:
|
||||
try:
|
||||
webhook = DiscordWebhook(url="https://discord.com/api/webhooks/" + data['discord'])
|
||||
|
@ -33,7 +38,7 @@ def send_alert(data):
|
|||
embed = DiscordEmbed(title=data['msg'])
|
||||
webhook.add_embed(embed)
|
||||
response = webhook.execute()
|
||||
except Exception as e:
|
||||
except Exception as e:
|
||||
print('[X] Discord Error:\n>', e)
|
||||
|
||||
if config.send_slack_alerts:
|
||||
|
@ -43,7 +48,7 @@ def send_alert(data):
|
|||
except KeyError:
|
||||
slack = Slack(url='https://hooks.slack.com/services/' + config.slack_webhook)
|
||||
slack.post(text=data['msg'])
|
||||
except Exception as e:
|
||||
except Exception as e:
|
||||
print('[X] Slack Error:\n>', e)
|
||||
|
||||
if config.send_twitter_alerts:
|
||||
|
@ -54,17 +59,17 @@ def send_alert(data):
|
|||
tw_api.update_status(status=data)
|
||||
except Exception as e:
|
||||
print('[X] Twitter Error:\n>', e)
|
||||
|
||||
|
||||
if config.send_email_alerts:
|
||||
try:
|
||||
email_msg = MIMEText(data)
|
||||
email_msg['Subject'] = config.email_subject
|
||||
email_msg['From'] = config.email_sender
|
||||
email_msg['To'] = config.email_sender
|
||||
email_msg['From'] = config.email_sender
|
||||
email_msg['To'] = config.email_sender
|
||||
context = ssl.create_default_context()
|
||||
with smtplib.SMTP_SSL(config.email_host, config.email_port, context=context) as server:
|
||||
server.login(config.email_user, config.email_password)
|
||||
server.sendmail(config.email_sender, config.email_receivers, email_msg.as_string())
|
||||
server.quit()
|
||||
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__)
|
||||
|
||||
|
||||
def get_timestamp():
|
||||
timestamp = time.strftime("%Y-%m-%d %X")
|
||||
return timestamp
|
||||
|
||||
@app.route('/webhook', methods=['POST'])
|
||||
|
||||
|
||||
@app.route('/webhook', methods=['POST'])
|
||||
def webhook():
|
||||
try:
|
||||
if request.method == 'POST':
|
||||
|
@ -30,11 +32,13 @@ def webhook():
|
|||
else:
|
||||
print('[X]', get_timestamp(), 'Alert Received & Refused! (Wrong Key)')
|
||||
return 'Refused alert', 400
|
||||
|
||||
|
||||
except Exception as e:
|
||||
print('[X]', get_timestamp(), 'Error:\n>', e)
|
||||
return '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=80)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
flask
|
||||
waitress
|
||||
python-telegram-bot
|
||||
discord-webhook
|
||||
slack-webhook
|
||||
flask
|
||||
waitress
|
||||
python-telegram-bot
|
||||
discord-webhook
|
||||
slack-webhook
|
||||
tweepy
|
Ładowanie…
Reference in New Issue