-
-
Notifications
You must be signed in to change notification settings - Fork 430
/
push.go
74 lines (64 loc) · 1.87 KB
/
push.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) 2024 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package whatsmeow
import (
"context"
"encoding/base64"
waBinary "go.mau.fi/whatsmeow/binary"
"go.mau.fi/whatsmeow/types"
)
type PushConfig interface {
GetPushConfigAttrs() waBinary.Attrs
}
type FCMPushConfig struct {
Token string `json:"token"`
}
func (fpc *FCMPushConfig) GetPushConfigAttrs() waBinary.Attrs {
return waBinary.Attrs{
"id": fpc.Token,
"num_acc": 1,
"platform": "gcm",
}
}
type WebPushConfig struct {
Endpoint string `json:"endpoint"`
Auth []byte `json:"auth"`
P256DH []byte `json:"p256dh"`
}
func (wpc *WebPushConfig) GetPushConfigAttrs() waBinary.Attrs {
return waBinary.Attrs{
"platform": "web",
"endpoint": wpc.Endpoint,
"auth": base64.StdEncoding.EncodeToString(wpc.Auth),
"p256dh": base64.StdEncoding.EncodeToString(wpc.P256DH),
}
}
func (cli *Client) GetServerPushNotificationConfig(ctx context.Context) (*waBinary.Node, error) {
resp, err := cli.sendIQ(infoQuery{
Namespace: "urn:xmpp:whatsapp:push",
Type: iqGet,
To: types.ServerJID,
Content: []waBinary.Node{{Tag: "settings"}},
Context: ctx,
})
return resp, err
}
// RegisterForPushNotifications registers a token to receive push notifications for new WhatsApp messages.
//
// This is generally not necessary for anything. Don't use this if you don't know what you're doing.
func (cli *Client) RegisterForPushNotifications(ctx context.Context, pc PushConfig) error {
_, err := cli.sendIQ(infoQuery{
Namespace: "urn:xmpp:whatsapp:push",
Type: iqSet,
To: types.ServerJID,
Content: []waBinary.Node{{
Tag: "config",
Attrs: pc.GetPushConfigAttrs(),
}},
Context: ctx,
})
return err
}