Skip to content

Instantly share code, notes, and snippets.

@MarieKirya
Created January 19, 2026 07:16
Show Gist options
  • Select an option

  • Save MarieKirya/e0c77a180fe2dfbeffd4d0a0ffaddbd4 to your computer and use it in GitHub Desktop.

Select an option

Save MarieKirya/e0c77a180fe2dfbeffd4d0a0ffaddbd4 to your computer and use it in GitHub Desktop.
Grafana Discord Component Custom Webhook Implementation
{{- /* Create a custom webhook, and point it to your Discord webhook, make sure you append ?with_components=true to your webhook */ -}}
{{ define "__discord_values_list" }}{{ if len .Values }}{{ $first := true }}{{ range $refID, $value := .Values -}}
{{ if $first }}{{ $first = false }}{{ else }}, {{ end }}{{ $refID }}={{ $value }}{{ end -}}
{{ else }}[no value]{{ end }}{{ end }}
{{ define "__discord_labels_list" }}{{ if len .Values }}{{ $first := true }}{{ range $refID, $value := .Labels -}}
{{ if $first }}{{ $first = false }}{{ else }}, {{ end }}{{ $refID }}={{ $value }}{{ end -}}
{{ else }}[no value]{{ end }}{{ end }}
{{ define "discord.payload" -}}
{{ coll.Dict
"flags" 32768
"components" (tmpl.Exec "discord.payload.alerts" .Alerts | data.JSON)
| data.ToJSONPretty " "}}
{{- end }}
{{ define "discord.payload.alerts" -}}
{{- $alerts := coll.Slice -}}
{{- range . -}}
{{ if eq .Status "firing" }}
{{ $accent_color := 16711680 }}
{{ else }}
{{ $accent_color := 65280 }}
{{ end }}
{{ $alerts = coll.Append (coll.Dict
"type" 17
"accent_color" 65280
"components" (tmpl.Exec "discord.payload.alert" . | data.JSON)
) $alerts}}
{{- end -}}
{{- $alerts | data.ToJSON -}}
{{- end }}
{{ define "discord.payload.alert" -}}
{{- $alert := coll.Slice -}}
{{ if .Annotations.summary}}
{{ if eq .Status "firing" }}
{{ $alert = coll.Append (coll.Dict
"type" 10
"content" (tmpl.Inline "## Alert: {{.Annotations.summary}}" .)
) $alert}}
{{ else }}
{{ $alert = coll.Append (coll.Dict
"type" 10
"content" (tmpl.Inline "## Resolved: {{.Annotations.summary}}" .)
) $alert}}
{{ end }}
{{else}}
{{ if eq .Status "firing" }}
{{ $alert = coll.Append (coll.Dict
"type" 10
"content" "## Alert"
) $alert}}
{{ else }}
{{ $alert = coll.Append (coll.Dict
"type" 10
"content" "## Resolved"
) $alert}}
{{ end }}
{{ end }}
{{ $alert = coll.Append (coll.Dict
"type" 10
"content" (tmpl.Inline "-# Values: {{ template \"__discord_values_list\" . }}" .)
) $alert}}
{{ $alert = coll.Append (coll.Dict
"type" 10
"content" (tmpl.Inline "-# Labels: {{ template \"__discord_labels_list\" . }}" .)
) $alert}}
{{ $alert = coll.Append (coll.Dict
"type" 1
"components" (tmpl.Exec "discord.payload.alert.buttons" . | data.JSON)
) $alert}}
{{- $alert | data.ToJSON -}}
{{- end }}
{{ define "discord.payload.alert.buttons" }}
{{- $buttons := coll.Slice -}}
{{- /* GeneratorURL typically is only the query params and Discord will reject it. */ -}}
{{ if .SilenceURL }}
{{ $buttons = coll.Append (coll.Dict
"type" 2
"style" 5
"label" "Silence"
"url" .SilenceURL
) $buttons}}
{{ end -}}
{{ if .DashboardURL }}
{{ $buttons = coll.Append (coll.Dict
"type" 2
"style" 5
"label" "Dashboard"
"url" .DashboardURL
) $buttons}}
{{ end -}}
{{ if .PanelURL }}
{{ $buttons = coll.Append (coll.Dict
"type" 2
"style" 5
"label" "Panel"
"url" .PanelURL
) $buttons}}
{{ end -}}
{{- $buttons | data.ToJSON -}}
{{ end }}
@MarieKirya
Copy link
Author

Alerts will look like this:
Screenshot 2026-01-19 at 02 16 56

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