Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,11 @@ 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"`
Username string `yaml:"username,omitempty" json:"username,omitempty"`
}

// UnmarshalYAML implements the yaml.Unmarshaler interface.
Expand Down
41 changes: 35 additions & 6 deletions notify/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"strings"

Expand All @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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{
Expand All @@ -142,12 +155,28 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
}},
}

if len(content) != 0 {
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
} 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)
}
Expand Down