Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# ----------------------------------------------- #
# Plugin Name : TradingView-Webhook-Bot #
# Author Name : fabston #
# File Name : config.py #
# ----------------------------------------------- #
# ----------------------------------------------- #
# Plugin Name : TradingView-Webhook-Bot #
# Author Name : fabston #
# File Name : config.py #
# ----------------------------------------------- #

import os

# TradingView Example Alert Message:
# {
Expand All @@ -26,12 +28,19 @@
send_slack_alerts = False
slack_webhook = "" # Slack Webhook URL (https://api.slack.com/messaging/webhooks)

# Twitter Settings
send_twitter_alerts = False
tw_ckey = ""
tw_csecret = ""
tw_atoken = ""
tw_asecret = ""
# Twitter Settings
send_twitter_alerts = False
twitter_backend = os.getenv("TWITTER_BACKEND", "twitter")
tw_ckey = ""
tw_csecret = ""
tw_atoken = ""
tw_asecret = ""

# Xquik Settings. Used when TWITTER_BACKEND is set to "xquik".
xquik_api_url = os.getenv("XQUIK_API_URL", "https://xquik.com/api/v1/x/tweets")
xquik_api_key = os.getenv("XQUIK_API_KEY", "")
xquik_account = os.getenv("XQUIK_ACCOUNT", "")
xquik_timeout = 30

# Email Settings
send_email_alerts = False
Expand Down
79 changes: 54 additions & 25 deletions handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,52 @@
import ssl
from email.mime.text import MIMEText

import tweepy
from discord_webhook import DiscordEmbed, DiscordWebhook
from slack_webhook import Slack
from telegram import Bot

import config


def send_alert(data):
msg = data["msg"].encode("latin-1", "backslashreplace").decode("unicode_escape")
import requests
import tweepy
from discord_webhook import DiscordEmbed, DiscordWebhook
from slack_webhook import Slack
from telegram import Bot

import config


def format_twitter_status(msg):
return msg.replace("*", "").replace("_", "").replace("`", "")


def send_tweepy_alert(msg):
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)
tw_api.update_status(status=format_twitter_status(msg))


def send_xquik_alert(msg):
if not config.xquik_api_key or not config.xquik_account:
raise ValueError("Xquik API key and account are required")

response = requests.post(
config.xquik_api_url,
headers={"x-api-key": config.xquik_api_key},
json={"account": config.xquik_account, "text": format_twitter_status(msg)},
timeout=config.xquik_timeout,
)
if response.status_code not in (200, 202):
response.raise_for_status()
return response.json()


def send_twitter_alert(msg):
backend = config.twitter_backend.strip().lower()
if backend == "xquik":
return send_xquik_alert(msg)
if backend == "twitter":
return send_tweepy_alert(msg)
raise ValueError("twitter_backend must be 'twitter' or 'xquik'")


def send_alert(data):
msg = data["msg"].encode("latin-1", "backslashreplace").decode("unicode_escape")
if config.send_telegram_alerts:
tg_bot = Bot(token=config.tg_token)
try:
Expand Down Expand Up @@ -65,22 +101,15 @@ def send_alert(data):
except Exception as e:
print("[X] Slack Error:\n>", e)

if config.send_twitter_alerts:
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)
try:
tw_api.update_status(
status=msg.replace("*", "").replace("_", "").replace("`", "")
)
except Exception as e:
print("[X] Twitter Error:\n>", e)
if config.send_twitter_alerts:
try:
send_twitter_alert(msg)
except Exception as e:
print("[X] Twitter Error:\n>", e)

if config.send_email_alerts:
try:
email_msg = MIMEText(
msg.replace("*", "").replace("_", "").replace("`", "")
)
if config.send_email_alerts:
try:
email_msg = MIMEText(format_twitter_status(msg))
email_msg["Subject"] = config.email_subject
email_msg["From"] = config.email_sender
email_msg["To"] = config.email_sender
Expand Down