diff options
author | MOZGIII <mike-n@narod.ru> | 2019-08-29 01:13:10 +0300 |
---|---|---|
committer | Wim <wim@42.be> | 2019-08-29 00:13:10 +0200 |
commit | cec086994e1b00ee1230c6657f5fedaa2ce90372 (patch) | |
tree | 5c628c3a0e7297d7ec3c2711ccd182b90604030e /bridge/discord/discord.go | |
parent | 942d8f1cedfb7325c0720f8967751bb1b77f5b82 (diff) | |
download | matterbridge-msglm-cec086994e1b00ee1230c6657f5fedaa2ce90372.tar.gz matterbridge-msglm-cec086994e1b00ee1230c6657f5fedaa2ce90372.tar.bz2 matterbridge-msglm-cec086994e1b00ee1230c6657f5fedaa2ce90372.zip |
Add support for sending files via webhook (discord) (#872)
Diffstat (limited to 'bridge/discord/discord.go')
-rw-r--r-- | bridge/discord/discord.go | 80 |
1 files changed, 68 insertions, 12 deletions
diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index 1ae655bf..84171fa5 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -237,8 +237,10 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) { } } } + // 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 } @@ -261,16 +263,16 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) { return "", err } } - msg, err := b.webhookExecute( - wID, - wToken, - true, - &discordgo.WebhookParams{ - Content: msg.Text, - Username: msg.Username, - AvatarURL: msg.Avatar, - }) - return msg.ID, err + b.Log.Debugf("Processing webhook sending for message %#v", msg) + msg, err := b.webhookSend(&msg, wID, wToken) + if err != nil { + b.Log.Errorf("Could not broadcast via webook for message %#v: %s", msg, err) + return "", err + } + if msg == nil { + return "", nil + } + return msg.ID, nil } b.Log.Debugf("Broadcasting using token (API)") @@ -312,7 +314,7 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) { if err != nil { return "", err } - return res.ID, err + return res.ID, nil } // useWebhook returns true if we have a webhook defined somewhere @@ -376,3 +378,57 @@ func (b *Bdiscord) handleUploadFile(msg *config.Message, channelID string) (stri } return "", nil } + +// webhookSend send one or more message via webhook, taking care of file +// uploads (from slack, telegram or mattermost). +// Returns messageID and error. +func (b *Bdiscord) webhookSend(msg *config.Message, webhookID, token string) (*discordgo.Message, error) { + var ( + res *discordgo.Message + err error + ) + + // WebhookParams can have either `Content` or `File`. + + // We can't send empty messages. + if msg.Text != "" { + res, err = b.c.WebhookExecute( + webhookID, + token, + true, + &discordgo.WebhookParams{ + Content: msg.Text, + Username: msg.Username, + AvatarURL: msg.Avatar, + }, + ) + if err != nil { + b.Log.Errorf("Could not send text (%s) for message %#v: %s", msg.Text, msg, err) + } + } + + if msg.Extra != nil { + for _, f := range msg.Extra["file"] { + fi := f.(config.FileInfo) + file := discordgo.File{ + Name: fi.Name, + ContentType: "", + Reader: bytes.NewReader(*fi.Data), + } + _, e2 := b.c.WebhookExecute( + webhookID, + token, + false, + &discordgo.WebhookParams{ + Username: msg.Username, + AvatarURL: msg.Avatar, + File: &file, + }, + ) + if e2 != nil { + b.Log.Errorf("Could not send file %#v for message %#v: %s", file, msg, e2) + } + } + } + return res, err +} |