From 07d6c0a099f06df9af74764ef931f451b1f93b89 Mon Sep 17 00:00:00 2001 From: Levent YALCIN Date: Sun, 1 Jun 2014 23:44:46 +0300 Subject: [PATCH 1/2] Slack integration added --- notification/models.py | 3 ++- notification/notifier/slack.py | 10 ++++++++++ notification/tasks.py | 2 ++ openduty/settings.py | 3 +++ requirements.txt | 3 ++- 5 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 notification/notifier/slack.py diff --git a/notification/models.py b/notification/models.py index 2d8bc7f..a26e482 100644 --- a/notification/models.py +++ b/notification/models.py @@ -18,8 +18,9 @@ class UserNotificationMethod(models.Model): METHOD_EMAIL = 'email' METHOD_PUSHOVER = 'pushover' METHOD_XMPP = 'xmpp' + METHOD_SLACK = 'slack' - methods = [METHOD_XMPP, METHOD_PUSHOVER, METHOD_EMAIL, METHOD_TWILIO_SMS, METHOD_TWILIO_CALL] + methods = [METHOD_XMPP, METHOD_PUSHOVER, METHOD_EMAIL, METHOD_TWILIO_SMS, METHOD_TWILIO_CALL, METHOD_SLACK] user = models.ForeignKey(User, related_name='notification_methods') position = models.IntegerField() diff --git a/notification/notifier/slack.py b/notification/notifier/slack.py new file mode 100644 index 0000000..b1e2a57 --- /dev/null +++ b/notification/notifier/slack.py @@ -0,0 +1,10 @@ +from slacker import Slacker + +class SlackNotifier: + def notify(self, notification): + slack = Slacker(notification.user_to_notify.profile.slack_apikey) + response = slack.chat.post_message(notification.user_to_notify.profile.slack_channel, notification.message) + if not response.error: + print "Slack message sent" + else: + print "Failed to send Slack message" diff --git a/notification/tasks.py b/notification/tasks.py index b50b42e..d3e539b 100644 --- a/notification/tasks.py +++ b/notification/tasks.py @@ -21,6 +21,8 @@ def send_notifications(notification_id): notifier = TwilioSmsNotifier(settings.TWILIO_SETTINGS) if notification.notifier == UserNotificationMethod.METHOD_TWILIO_CALL: notifier = TwilioCallNotifier(settings.TWILIO_SETTINGS) + if notification.notifier == UserNotificationMethod.METHOD_SLACK: + notifier = SlackNotifier(settings.SLACK_SETTINGS) elif notification.notifier == UserNotificationMethod.METHOD_PUSHOVER: notifier = PushoverNotifier() notifier.notify(notification) diff --git a/openduty/settings.py b/openduty/settings.py index da4f877..a80b738 100644 --- a/openduty/settings.py +++ b/openduty/settings.py @@ -108,6 +108,9 @@ TWILIO_SETTINGS = { } +SLACK_SETTINGS = { +} + # Database # https://docs.djangoproject.com/en/1.6/ref/settings/#databases diff --git a/requirements.txt b/requirements.txt index 8000e6b..944ffe1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,4 +13,5 @@ pyasn1_modules dnspython oauth2client sleekxmpp -dj-database-url \ No newline at end of file +dj-database-url +slacker From eebcbf80cf4482616c4eae91c5f22e5636a169b1 Mon Sep 17 00:00:00 2001 From: Levent YALCIN Date: Mon, 2 Jun 2014 00:45:57 +0300 Subject: [PATCH 2/2] identation fixed and slack settings matched with settings not user profile model --- notification/notifier/slack.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/notification/notifier/slack.py b/notification/notifier/slack.py index b1e2a57..584d868 100644 --- a/notification/notifier/slack.py +++ b/notification/notifier/slack.py @@ -1,10 +1,14 @@ from slacker import Slacker class SlackNotifier: - def notify(self, notification): - slack = Slacker(notification.user_to_notify.profile.slack_apikey) - response = slack.chat.post_message(notification.user_to_notify.profile.slack_channel, notification.message) - if not response.error: - print "Slack message sent" - else: - print "Failed to send Slack message" + + def __init__(self, config): + self.__config = config + + def notify(self, notification): + slack = Slacker(self.__config['apikey']) + response = slack.chat.post_message(self.__config['channel'], notification.message) + if not response.error: + print "Slack message sent" + else: + print "Failed to send Slack message"