From 0de77598d77996994dfcc46e5c18c01cb92be938 Mon Sep 17 00:00:00 2001 From: Magnus Berglund Date: Wed, 20 Mar 2019 18:23:36 +0100 Subject: [PATCH 1/3] Don't include non-existing image in MS Teams alert If an image section is included in the JSON payload for MS Teams alerts when no image URL exists, rendering of the alert in the client fails. This change makes sure that an image section is only included in the JSON payload if an image URL exists. Closes #16082 --- pkg/services/alerting/notifiers/teams.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/services/alerting/notifiers/teams.go b/pkg/services/alerting/notifiers/teams.go index 2dad11285b4..2660cacb7a4 100644 --- a/pkg/services/alerting/notifiers/teams.go +++ b/pkg/services/alerting/notifiers/teams.go @@ -78,6 +78,15 @@ func (this *TeamsNotifier) Notify(evalContext *alerting.EvalContext) error { message = evalContext.Rule.Message } + images := "" + if evalContext.ImagePublicUrl != "" { + images = []map[string]interface{}{ + { + "image": evalContext.ImagePublicUrl + } + } + } + body := map[string]interface{}{ "@type": "MessageCard", "@context": "http://schema.org/extensions", @@ -90,11 +99,7 @@ func (this *TeamsNotifier) Notify(evalContext *alerting.EvalContext) error { { "title": "Details", "facts": fields, - "images": []map[string]interface{}{ - { - "image": evalContext.ImagePublicUrl, - }, - }, + "images": images, "text": message, }, }, From 0511095303838c7b7dbfb88f34ec3f2829a195bf Mon Sep 17 00:00:00 2001 From: Magnus Berglund Date: Wed, 20 Mar 2019 18:42:48 +0100 Subject: [PATCH 2/3] Added missing commas --- pkg/services/alerting/notifiers/teams.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/services/alerting/notifiers/teams.go b/pkg/services/alerting/notifiers/teams.go index 2660cacb7a4..6a60c4a619b 100644 --- a/pkg/services/alerting/notifiers/teams.go +++ b/pkg/services/alerting/notifiers/teams.go @@ -82,8 +82,8 @@ func (this *TeamsNotifier) Notify(evalContext *alerting.EvalContext) error { if evalContext.ImagePublicUrl != "" { images = []map[string]interface{}{ { - "image": evalContext.ImagePublicUrl - } + "image": evalContext.ImagePublicUrl, + }, } } @@ -97,10 +97,10 @@ func (this *TeamsNotifier) Notify(evalContext *alerting.EvalContext) error { "themeColor": evalContext.GetStateModel().Color, "sections": []map[string]interface{}{ { - "title": "Details", - "facts": fields, + "title": "Details", + "facts": fields, "images": images, - "text": message, + "text": message, }, }, "potentialAction": []map[string]interface{}{ From e6623de6b212aae7222460f8b20e03a3b49298cc Mon Sep 17 00:00:00 2001 From: Magnus Berglund Date: Wed, 20 Mar 2019 18:59:13 +0100 Subject: [PATCH 3/3] Rewrote creation of images tag --- pkg/services/alerting/notifiers/teams.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pkg/services/alerting/notifiers/teams.go b/pkg/services/alerting/notifiers/teams.go index 6a60c4a619b..e19357d7b7e 100644 --- a/pkg/services/alerting/notifiers/teams.go +++ b/pkg/services/alerting/notifiers/teams.go @@ -78,13 +78,11 @@ func (this *TeamsNotifier) Notify(evalContext *alerting.EvalContext) error { message = evalContext.Rule.Message } - images := "" + images := make([]map[string]interface{}, 0) if evalContext.ImagePublicUrl != "" { - images = []map[string]interface{}{ - { - "image": evalContext.ImagePublicUrl, - }, - } + images = append(images, map[string]interface{}{ + "image": evalContext.ImagePublicUrl, + }) } body := map[string]interface{}{