TradingView-Webhook-Bot/handler.py

67 wiersze
2.1 KiB
Python
Czysty Zwykły widok Historia

2020-07-04 07:38:38 +00:00
# ----------------------------------------------- #
# Plugin Name : TradingView-Webhook-Bot #
# Author Name : sixBit #
# 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
from discord_webhook import DiscordWebhook
import tweepy
2020-04-22 13:57:28 +00:00
import smtplib, ssl
from email.mime.text import MIMEText
2020-04-21 14:29:00 +00:00
telegram = config.send_telegram_alerts
discord = config.send_discord_alerts
twitter = config.send_twitter_alerts
2020-04-22 13:57:28 +00:00
email = config.send_email_alerts
2020-04-21 14:29:00 +00:00
2020-04-22 13:57:28 +00:00
if telegram:
tg_bot = Bot(token=config.tg_token)
if twitter:
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-04-22 13:57:28 +00:00
2020-07-11 15:52:48 +00:00
def send_alert(data):
2020-04-21 14:29:00 +00:00
if telegram:
2020-07-11 15:52:48 +00:00
try:
tg_bot.sendMessage(config.channel, data)
except Exception as e:
print(e)
2020-04-21 14:29:00 +00:00
else:
2020-07-04 07:38:38 +00:00
pass
2020-04-21 14:29:00 +00:00
if discord:
2020-07-11 15:52:48 +00:00
try:
discord_alert = DiscordWebhook(url=config.discord_webhook, content=data)
response = discord_alert.execute()
except Exception as e:
print(e)
2020-04-21 14:29:00 +00:00
else:
2020-07-04 07:38:38 +00:00
pass
if twitter:
2020-07-11 15:52:48 +00:00
try:
tw_api.update_status(status=data)
except Exception as e:
print(e)
else:
2020-07-04 07:38:38 +00:00
pass
2020-04-22 13:57:28 +00:00
if email:
2020-07-11 15:52:48 +00:00
try:
email_msg = MIMEText(data)
email_msg['Subject'] = config.email_subject
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(e)
2020-04-22 13:57:28 +00:00
else:
2020-07-04 07:38:38 +00:00
pass