Notification template customization

Hello Guys,

I have an working template where I get the alert description and summary alert to my teams channel.

I have a scenario where, I would like to silence alerts for particular pods using labels (key=value).
However, when I try to preview my template with payload, functions within the template are getting invalid.

Would like to check, if there is any alternative option to achieve this scenario using grafana template.

{{ define "teams.print_alert" -}}
{{ if .Annotations -}}
- *Summary*: {{ .Annotations.summary}}
- *Description*: {{ .Annotations.description }}
{{ end -}}
{{- end }}

{{ define "alert-description" -}}
{{ $firingAlerts := .Alerts.Firing }}
{{ $resolvedAlerts := .Alerts.Resolved }}

{{/* Define the label key and value to identify pods to be silenced */}}
{{ $labelKey := "app_kubernetes_io_name" }}
{{ $labelValue := "nginx-84f55d4f9d-vsjxm" }}

{{/* Filter out alerts associated with pods to be silenced */}}
{{ $filteredFiringAlerts := slice }}
{{ range $index, $alert := $firingAlerts }}
    {{ $podLabels := .Labels }}
    {{ $labelMatch := index $podLabels $labelKey }}
    {{ if and $labelMatch (eq $labelMatch $labelValue) }}
        {{/* Do not add the alert to filtered alerts list */}}
    {{ else }}
        {{/* Add the alert to filtered alerts list */}}
        {{ $filteredFiringAlerts = $filteredFiringAlerts | append $alert }}
    {{ end }}
{{ end }}

{{ if gt (len $filteredFiringAlerts) 0 }}
{{ len $filteredFiringAlerts }} firing alert(s):
{{ range $filteredFiringAlerts }}
{{ template "teams.print_alert" . }}
{{ end -}}
{{ end }}

{{ if gt (len $resolvedAlerts) 0 }}
{{ len $resolvedAlerts }} Resolved alert(s):
{{ range $resolvedAlerts }}
{{ template "teams.print_alert" . }}
{{ end -}}
{{ end }}
{{ end }}

NOTE:
I know we can achieve this within the promethues query, but I would like to do this via template.

I do not think this could work. Try to rewrite your templates a way that does not create an intermediate slice and populate it. You can just iterate .Alerts.Firing and skip ones that do not meet your criteria.

Also, you can use silences to mute alerts.