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:
@@ -1,3 +1,4 @@
|
|||||||
|
//go:build whatsappmulti
|
||||||
// +build whatsappmulti
|
// +build whatsappmulti
|
||||||
|
|
||||||
package bwhatsapp
|
package bwhatsapp
|
||||||
@@ -93,8 +94,12 @@ func (b *Bwhatsapp) handleTextMessage(messageInfo types.MessageInfo, msg *proto.
|
|||||||
Account: b.Account,
|
Account: b.Account,
|
||||||
Protocol: b.Protocol,
|
Protocol: b.Protocol,
|
||||||
Extra: make(map[string][]interface{}),
|
Extra: make(map[string][]interface{}),
|
||||||
// ParentID: TODO, // TODO handle thread replies // map from Info.QuotedMessageID string
|
ID: getMessageIdFormat(senderJID.String(), messageInfo.ID),
|
||||||
ID: messageInfo.ID,
|
}
|
||||||
|
|
||||||
|
if msg.GetExtendedTextMessage() != nil {
|
||||||
|
ci := msg.GetExtendedTextMessage().GetContextInfo()
|
||||||
|
appendParentID(ci, &rmsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
|
if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
|
||||||
@@ -126,9 +131,11 @@ func (b *Bwhatsapp) handleImageMessage(msg *events.Message) {
|
|||||||
Account: b.Account,
|
Account: b.Account,
|
||||||
Protocol: b.Protocol,
|
Protocol: b.Protocol,
|
||||||
Extra: make(map[string][]interface{}),
|
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 {
|
if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
|
||||||
rmsg.Avatar = avatarURL
|
rmsg.Avatar = avatarURL
|
||||||
}
|
}
|
||||||
@@ -189,9 +196,11 @@ func (b *Bwhatsapp) handleVideoMessage(msg *events.Message) {
|
|||||||
Account: b.Account,
|
Account: b.Account,
|
||||||
Protocol: b.Protocol,
|
Protocol: b.Protocol,
|
||||||
Extra: make(map[string][]interface{}),
|
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 {
|
if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
|
||||||
rmsg.Avatar = avatarURL
|
rmsg.Avatar = avatarURL
|
||||||
}
|
}
|
||||||
@@ -238,7 +247,6 @@ func (b *Bwhatsapp) handleAudioMessage(msg *events.Message) {
|
|||||||
if senderJID == (types.JID{}) && ci.Participant != nil {
|
if senderJID == (types.JID{}) && ci.Participant != nil {
|
||||||
senderJID = types.NewJID(ci.GetParticipant(), types.DefaultUserServer)
|
senderJID = types.NewJID(ci.GetParticipant(), types.DefaultUserServer)
|
||||||
}
|
}
|
||||||
|
|
||||||
rmsg := config.Message{
|
rmsg := config.Message{
|
||||||
UserID: senderJID.String(),
|
UserID: senderJID.String(),
|
||||||
Username: senderName,
|
Username: senderName,
|
||||||
@@ -246,9 +254,11 @@ func (b *Bwhatsapp) handleAudioMessage(msg *events.Message) {
|
|||||||
Account: b.Account,
|
Account: b.Account,
|
||||||
Protocol: b.Protocol,
|
Protocol: b.Protocol,
|
||||||
Extra: make(map[string][]interface{}),
|
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 {
|
if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
|
||||||
rmsg.Avatar = avatarURL
|
rmsg.Avatar = avatarURL
|
||||||
}
|
}
|
||||||
@@ -303,9 +313,11 @@ func (b *Bwhatsapp) handleDocumentMessage(msg *events.Message) {
|
|||||||
Account: b.Account,
|
Account: b.Account,
|
||||||
Protocol: b.Protocol,
|
Protocol: b.Protocol,
|
||||||
Extra: make(map[string][]interface{}),
|
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 {
|
if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
|
||||||
rmsg.Avatar = avatarURL
|
rmsg.Avatar = avatarURL
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/42wim/matterbridge/bridge/config"
|
||||||
|
|
||||||
"go.mau.fi/whatsmeow"
|
"go.mau.fi/whatsmeow"
|
||||||
|
"go.mau.fi/whatsmeow/binary/proto"
|
||||||
"go.mau.fi/whatsmeow/store"
|
"go.mau.fi/whatsmeow/store"
|
||||||
"go.mau.fi/whatsmeow/store/sqlstore"
|
"go.mau.fi/whatsmeow/store/sqlstore"
|
||||||
"go.mau.fi/whatsmeow/types"
|
"go.mau.fi/whatsmeow/types"
|
||||||
@@ -122,3 +125,15 @@ func (b *Bwhatsapp) getDevice() (*store.Device, error) {
|
|||||||
|
|
||||||
return device, nil
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"mime"
|
"mime"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/42wim/matterbridge/bridge"
|
"github.com/42wim/matterbridge/bridge"
|
||||||
@@ -402,12 +403,43 @@ func (b *Bwhatsapp) Send(msg config.Message) (string, error) {
|
|||||||
|
|
||||||
text := msg.Username + msg.Text
|
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
|
var message proto.Message
|
||||||
|
|
||||||
message.Conversation = &text
|
message.Conversation = &text
|
||||||
|
|
||||||
ID := whatsmeow.GenerateMessageID()
|
|
||||||
_, err := b.wc.SendMessage(context.TODO(), groupJID, &message, whatsmeow.SendRequestExtra{ID: ID})
|
_, 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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user