Handle Whatsapp threading/replies.

In this change we are updating whatsapp message IDs to include sender JID as this is necessary to reply to a message
https://github.com/tulir/whatsmeow/issues/88#issuecomment-1093195237
https://github.com/tulir/whatsmeow/discussions/148#discussioncomment-3094325

Based on commit 6afa93e537 from #1934
    Author: Iiro Laiho <iiro.laiho@iki.fi>
This commit is contained in:
Yousef Mansy
2023-02-18 01:15:37 -08:00
parent 24f6747516
commit 39f909b28e
3 changed files with 68 additions and 9 deletions

View File

@@ -1,3 +1,4 @@
//go:build whatsappmulti
// +build whatsappmulti
package bwhatsapp
@@ -93,8 +94,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 {
@@ -126,9 +131,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
}
@@ -189,9 +196,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
}
@@ -238,7 +247,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,
@@ -246,9 +254,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
}
@@ -303,9 +313,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
}

View File

@@ -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)
}

View File

@@ -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
}