Skip to content

Instantly share code, notes, and snippets.

@Himura2la
Last active June 28, 2026 11:25
Show Gist options
  • Select an option

  • Save Himura2la/342603218ba2ee871f7b0e35eff9eaae to your computer and use it in GitHub Desktop.

Select an option

Save Himura2la/342603218ba2ee871f7b0e35eff9eaae to your computer and use it in GitHub Desktop.
How to use Grafana Alerts with the Telegram
  • Template name: telegram.message

  • Content:

    {{ define "alert_list" }}{{ range . }}{{ .Labels.alertname }}
    {{ range .Annotations.SortedPairs }}<strong>{{ .Name }}</strong>: {{ .Value }}
    {{ end }}| {{ if gt (len .GeneratorURL) 0 }}<a href="{{ .GeneratorURL }}">source</a> | {{ end }}{{ if gt (len .SilenceURL) 0 }}<a href="{{ .SilenceURL }}">silence</a> | {{ end }}{{ if gt (len .DashboardURL) 0 }}<a href="{{ .DashboardURL }}">dashboard</a> | {{ end }}{{ if gt (len .PanelURL) 0 }}<a href="{{ .PanelURL }}">panel</a> |{{ end }}
    β€”
    {{ end }}{{ end }}
    {{ define "telegram.message" }}
    {{ if gt (len .Alerts.Firing) 0 }}<strong>FIRING!!!</strong>
    β€”
    {{ template "alert_list" .Alerts.Firing }}
    {{ if gt (len .Alerts.Resolved) 0 }}
    {{ end }}{{ end }}{{ if gt (len .Alerts.Resolved) 0 }}<strong>Resolved</strong>
    β€”
    {{ template "alert_list" .Alerts.Resolved }}
    {{ end }}{{ end }}
    
  • Contact point | Optional Telegram settings | Message: {{ template "telegram.message" . }}

  • Add verbose annotations to your alerts. Example: The <code>{{ $labels.host }}</code> host is low on free space! Used: {{ $values.display.Value }}% (where display is one of the queries).

  • I usually add the following queries: data (the datasource query), metric (the reduce expression), condition, and display, which is round($metric). Multi-dimensional rules documentation.

Also, check out this fork by @gelldur, this templeate is much more cmprehensive.

@Konano

Konano commented Mar 15, 2023

Copy link
Copy Markdown

Helps a lot!
When I was learning how to write message templates, it was much better to read this template than to read the documentation directly.

@faustinoaq

Copy link
Copy Markdown

Thanks you I managed to find the soruce code and create my own custom alerts ✨

{{/* Plantilla Telegram */}}
{{ define "alertas" -}}
{{ range . }}
{{ if len .Values -}}
Values:
{{- range $refID, $value := .Values -}}
    - {{ $refID }} = {{ $value }}
{{ end -}}
{{ end -}}
{{ if len .Labels.SortedPairs }}
Labels:
{{ range .Labels.SortedPairs -}}
    - {{ .Name }} = {{ .Value }}
{{ end -}}
{{ end -}}
{{ if len .Annotations.SortedPairs }}
Annotations:
{{ range .Annotations.SortedPairs -}}
    - {{ .Name }} = {{ .Value }}
{{ end -}}
{{ end -}}
{{ end -}} {{/* range */}}
{{ end -}} {{/* define alertas */}}

{{ define "plantilla.message" }}
{{ if gt (len .Alerts.Firing) 0 }}
🚨 FIRING!!!
{{- template "alertas" .Alerts.Firing -}}
{{ end -}}
{{ if gt (len .Alerts.Resolved) 0 }}
βœ… RESOLVED!
{{- template "alertas" .Alerts.Resolved -}}
{{ end -}}
<a href="192.168.0.11:3000">Dashboard</a>
{{ end }}

image

@gelldur

gelldur commented Mar 30, 2023

Copy link
Copy Markdown

Thanks for sharing!

Checkout my fork for Grafana9 update ;)
https://gist.github.com/gelldur/94b57b2fa276fe9de180378bf6855877

image

@jdnielss

jdnielss commented Jul 4, 2023

Copy link
Copy Markdown

@gelldur can you make a tutorial to how to use telegram notification template?

@elhananjair

Copy link
Copy Markdown

Hello @Himura2la
Is it possible to include the server in value parameter of solved? The empty value will make ambuigty to identify which system have been solved.
Another question is, is it possible to change summary of solved? For now in both cases (firing and solved) it uses the same summary.

@mathematicw

Copy link
Copy Markdown

Hey guys, hello @Himura2la, thank you so much. Please help a little bit. How to really use this template? How to add here the value i want to monitor? from my expression

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment