From c9d5f4c8989ddf2465a20aac46537369c654b580 Mon Sep 17 00:00:00 2001 From: Guillaume Lazzara Date: Thu, 9 Jan 2020 18:14:01 +0100 Subject: 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 --- bridge/whatsapp/whatsapp.go | 101 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 85 insertions(+), 16 deletions(-) (limited to 'bridge/whatsapp/whatsapp.go') 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 -- cgit v1.2.3