Skip to content

Commit

Permalink
Extend discord messages with content (#4007)
Browse files Browse the repository at this point in the history
* Extend discord messages with content
Signed-off-by: Yevhen Sydorenko <[email protected]>

Signed-off-by: Yevhen Sydorenko <[email protected]>

* Updated configuration.md
Updated assets_vfsdata.go

Signed-off-by: Yevhen Sydorenko <[email protected]>

* Fix grammar

Signed-off-by: Yevhen Sydorenko <[email protected]>

* Updated vfsdata

Signed-off-by: Yevhen Sydorenko <[email protected]>

---------

Signed-off-by: Yevhen Sydorenko <[email protected]>
  • Loading branch information
mogoll92 authored Oct 23, 2024
1 parent 69fe3f8 commit 6b77acd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions asset/assets_vfsdata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ type DiscordConfig struct {
WebhookURL *SecretURL `yaml:"webhook_url,omitempty" json:"webhook_url,omitempty"`
WebhookURLFile string `yaml:"webhook_url_file,omitempty" json:"webhook_url_file,omitempty"`

Content string `yaml:"content,omitempty" json:"content,omitempty"`
Title string `yaml:"title,omitempty" json:"title,omitempty"`
Message string `yaml:"message,omitempty" json:"message,omitempty"`
}
Expand Down
3 changes: 3 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,9 @@ webhook_url_file: <filepath>
# Message body template.
[ message: <tmpl_string> | default = '{{ template "discord.default.message" . }}' ]
# Message content template. Limited to 2000 characters.
[ content: <tmpl_string> | default = '{{ template "discord.default.content" . }}' ]
# The HTTP client's configuration.
[ http_config: <http_config> | default = global.http_config ]
```
Expand Down
11 changes: 11 additions & 0 deletions notify/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const (
maxTitleLenRunes = 256
// https://discord.com/developers/docs/resources/channel#embed-object-embed-limits - 4096 characters or runes.
maxDescriptionLenRunes = 4096

maxContentLenRunes = 2000
)

const (
Expand Down Expand Up @@ -115,6 +117,14 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
level.Warn(n.logger).Log("msg", "Truncated message", "key", key, "max_runes", maxDescriptionLenRunes)
}

content, truncated := notify.TruncateInRunes(tmpl(n.conf.Content), maxContentLenRunes)
if err != nil {
return false, err
}
if truncated {
level.Warn(n.logger).Log("msg", "Truncated message", "key", key, "max_runes", maxContentLenRunes)
}

color := colorGrey
if alerts.Status() == model.AlertFiring {
color = colorRed
Expand All @@ -135,6 +145,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
}

w := webhook{
Content: content,
Embeds: []webhookEmbed{{
Title: title,
Description: description,
Expand Down
1 change: 1 addition & 0 deletions template/default.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ Alerts Resolved:
{{ end }}
{{ end }}

{{ define "discord.default.content" }}{{ end }}
{{ define "discord.default.title" }}{{ template "__subject" . }}{{ end }}
{{ define "discord.default.message" }}
{{ if gt (len .Alerts.Firing) 0 }}
Expand Down

0 comments on commit 6b77acd

Please sign in to comment.