Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend discord messages with content #4007

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions asset/assets_vfsdata.go

Large diffs are not rendered by default.

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 with 2000 characters.
gotjosh marked this conversation as resolved.
Show resolved Hide resolved
[ 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 }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want to update DefaultDiscordConfig on Line 46 of config/notifiers.go so this template is used.

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