# MS Teams alerting

**URL:** <https://community.grafana.com/t/ms-teams-alerting/82318>\
**Category:** Alerting\
**Tags:** alerting\
**Created:** [February 22, 2023, 12:24pm UTC](https://community.grafana.com/t/ms-teams-alerting/82318 "2023-02-22T12:24:21Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![twocats](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@twocats](https://community.grafana.com/u/twocats)\
**Post date:** [February 22, 2023, 12:24pm UTC](https://community.grafana.com/t/ms-teams-alerting/82318/1 "2023-02-22T12:24:21Z")

</div>

Hi! Will try one more time describe my issue.  
I have Grafana 9.3.6 via kube-prometheus-stack and bunch of alerts (almost 500) configured. Alerts are sending to teams private channel via webhook, and there is one thing I can not make work normally. Have tried everything from different posts from community, nothing really works.  
My issue is following, alert dashboards are configured to check servers, for example call processing queue, there are 10 servers. When queue is over 100, there is alert sent to teams, but in teams alert is so ugly You can break eyes to see which servers are firing alert.

 ![Screenshot 2023-02-22 at 14.13.06](https://us1.discourse-cdn.com/grafana/original/3X/9/d/9d4004cb266256dab347d7a7015242b649c1c9ad.png)  
My goal is to sort them like:  
SERVER1 = VALUE  
SERVER2 = VALUE  
SERVER3= VALUE

At the moment, all my tries are without success. Have tried new template, have tried to add to alert code from [this post](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/3) and also tried what @georgerobinson suggested to add to message this :

```auto
{{ range .Alerts }}
{{ range $k, $v := .Values }}
{{ $k }} = {{ $v.Labels }} {{ $v.Value }}
{{ end }}
{{ end }}

```

I am not going to paste everything I have tried, but maybe have some idea, where to find solution?

---

<div class="post-metadata">

**Author:** ![georgerobinson](https://avatars.discourse-cdn.com/v4/letter/g/ad7895/32.png) [@georgerobinson](https://community.grafana.com/u/georgerobinson)\
**Post date:** [February 22, 2023, 1:07pm UTC](https://community.grafana.com/t/ms-teams-alerting/82318/2 "2023-02-22T13:07:14Z")

</div>

Hi! I understand you created a template called `myalert`. Can you show me where you use this template from your contact point? You can find more information about this here [Use notification templates | Grafana documentation](https://grafana.com/docs/grafana/next/alerting/manage-notifications/template-notifications/use-notification-templates/)

---

<div class="post-metadata">

**Author:** ![twocats](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@twocats](https://community.grafana.com/u/twocats)\
**Post date:** [February 22, 2023, 1:26pm UTC](https://community.grafana.com/t/ms-teams-alerting/82318/3 "2023-02-22T13:26:07Z")

</div>

Hi!  
So, my message for now looks like this (reverted back and have not saved otrher options, because none of them were working):

```auto
{{ define "custom.alert_message" }}{{ range . }}
Value: {{ or .ValueString "[no value]" }}
Labels:
{{ range .Labels.SortedPairs }} - {{ .Name }} = {{ .Value }}
{{ end }}
Annotations:
{{ range .Annotations.SortedPairs }} - {{ .Name }} = {{ .Value }}
{{ end }}
{{ if gt (len .DashboardURL) 0 }}Dashboard: {{ .DashboardURL }}\n{{ end }}
{{ if gt (len .PanelURL) 0 }}Panel: {{ .PanelURL }}\n{{ end }}
{{ end }}{{ end }}
{{ define "custom.subject" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ if gt (.Alerts.Resolved | len) 0 }}, RESOLVED:{{ .Alerts.Resolved | len }}{{ end }}{{ end }}] {{ .GroupLabels.SortedPairs.Values | join " " }} {{ if gt (len .CommonLabels) (len .GroupLabels) }}({{ with .CommonLabels.Remove .GroupLabels.Names }}{{ .Values | join " " }}{{ end }}){{ end }}{{ end }}

{{ define "custom.title" }}{{ template "custom.subject" . }}{{ end }}

{{ define "custom.message" }}{{ if gt (len .Alerts.Firing) 0 }} **Firing**
{{ template "custom.alert_message" .Alerts.Firing }}{{ if gt (len .Alerts.Resolved) 0 }}
{{ end }}{{ end }}{{ if gt (len .Alerts.Resolved) 0 }} **Resolved**
{{ template "custom.alert_message" .Alerts.Resolved }}{{ end }}{{ end }}

```

And contact point looks like this:  
 ![Screenshot 2023-02-22 at 15.22.57](https://us1.discourse-cdn.com/grafana/original/3X/b/a/ba78f486b7d74e8bfa84a19ee9b5393d607201a3.png)

@georgerobinson Do You need with code You provided?

---

<div class="post-metadata">

**Author:** ![georgerobinson](https://avatars.discourse-cdn.com/v4/letter/g/ad7895/32.png) [@georgerobinson](https://community.grafana.com/u/georgerobinson)\
**Post date:** [February 22, 2023, 1:38pm UTC](https://community.grafana.com/t/ms-teams-alerting/82318/4 "2023-02-22T13:38:19Z")

</div>

Thanks!

I see that you are using `Value: {{ or .ValueString "[no value]" }}`. Did you mean to change this to range over the values in `$values`? I don’t see anywhere else where `$values` is being used in your template.

---

<div class="post-metadata">

**Author:** ![twocats](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@twocats](https://community.grafana.com/u/twocats)\
**Post date:** [February 22, 2023, 1:57pm UTC](https://community.grafana.com/t/ms-teams-alerting/82318/5 "2023-02-22T13:57:43Z")

</div>

Sorry, as I said, I deleted previous tries and left only working template.  
I tried something more or less like this as You advised in other post:

```auto
{{ define "custom.alert_message" }}{{ range . }}
{{ range .Alerts }}
{{ range $k, $v := .Values }}
{{ $k }} = {{ $v.Labels }} {{ $v.Value }}
{{ end }}
{{ end }}
Labels:
{{ range .Labels.SortedPairs }} - {{ .Name }} = {{ .Value }}
{{ end }}
Annotations:
{{ range .Annotations.SortedPairs }} - {{ .Name }} = {{ .Value }}
{{ end }}
{{ if gt (len .DashboardURL) 0 }}Dashboard: {{ .DashboardURL }}\n{{ end }}
{{ if gt (len .PanelURL) 0 }}Panel: {{ .PanelURL }}\n{{ end }}
{{ end }}{{ end }}
{{ define "custom.subject" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ if gt (.Alerts.Resolved | len) 0 }}, RESOLVED:{{ .Alerts.Resolved | len }}{{ end }}{{ end }}] {{ .GroupLabels.SortedPairs.Values | join " " }} {{ if gt (len .CommonLabels) (len .GroupLabels) }}({{ with .CommonLabels.Remove .GroupLabels.Names }}{{ .Values | join " " }}{{ end }}){{ end }}{{ end }}

{{ define "custom.title" }}{{ template "custom.subject" . }}{{ end }}

{{ define "custom.message" }}{{ if gt (len .Alerts.Firing) 0 }} **Firing**
{{ template "custom.alert_message" .Alerts.Firing }}{{ if gt (len .Alerts.Resolved) 0 }}
{{ end }}{{ end }}{{ if gt (len .Alerts.Resolved) 0 }} **Resolved**
{{ template "custom.alert_message" .Alerts.Resolved }}{{ end }}{{ end }}

```

Also, besides to this, I tried to add Alertvalues to Annotation and Summary field with such code:

```auto
{{ with $values }}
{{ range $k, $v := . }}
   Location: {{$v.Labels.server}}
   Alerting value: {{ $v }}
{{ end }}
{{ end }}

```

This kind of gave me what I needed

 ![Screenshot 2023-02-22 at 15.56.34](https://us1.discourse-cdn.com/grafana/original/3X/8/3/83035f4f4a08847165ff0587b4b645bb2723091a.png)  
but I cant get these values to send over teams in message. This screenshot is from alerting/list  
Looks like with this I am pretty close, but my tries to write code for message did not work out

---

<div class="post-metadata">

**Author:** ![georgerobinson](https://avatars.discourse-cdn.com/v4/letter/g/ad7895/32.png) [@georgerobinson](https://community.grafana.com/u/georgerobinson)\
**Post date:** [February 22, 2023, 2:43pm UTC](https://community.grafana.com/t/ms-teams-alerting/82318/6 "2023-02-22T14:43:32Z")

</div>

The second screenshot, that looks like an Annotation called `AlertValues`, is that correct? If so, I think you might be confusing two separate templating systems in Grafana: templating labels and annotations, and templating notifications.

---

<div class="post-metadata">

**Author:** ![twocats](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@twocats](https://community.grafana.com/u/twocats)\
**Post date:** [February 22, 2023, 7:51pm UTC](https://community.grafana.com/t/ms-teams-alerting/82318/7 "2023-02-22T19:51:06Z")

</div>

I am sorry, if I am not describing properly what I need, as I am like beginner with whole Grafana, alerting etc. But, You want to say that is not the same? As I understand from alerts in teams and dashboard, they are same - value. If You see my first screenshot in this post, there is firing alert and Values are printed out in ugly way (ok, there ir var, metric and labels, but still, values (no?), but they are same as screen from Annotation called AlertValues. E1, E2, E3 etc. I want to send these values via teams in ordered way. If I am not correct, please explain, as I have not understood whole thing

---

<div class="post-metadata">

**Author:** ![georgerobinson](https://avatars.discourse-cdn.com/v4/letter/g/ad7895/32.png) [@georgerobinson](https://community.grafana.com/u/georgerobinson)\
**Post date:** [February 23, 2023, 12:48pm UTC](https://community.grafana.com/t/ms-teams-alerting/82318/8 "2023-02-23T12:48:25Z")

</div>

Hi! Let’s start from the beginning. It looks like you are using Classic Conditions, but your alert is working on multi-dimensional data because I can see multiple servers in the Teams message.

1. I would suggest is that you change your alert from Classic Conditions to Reduce expression with a RefID `B` and a Threshold expression with a RefID `C`. This will create one alert per server, rather than a single alert for all servers.

2. I would recommend creating a Summary annotation for this alert rule that contains the following:

```auto
{{ $labels.server }} = {{ $values.B }}

```

1. I would recommend creating the following notification template:

```auto
{{ define "simple.message" -}}
{{ if .Alerts.Firing -}}
{{ len .Alerts.Firing }} firing alert(s)
{{ template "alerts.summarize" .Alerts.Firing }}
{{- end }}
{{- if .Alerts.Resolved -}}
{{ len .Alerts.Resolved }} resolved alert(s)
{{ template "alerts.summarize" .Alerts.Resolved }}
{{- end }}
{{- end }}

{{ define "alerts.summarize" }}
{{ range . -}}
- {{ index .Annotations "summary" }}
{{ end }}
{{ end }}

```

This should give you the following message:

```auto
1 firing alert(s)

- server1 = value1

1 resolved alert(s)

- server2 = value2

```

---

<div class="post-metadata">

**Author:** ![twocats](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@twocats](https://community.grafana.com/u/twocats)\
**Post date:** [February 24, 2023, 1:16pm UTC](https://community.grafana.com/t/ms-teams-alerting/82318/9 "2023-02-24T13:16:57Z")

</div>

Thanks! I will try and let You know, if this was the solution. One thing about classic conditions, this alert indeed is using classic condition, but we have most of alerts with expressions

---

<div class="post-metadata">

**Author:** ![twocats](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@twocats](https://community.grafana.com/u/twocats)\
**Post date:** [April 20, 2023, 10:22am UTC](https://community.grafana.com/t/ms-teams-alerting/82318/10 "2023-04-20T10:22:43Z")

</div>

Hi @georgerobinson , sorry for not providing feedback, was busy with other tasks.  
I want to say huge thanks for this, Your solution fixed my issue

---

<div class="post-metadata">

**Author:** ![twocats](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@twocats](https://community.grafana.com/u/twocats)\
**Post date:** [April 24, 2023, 10:03am UTC](https://community.grafana.com/t/ms-teams-alerting/82318/11 "2023-04-24T10:03:21Z")

</div>

Hi @georgerobinson , ran into one small issue, which again could not fix:  
If I use Your provided simple message, I do not have any links added to message:

 ![Screenshot 2023-04-24 at 12.31.27](https://us1.discourse-cdn.com/grafana/original/3X/d/2/d2c0b44f1238ce1b123a08b68900eb817fd05948.png)

Tried to modify message template, but I got only like this:

 ![Screenshot 2023-04-24 at 12.37.00](https://us1.discourse-cdn.com/grafana/original/3X/5/e/5e2fbe65c3a1267b931a9bc27df36d73874b3ccf.png)

My main question, is there any way to add one link per message, not per alert? I am looking forward to keep only darshboard link.  
Also one more, there is button View URL in the end of message, which forwards to the alert configuration itself, I am wondering, is there any use case and even is it possible to put Dashboard URL under that button?

---

<div class="post-metadata">

**Author:** ![georgerobinson](https://avatars.discourse-cdn.com/v4/letter/g/ad7895/32.png) [@georgerobinson](https://community.grafana.com/u/georgerobinson)\
**Post date:** [April 24, 2023, 10:40am UTC](https://community.grafana.com/t/ms-teams-alerting/82318/12 "2023-04-24T10:40:28Z")

</div>

> [@twocats](#):
>
> My main question, is there any way to add one link per message, not per alert?

Yes it’s possible for Dashboard URL, but whether the link goes to the right place depends on how you have configured grouping in your notification policies.

For example, if you have a notification that contains an alert from Dashboard A and another alert from Dashboard B, but you only want one link, which one do you want? The link to Dashboard A or the link to Dashboard B?

The SilenceURL is different for each alert so I would recommend either including it for all alerts or for none of them.

---

<div class="post-metadata">

**Author:** ![twocats](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@twocats](https://community.grafana.com/u/twocats)\
**Post date:** [April 24, 2023, 10:53am UTC](https://community.grafana.com/t/ms-teams-alerting/82318/13 "2023-04-24T10:53:34Z")

</div>

@georgerobinson As far as I know, we use notifications from one Dashboard, in 90%, there are not combined notifications, so answer is the link form Dashboard A. To be more precise, we have one notification per panel in most cases. So for this case I would be very happy if I could change button to Dashboard URL. Is there any documentation? Could not find any info about this

And in this company alerts goes to Incident Managers and they do not use silence, they pass information further and we can silence alert if needed through Grafana GUI.

---

<div class="post-metadata">

**Author:** ![georgerobinson](https://avatars.discourse-cdn.com/v4/letter/g/ad7895/32.png) [@georgerobinson](https://community.grafana.com/u/georgerobinson)\
**Post date:** [April 24, 2023, 3:06pm UTC](https://community.grafana.com/t/ms-teams-alerting/82318/14 "2023-04-24T15:06:43Z")

</div>

You could write something like this (I’ve used `...` here to represent the rest of the template from the original example):

```auto
{{ define "simple.message" -}}
...
Dashboard URL: {{ (index .Alerts 0).DashboardURL }}
{{- end }}

```

This template however requires the following to work:

1. All alerts are linked to a dashboard
2. Each notification contains alerts from **the same** dashboard. If you have two alerts firing from different dashboards then these must be delivered as **separate notifications**

---

<div class="post-metadata">

**Author:** ![twocats](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@twocats](https://community.grafana.com/u/twocats)\
**Post date:** [April 27, 2023, 6:32am UTC](https://community.grafana.com/t/ms-teams-alerting/82318/15 "2023-04-27T06:32:56Z")

</div>

> [@georgerobinson](#):
>
> ```auto
> Dashboard URL: {{ (index .Alerts 0).DashboardURL }}
> {{- end }}
> 
> ```

Thanks! This worked again but link is not as link, it is as plain text.

 ![Screenshot 2023-04-27 at 09.27.29](https://us1.discourse-cdn.com/grafana/original/3X/7/e/7e079d68f1276daebb7f6f5d416ce8fac864d155.png)

Asked before, but is it possible to reconfigure “View URL” button to not open alert configuration, but for example this dashboard URL?

---

<div class="post-metadata">

**Author:** ![georgerobinson](https://avatars.discourse-cdn.com/v4/letter/g/ad7895/32.png) [@georgerobinson](https://community.grafana.com/u/georgerobinson)\
**Post date:** [April 27, 2023, 7:22am UTC](https://community.grafana.com/t/ms-teams-alerting/82318/16 "2023-04-27T07:22:56Z")

</div>

Ah, when using Microsoft Teams links need to be formatted as Markdown:

```auto
Dashboard URL: [{{ (index .Alerts 0).DashboardURL }}]({{ (index .Alerts 0).DashboardURL }})

```

---

<div class="post-metadata">

**Author:** ![twocats](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@twocats](https://community.grafana.com/u/twocats)\
**Post date:** [April 27, 2023, 1:53pm UTC](https://community.grafana.com/t/ms-teams-alerting/82318/17 "2023-04-27T13:53:40Z")

</div>

Worked, thank You very much!

---

<div class="post-metadata">

**Author:** ![faridtech2021](https://avatars.discourse-cdn.com/v4/letter/f/b5ac83/32.png) [@faridtech2021](https://community.grafana.com/u/faridtech2021)\
**Post date:** [November 9, 2025, 8:04pm UTC](https://community.grafana.com/t/ms-teams-alerting/82318/18 "2025-11-09T20:04:39Z")

</div>

Kindly could you explain from scratch how can I create alerts for Ms teams.

I tried to follow the whole conversation between you and George.

I wish 🙏 if you can assist me when you have few minutes to explain it step by step
