Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 03921c58e5a87bd8e3181e02922ed2e8e284029e Author: Alexander <papatutuwawa@polynom.me> Date: Thu Jan 21 22:50:04 2021 +0000 Allow the XMPP bridge to use slack compatible webhooks (xmpp) (#1364) * Add mod_slack_webhook support to the XMPP bridge * Replace b.webhookURL with b.GetString * Do not return a message ID on webhook POST * Add the XMPP webhook to the sample configuration diff --git a/bridge/xmpp/xmpp.go b/bridge/xmpp/xmpp.go index 45c5beb..e92c3d6 100644 --- a/bridge/xmpp/xmpp.go +++ b/bridge/xmpp/xmpp.go @@ -18 +111 @@  package bxmpp    import ( + "bytes"   "crypto/tls" + "encoding/json"   "fmt" + "net/http"   "strings"   "sync"   "time" @@ -8614 +8921 @@ func (b *Bxmpp) Send(msg config.Message) (string, error) {   }     // Upload a file (in XMPP case send the upload URL because XMPP has no native upload support). + var err error   if msg.Extra != nil {   for _, rmsg := range helper.HandleExtra(&msg, b.General) {   b.Log.Debugf("=> Sending attachement message %#v", rmsg) - if _, err := b.xc.Send(xmpp.Chat{ - Type: "groupchat", - Remote: rmsg.Channel + "@" + b.GetString("Muc"), - Text: rmsg.Username + rmsg.Text, - }); err != nil { + if b.GetString("WebhookURL") != "" { + err = b.postSlackCompatibleWebhook(msg) + } else { + _, err = b.xc.Send(xmpp.Chat{ + Type: "groupchat", + Remote: rmsg.Channel + "@" + b.GetString("Muc"), + Text: rmsg.Username + rmsg.Text, + }) + } + + if err != nil {   b.Log.WithError(err).Error("Unable to send message with share URL.")   }   } @@ -10213 +11224 @@ func (b *Bxmpp) Send(msg config.Message) (string, error) {   }   }   + if b.GetString("WebhookURL") != "" { + b.Log.Debugf("Sending message using Webhook") + err := b.postSlackCompatibleWebhook(msg) + if err != nil { + b.Log.Errorf("Failed to send message using webhook: %s", err) + return "", err + } + + return "", nil + } + + // Post normal message.   var msgReplaceID string   msgID := xid.New().String()   if msg.ID != "" {   msgID = msg.ID   msgReplaceID = msg.ID   } - // Post normal message.   b.Log.Debugf("=> Sending message %#v", msg)   if _, err := b.xc.Send(xmpp.Chat{   Type: "groupchat", @@ -1226 +14325 @@ func (b *Bxmpp) Send(msg config.Message) (string, error) {   return msgID, nil  }   +func (b *Bxmpp) postSlackCompatibleWebhook(msg config.Message) error { + type XMPPWebhook struct { + Username string `json:"username"` + Text string `json:"text"` + } + webhookBody, err := json.Marshal(XMPPWebhook{ + Username: msg.Username, + Text: msg.Text, + }) + if err != nil { + b.Log.Errorf("Failed to marshal webhook: %s", err) + return err + } + + resp, err := http.Post(b.GetString("WebhookURL")+"/"+msg.Channel, "application/json", bytes.NewReader(webhookBody)) + resp.Body.Close() + return err +} +  func (b *Bxmpp) createXMPP() error {   if !strings.Contains(b.GetString("Jid"), "@") {   return fmt.Errorf("the Jid %s doesn't contain an @", b.GetString("Jid")) @@ -3786 +41811 @@ func (b *Bxmpp) skipMessage(message xmpp.Chat) bool {   return true   }   + // Ignore messages posted by our webhook + if b.GetString("WebhookURL") != "" && strings.Contains(message.ID, "webhookbot") { + return true + } +   // skip delayed messages   return !message.Stamp.IsZero() && time.Since(message.Stamp).Minutes() > 5  } diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index a92892b..a398961 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -3106 +31011 @@ StripNick=false  #OPTIONAL (default false)  ShowTopicChange=false   +#Enable sending messages using a webhook instead of regular MUC messages. +#Only works with a prosody server using mod_slack_webhook. Does not support editing. +#OPTIONAL (default "") +WebhookURL="https://yourdomain/prosody/msg/someid" +  ###################################################################  #mattermost section  ###################################################################