SickGear/sickbeard/notifiers/slack.py

52 lines
2.1 KiB
Python
Raw Normal View History

2017-09-14 03:39:54 +00:00
# coding=utf-8
#
# This file is part of SickGear.
#
2017-09-15 23:27:23 +00:00
# Thanks to: mallen86, generica
2017-09-14 03:39:54 +00:00
#
# SickGear is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# SickGear is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SickGear. If not, see <http://www.gnu.org/licenses/>.
import sickbeard
from sickbeard.notifiers.generic import Notifier
2017-09-14 03:39:54 +00:00
class SlackNotifier(Notifier):
2017-09-15 23:27:23 +00:00
def __init__(self):
super(SlackNotifier, self).__init__()
def _notify(self, title, body, channel='', as_authed=None, bot_name='', icon_url='', access_token='', **kwargs):
2017-09-15 23:27:23 +00:00
custom = not self._choose(as_authed, sickbeard.SLACK_AS_AUTHED)
resp = sickbeard.helpers.getURL(
2017-09-15 23:27:23 +00:00
url='https://slack.com/api/chat.postMessage',
post_data=dict(
[('text', self._body_only(title, body)),
('channel', self._choose(channel, sickbeard.SLACK_CHANNEL)), ('as_authed', not custom),
('token', self._choose(access_token, sickbeard.SLACK_ACCESS_TOKEN))]
+ ([], [('username', self._choose(bot_name, sickbeard.SLACK_BOT_NAME) or 'SickGear'),
('icon_url', self._choose(icon_url, sickbeard.SLACK_ICON_URL) or self._sg_logo_url)])[custom]),
2017-09-15 23:27:23 +00:00
json=True)
result = resp and resp.get('ok') or 'response: "%s"' % (resp.get('error') or self._choose(
'bad oath access token?', None))
2017-09-15 23:27:23 +00:00
if True is not result:
self._log_error('Failed to send message, %s' % result)
2017-09-14 03:39:54 +00:00
return self._choose(('Successful test notice sent. (Note: %s clients display icon once in a sequence)'
% self.name, 'Error sending notification, %s' % result)[True is not result], result)
2017-09-14 03:39:54 +00:00
notifier = SlackNotifier