diff --git a/config/notifiers.go b/config/notifiers.go index ac437096e8..fe28ca05c4 100644 --- a/config/notifiers.go +++ b/config/notifiers.go @@ -251,9 +251,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"` - Content string `yaml:"content,omitempty" json:"content,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"` + Title string `yaml:"title,omitempty" json:"title,omitempty"` + Message string `yaml:"message,omitempty" json:"message,omitempty"` + Username string `yaml:"username,omitempty" json:"username,omitempty"` + AvatarURL string `yaml:"avatar_url,omitempty" json:"avatar_url,omitempty"` } // UnmarshalYAML implements the yaml.Unmarshaler interface. diff --git a/docs/configuration.md b/docs/configuration.md index 696548658c..a958189917 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -886,6 +886,12 @@ webhook_url_file: # Message content template. Limited to 2000 characters. [ content: | default = '{{ template "discord.default.content" . }}' ] +# Message username. +[ username: | default = '' ] + +# Message avatar URL. +[ avatar_url: | default = '' ] + # The HTTP client's configuration. [ http_config: | default = global.http_config ] ``` diff --git a/notify/discord/discord.go b/notify/discord/discord.go index 1202a9caaf..64b831d1f7 100644 --- a/notify/discord/discord.go +++ b/notify/discord/discord.go @@ -19,6 +19,7 @@ import ( "encoding/json" "fmt" "net/http" + netUrl "net/url" "os" "strings" @@ -76,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"` + Content string `json:"content"` + Embeds []webhookEmbed `json:"embeds"` + Username string `json:"username,omitempty"` + AvatarURL string `json:"avatar_url,omitempty"` } type webhookEmbed struct { @@ -145,7 +148,8 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) } w := webhook{ - Content: content, + Content: content, + Username: n.conf.Username, Embeds: []webhookEmbed{{ Title: title, Description: description, @@ -153,6 +157,14 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) }}, } + if len(n.conf.AvatarURL) != 0 { + if _, err := netUrl.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