client: send responses with push notification data

This commit is contained in:
Tulir Asokan 2025-02-27 16:16:06 +02:00
parent b53bcb7ee1
commit 533f3a4e87
2 changed files with 52 additions and 2 deletions

2
go.mod
View file

@ -9,6 +9,7 @@ require (
github.com/gorilla/websocket v1.5.0
github.com/lib/pq v1.10.9
github.com/rs/zerolog v1.33.0
github.com/tidwall/gjson v1.18.0
go.mau.fi/util v0.8.5
go.mau.fi/webp v0.2.0
go.mau.fi/whatsmeow v0.0.0-20250225112721-b7530f3a5056
@ -32,7 +33,6 @@ require (
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/rs/xid v1.6.0 // indirect
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
github.com/tidwall/gjson v1.18.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect

View file

@ -24,7 +24,9 @@ import (
"time"
"github.com/rs/zerolog"
"github.com/tidwall/gjson"
"go.mau.fi/whatsmeow"
waBinary "go.mau.fi/whatsmeow/binary"
"go.mau.fi/whatsmeow/proto/waHistorySync"
"go.mau.fi/whatsmeow/store"
"go.mau.fi/whatsmeow/types"
@ -182,7 +184,7 @@ func (wa *WhatsAppClient) notifyOfflineSyncWaiter(err error) {
}
}
func (wa *WhatsAppClient) ConnectBackground(ctx context.Context, _ *bridgev2.ConnectBackgroundParams) error {
func (wa *WhatsAppClient) ConnectBackground(ctx context.Context, params *bridgev2.ConnectBackgroundParams) error {
if wa.Client == nil {
return bridgev2.ErrNotLoggedIn
}
@ -200,10 +202,58 @@ func (wa *WhatsAppClient) ConnectBackground(ctx context.Context, _ *bridgev2.Con
case <-ctx.Done():
return ctx.Err()
case err = <-wa.offlineSyncWaiter:
if err == nil {
pn := gjson.GetBytes(params.RawData, "data.pn").Str
if pn != "" {
err = wa.sendPNData(ctx, pn)
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("Failed to send PN data")
}
}
}
return err
}
}
func (wa *WhatsAppClient) sendPNData(ctx context.Context, pn string) error {
//lint:ignore SA1019 this is supposed to be dangerous
resp, err := wa.Client.DangerousInternals().SendIQ(whatsmeow.DangerousInfoQuery{
Namespace: "urn:xmpp:whatsapp:push",
Type: "get",
To: types.ServerJID,
Content: []waBinary.Node{{
Tag: "pn",
Content: pn,
}},
Context: ctx,
})
if err != nil {
return fmt.Errorf("failed to send pn: %w", err)
}
cat, ok := resp.GetOptionalChildByTag("cat")
if !ok {
return fmt.Errorf("cat element not found in response")
}
catContentBytes, ok := cat.Content.([]byte)
if !ok {
return fmt.Errorf("cat element content is not a byte slice")
}
zerolog.Ctx(ctx).Debug().Str("cat_data", string(catContentBytes)).Msg("Received cat response from sending pn data")
//lint:ignore SA1019 this is supposed to be dangerous
err = wa.Client.DangerousInternals().SendNode(waBinary.Node{
Tag: "ib",
Content: []waBinary.Node{{
Tag: "cat",
Content: cat.Content,
}},
})
if err != nil {
return fmt.Errorf("failed to broadcast cat: %w", err)
}
zerolog.Ctx(ctx).Debug().Msg("Broadcasted cat from pn data")
return nil
}
func (wa *WhatsAppClient) startLoops() {
ctx, cancel := context.WithCancel(context.Background())
oldStop := wa.stopLoops.Swap(&cancel)