Corrected discord webhook sending logic for text and files

This commit is contained in:
MOZGIII
2019-08-15 01:50:15 +03:00
committed by Wim
parent b5c8522a4e
commit 7148262e4a
2 changed files with 30 additions and 24 deletions

View File

@@ -237,8 +237,10 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) {
} }
} }
} }
// skip empty messages // skip empty messages
if msg.Text == "" { if msg.Text == "" && (msg.Extra == nil || len(msg.Extra["file"]) == 0) {
b.Log.Debugf("Skipping empty message %#v", msg)
return "", nil return "", nil
} }
@@ -261,10 +263,15 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) {
return "", err return "", err
} }
} }
b.Log.Debugf("Processing webhook sending for message %#v", msg)
msg, err := b.webhookSend(&msg, wID, wToken) msg, err := b.webhookSend(&msg, wID, wToken)
if err != nil { if err != nil {
b.Log.Errorf("Could not broadcast via webook for message %#v: %s", msg, err)
return "", err return "", err
} }
if msg == nil {
return "", nil
}
return msg.ID, nil return msg.ID, nil
} }
@@ -307,7 +314,7 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
return res.ID, err return res.ID, nil
} }
// useWebhook returns true if we have a webhook defined somewhere // useWebhook returns true if we have a webhook defined somewhere
@@ -375,22 +382,24 @@ func (b *Bdiscord) handleUploadFile(msg *config.Message, channelID string) (stri
// webhookSend send one or more message via webhook, taking care of file // webhookSend send one or more message via webhook, taking care of file
// uploads (from slack, telegram or mattermost). // uploads (from slack, telegram or mattermost).
// Returns messageID and error. // Returns messageID and error.
func (b *Bdiscord) webhookSend(msg *config.Message, webhookID, token string) (*discordgo.Message, error) { func (b *Bdiscord) webhookSend(msg *config.Message, webhookID, token string) (res *discordgo.Message, err error) {
var err error
// WebhookParams can have either `Content` or `File`. // WebhookParams can have either `Content` or `File`.
res, err := b.c.WebhookExecute(
webhookID, // We can't send empty messages.
token, if msg.Text != "" {
true, res, err = b.c.WebhookExecute(
&discordgo.WebhookParams{ webhookID,
Content: msg.Text, token,
Username: msg.Username, true,
AvatarURL: msg.Avatar, &discordgo.WebhookParams{
}, Content: msg.Text,
) Username: msg.Username,
if err != nil { AvatarURL: msg.Avatar,
return nil, err },
)
if err != nil {
b.Log.Errorf("Could not send text (%s) for message %#v: %s", msg.Text, msg, err)
}
} }
if msg.Extra != nil { if msg.Extra != nil {
@@ -401,7 +410,7 @@ func (b *Bdiscord) webhookSend(msg *config.Message, webhookID, token string) (*d
ContentType: "", ContentType: "",
Reader: bytes.NewReader(*fi.Data), Reader: bytes.NewReader(*fi.Data),
} }
_, err := b.c.WebhookExecute( _, e2 := b.c.WebhookExecute(
webhookID, webhookID,
token, token,
false, false,
@@ -411,11 +420,10 @@ func (b *Bdiscord) webhookSend(msg *config.Message, webhookID, token string) (*d
File: &file, File: &file,
}, },
) )
if err != nil { if e2 != nil {
return nil, fmt.Errorf("file upload failed: %s", err) b.Log.Errorf("Could not send file %#v for message %#v: %s", file, msg, e2)
} }
} }
} }
return res, err
return res, nil
} }

2
go.sum
View File

@@ -7,8 +7,6 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Jeffail/gabs v1.1.1 h1:V0uzR08Hj22EX8+8QMhyI9sX2hwRu+/RJhJUmnwda/E= github.com/Jeffail/gabs v1.1.1 h1:V0uzR08Hj22EX8+8QMhyI9sX2hwRu+/RJhJUmnwda/E=
github.com/Jeffail/gabs v1.1.1/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc= github.com/Jeffail/gabs v1.1.1/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc=
github.com/MOZGIII/discordgo v0.19.1-0.20190812115637-1e74183814f9 h1:2AlsZSdWfhhuyzNgRejZDkkLq1cAA2K8TaMoadSXeJE=
github.com/MOZGIII/discordgo v0.19.1-0.20190812115637-1e74183814f9/go.mod h1:O9S4p+ofTFwB02em7jkpkV8M3R0/PUVOwN61zSZ0r4Q=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/Philipp15b/go-steam v1.0.1-0.20180818081528-681bd9573329 h1:xZBoq249G9MSt+XuY7sVQzcfONJ6IQuwpCK+KAaOpnY= github.com/Philipp15b/go-steam v1.0.1-0.20180818081528-681bd9573329 h1:xZBoq249G9MSt+XuY7sVQzcfONJ6IQuwpCK+KAaOpnY=
github.com/Philipp15b/go-steam v1.0.1-0.20180818081528-681bd9573329/go.mod h1:HuVM+sZFzumUdKPWiz+IlCMb4RdsKdT3T+nQBKL+sYg= github.com/Philipp15b/go-steam v1.0.1-0.20180818081528-681bd9573329/go.mod h1:HuVM+sZFzumUdKPWiz+IlCMb4RdsKdT3T+nQBKL+sYg=