forked from jshiffer/matterbridge
Proper support for uploading files via webhooks for discord
This commit is contained in:
parent
79a006c8de
commit
d93879bca5
@ -261,16 +261,11 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
msg, err := b.webhookExecute(
|
msg, err := b.webhookSend(&msg, wID, wToken)
|
||||||
wID,
|
if err != nil {
|
||||||
wToken,
|
return "", err
|
||||||
true,
|
}
|
||||||
&discordgo.WebhookParams{
|
return msg.ID, nil
|
||||||
Content: msg.Text,
|
|
||||||
Username: msg.Username,
|
|
||||||
AvatarURL: msg.Avatar,
|
|
||||||
})
|
|
||||||
return msg.ID, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
b.Log.Debugf("Broadcasting using token (API)")
|
b.Log.Debugf("Broadcasting using token (API)")
|
||||||
@ -376,3 +371,51 @@ func (b *Bdiscord) handleUploadFile(msg *config.Message, channelID string) (stri
|
|||||||
}
|
}
|
||||||
return "", nil
|
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 err error
|
||||||
|
|
||||||
|
// WebhookParams can have either `Content` or `File`.
|
||||||
|
res, err := b.c.WebhookExecute(
|
||||||
|
webhookID,
|
||||||
|
token,
|
||||||
|
true,
|
||||||
|
&discordgo.WebhookParams{
|
||||||
|
Content: msg.Text,
|
||||||
|
Username: msg.Username,
|
||||||
|
AvatarURL: msg.Avatar,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 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),
|
||||||
|
}
|
||||||
|
_, err := b.c.WebhookExecute(
|
||||||
|
webhookID,
|
||||||
|
token,
|
||||||
|
false,
|
||||||
|
&discordgo.WebhookParams{
|
||||||
|
Username: msg.Username,
|
||||||
|
AvatarURL: msg.Avatar,
|
||||||
|
File: &file,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("file upload failed: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package bdiscord
|
package bdiscord
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
@ -236,26 +235,3 @@ func enumerateUsernames(s string) []string {
|
|||||||
}
|
}
|
||||||
return usernames
|
return usernames
|
||||||
}
|
}
|
||||||
|
|
||||||
// webhookExecute executes a webhook.
|
|
||||||
// webhookID: The ID of a webhook.
|
|
||||||
// token : The auth token for the webhook
|
|
||||||
// wait : Waits for server confirmation of message send and ensures that the return struct is populated (it is nil otherwise)
|
|
||||||
func (b *Bdiscord) webhookExecute(webhookID, token string, wait bool, data *discordgo.WebhookParams) (st *discordgo.Message, err error) {
|
|
||||||
uri := discordgo.EndpointWebhookToken(webhookID, token)
|
|
||||||
|
|
||||||
if wait {
|
|
||||||
uri += "?wait=true"
|
|
||||||
}
|
|
||||||
response, err := b.c.RequestWithBucketID("POST", uri, data, discordgo.EndpointWebhookToken("", ""))
|
|
||||||
if !wait || err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = json.Unmarshal(response, &st)
|
|
||||||
if err != nil {
|
|
||||||
return nil, discordgo.ErrJSONUnmarshal
|
|
||||||
}
|
|
||||||
|
|
||||||
return st, nil
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user