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 #
|
|
|
|
# ----------------------------------------------- #
|
|
|
|
|
2021-06-22 12:34:31 +00:00
|
|
|
import smtplib
|
|
|
|
import ssl
|
2020-04-22 13:57:28 +00:00
|
|
|
from email.mime.text import MIMEText
|
2020-12-19 14:15:46 +00:00
|
|
|
|
2021-06-22 12:34:31 +00:00
|
|
|
import tweepy
|
|
|
|
from discord_webhook import DiscordEmbed, DiscordWebhook
|
|
|
|
from slack_webhook import Slack
|
|
|
|
from telegram import Bot
|
|
|
|
|
|
|
|
import config
|
|
|
|
|
2021-03-27 07:20:00 +00:00
|
|
|
|
2020-07-11 15:52:48 +00:00
|
|
|
def send_alert(data):
|
2021-08-13 12:58:40 +00:00
|
|
|
msg = data["msg"].encode("latin-1", "backslashreplace").decode("unicode_escape")
|
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-06-22 12:34:31 +00:00
|
|
|
tg_bot.sendMessage(
|
|
|
|
data["telegram"],
|
2021-08-13 12:58:40 +00:00
|
|
|
msg,
|
2021-06-22 12:34:31 +00:00
|
|
|
parse_mode="MARKDOWN",
|
|
|
|
)
|
2020-12-19 14:15:46 +00:00
|
|
|
except KeyError:
|
2021-06-22 12:34:31 +00:00
|
|
|
tg_bot.sendMessage(
|
|
|
|
config.channel,
|
2021-08-13 12:58:40 +00:00
|
|
|
msg,
|
2021-06-22 12:34:31 +00:00
|
|
|
parse_mode="MARKDOWN",
|
|
|
|
)
|
2021-03-27 07:20:00 +00:00
|
|
|
except Exception as e:
|
2021-06-22 12:34:31 +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:
|
2021-06-22 12:34:31 +00:00
|
|
|
webhook = DiscordWebhook(
|
|
|
|
url="https://discord.com/api/webhooks/" + data["discord"]
|
|
|
|
)
|
2021-08-13 12:58:40 +00:00
|
|
|
embed = DiscordEmbed(title=msg)
|
2020-12-19 14:15:46 +00:00
|
|
|
webhook.add_embed(embed)
|
2021-06-22 12:34:31 +00:00
|
|
|
webhook.execute()
|
2020-12-19 14:15:46 +00:00
|
|
|
except KeyError:
|
2021-06-22 12:34:31 +00:00
|
|
|
webhook = DiscordWebhook(
|
|
|
|
url="https://discord.com/api/webhooks/" + config.discord_webhook
|
|
|
|
)
|
2021-08-13 12:58:40 +00:00
|
|
|
embed = DiscordEmbed(title=msg)
|
2020-12-19 14:15:46 +00:00
|
|
|
webhook.add_embed(embed)
|
2021-06-22 12:34:31 +00:00
|
|
|
webhook.execute()
|
2021-03-27 07:20:00 +00:00
|
|
|
except Exception as e:
|
2021-06-22 12:34:31 +00:00
|
|
|
print("[X] Discord Error:\n>", e)
|
2021-02-05 17:27:05 +00:00
|
|
|
|
|
|
|
if config.send_slack_alerts:
|
|
|
|
try:
|
2021-06-22 12:34:31 +00:00
|
|
|
slack = Slack(url="https://hooks.slack.com/services/" + data["slack"])
|
2021-08-13 12:58:40 +00:00
|
|
|
slack.post(text=msg)
|
2021-02-05 17:27:05 +00:00
|
|
|
except KeyError:
|
2021-06-22 12:34:31 +00:00
|
|
|
slack = Slack(
|
|
|
|
url="https://hooks.slack.com/services/" + config.slack_webhook
|
|
|
|
)
|
2021-08-13 12:58:40 +00:00
|
|
|
slack.post(text=msg)
|
2021-03-27 07:20:00 +00:00
|
|
|
except Exception as e:
|
2021-06-22 12:34:31 +00:00
|
|
|
print("[X] Slack Error:\n>", e)
|
2021-02-05 17:27:05 +00:00
|
|
|
|
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-06-22 12:34:31 +00:00
|
|
|
tw_api.update_status(
|
2021-08-13 12:58:40 +00:00
|
|
|
status=msg.replace("*", "").replace("_", "").replace("`", "")
|
2021-06-22 12:34:31 +00:00
|
|
|
)
|
2020-07-11 15:52:48 +00:00
|
|
|
except Exception as e:
|
2021-06-22 12:34:31 +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-06-22 12:34:31 +00:00
|
|
|
email_msg = MIMEText(
|
2021-08-13 12:58:40 +00:00
|
|
|
msg.replace("*", "").replace("_", "").replace("`", "")
|
2021-06-22 12:34:31 +00:00
|
|
|
)
|
|
|
|
email_msg["Subject"] = config.email_subject
|
|
|
|
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()
|
2021-06-22 12:34:31 +00:00
|
|
|
with smtplib.SMTP_SSL(
|
|
|
|
config.email_host, config.email_port, context=context
|
|
|
|
) as server:
|
2020-07-11 15:52:48 +00:00
|
|
|
server.login(config.email_user, config.email_password)
|
2021-06-22 12:34:31 +00:00
|
|
|
server.sendmail(
|
|
|
|
config.email_sender, config.email_receivers, email_msg.as_string()
|
|
|
|
)
|
2020-07-11 15:52:48 +00:00
|
|
|
server.quit()
|
|
|
|
except Exception as e:
|
2021-06-22 12:34:31 +00:00
|
|
|
print("[X] Email Error:\n>", e)
|