Skip to content

Commit

Permalink
Handle Whatsapp threading/replies.
Browse files Browse the repository at this point in the history
In this change we are updating whatsapp message IDs to include sender JID as this is necessary to reply to a message
tulir/whatsmeow#88 (comment)
tulir/whatsmeow#148 (comment)

Based on commit 6afa93e from 42wim#1934
    Author: Iiro Laiho <[email protected]>
  • Loading branch information
yousefmansy1 committed Feb 22, 2023
1 parent 476205c commit 4a1308d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
26 changes: 19 additions & 7 deletions bridge/whatsappmulti/handlers.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build whatsappmulti
// +build whatsappmulti

package bwhatsapp
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -251,17 +260,18 @@ 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,
Channel: msg.Info.Chat.String(),
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
}
Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 15 additions & 0 deletions bridge/whatsappmulti/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
36 changes: 34 additions & 2 deletions bridge/whatsappmulti/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"mime"
"os"
"path/filepath"
"strings"
"time"

"github.com/42wim/matterbridge/bridge"
Expand Down Expand Up @@ -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
}

0 comments on commit 4a1308d

Please sign in to comment.