From b538e5b321a7e6b60df3b57eaaddb8b54e66cb15 Mon Sep 17 00:00:00 2001 From: Nicolas Le Gall Date: Sun, 18 Dec 2016 10:32:39 +0100 Subject: [PATCH] Add Slack notification --- data/interfaces/default/config.html | 41 +++++++++++++++++++++++++++++ headphones/config.py | 5 ++++ headphones/notifiers.py | 27 +++++++++++++++++++ headphones/searcher.py | 4 +++ headphones/webserve.py | 7 ++++- 5 files changed, 83 insertions(+), 1 deletion(-) diff --git a/data/interfaces/default/config.html b/data/interfaces/default/config.html index 5dc6ab58..1388de17 100644 --- a/data/interfaces/default/config.html +++ b/data/interfaces/default/config.html @@ -1302,6 +1302,26 @@ +
+
+ +
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
@@ -2099,6 +2119,27 @@ } }); + if ($("#slack").is(":checked")) + { + $("#slackoptions").show(); + } + else + { + $("#slackoptions").hide(); + } + + $("#slack").click(function(){ + if ($("#slack").is(":checked")) + { + $("#slackoptions").slideDown(); + } + else + { + $("#slackoptions").slideUp(); + } + }); + + if ($("#telegram").is(":checked")) { $("#telegramoptions").show(); diff --git a/headphones/config.py b/headphones/config.py index 41f50ca9..3f75c30a 100644 --- a/headphones/config.py +++ b/headphones/config.py @@ -249,6 +249,11 @@ _CONFIG_DEFINITIONS = { 'SAB_USERNAME': (str, 'SABnzbd', ''), 'SAMPLINGFREQUENCY': (int, 'General', 44100), 'SEARCH_INTERVAL': (int, 'General', 1440), + 'SLACK_ENABLED': (int, 'Slack', 0), + 'SLACK_URL': (str, 'Slack', ''), + 'SLACK_CHANNEL': (str, 'Slack', ''), + 'SLACK_EMOJI': (str, 'Slack', ''), + 'SLACK_ONSNATCH': (int, 'Slack', 0), 'SOFT_CHROOT': (path, 'General', ''), 'SONGKICK_APIKEY': (str, 'Songkick', 'nd1We7dFW2RqxPw8'), 'SONGKICK_ENABLED': (int, 'Songkick', 1), diff --git a/headphones/notifiers.py b/headphones/notifiers.py index 1b885335..79923eb3 100644 --- a/headphones/notifiers.py +++ b/headphones/notifiers.py @@ -888,3 +888,30 @@ class TELEGRAM(object): logger.info(u"Telegram notifications sent.") return sent_successfuly + +class SLACK(object): + + def notify(self, message, status): + if not headphones.CONFIG.SLACK_ENABLED: + return + + import requests + + SLACK_URL = headphones.CONFIG.SLACK_URL + channel = headphones.CONFIG.SLACK_CHANNEL + emoji = headphones.CONFIG.SLACK_EMOJI + + payload = { 'channel': channel, 'text': status + ': ' + message, 'icon_emoji': emoji} + + try: + response = requests.post(SLACK_URL, json=payload) + except Exception, e: + logger.info(u'Slack notify failed: ' + str(e)) + + sent_successfuly = True + if not response.status_code == 200: + logger.info(u'Could not send notification to Slack. Response: [%s]', (response.text)) + sent_successfuly = False + + logger.info(u"Slack notifications sent.") + return sent_successfuly \ No newline at end of file diff --git a/headphones/searcher.py b/headphones/searcher.py index 4b52216b..4c155810 100644 --- a/headphones/searcher.py +++ b/headphones/searcher.py @@ -1052,6 +1052,10 @@ def send_to_downloader(data, bestqual, album): logger.info(u"Sending PushBullet notification") pushbullet = notifiers.PUSHBULLET() pushbullet.notify(name, "Download started") + if headphones.CONFIG.SLACK_ENABLED and headphones.CONFIG.SLACK_ONSNATCH: + logger.info(u"Sending Slack notification") + slack = notifiers.SLACK() + slack.notify(name, "Download started") if headphones.CONFIG.TELEGRAM_ENABLED and headphones.CONFIG.TELEGRAM_ONSNATCH: logger.info(u"Sending Telegram notification") telegram = notifiers.TELEGRAM() diff --git a/headphones/webserve.py b/headphones/webserve.py index 278e512b..b282642f 100644 --- a/headphones/webserve.py +++ b/headphones/webserve.py @@ -1388,7 +1388,12 @@ class WebInterface(object): "email_ssl": checked(headphones.CONFIG.EMAIL_SSL), "email_tls": checked(headphones.CONFIG.EMAIL_TLS), "email_onsnatch": checked(headphones.CONFIG.EMAIL_ONSNATCH), - "idtag": checked(headphones.CONFIG.IDTAG) + "idtag": checked(headphones.CONFIG.IDTAG), + "slack_enabled": checked(headphones.CONFIG.SLACK_ENABLED), + "slack_url": headphones.CONFIG.SLACK_URL, + "slack_channel": headphones.CONFIG.SLACK_CHANNEL, + "slack_emoji": headphones.CONFIG.SLACK_EMOJI, + "slack_onsnatch": checked(headphones.CONFIG.SLACK_ONSNATCH) } for k, v in config.iteritems():