ashb commented on a change in pull request #2412: AIRFLOW-1216 introducing 
notifiers
URL: https://github.com/apache/incubator-airflow/pull/2412#discussion_r240145824
 
 

 ##########
 File path: airflow/notifiers/slack.py
 ##########
 @@ -0,0 +1,70 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import logging
+from airflow.notifiers import AirflowNotifier
+
+from slackclient import SlackClient
+from airflow.exceptions import AirflowException
+from airflow import configuration
+from airflow.utils.state import State
+
+
+SLACK_ICON_URL = ('https://raw.githubusercontent.com/airbnb/'
+                  'airflow/master/airflow/www/static/pin_100.png')
+
+
+class SlackAPI(object):
+
+    @staticmethod
+    def send_message(method, token, body):
+        sc = SlackClient(token)
+        rc = sc.api_call(method, **body)
+        if not rc['ok']:
+            logging.error("Slack API call failed ({})".format(rc['error']))
+            raise AirflowException("Slack API call failed: 
({})".format(rc['error']))
+
+
+class SlackNotifier(AirflowNotifier):
+
+    def __init__(self, channel, notify_on=None):
+        self.channel = channel
+
+        if notify_on is None:
+            self.notify_on = {State.UP_FOR_RETRY: 'text_body.tpl',
+                              State.FAILED: 'text_body.tpl'}
+        else:
+            self.notify_on = notify_on
+
+    def send_notification(self, task_instance, state, message, **kwargs):
+        if state in self.notify_on:
+            text_tpl = self.notify_on[state]
+            text = self.render_template(text_tpl,
+                                        task_instance,
+                                        message=message,
+                                        **kwargs)
+
+            token = configuration.get('slack', 'token')
+
+            if configuration.has_option('slack', 'username'):
+                username = configuration.get('slack', 'username')
+            else:
+                username = 'Airflow'
+
+            body = {'channel': self.channel,
+                    'username': username,
+                    'text': text,
+                    'icon_url': SLACK_ICON_URL}
+
+            SlackAPI.send_message('chat.postMessage', token, body)
 
 Review comment:
   We should use the SlackHook/SlackWebhookHook here as that already 
encapsulates the slack API. (They may not have existed when this PR was opened)

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to