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