From 1dcdac6d6b164925fa9d670b6522f9beb62616be Mon Sep 17 00:00:00 2001 From: Iiro Laiho Date: Thu, 24 Nov 2022 03:17:02 +0200 Subject: [PATCH] Improve attachment handling --- bridge/whatsappmulti/whatsapp.go | 50 ++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/bridge/whatsappmulti/whatsapp.go b/bridge/whatsappmulti/whatsapp.go index 6642fbe8..686396d3 100644 --- a/bridge/whatsappmulti/whatsapp.go +++ b/bridge/whatsappmulti/whatsapp.go @@ -235,7 +235,7 @@ func (b *Bwhatsapp) PostDocumentMessage(msg config.Message, filetype string) (st Url: &resp.URL, } - b.Log.Debugf("=> Sending %#v", msg) + b.Log.Debugf("=> Sending %#v as a document", msg) ID := whatsmeow.GenerateMessageID() _, err = b.wc.SendMessage(context.TODO(), groupJID, ID, &message) @@ -269,7 +269,40 @@ func (b *Bwhatsapp) PostImageMessage(msg config.Message, filetype string) (strin Url: &resp.URL, } - b.Log.Debugf("=> Sending %#v", msg) + b.Log.Debugf("=> Sending %#v as an image", msg) + + ID := whatsmeow.GenerateMessageID() + _, err = b.wc.SendMessage(context.TODO(), groupJID, ID, &message) + + return ID, err +} + +// Post a video message from the bridge to WhatsApp +func (b *Bwhatsapp) PostVideoMessage(msg config.Message, filetype string) (string, error) { + groupJID, _ := types.ParseJID(msg.Channel) + + fi := msg.Extra["file"][0].(config.FileInfo) + + caption := msg.Username + fi.Comment + + resp, err := b.wc.Upload(context.Background(), *fi.Data, whatsmeow.MediaVideo) + if err != nil { + return "", err + } + + var message proto.Message + + message.VideoMessage = &proto.VideoMessage{ + Mimetype: &filetype, + Caption: &caption, + MediaKey: resp.MediaKey, + FileEncSha256: resp.FileEncSHA256, + FileSha256: resp.FileSHA256, + FileLength: goproto.Uint64(resp.FileLength), + Url: &resp.URL, + } + + b.Log.Debugf("=> Sending %#v as a video", msg) ID := whatsmeow.GenerateMessageID() _, err = b.wc.SendMessage(context.TODO(), groupJID, ID, &message) @@ -283,8 +316,6 @@ func (b *Bwhatsapp) PostAudioMessage(msg config.Message, filetype string) (strin fi := msg.Extra["file"][0].(config.FileInfo) - //caption := msg.Username + fi.Comment TODO: Handle audio captions - resp, err := b.wc.Upload(context.Background(), *fi.Data, whatsmeow.MediaAudio) if err != nil { return "", err @@ -301,11 +332,18 @@ func (b *Bwhatsapp) PostAudioMessage(msg config.Message, filetype string) (strin Url: &resp.URL, } - b.Log.Debugf("=> Sending %#v", msg) + b.Log.Debugf("=> Sending %#v as audio", msg) ID := whatsmeow.GenerateMessageID() _, err = b.wc.SendMessage(context.TODO(), groupJID, ID, &message) + var captionMessage proto.Message + caption := msg.Username + fi.Comment + "\u2B06" // the char on the end is upwards arrow emoji + captionMessage.Conversation = &caption + + captionID := whatsmeow.GenerateMessageID() + _, err = b.wc.SendMessage(context.TODO(), groupJID, captionID, &captionMessage) + return ID, err } @@ -351,6 +389,8 @@ func (b *Bwhatsapp) Send(msg config.Message) (string, error) { switch filetype { case "image/jpeg", "image/png", "image/gif": return b.PostImageMessage(msg, filetype) + case "video/mp4", "video/3gpp": //TODO: Check if codecs are supported by WA + return b.PostVideoMessage(msg, filetype) case "audio/ogg": return b.PostAudioMessage(msg, "audio/ogg; codecs=opus") //TODO: Detect if it is actually OPUS case "audio/aac", "audio/mp4", "audio/amr", "audio/mpeg":