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