Skip to content

Commit bb6f00f

Browse files
committed
fix(alerts): Fix Slack custom template notification issues
- Fix KeyError when using custom template in alert notifications - Prevent default template fields from appearing alongside custom template Changes: - Use .get() with fallback for selector/op/value in render_template() - Restructure Slack destination to send only custom template when set
1 parent 9743820 commit bb6f00f

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

redash/destinations/slack.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,35 @@ def icon(cls):
2323

2424
def notify(self, alert, query, user, new_state, app, host, metadata, options):
2525
# Documentation: https://api.slack.com/docs/attachments
26-
fields = [
27-
{
28-
"title": "Query",
29-
"type": "mrkdwn",
30-
"value": "{host}/queries/{query_id}".format(host=host, query_id=query.id),
31-
},
32-
{
33-
"title": "Alert",
34-
"type": "mrkdwn",
35-
"value": "{host}/alerts/{alert_id}".format(host=host, alert_id=alert.id),
36-
},
37-
]
38-
if alert.custom_body:
39-
fields.append({"title": "Description", "value": alert.custom_body})
4026
if new_state == "triggered":
41-
if alert.custom_subject:
42-
text = alert.custom_subject
43-
else:
44-
text = alert.name + " just triggered"
4527
color = "#c0392b"
4628
else:
47-
text = alert.name + " went back to normal"
4829
color = "#27ae60"
4930

50-
payload = {"attachments": [{"text": text, "color": color, "fields": fields}]}
31+
if alert.custom_body:
32+
if alert.custom_subject:
33+
text = alert.custom_subject
34+
else:
35+
text = alert.name + (" just triggered" if new_state == "triggered" else " went back to normal")
36+
payload = {"attachments": [{"text": text, "color": color, "mrkdwn_in": ["text"], "fields": [{"value": alert.custom_body}]}]}
37+
else:
38+
fields = [
39+
{
40+
"title": "Query",
41+
"type": "mrkdwn",
42+
"value": "{host}/queries/{query_id}".format(host=host, query_id=query.id),
43+
},
44+
{
45+
"title": "Alert",
46+
"type": "mrkdwn",
47+
"value": "{host}/alerts/{alert_id}".format(host=host, alert_id=alert.id),
48+
},
49+
]
50+
if new_state == "triggered":
51+
text = alert.name + " just triggered"
52+
else:
53+
text = alert.name + " went back to normal"
54+
payload = {"attachments": [{"text": text, "color": color, "fields": fields}]}
5155

5256
try:
5357
resp = requests.post(options.get("url"), data=json_dumps(payload).encode("utf-8"), timeout=5.0)

redash/models/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,9 +1071,9 @@ def render_template(self, template):
10711071
"ALERT_NAME": self.name,
10721072
"ALERT_URL": "{host}/alerts/{alert_id}".format(host=host, alert_id=self.id),
10731073
"ALERT_STATUS": self.state.upper(),
1074-
"ALERT_SELECTOR": self.options["selector"],
1075-
"ALERT_CONDITION": self.options["op"],
1076-
"ALERT_THRESHOLD": self.options["value"],
1074+
"ALERT_SELECTOR": self.options.get("selector", self.options.get("column", "")),
1075+
"ALERT_CONDITION": self.options.get("op", ""),
1076+
"ALERT_THRESHOLD": self.options.get("value", ""),
10771077
"QUERY_NAME": self.query_rel.name,
10781078
"QUERY_URL": "{host}/queries/{query_id}".format(host=host, query_id=self.query_rel.id),
10791079
"QUERY_RESULT_VALUE": result_value,

0 commit comments

Comments
 (0)