From 72892c8fa1f70d8c17297beacea21a94c3acf4c5 Mon Sep 17 00:00:00 2001 From: Almog Baku Date: Fri, 13 Jun 2025 04:37:57 +0300 Subject: [PATCH] fix: normalize lid addresses (#291) * fix: normalize lid addresses * fix: parse lid properly in groups * fix: swapped params * fix: reuse ctx and better error handling * fix: tags lid mapping --- src/infrastructure/whatsapp/webhook.go | 48 ++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/infrastructure/whatsapp/webhook.go b/src/infrastructure/whatsapp/webhook.go index d70a16d..052d818 100644 --- a/src/infrastructure/whatsapp/webhook.go +++ b/src/infrastructure/whatsapp/webhook.go @@ -5,7 +5,10 @@ import ( "context" "encoding/json" "fmt" + "go.mau.fi/whatsmeow/types" "net/http" + "regexp" + "strings" "time" "github.com/aldinokemal/go-whatsapp-web-multidevice/config" @@ -41,8 +44,53 @@ func createPayload(ctx context.Context, evt *events.Message) (map[string]interfa if from := evt.Info.SourceString(); from != "" { body["from"] = from + + from_user, from_group := from, "" + if strings.Contains(from, " in ") { + from_user = strings.Split(from, " in ")[0] + from_group = strings.Split(from, " in ")[1] + } + + if strings.HasSuffix(from_user, "@lid") { + body["from_lid"] = from_user + lid, err := types.ParseJID(from_user) + if err != nil { + logrus.Errorf("Error when parse jid: %v", err) + } else { + pn, err := cli.Store.LIDs.GetPNForLID(ctx, lid) + if err != nil { + logrus.Errorf("Error when get pn for lid %s: %v", lid.String(), err) + } + if !pn.IsEmpty() { + if from_group != "" { + body["from"] = fmt.Sprintf("%s in %s", pn.String(), from_group) + } else { + body["from"] = pn.String() + } + } + } + } } if message.ID != "" { + tags := regexp.MustCompile(`\B@\w+`).FindAllString(message.Text, -1) + tagsMap := make(map[string]bool) + for _, tag := range tags { + tagsMap[tag] = true + } + for tag := range tagsMap { + lid, err := types.ParseJID(tag[1:] + "@lid") + if err != nil { + logrus.Errorf("Error when parse jid: %v", err) + } else { + pn, err := cli.Store.LIDs.GetPNForLID(ctx, lid) + if err != nil { + logrus.Errorf("Error when get pn for lid %s: %v", lid.String(), err) + } + if !pn.IsEmpty() { + message.Text = strings.Replace(message.Text, tag, fmt.Sprintf("@%s", pn.User), -1) + } + } + } body["message"] = message } if pushname := evt.Info.PushName; pushname != "" {