Compare commits

...

2 Commits

Author SHA1 Message Date
MOZGIII
831b5b332f Switched to custom discordgo branch 2019-08-13 03:56:49 +03:00
MOZGIII
d93879bca5 Proper support for uploading files via webhooks for discord 2019-08-12 15:41:24 +03:00
4 changed files with 58 additions and 36 deletions

View File

@ -261,16 +261,11 @@ 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
msg, err := b.webhookSend(&msg, wID, wToken)
if err != nil {
return "", err
}
return msg.ID, nil
}
b.Log.Debugf("Broadcasting using token (API)")
@ -376,3 +371,51 @@ 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 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
}

View File

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

3
go.mod
View File

@ -7,6 +7,7 @@ require (
github.com/Philipp15b/go-steam v1.0.1-0.20180818081528-681bd9573329
github.com/Rhymen/go-whatsapp v0.0.2
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
@ -64,3 +65,5 @@ require (
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
)
replace github.com/bwmarrin/discordgo v0.19.0 => github.com/MOZGIII/discordgo v0.19.1-0.20190812115637-1e74183814f9

4
go.sum
View File

@ -7,6 +7,8 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
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/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/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=
@ -24,8 +26,6 @@ github.com/alexcesaro/log v0.0.0-20150915221235-61e686294e58/go.mod h1:YNfsMyWSs
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/bwmarrin/discordgo v0.19.0 h1:kMED/DB0NR1QhRcalb85w0Cu3Ep2OrGAqZH1R5awQiY=
github.com/bwmarrin/discordgo v0.19.0/go.mod h1:O9S4p+ofTFwB02em7jkpkV8M3R0/PUVOwN61zSZ0r4Q=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=