Thumbnail

rani/matterbridge.git

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

commit 4e2a6503434fb63d809f1d3cc9f95d18a4787af4 Author: ilmaisin <iiro.laiho@iki.fi> Date: Sun Nov 27 01:55:37 2022 +0000 Improve attachment handling (whatsapp) (#1928) * Allow captions for document attachments, too * Handle audio messages * Improve attachment handling diff --git a/bridge/whatsappmulti/whatsapp.go b/bridge/whatsappmulti/whatsapp.go index 02eadf1..686396d 100644 --- a/bridge/whatsappmulti/whatsapp.go +++ b/bridge/whatsappmulti/whatsapp.go @@ -2136 +2138 @@ func (b *Bwhatsapp) PostDocumentMessage(msg config.Message, filetype string) (st     fi := msg.Extra["file"][0].(config.FileInfo)   + caption := msg.Username + fi.Comment +   resp, err := b.wc.Upload(context.Background(), *fi.Data, whatsmeow.MediaDocument)   if err != nil {   return "", err @@ -2256 +2277 @@ func (b *Bwhatsapp) PostDocumentMessage(msg config.Message, filetype string) (st   Title: &fi.Name,   FileName: &fi.Name,   Mimetype: &filetype, + Caption: &caption,   MediaKey: resp.MediaKey,   FileEncSha256: resp.FileEncSHA256,   FileSha256: resp.FileSHA256, @@ -2327 +2357 @@ 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) @@ -2667 +26940 @@ 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) @@ -2746 +31043 @@ func (b *Bwhatsapp) PostImageMessage(msg config.Message, filetype string) (strin   return ID, err  }   +// Post audio inline +func (b *Bwhatsapp) PostAudioMessage(msg config.Message, filetype string) (string, error) { + groupJID, _ := types.ParseJID(msg.Channel) + + fi := msg.Extra["file"][0].(config.FileInfo) + + resp, err := b.wc.Upload(context.Background(), *fi.Data, whatsmeow.MediaAudio) + if err != nil { + return "", err + } + + var message proto.Message + + message.AudioMessage = &proto.AudioMessage{ + Mimetype: &filetype, + MediaKey: resp.MediaKey, + FileEncSha256: resp.FileEncSHA256, + FileSha256: resp.FileSHA256, + FileLength: goproto.Uint64(resp.FileLength), + Url: &resp.URL, + } + + 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 +} +  // Send a message from the bridge to WhatsApp  func (b *Bwhatsapp) Send(msg config.Message) (string, error) {   groupJID, _ := types.ParseJID(msg.Channel) @@ -3166 +38912 @@ 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": + return b.PostAudioMessage(msg, filetype)   default:   return b.PostDocumentMessage(msg, filetype)   }