Browse Source

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
Magnus Berglund 6 years ago
parent
commit
0de77598d7
1 changed files with 10 additions and 5 deletions
  1. 10 5
      pkg/services/alerting/notifiers/teams.go

+ 10 - 5
pkg/services/alerting/notifiers/teams.go

@@ -78,6 +78,15 @@ func (this *TeamsNotifier) Notify(evalContext *alerting.EvalContext) error {
 		message = evalContext.Rule.Message
 		message = evalContext.Rule.Message
 	}
 	}
 
 
+	images := ""
+	if evalContext.ImagePublicUrl != "" {
+		images = []map[string]interface{}{
+			{
+				"image": evalContext.ImagePublicUrl
+			}
+		}
+	}
+
 	body := map[string]interface{}{
 	body := map[string]interface{}{
 		"@type":    "MessageCard",
 		"@type":    "MessageCard",
 		"@context": "http://schema.org/extensions",
 		"@context": "http://schema.org/extensions",
@@ -90,11 +99,7 @@ func (this *TeamsNotifier) Notify(evalContext *alerting.EvalContext) error {
 			{
 			{
 				"title": "Details",
 				"title": "Details",
 				"facts": fields,
 				"facts": fields,
-				"images": []map[string]interface{}{
-					{
-						"image": evalContext.ImagePublicUrl,
-					},
-				},
+				"images": images,
 				"text": message,
 				"text": message,
 			},
 			},
 		},
 		},