Skip to content

Commit f370589

Browse files
committed
fix(slack): Send only custom template when custom_body is set
When a custom template is configured for an alert, the Slack destination was appending the custom_body to the default fields (Query/Alert links) instead of replacing them. This resulted in both default and custom templates being displayed together. This fix restructures the notification logic: - If custom_body is set: send only the custom template - If custom_body is not set: send default template (existing behavior)
1 parent 9743820 commit f370589

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
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)

0 commit comments

Comments
 (0)