summaryrefslogtreecommitdiffstats
path: root/bridge
diff options
context:
space:
mode:
authorSacha Aury - Wolfman <sacha.aury@gmail.com>2017-06-26 20:07:27 +0200
committerWim <wim@42.be>2017-06-26 20:07:27 +0200
commitc17512b7ab759ab0fba0ecc8be20ff829946bb24 (patch)
tree287e012a2b6c95adb0a9cb36edc0e9e1213befea /bridge
parent1b837b3dc7579351a6018b52b58613c446ce08a5 (diff)
downloadmatterbridge-msglm-c17512b7ab759ab0fba0ecc8be20ff829946bb24.tar.gz
matterbridge-msglm-c17512b7ab759ab0fba0ecc8be20ff829946bb24.tar.bz2
matterbridge-msglm-c17512b7ab759ab0fba0ecc8be20ff829946bb24.zip
Add webhook posting mode for discord. (#204)
Using it implies to configure a Webhook on discord and set the parameter : - WebhookURL (New parameter, discord-specific) Discord API does not allow to change the name of the user posting, but webhooks does. This makes the relay much more elegant, even if we might lose some more advanced features. Signed-off-by: saury07 <sacha.aury@gmail.com>
Diffstat (limited to 'bridge')
-rw-r--r--bridge/config/config.go1
-rw-r--r--bridge/discord/discord.go28
2 files changed, 28 insertions, 1 deletions
diff --git a/bridge/config/config.go b/bridge/config/config.go
index 246d6b82..6244dd6a 100644
--- a/bridge/config/config.go
+++ b/bridge/config/config.go
@@ -77,6 +77,7 @@ type Protocol struct {
UseSASL bool // IRC
UseTLS bool // IRC
UseFirstName bool // telegram
+ WebhookURL string // discord
}
type ChannelOptions struct {
diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go
index 8c7d9711..e03aaa4d 100644
--- a/bridge/discord/discord.go
+++ b/bridge/discord/discord.go
@@ -19,6 +19,8 @@ type bdiscord struct {
UseChannelID bool
userMemberMap map[string]*discordgo.Member
guildID string
+ webhookID string
+ webhookToken string
sync.RWMutex
}
@@ -35,6 +37,12 @@ func New(cfg config.Protocol, account string, c chan config.Message) *bdiscord {
b.Remote = c
b.Account = account
b.userMemberMap = make(map[string]*discordgo.Member)
+ if b.Config.WebhookURL != "" {
+ flog.Debug("Configuring Discord Incoming Webhook")
+ webhookURLSplit := strings.Split(b.Config.WebhookURL, "/")
+ b.webhookToken = webhookURLSplit[len(webhookURLSplit)-1]
+ b.webhookID = webhookURLSplit[len(webhookURLSplit)-2]
+ }
return b
}
@@ -101,7 +109,21 @@ func (b *bdiscord) Send(msg config.Message) error {
flog.Errorf("Could not find channelID for %v", msg.Channel)
return nil
}
- b.c.ChannelMessageSend(channelID, msg.Username+msg.Text)
+ if b.Config.WebhookURL == ""{
+ flog.Debugf("Broadcasting using API")
+ b.c.ChannelMessageSend(channelID, msg.Username+msg.Text)
+ } else {
+ flog.Debugf("Broadcasting using Webhook")
+ b.c.WebhookExecute(
+ b.webhookID,
+ b.webhookToken,
+ true,
+ &discordgo.WebhookParams{
+ Content: msg.Text,
+ Username: msg.Username,
+ AvatarURL: msg.Avatar,
+ })
+ }
return nil
}
@@ -122,6 +144,10 @@ func (b *bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
if m.Author.Username == b.Nick {
return
}
+ // if using webhooks, do not relay if it's ours
+ if b.Config.WebhookURL != "" && m.Author.Bot && m.Author.ID == b.webhookID {
+ return
+ }
if len(m.Attachments) > 0 {
for _, attach := range m.Attachments {
m.Content = m.Content + "\n" + attach.URL