From 04d4582ed844af5253f782a31eabd220c3c3801a Mon Sep 17 00:00:00 2001 From: yaser Date: Fri, 26 Apr 2024 19:45:10 +0330 Subject: [PATCH 1/2] Feat(discord): Add Discord webhook content, avatar url and username fields Signed-off-by: yaser --- config/notifiers.go | 6 ++++-- notify/discord/discord.go | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/config/notifiers.go b/config/notifiers.go index 7d52aed1a8..6b3d0f3664 100644 --- a/config/notifiers.go +++ b/config/notifiers.go @@ -220,8 +220,10 @@ 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"` - Title string `yaml:"title,omitempty" json:"title,omitempty"` - Message string `yaml:"message,omitempty" json:"message,omitempty"` + Title string `yaml:"title,omitempty" json:"title,omitempty"` + Message string `yaml:"message,omitempty" json:"message,omitempty"` + Content string `yaml:"content,omitempty" json:"content,omitempty"` + AvatarURL string `yaml:"avatar_url,omitempty" json:"avatar_url,omitempty"` } // UnmarshalYAML implements the yaml.Unmarshaler interface. diff --git a/notify/discord/discord.go b/notify/discord/discord.go index 69aff39957..a92dd0d6a4 100644 --- a/notify/discord/discord.go +++ b/notify/discord/discord.go @@ -19,6 +19,7 @@ import ( "encoding/json" "fmt" "net/http" + "net/url" "os" "strings" @@ -38,6 +39,8 @@ const ( maxTitleLenRunes = 256 // https://discord.com/developers/docs/resources/channel#embed-object-embed-limits - 4096 characters or runes. maxDescriptionLenRunes = 4096 + + maxContentLenRunes = 2000 ) const ( @@ -74,8 +77,10 @@ func New(c *config.DiscordConfig, t *template.Template, l log.Logger, httpOpts . } type webhook struct { - Content string `json:"content"` - Embeds []webhookEmbed `json:"embeds"` + Username string `json:"username,omitempty"` + AvatarURL string `json:"avatar_url,omitempty"` + Content string `json:"content"` + Embeds []webhookEmbed `json:"embeds"` } type webhookEmbed struct { @@ -115,6 +120,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 @@ -123,15 +136,15 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) color = colorGreen } - var url string + var whURL string if n.conf.WebhookURL != nil { - url = n.conf.WebhookURL.String() + whURL = n.conf.WebhookURL.String() } else { content, err := os.ReadFile(n.conf.WebhookURLFile) if err != nil { return false, fmt.Errorf("read webhook_url_file: %w", err) } - url = strings.TrimSpace(string(content)) + whURL = strings.TrimSpace(string(content)) } w := webhook{ @@ -142,12 +155,24 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) }}, } + if len(content) != 0 { + w.Content = content + } + + if len(n.conf.AvatarURL) != 0 { + if _, err := url.Parse(n.conf.AvatarURL); err == nil { + w.AvatarURL = n.conf.AvatarURL + } else { + level.Warn(n.logger).Log("msg", "Bad avatar url", "key", key) + } + } + var payload bytes.Buffer if err = json.NewEncoder(&payload).Encode(w); err != nil { return false, err } - resp, err := notify.PostJSON(ctx, n.client, url, &payload) + resp, err := notify.PostJSON(ctx, n.client, whURL, &payload) if err != nil { return true, notify.RedactURL(err) } From 85d44df6f937e9ce286b0fd5365fa793c0dd52d9 Mon Sep 17 00:00:00 2001 From: yaser Date: Fri, 26 Apr 2024 20:23:37 +0330 Subject: [PATCH 2/2] Chore(discord): Add Username Signed-off-by: yaser --- config/notifiers.go | 1 + notify/discord/discord.go | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/config/notifiers.go b/config/notifiers.go index 6b3d0f3664..6c70e1e5ab 100644 --- a/config/notifiers.go +++ b/config/notifiers.go @@ -224,6 +224,7 @@ type DiscordConfig struct { Message string `yaml:"message,omitempty" json:"message,omitempty"` Content string `yaml:"content,omitempty" json:"content,omitempty"` AvatarURL string `yaml:"avatar_url,omitempty" json:"avatar_url,omitempty"` + Username string `yaml:"username,omitempty" json:"username,omitempty"` } // UnmarshalYAML implements the yaml.Unmarshaler interface. diff --git a/notify/discord/discord.go b/notify/discord/discord.go index a92dd0d6a4..0e0de71994 100644 --- a/notify/discord/discord.go +++ b/notify/discord/discord.go @@ -159,6 +159,10 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) w.Content = content } + if len(n.conf.Username) != 0 { + w.Username = n.conf.Username + } + if len(n.conf.AvatarURL) != 0 { if _, err := url.Parse(n.conf.AvatarURL); err == nil { w.AvatarURL = n.conf.AvatarURL