Skip to content

Commit

Permalink
Add slack contrib package for chat notifications.
Browse files Browse the repository at this point in the history
  • Loading branch information
riga committed Jun 21, 2018
1 parent e0b3b87 commit e041b03
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/contrib/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ To use on of the following packages in your code, you must import them explicitl
mercurial
numpy
root
slack
tasks
tensorflow
wlcg
13 changes: 13 additions & 0 deletions docs/contrib/slack.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
slack
=====

.. automodule:: law.contrib.slack

.. contents::


Class ``NotifySlackParameter``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. autoclass:: NotifySlackParameter
:members:
3 changes: 3 additions & 0 deletions law/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class Config(ConfigParser):
"mail_sender": "",
"mail_smtp_host": "127.0.0.1",
"mail_smtp_port": 25,
# contrib
"slack_token": "",
"slack_channel": "",
},
}

Expand Down
13 changes: 13 additions & 0 deletions law/contrib/slack/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
# flake8: noqa

"""
Slack contrib functionality.
"""


__all__ = ["NotifySlackParameter"]


# provisioning imports
from law.contrib.slack.parameter import NotifySlackParameter
42 changes: 42 additions & 0 deletions law/contrib/slack/parameter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-

"""
Slack notification.
"""


from law.config import Config
from law.parameter import NotifyParameter


class NotifySlackParameter(NotifyParameter):

@staticmethod
def notify(title, parts, token=None, channel=None, **kwargs):
import slackclient

cfg = Config.instance()

if not token:
token = cfg.get("notifications", "slack_token")
if not channel:
channel = cfg.get("notifications", "slack_channel")

if token and channel:
# minimal markup
text = "*{}*\n\n".format(title)
for key, value in parts:
text += "_{}_: {}\n".format(key, value)

sc = slackclient.SlackClient(token)
sc.api_call(
"chat.postMessage",
channel=channel,
text=text,
)

def get_transport(self):
return {
"func": self.notify,
"raw": True,
}
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def run(self):
"law.contrib.mercurial",
"law.contrib.numpy",
"law.contrib.root",
"law.contrib.slack",
"law.contrib.tasks",
"law.contrib.tensorflow",
"law.contrib.wlcg",
Expand Down

0 comments on commit e041b03

Please sign in to comment.