From bf908424a71816bb27718ce70a505ff8d796030f Mon Sep 17 00:00:00 2001 From: Arve Knudsen Date: Fri, 21 Feb 2020 15:43:54 +0100 Subject: [PATCH] Alerting: Don't include image_url field with Slack message if empty (#22372) --- pkg/services/alerting/notifiers/slack.go | 28 ++++++++++++++---------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/pkg/services/alerting/notifiers/slack.go b/pkg/services/alerting/notifiers/slack.go index ebd621a981e..e54e33704df 100644 --- a/pkg/services/alerting/notifiers/slack.go +++ b/pkg/services/alerting/notifiers/slack.go @@ -276,22 +276,25 @@ func (sn *SlackNotifier) Notify(evalContext *alerting.EvalContext) error { }, } } + attachment := map[string]interface{}{ + "color": evalContext.GetStateModel().Color, + "title": evalContext.GetNotificationTitle(), + "title_link": ruleURL, + "text": msg, + "fallback": evalContext.GetNotificationTitle(), + "fields": fields, + "footer": "Grafana v" + setting.BuildVersion, + "footer_icon": "https://grafana.com/assets/img/fav32.png", + "ts": time.Now().Unix(), + } + if imageURL != "" { + attachment["image_url"] = imageURL + } body := map[string]interface{}{ "text": evalContext.GetNotificationTitle(), "blocks": blocks, "attachments": []map[string]interface{}{ - { - "color": evalContext.GetStateModel().Color, - "title": evalContext.GetNotificationTitle(), - "title_link": ruleURL, - "text": msg, - "fallback": evalContext.GetNotificationTitle(), - "fields": fields, - "image_url": imageURL, - "footer": "Grafana v" + setting.BuildVersion, - "footer_icon": "https://grafana.com/assets/img/fav32.png", - "ts": time.Now().Unix(), - }, + attachment, }, "parse": "full", // to linkify urls, users and channels in alert message. } @@ -313,6 +316,7 @@ func (sn *SlackNotifier) Notify(evalContext *alerting.EvalContext) error { if err != nil { return err } + cmd := &models.SendWebhookSync{Url: sn.URL, Body: string(data)} if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil { sn.log.Error("Failed to send slack notification", "error", err, "webhook", sn.Name)