diff --git a/docs/notif/discord.md b/docs/notif/discord.md index e72d65dd..88103efb 100644 --- a/docs/notif/discord.md +++ b/docs/notif/discord.md @@ -9,16 +9,19 @@ Allow to send notifications to your Discord channel. notif: discord: webhookURL: https://discordapp.com/api/webhooks/1234567890/Abcd-eFgh-iJklmNo_pqr + mention: here timeout: 10s ``` !!! abstract "Environment variables" * `DIUN_NOTIF_DISCORD_WEBHOOK` + * `DIUN_NOTIF_DISCORD_MENTION` * `DIUN_NOTIF_DISCORD_TIMEOUT` | Name | Default | Description | |--------------------|---------------|---------------| | `webhookURL`[^1] | | Discord [incoming webhook URL](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks) | +| `mention` | | Mention to directly notify a user, multiple users or roles in messages (eg. `here`) | | `timeout` | `10s` | Timeout specifies a time limit for the request to be made | ## Sample diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 4badcfc1..0ea7695f 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -62,6 +62,7 @@ func TestLoadFile(t *testing.T) { }, Discord: &model.NotifDiscord{ WebhookURL: "https://discordapp.com/api/webhooks/1234567890/Abcd-eFgh-iJklmNo_pqr", + Mention: "here", Timeout: utl.NewDuration(10 * time.Second), }, Gotify: &model.NotifGotify{ diff --git a/internal/config/fixtures/config.test.yml b/internal/config/fixtures/config.test.yml index 5e88a6d7..647e51c3 100644 --- a/internal/config/fixtures/config.test.yml +++ b/internal/config/fixtures/config.test.yml @@ -15,6 +15,7 @@ notif: queue: queue discord: webhookURL: https://discordapp.com/api/webhooks/1234567890/Abcd-eFgh-iJklmNo_pqr + mention: here timeout: 10s gotify: endpoint: http://gotify.foo.com diff --git a/internal/config/fixtures/config.validate.yml b/internal/config/fixtures/config.validate.yml index fa4d81ed..dc065e5f 100644 --- a/internal/config/fixtures/config.validate.yml +++ b/internal/config/fixtures/config.validate.yml @@ -15,6 +15,7 @@ notif: queue: queue discord: webhookURL: https://discordapp.com/api/webhooks/1234567890/Abcd-eFgh-iJklmNo_pqr + mention: here timeout: 10s gotify: endpoint: http://gotify.foo.com diff --git a/internal/model/notif_discord.go b/internal/model/notif_discord.go index a4b94dd9..521cc68e 100644 --- a/internal/model/notif_discord.go +++ b/internal/model/notif_discord.go @@ -9,6 +9,7 @@ import ( // NotifDiscord holds Discord notification configuration details type NotifDiscord struct { WebhookURL string `yaml:"webhookURL,omitempty" json:"webhookURL,omitempty" validate:"required"` + Mention string `yaml:"mention,omitempty" json:"mention,omitempty"` Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" validate:"required"` } diff --git a/internal/notif/discord/client.go b/internal/notif/discord/client.go index 6529b124..10ff7457 100644 --- a/internal/notif/discord/client.go +++ b/internal/notif/discord/client.go @@ -38,13 +38,19 @@ func (c *Client) Name() string { // Send creates and sends a discord notification with an entry // https://discord.com/developers/docs/resources/webhook#execute-webhook func (c *Client) Send(entry model.NotifEntry) error { + var content bytes.Buffer + hc := http.Client{ Timeout: *c.cfg.Timeout, } - content := fmt.Sprintf("@here Image update for %s", entry.Image.String()) + if len(c.cfg.Mention) > 0 { + content.WriteString(fmt.Sprintf("@%s ", c.cfg.Mention)) + } if entry.Status == model.ImageStatusNew { - content = fmt.Sprintf("@here New image %s has been added", entry.Image.String()) + content.WriteString(fmt.Sprintf("@here New image %s has been added", entry.Image.String())) + } else { + content.WriteString(fmt.Sprintf("@here Image update for %s", entry.Image.String())) } tagTpl := "**{{ .Entry.Image.Domain }}/{{ .Entry.Image.Path }}:{{ .Entry.Image.Tag }}**" @@ -95,7 +101,7 @@ func (c *Client) Send(entry model.NotifEntry) error { dataBuf := new(bytes.Buffer) if err := json.NewEncoder(dataBuf).Encode(Message{ - Content: content, + Content: content.String(), Username: c.meta.Name, AvatarURL: c.meta.Logo, Embeds: []Embed{