mirror of
https://github.com/42wim/matterbridge.git
synced 2024-12-26 13:12:37 +01:00
Add support for sending files via webhook (discord) (#872)
This commit is contained in:
parent
942d8f1ced
commit
cec086994e
@ -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
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package bdiscord
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
@ -236,26 +235,3 @@ func enumerateUsernames(s string) []string {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
1
go.mod
1
go.mod
@ -7,6 +7,7 @@ require (
|
||||
github.com/Philipp15b/go-steam v1.0.1-0.20180818081528-681bd9573329
|
||||
github.com/Rhymen/go-whatsapp v0.0.3-0.20190729104911-5c79b2cf277a
|
||||
github.com/bwmarrin/discordgo v0.19.0
|
||||
// github.com/bwmarrin/discordgo v0.19.0
|
||||
github.com/d5/tengo v1.24.1
|
||||
github.com/dfordsoft/golib v0.0.0-20180902042739-76ee6ab99bec
|
||||
github.com/fsnotify/fsnotify v1.4.7
|
||||
|
Loading…
Reference in New Issue
Block a user