Working configuration example for alerts templating - Telegram

Hi Grafana community!

I’ve tried several times to make the alert message on Telegram easy to read, informative, and remove unnecessary messages. After some trial and error, I found a good template here https://community.grafana.com/t/re-working-configuration-example-for-alerts-templating-telegram-and-slack/119950. However, there are a few things that I added (according to my needs). I really appreciate to @bLd759 for creating such a good and understandable alert example.

Important : This configuration is running on Grafana version 10.4.2, Prometheus 2.47.2

Alert rules : The following alert rules apply to a Prometheus datasource where I have added a custom label “node” to each target.
If you are using a different datasource, you have to adapt these.

Alerts are set :

  • A = query
  • B = Reduce last of A
  • C = Threshold of B - set as Alert condition

And here is my Telegram template message:

{{ define "telegram_message" }}
	  {{ if gt (len .Alerts.Firing) 0 }}
	  <b>🔥 {{ len .Alerts.Firing }} alert(s) firing:</b>
	    {{ range .Alerts.Firing }} {{ template "telegram_alert_firing" .}} {{ end }} {{ end }}
	  {{ if gt (len .Alerts.Resolved) 0 }}
	  <b>✅ {{ len .Alerts.Resolved }} alert(s) resolved:</b>
	    {{ range .Alerts.Resolved }} {{ template "telegram_alert_resolved" .}} {{ end }} {{ end }}
	{{ end }}
	{{ define "telegram_alert_firing" }}
	  <b>Value = </b>
	  {{- $value := .Values }}
	  {{- if ne (printf "%.1f" $value.A) "0.0" }}
	  {{- printf "%.1f" $value.A }}{{- end }}
	  {{- if ne (printf "%.1f" $value.B) "0.0" }} {{- printf "%.1f" $value.B }}{{- end }}
	  <b>Labels:</b>
	  {{- if index .Labels "name" }}
	  - Name = {{ index .Labels "name" }}
	  {{- end }}
	  {{- if index .Labels "instance" }}
	  - IP = {{ index .Labels "instance" }}
	  {{- end }}
	  {{- if index .Labels "Severity" }}
	  - Severity = {{ index .Labels "Severity" }}
	  {{- end }}
	  {{- if index .Labels "grafana_folder" }}
	  - Grafana_folder = {{ index .Labels "grafana_folder" }}
	  {{- end }}
	  {{- if index .Labels "volume" }}
	  - Volume = {{ index .Labels "volume" }}
	  {{- end }}
	  {{- if index .Labels "mountpoint" }}
	  - Mountpoint = {{ index .Labels "mountpoint" }}
	  {{- end }}
	  {{- if index .Labels "job" }}
	  - OS = {{ index .Labels "job" }}
	  {{- end }}
	  {{- if index .Labels "loc" }}
	  - Location = {{ index .Labels "loc" }}
	  {{- end }}
	{{ end }}
	{{ define "telegram_alert_resolved" }}
	  <b>{{ .Labels.alertname }}</b>
	  Node: <b>{{ .Annotations.Node }}</b>
	  {{ .Annotations.AlertValues }}
	  <b>Labels:</b>
	  {{- if index .Labels "name" }}
	  - Name = {{ index .Labels "name" }}
	  {{- end }}
	  {{- if index .Labels "instance" }}
	  - IP = {{ index .Labels "instance" }}
	  {{- end }}
	  {{- if index .Labels "Severity" }}
	  - Severity = {{ index .Labels "Severity" }}
	  {{- end }}
	  {{- if index .Labels "grafana_folder" }}
	  - Grafana_folder = {{ index .Labels "grafana_folder" }}
	  {{- end }}
	  {{- if index .Labels "volume" }}
	  - Volume = {{ index .Labels "volume" }}
	  {{- end }}
	  {{- if index .Labels "mountpoint" }}
	  - Mountpoint = {{ index .Labels "mountpoint" }}
	  {{- end }}
	  {{- if index .Labels "job" }}
	  - OS = {{ index .Labels "job" }}
	  {{- end }}
	  {{- if index .Labels "loc" }}
	  - Location = {{ index .Labels "loc" }}
	  {{- end }}
	{{ end }}

This is the result

Contact points

Telegram
Message = {{ template "telegram_message" . }}

hope this be useful.

2 Likes

Thanks for sharing, I’m sure it will help a lot of people!

1 Like

Thank you for this!

1 Like