Browse Source

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
pull/301/head
Almog Baku 9 months ago
committed by GitHub
parent
commit
72892c8fa1
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 48
      src/infrastructure/whatsapp/webhook.go

48
src/infrastructure/whatsapp/webhook.go

@ -5,7 +5,10 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"go.mau.fi/whatsmeow/types"
"net/http" "net/http"
"regexp"
"strings"
"time" "time"
"github.com/aldinokemal/go-whatsapp-web-multidevice/config" "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 != "" { if from := evt.Info.SourceString(); from != "" {
body["from"] = 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 != "" { 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 body["message"] = message
} }
if pushname := evt.Info.PushName; pushname != "" { if pushname := evt.Info.PushName; pushname != "" {

Loading…
Cancel
Save