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 : handler.py #
|
|
|
|
# ----------------------------------------------- #
|
|
|
|
|
2020-07-11 15:52:48 +00:00
|
|
|
import config
|
2020-04-21 14:29:00 +00:00
|
|
|
from telegram import Bot
|
2020-12-19 14:15:46 +00:00
|
|
|
from discord_webhook import DiscordWebhook, DiscordEmbed
|
2021-02-05 17:27:05 +00:00
|
|
|
from slack_webhook import Slack
|
2020-04-24 11:30:24 +00:00
|
|
|
import tweepy
|
2020-04-22 13:57:28 +00:00
|
|
|
import smtplib, ssl
|
|
|
|
from email.mime.text import MIMEText
|
2020-12-19 14:15:46 +00:00
|
|
|
|
2021-03-27 07:20:00 +00:00
|
|
|
|
2020-07-11 15:52:48 +00:00
|
|
|
def send_alert(data):
|
2020-12-02 14:06:24 +00:00
|
|
|
if config.send_telegram_alerts:
|
2020-12-19 14:15:46 +00:00
|
|
|
tg_bot = Bot(token=config.tg_token)
|
2020-07-11 15:52:48 +00:00
|
|
|
try:
|
2021-03-27 07:20:00 +00:00
|
|
|
tg_bot.sendMessage(data['telegram'],
|
|
|
|
data['msg'].encode('latin-1', 'backslashreplace').decode('unicode_escape'),
|
|
|
|
parse_mode='MARKDOWN')
|
2020-12-19 14:15:46 +00:00
|
|
|
except KeyError:
|
2021-03-27 07:20:00 +00:00
|
|
|
tg_bot.sendMessage(config.channel,
|
|
|
|
data['msg'].encode('latin-1', 'backslashreplace').decode('unicode_escape'),
|
|
|
|
parse_mode='MARKDOWN')
|
|
|
|
except Exception as e:
|
2020-12-19 14:15:46 +00:00
|
|
|
print('[X] Telegram Error:\n>', e)
|
2021-03-27 07:20:00 +00:00
|
|
|
|
2020-12-02 14:06:24 +00:00
|
|
|
if config.send_discord_alerts:
|
2020-07-11 15:52:48 +00:00
|
|
|
try:
|
2020-12-19 14:15:46 +00:00
|
|
|
webhook = DiscordWebhook(url="https://discord.com/api/webhooks/" + data['discord'])
|
|
|
|
embed = DiscordEmbed(title=data['msg'])
|
|
|
|
webhook.add_embed(embed)
|
|
|
|
response = webhook.execute()
|
|
|
|
except KeyError:
|
|
|
|
webhook = DiscordWebhook(url="https://discord.com/api/webhooks/" + config.discord_webhook)
|
|
|
|
embed = DiscordEmbed(title=data['msg'])
|
|
|
|
webhook.add_embed(embed)
|
|
|
|
response = webhook.execute()
|
2021-03-27 07:20:00 +00:00
|
|
|
except Exception as e:
|
2020-12-19 14:15:46 +00:00
|
|
|
print('[X] Discord Error:\n>', e)
|
2021-02-05 17:27:05 +00:00
|
|
|
|
|
|
|
if config.send_slack_alerts:
|
|
|
|
try:
|
|
|
|
slack = Slack(url='https://hooks.slack.com/services/' + data['slack'])
|
|
|
|
slack.post(text=data['msg'])
|
|
|
|
except KeyError:
|
|
|
|
slack = Slack(url='https://hooks.slack.com/services/' + config.slack_webhook)
|
|
|
|
slack.post(text=data['msg'])
|
2021-03-27 07:20:00 +00:00
|
|
|
except Exception as e:
|
2021-02-05 17:27:05 +00:00
|
|
|
print('[X] Slack Error:\n>', e)
|
|
|
|
|
2020-12-02 14:06:24 +00:00
|
|
|
if config.send_twitter_alerts:
|
2020-11-19 08:58:06 +00:00
|
|
|
tw_auth = tweepy.OAuthHandler(config.tw_ckey, config.tw_csecret)
|
|
|
|
tw_auth.set_access_token(config.tw_atoken, config.tw_asecret)
|
|
|
|
tw_api = tweepy.API(tw_auth)
|
2020-07-11 15:52:48 +00:00
|
|
|
try:
|
2021-04-27 11:47:50 +00:00
|
|
|
tw_api.update_status(status=data['msg'].replace('*', '').replace('_', '').replace('`', ''))
|
2020-07-11 15:52:48 +00:00
|
|
|
except Exception as e:
|
2020-12-19 14:15:46 +00:00
|
|
|
print('[X] Twitter Error:\n>', e)
|
2021-03-27 07:20:00 +00:00
|
|
|
|
2020-12-02 14:06:24 +00:00
|
|
|
if config.send_email_alerts:
|
2020-07-11 15:52:48 +00:00
|
|
|
try:
|
2021-04-27 11:47:50 +00:00
|
|
|
email_msg = MIMEText(data['msg'].replace('*', '').replace('_', '').replace('`', ''))
|
2020-07-11 15:52:48 +00:00
|
|
|
email_msg['Subject'] = config.email_subject
|
2021-03-27 07:20:00 +00:00
|
|
|
email_msg['From'] = config.email_sender
|
|
|
|
email_msg['To'] = config.email_sender
|
2020-07-11 15:52:48 +00:00
|
|
|
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:
|
2021-03-27 07:20:00 +00:00
|
|
|
print('[X] Email Error:\n>', e)
|