-
-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
push: allow registering for push notifications
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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": "fcm", | ||
} | ||
} | ||
|
||
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 | ||
} |