finished slack integration

This commit is contained in:
Ricardo Tapia 2015-12-31 21:04:33 -06:00
parent 0b59f5e29c
commit 1d48688518
3 changed files with 61 additions and 44 deletions

View file

@ -132,6 +132,11 @@ from plexpy import helpers
$('#ajaxMsg').addClass('success').fadeIn().delay(3000).fadeOut(); $('#ajaxMsg').addClass('success').fadeIn().delay(3000).fadeOut();
}); });
$('#testSlack').click(function() {
$.get("/test_slack", function(data) { $('#ajaxMsg').html("<i class='fa fa-check'></i>" + data); });
$('#ajaxMsg').addClass('success').fadeIn().delay(3000).fadeOut();
});
$('#pushbullet_apikey').on('change', function () { $('#pushbullet_apikey').on('change', function () {
doAjaxCall('set_notification_config', $(this), 'tabs', true); doAjaxCall('set_notification_config', $(this), 'tabs', true);
reloadModal(); reloadModal();

View file

@ -19,6 +19,7 @@ from plexpy.helpers import checked, radio
from xml.dom import minidom from xml.dom import minidom
from httplib import HTTPSConnection from httplib import HTTPSConnection
from urlparse import parse_qsl from urlparse import parse_qsl
from urlparse import urlparse
from urllib import urlencode from urllib import urlencode
from pynma import pynma from pynma import pynma
@ -419,7 +420,7 @@ def send_notification(config_id, subject, body):
elif config_id == 13: elif config_id == 13:
telegramClient = TELEGRAM() telegramClient = TELEGRAM()
telegramClient.notify(message=body, event=subject) telegramClient.notify(message=body, event=subject)
elif config_id == 13: elif config_id == 14:
slackClient = SLACK() slackClient = SLACK()
slackClient.notify(message=body, event=subject) slackClient.notify(message=body, event=subject)
else: else:
@ -427,7 +428,6 @@ def send_notification(config_id, subject, body):
else: else:
logger.debug(u"PlexPy Notifier :: Notification requested but no agent id received.") logger.debug(u"PlexPy Notifier :: Notification requested but no agent id received.")
class GROWL(object): class GROWL(object):
""" """
Growl notifications, for OS X. Growl notifications, for OS X.
@ -1469,7 +1469,6 @@ class Email(object):
return config_option return config_option
class IFTTT(object): class IFTTT(object):
def __init__(self): def __init__(self):
@ -1556,7 +1555,7 @@ class TELEGRAM(object):
http_handler.request("POST", http_handler.request("POST",
"/bot%s/%s" % (self.bot_token, "sendMessage"), "/bot%s/%s" % (self.bot_token, "sendMessage"),
headers={'Content-type': "application/x-www-form-urlencoded"}, headers={'Content-type': "application/x-www-form-urlencoded"},
body=urlencode(data)) payload=urlencode(data))
response = http_handler.getresponse() response = http_handler.getresponse()
request_status = response.status request_status = response.status
@ -1611,7 +1610,7 @@ class SLACK(object):
self.icon_emoji = plexpy.CONFIG.SLACK_ICON_EMOJI self.icon_emoji = plexpy.CONFIG.SLACK_ICON_EMOJI
def conf(self, options): def conf(self, options):
return cherrypy.config['config'].get('Telegram', options) return cherrypy.config['config'].get('Slack', options)
def notify(self, message, event): def notify(self, message, event):
if not message or not event: if not message or not event:
@ -1619,16 +1618,17 @@ class SLACK(object):
http_handler = HTTPSConnection("hooks.slack.com") http_handler = HTTPSConnection("hooks.slack.com")
data = {'text': event.encode('utf-8') + ': ' + message.encode("utf-8")} data = {'text': event.encode('utf-8') + ': ' + message.encode("utf-8")}
data['channel'] = self.channel if self.channel != '' if self.channel != '': data['channel'] = self.channel
data['username'] = self.username if self.username != '' if self.username != '': data['username'] = self.username
data['icon_emoji'] = self.icon_emoji if self.icon_emoji != '' if self.icon_emoji != '':
data['icon_emoji'] = self.icon_emoji
url = urlparse(self.slack_hook).path url = urlparse(self.slack_hook).path
http_handler.request("POST", http_handler.request("POST",
url, url,
headers={'Content-type': "application/x-www-form-urlencoded"}, headers={'Content-type': "application/x-www-form-urlencoded"},
body=urlencode(data)) body=json.dumps(data))
response = http_handler.getresponse() response = http_handler.getresponse()
request_status = response.status request_status = response.status
@ -1647,14 +1647,9 @@ class SLACK(object):
#For uniformity reasons not removed #For uniformity reasons not removed
return return
def test(self, slack_hook, channel, username, icon_emoji): def test(self):
self.enabled = True self.enabled = True
self.slack_hook = slack_hook return self.notify('Main Screen Activate', 'Test Message')
self.channel = channel
self.username = username
self.icon_emoji = icon_emoji
self.notify('Main Screen Activate', 'Test Message')
def return_config_options(self): def return_config_options(self):
config_option = [{'label': 'Slack Hook', config_option = [{'label': 'Slack Hook',
@ -1666,7 +1661,7 @@ class SLACK(object):
{'label': 'Slack Channel', {'label': 'Slack Channel',
'value': self.channel, 'value': self.channel,
'name': 'slack_channel', 'name': 'slack_channel',
'description': 'Your slack channel name. The channel in which the messages will be sent.', 'description': 'Your slack channel name. (Begin with \'#\')',
'input_type': 'text' 'input_type': 'text'
}, },
{'label': 'Slack Username', {'label': 'Slack Username',
@ -1675,11 +1670,17 @@ class SLACK(object):
'description': 'Slack username which will be shown', 'description': 'Slack username which will be shown',
'input_type': 'text' 'input_type': 'text'
}, },
{'label': 'Slack Icon Emoji', {'label': 'Slack Icon',
'value': self.icon_emoji, 'value': self.icon_emoji,
'description': 'Slack Icon emoji', 'description': 'Your icon you wish to show, use Slack emoji or image url',
'name': 'Slack Icon', 'name': 'slack_icon_emoji',
'input_type': 'text' 'input_type': 'text'
},
{'label': 'Test Event',
'value': 'Test Event',
'name': 'testSlack',
'description': 'Test if Slack notifications are working. See logs for troubleshooting.',
'input_type': 'button'
} }
] ]

View file

@ -696,6 +696,17 @@ class WebInterface(object):
else: else:
return "Error sending event." return "Error sending event."
@cherrypy.expose
def test_slack(self):
cherrypy.response.headers['Cache-Control'] = "max-age=0,no-cache,no-store"
event = notifiers.SLACK()
result = event.test()
if result:
return "Notification successful."
else:
return "Error sending event."
@cherrypy.expose @cherrypy.expose
def osxnotifyregister(self, app): def osxnotifyregister(self, app):
cherrypy.response.headers['Cache-Control'] = "max-age=0,no-cache,no-store" cherrypy.response.headers['Cache-Control'] = "max-age=0,no-cache,no-store"