Add option to mention specific user for Discord notifier (#188)

This commit is contained in:
CrazyMax
2020-10-07 22:40:46 +02:00
parent 65182db118
commit b57d49dae2
6 changed files with 16 additions and 3 deletions

View File

@@ -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

View File

@@ -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{

View File

@@ -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

View File

@@ -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

View File

@@ -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"`
}

View File

@@ -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{