summaryrefslogtreecommitdiffstats
path: root/bridge/whatsapp
diff options
context:
space:
mode:
authorGuillaume Lazzara <glazzara@gmail.com>2020-01-09 18:14:01 +0100
committerWim <wim@42.be>2020-01-09 18:14:01 +0100
commitc9d5f4c8989ddf2465a20aac46537369c654b580 (patch)
tree4d4ec88f86cd99837029e50cf6a5e29db9243c52 /bridge/whatsapp
parent810c15078158e221585b9a3dda819544432628f5 (diff)
downloadmatterbridge-msglm-c9d5f4c8989ddf2465a20aac46537369c654b580.tar.gz
matterbridge-msglm-c9d5f4c8989ddf2465a20aac46537369c654b580.tar.bz2
matterbridge-msglm-c9d5f4c8989ddf2465a20aac46537369c654b580.zip
Add support for WhatsApp media (jpeg/png/gif) bridging (#974)
* Whatsapp image bridging * Prevent double message in telegram when media with caption received Co-authored-by: imShara <shara@protonmail.com>
Diffstat (limited to 'bridge/whatsapp')
-rw-r--r--bridge/whatsapp/handlers.go103
-rw-r--r--bridge/whatsapp/whatsapp.go101
2 files changed, 170 insertions, 34 deletions
diff --git a/bridge/whatsapp/handlers.go b/bridge/whatsapp/handlers.go
index 3fe15889..2873f0c4 100644
--- a/bridge/whatsapp/handlers.go
+++ b/bridge/whatsapp/handlers.go
@@ -1,10 +1,13 @@
package bwhatsapp
import (
+ "fmt"
+ "mime"
"strings"
"time"
"github.com/42wim/matterbridge/bridge/config"
+ "github.com/42wim/matterbridge/bridge/helper"
"github.com/Rhymen/go-whatsapp"
)
@@ -36,16 +39,16 @@ func (b *Bwhatsapp) HandleTextMessage(message whatsapp.TextMessage) {
}
messageTime := time.Unix(int64(message.Info.Timestamp), 0) // TODO check how behaves between timezones
- groupJid := message.Info.RemoteJid
+ groupJID := message.Info.RemoteJid
- senderJid := message.Info.SenderJid
- if len(senderJid) == 0 {
+ senderJID := message.Info.SenderJid
+ if len(senderJID) == 0 {
// TODO workaround till https://github.com/Rhymen/go-whatsapp/issues/86 resolved
- senderJid = *message.Info.Source.Participant
+ senderJID = *message.Info.Source.Participant
}
- // translate sender's Jid to the nicest username we can get
- senderName := b.getSenderName(senderJid)
+ // translate sender's JID to the nicest username we can get
+ senderName := b.getSenderName(senderJID)
if senderName == "" {
senderName = "Someone" // don't expose telephone number
}
@@ -53,8 +56,8 @@ func (b *Bwhatsapp) HandleTextMessage(message whatsapp.TextMessage) {
extText := message.Info.Source.Message.ExtendedTextMessage
if extText != nil && extText.ContextInfo != nil && extText.ContextInfo.MentionedJid != nil {
// handle user mentions
- for _, mentionedJid := range extText.ContextInfo.MentionedJid {
- numberAndSuffix := strings.SplitN(mentionedJid, "@", 2)
+ for _, mentionedJID := range extText.ContextInfo.MentionedJid {
+ numberAndSuffix := strings.SplitN(mentionedJID, "@", 2)
// mentions comes as telephone numbers and we don't want to expose it to other bridges
// replace it with something more meaninful to others
@@ -66,22 +69,22 @@ func (b *Bwhatsapp) HandleTextMessage(message whatsapp.TextMessage) {
}
}
- b.Log.Debugf("<= Sending message from %s on %s to gateway", senderJid, b.Account)
+ b.Log.Debugf("<= Sending message from %s on %s to gateway", senderJID, b.Account)
rmsg := config.Message{
- UserID: senderJid,
+ UserID: senderJID,
Username: senderName,
Text: message.Text,
Timestamp: messageTime,
- Channel: groupJid,
+ Channel: groupJID,
Account: b.Account,
Protocol: b.Protocol,
Extra: make(map[string][]interface{}),
- // ParentID: TODO, // TODO handle thread replies // map from Info.QuotedMessageID string
+ // ParentID: TODO, // TODO handle thread replies // map from Info.QuotedMessageID string
// Event string `json:"event"`
// Gateway string // will be added during message processing
ID: message.Info.Id}
- if avatarURL, exists := b.userAvatars[senderJid]; exists {
+ if avatarURL, exists := b.userAvatars[senderJID]; exists {
rmsg.Avatar = avatarURL
}
@@ -89,11 +92,75 @@ func (b *Bwhatsapp) HandleTextMessage(message whatsapp.TextMessage) {
b.Remote <- rmsg
}
-//
-//func (b *Bwhatsapp) HandleImageMessage(message whatsapp.ImageMessage) {
-// fmt.Println(message) // TODO implement
-//}
-//
+// HandleImageMessage sent from WhatsApp, relay it to the brige
+func (b *Bwhatsapp) HandleImageMessage(message whatsapp.ImageMessage) {
+ if message.Info.FromMe { // || !strings.Contains(strings.ToLower(message.Text), "@echo") {
+ return
+ }
+
+ // whatsapp sends last messages to show context , cut them
+ if message.Info.Timestamp < b.startedAt {
+ return
+ }
+
+ messageTime := time.Unix(int64(message.Info.Timestamp), 0) // TODO check how behaves between timezones
+ groupJID := message.Info.RemoteJid
+
+ senderJID := message.Info.SenderJid
+ // if len(senderJid) == 0 {
+ // // TODO workaround till https://github.com/Rhymen/go-whatsapp/issues/86 resolved
+ // senderJid = *message.Info.Source.Participant
+ // }
+
+ // translate sender's Jid to the nicest username we can get
+ senderName := b.getSenderName(senderJID)
+ if senderName == "" {
+ senderName = "Someone" // don't expose telephone number
+ }
+
+ b.Log.Debugf("<= Sending message from %s on %s to gateway", senderJID, b.Account)
+ rmsg := config.Message{
+ UserID: senderJID,
+ Username: senderName,
+ Timestamp: messageTime,
+ Channel: groupJID,
+ Account: b.Account,
+ Protocol: b.Protocol,
+ Extra: make(map[string][]interface{}),
+ // ParentID: TODO, // TODO handle thread replies // map from Info.QuotedMessageID string
+ // Event string `json:"event"`
+ // Gateway string // will be added during message processing
+ ID: message.Info.Id}
+
+ if avatarURL, exists := b.userAvatars[senderJID]; exists {
+ rmsg.Avatar = avatarURL
+ }
+
+ // Download and unencrypt content
+ data, err := message.Download()
+ if err != nil {
+ b.Log.Errorf("%v", err)
+ return
+ }
+
+ // Get file extension by mimetype
+ fileExt, err := mime.ExtensionsByType(message.Type)
+ if err != nil {
+ b.Log.Errorf("%v", err)
+ return
+ }
+
+ filename := fmt.Sprintf("%v%v", message.Info.Id, fileExt[0])
+
+ b.Log.Debugf("<= Image downloaded and unencrypted")
+
+ // Move file to bridge storage
+ helper.HandleDownloadData(b.Log, &rmsg, filename, message.Caption, "", &data, b.General)
+
+ b.Log.Debugf("<= Image Message is %#v", rmsg)
+ b.Remote <- rmsg
+}
+
//func (b *Bwhatsapp) HandleVideoMessage(message whatsapp.VideoMessage) {
// fmt.Println(message) // TODO implement
//}
diff --git a/bridge/whatsapp/whatsapp.go b/bridge/whatsapp/whatsapp.go
index 3b97eb25..1a633024 100644
--- a/bridge/whatsapp/whatsapp.go
+++ b/bridge/whatsapp/whatsapp.go
@@ -1,11 +1,14 @@
package bwhatsapp
import (
+ "bytes"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
+ "mime"
"os"
+ "path/filepath"
"strings"
"time"
@@ -230,6 +233,66 @@ func (b *Bwhatsapp) JoinChannel(channel config.ChannelInfo) error {
return nil
}
+// Post a document message from the bridge to WhatsApp
+func (b *Bwhatsapp) PostDocumentMessage(msg config.Message, filetype string) (string, error) {
+ fi := msg.Extra["file"][0].(config.FileInfo)
+
+ // Post document message
+ message := whatsapp.DocumentMessage{
+ Info: whatsapp.MessageInfo{
+ RemoteJid: msg.Channel,
+ },
+ Title: fi.Name,
+ FileName: fi.Name,
+ Type: filetype,
+ Content: bytes.NewReader(*fi.Data),
+ }
+
+ b.Log.Debugf("=> Sending %#v", msg)
+
+ // create message ID
+ // TODO follow and act if https://github.com/Rhymen/go-whatsapp/issues/101 implemented
+ idBytes := make([]byte, 10)
+ if _, err := rand.Read(idBytes); err != nil {
+ b.Log.Warn(err.Error())
+ }
+
+ message.Info.Id = strings.ToUpper(hex.EncodeToString(idBytes))
+ _, err := b.conn.Send(message)
+
+ return message.Info.Id, err
+}
+
+// Post an image message from the bridge to WhatsApp
+// Handle, for sure image/jpeg, image/png and image/gif MIME types
+func (b *Bwhatsapp) PostImageMessage(msg config.Message, filetype string) (string, error) {
+ fi := msg.Extra["file"][0].(config.FileInfo)
+
+ // Post image message
+ message := whatsapp.ImageMessage{
+ Info: whatsapp.MessageInfo{
+ RemoteJid: msg.Channel,
+ },
+ Type: filetype,
+ Caption: msg.Username + fi.Comment,
+ Content: bytes.NewReader(*fi.Data),
+ }
+
+ b.Log.Debugf("=> Sending %#v", msg)
+
+ // create message ID
+ // TODO follow and act if https://github.com/Rhymen/go-whatsapp/issues/101 implemented
+ idBytes := make([]byte, 10)
+ if _, err := rand.Read(idBytes); err != nil {
+ b.Log.Warn(err.Error())
+ }
+
+ message.Info.Id = strings.ToUpper(hex.EncodeToString(idBytes))
+ _, err := b.conn.Send(message)
+
+ return message.Info.Id, err
+}
+
// Send a message from the bridge to WhatsApp
// Required implementation of the Bridger interface
// https://github.com/42wim/matterbridge/blob/2cfd880cdb0df29771bf8f31df8d990ab897889d/bridge/bridge.go#L11-L16
@@ -259,18 +322,25 @@ func (b *Bwhatsapp) Send(msg config.Message) (string, error) {
// TODO handle edit as a message reply with updated text
}
- //// TODO Handle Upload a file
- //if msg.Extra != nil {
- // for _, rmsg := range helper.HandleExtra(&msg, b.General) {
- // b.c.SendMessage(roomID, rmsg.Username+rmsg.Text)
- // }
- // if len(msg.Extra["file"]) > 0 {
- // return b.handleUploadFile(&msg, roomID)
- // }
- //}
+ // Handle Upload a file
+ if msg.Extra["file"] != nil {
+ fi := msg.Extra["file"][0].(config.FileInfo)
+ filetype := mime.TypeByExtension(filepath.Ext(fi.Name))
+
+ b.Log.Debugf("Extra file is %#v", filetype)
+
+ // TODO: add different types
+ // TODO: add webp conversion
+ switch filetype {
+ case "image/jpeg", "image/png", "image/gif":
+ return b.PostImageMessage(msg, filetype)
+ default:
+ return b.PostDocumentMessage(msg, filetype)
+ }
+ }
// Post text message
- text := whatsapp.TextMessage{
+ message := whatsapp.TextMessage{
Info: whatsapp.MessageInfo{
RemoteJid: msg.Channel, // which equals to group id
},
@@ -281,15 +351,14 @@ func (b *Bwhatsapp) Send(msg config.Message) (string, error) {
// create message ID
// TODO follow and act if https://github.com/Rhymen/go-whatsapp/issues/101 implemented
- bytes := make([]byte, 10)
- if _, err := rand.Read(bytes); err != nil {
+ idBytes := make([]byte, 10)
+ if _, err := rand.Read(idBytes); err != nil {
b.Log.Warn(err.Error())
}
- text.Info.Id = strings.ToUpper(hex.EncodeToString(bytes))
-
- _, err := b.conn.Send(text)
+ message.Info.Id = strings.ToUpper(hex.EncodeToString(idBytes))
+ _, err := b.conn.Send(message)
- return text.Info.Id, err
+ return message.Info.Id, err
}
// TODO do we want that? to allow login with QR code from a bridged channel? https://github.com/tulir/mautrix-whatsapp/blob/513eb18e2d59bada0dd515ee1abaaf38a3bfe3d5/commands.go#L76