diff --git a/bridge/whatsappmulti/handlers.go b/bridge/whatsappmulti/handlers.go index fd75be09b7..1bf0002106 100644 --- a/bridge/whatsappmulti/handlers.go +++ b/bridge/whatsappmulti/handlers.go @@ -1,3 +1,4 @@ +//go:build whatsappmulti // +build whatsappmulti package bwhatsapp @@ -97,8 +98,12 @@ func (b *Bwhatsapp) handleTextMessage(messageInfo types.MessageInfo, msg *proto. Account: b.Account, Protocol: b.Protocol, Extra: make(map[string][]interface{}), - // ParentID: TODO, // TODO handle thread replies // map from Info.QuotedMessageID string - ID: messageInfo.ID, + ID: getMessageIdFormat(senderJID.String(), messageInfo.ID), + } + + if msg.GetExtendedTextMessage() != nil { + ci := msg.GetExtendedTextMessage().GetContextInfo() + appendParentID(ci, &rmsg) } if avatarURL, exists := b.userAvatars[senderJID.String()]; exists { @@ -130,9 +135,11 @@ func (b *Bwhatsapp) handleImageMessage(msg *events.Message) { Account: b.Account, Protocol: b.Protocol, Extra: make(map[string][]interface{}), - ID: msg.Info.ID, + ID: getMessageIdFormat(senderJID.String(), msg.Info.ID), } + appendParentID(ci, &rmsg) + if avatarURL, exists := b.userAvatars[senderJID.String()]; exists { rmsg.Avatar = avatarURL } @@ -193,9 +200,11 @@ func (b *Bwhatsapp) handleVideoMessage(msg *events.Message) { Account: b.Account, Protocol: b.Protocol, Extra: make(map[string][]interface{}), - ID: msg.Info.ID, + ID: getMessageIdFormat(senderJID.String(), msg.Info.ID), } + appendParentID(ci, &rmsg) + if avatarURL, exists := b.userAvatars[senderJID.String()]; exists { rmsg.Avatar = avatarURL } @@ -251,7 +260,6 @@ func (b *Bwhatsapp) handleAudioMessage(msg *events.Message) { if senderJID == (types.JID{}) && ci.Participant != nil { senderJID = types.NewJID(ci.GetParticipant(), types.DefaultUserServer) } - rmsg := config.Message{ UserID: senderJID.String(), Username: senderName, @@ -259,9 +267,11 @@ func (b *Bwhatsapp) handleAudioMessage(msg *events.Message) { Account: b.Account, Protocol: b.Protocol, Extra: make(map[string][]interface{}), - ID: msg.Info.ID, + ID: getMessageIdFormat(senderJID.String(), msg.Info.ID), } + appendParentID(ci, &rmsg) + if avatarURL, exists := b.userAvatars[senderJID.String()]; exists { rmsg.Avatar = avatarURL } @@ -316,9 +326,11 @@ func (b *Bwhatsapp) handleDocumentMessage(msg *events.Message) { Account: b.Account, Protocol: b.Protocol, Extra: make(map[string][]interface{}), - ID: msg.Info.ID, + ID: getMessageIdFormat(senderJID.String(), msg.Info.ID), } + appendParentID(ci, &rmsg) + if avatarURL, exists := b.userAvatars[senderJID.String()]; exists { rmsg.Avatar = avatarURL } diff --git a/bridge/whatsappmulti/helpers.go b/bridge/whatsappmulti/helpers.go index 75d9f5fdd1..513286f0ae 100644 --- a/bridge/whatsappmulti/helpers.go +++ b/bridge/whatsappmulti/helpers.go @@ -7,7 +7,10 @@ import ( "fmt" "strings" + "github.com/42wim/matterbridge/bridge/config" + "go.mau.fi/whatsmeow" + "go.mau.fi/whatsmeow/binary/proto" "go.mau.fi/whatsmeow/store" "go.mau.fi/whatsmeow/store/sqlstore" "go.mau.fi/whatsmeow/types" @@ -122,3 +125,15 @@ func (b *Bwhatsapp) getDevice() (*store.Device, error) { return device, nil } + +func appendParentID(ci *proto.ContextInfo, rmsg *config.Message) { + + if ci != nil && ci.StanzaId != nil { + // rmsg.ParentID = *ci.StanzaId + rmsg.ParentID = getMessageIdFormat(*ci.Participant, *ci.StanzaId) + } +} + +func getMessageIdFormat(authorJID string, messageID string) string { + return fmt.Sprintf("%s:%s", authorJID, messageID) +} diff --git a/bridge/whatsappmulti/whatsapp.go b/bridge/whatsappmulti/whatsapp.go index 76e4ae4680..e606e621ff 100644 --- a/bridge/whatsappmulti/whatsapp.go +++ b/bridge/whatsappmulti/whatsapp.go @@ -10,6 +10,7 @@ import ( "mime" "os" "path/filepath" + "strings" "time" "github.com/42wim/matterbridge/bridge" @@ -402,12 +403,43 @@ func (b *Bwhatsapp) Send(msg config.Message) (string, error) { text := msg.Username + msg.Text + ID := whatsmeow.GenerateMessageID() + + // If we have a parent ID send an extended message + if msg.ParentID != "" { + // in appendParentID() we combine the "Participant" and the "StanzaID" as both are needed to send a reply + replyInfo := strings.Split(msg.ParentID, ":") + + if len(replyInfo) != 2 { + b.Log.Debug("Malformed reply info to whatsapp: %s", msg.ParentID) + } else { + // Send message with reply (not working) + // https://github.com/tulir/whatsmeow/issues/88#issuecomment-1093195237 + _, err := b.wc.SendMessage( + context.Background(), + groupJID, + &proto.Message{ + ExtendedTextMessage: &proto.ExtendedTextMessage{ + Text: &text, + ContextInfo: &proto.ContextInfo{ + StanzaId: &replyInfo[1], + Participant: &replyInfo[0], + QuotedMessage: &proto.Message{Conversation: goproto.String("")}, + }, + }, + }, + whatsmeow.SendRequestExtra{ID: ID}, + ) + + return getMessageIdFormat(b.Config.GetString("Number")[1:]+"@s.whatsapp.net", ID), err + } + } + var message proto.Message message.Conversation = &text - ID := whatsmeow.GenerateMessageID() _, err := b.wc.SendMessage(context.TODO(), groupJID, &message, whatsmeow.SendRequestExtra{ID: ID}) - return ID, err + return getMessageIdFormat(b.Config.GetString("Number")[1:]+"@s.whatsapp.net", ID), err }