Skip to content

Commit 95655dc

Browse files
Discord notifier: Add username and avatar_url (#4081)
* Feat(discord): Allow for custom username and avatar URLs to be set in discord notifications. Add `username` and `avatar_url` to discord configuration, default empty string. Re-implement #3821 Signed-off-by: Jeff Wong <[email protected]> * Test the new fields Signed-off-by: gotjosh <[email protected]> * These are not templeatable strings Signed-off-by: gotjosh <[email protected]> --------- Signed-off-by: Jeff Wong <[email protected]> Signed-off-by: gotjosh <[email protected]> Co-authored-by: gotjosh <[email protected]>
1 parent 615d5ff commit 95655dc

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

config/notifiers.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,11 @@ type DiscordConfig struct {
251251
WebhookURL *SecretURL `yaml:"webhook_url,omitempty" json:"webhook_url,omitempty"`
252252
WebhookURLFile string `yaml:"webhook_url_file,omitempty" json:"webhook_url_file,omitempty"`
253253

254-
Content string `yaml:"content,omitempty" json:"content,omitempty"`
255-
Title string `yaml:"title,omitempty" json:"title,omitempty"`
256-
Message string `yaml:"message,omitempty" json:"message,omitempty"`
254+
Content string `yaml:"content,omitempty" json:"content,omitempty"`
255+
Title string `yaml:"title,omitempty" json:"title,omitempty"`
256+
Message string `yaml:"message,omitempty" json:"message,omitempty"`
257+
Username string `yaml:"username,omitempty" json:"username,omitempty"`
258+
AvatarURL string `yaml:"avatar_url,omitempty" json:"avatar_url,omitempty"`
257259
}
258260

259261
// UnmarshalYAML implements the yaml.Unmarshaler interface.

docs/configuration.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,12 @@ webhook_url_file: <filepath>
886886
# Message content template. Limited to 2000 characters.
887887
[ content: <tmpl_string> | default = '{{ template "discord.default.content" . }}' ]
888888
889+
# Message username.
890+
[ username: <string> | default = '' ]
891+
892+
# Message avatar URL.
893+
[ avatar_url: <string> | default = '' ]
894+
889895
# The HTTP client's configuration.
890896
[ http_config: <http_config> | default = global.http_config ]
891897
```

notify/discord/discord.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"encoding/json"
2020
"fmt"
2121
"net/http"
22+
netUrl "net/url"
2223
"os"
2324
"strings"
2425

@@ -76,8 +77,10 @@ func New(c *config.DiscordConfig, t *template.Template, l log.Logger, httpOpts .
7677
}
7778

7879
type webhook struct {
79-
Content string `json:"content"`
80-
Embeds []webhookEmbed `json:"embeds"`
80+
Content string `json:"content"`
81+
Embeds []webhookEmbed `json:"embeds"`
82+
Username string `json:"username,omitempty"`
83+
AvatarURL string `json:"avatar_url,omitempty"`
8184
}
8285

8386
type webhookEmbed struct {
@@ -145,14 +148,23 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
145148
}
146149

147150
w := webhook{
148-
Content: content,
151+
Content: content,
152+
Username: n.conf.Username,
149153
Embeds: []webhookEmbed{{
150154
Title: title,
151155
Description: description,
152156
Color: color,
153157
}},
154158
}
155159

160+
if len(n.conf.AvatarURL) != 0 {
161+
if _, err := netUrl.Parse(n.conf.AvatarURL); err == nil {
162+
w.AvatarURL = n.conf.AvatarURL
163+
} else {
164+
level.Warn(n.logger).Log("msg", "Bad avatar url", "key", key)
165+
}
166+
}
167+
156168
var payload bytes.Buffer
157169
if err = json.NewEncoder(&payload).Encode(w); err != nil {
158170
return false, err

notify/discord/discord_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ func TestDiscord_Notify(t *testing.T) {
201201
Title: "Test Title",
202202
Message: "Test Message",
203203
Content: "Test Content",
204+
Username: "Test Username",
205+
AvatarURL: "http://example.com/avatar.png",
204206
}
205207

206208
// Create a new Discord notifier
@@ -227,5 +229,5 @@ func TestDiscord_Notify(t *testing.T) {
227229
require.NoError(t, err)
228230
require.False(t, ok)
229231

230-
require.Equal(t, "{\"content\":\"Test Content\",\"embeds\":[{\"title\":\"Test Title\",\"description\":\"Test Message\",\"color\":10038562}]}\n", resp)
232+
require.Equal(t, "{\"content\":\"Test Content\",\"embeds\":[{\"title\":\"Test Title\",\"description\":\"Test Message\",\"color\":10038562}],\"username\":\"Test Username\",\"avatar_url\":\"http://example.com/avatar.png\"}\n", resp)
231233
}

0 commit comments

Comments
 (0)