From 203f6afa096ff22d6632898522d87c3fe6193cb6 Mon Sep 17 00:00:00 2001 From: Antoine Augusti Date: Wed, 15 Jun 2016 15:56:04 +0200 Subject: [PATCH] Improve Slack notification style --- redash/destinations/slack.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/redash/destinations/slack.py b/redash/destinations/slack.py index 89b53c37b4..b0d3f1c6ab 100644 --- a/redash/destinations/slack.py +++ b/redash/destinations/slack.py @@ -23,9 +23,27 @@ def icon(cls): return 'fa-slack' def notify(self, alert, query, user, new_state, app, host, options): - msg = "Check <{host}/alerts/{alert_id}|alert> / check <{host}/queries/{query_id}|query>".format( - host=host, alert_id=alert.id, query_id=query.id) - payload = {'text': msg} + # Documentation: https://api.slack.com/docs/attachments + fields = [ + { + "title": "Query", + "value": "{host}/queries/{query_id}".format(host=host, query_id=query.id), + "short": True + }, + { + "title": "Alert", + "value": "{host}/alerts/{alert_id}".format(host=host, alert_id=alert.id), + "short": True + } + ] + if new_state == "triggered": + text = alert.name + " just triggered" + color = "#c0392b" + else: + text = alert.name + " went back to normal" + color = "#27ae60" + + payload = {'attachments': [{'text': text, 'color': color, 'fields': fields}]} try: resp = requests.post(options.get('url'), data=json.dumps(payload)) logging.warning(resp.text) @@ -34,5 +52,4 @@ def notify(self, alert, query, user, new_state, app, host, options): except Exception: logging.exception("Slack send ERROR.") - register(Slack)