@skyzopedia/baileys-pro 8.0.1 → 8.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -117
- package/README.md.bak +1223 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,117 +4,10 @@ Then import the default function in your code:
|
|
|
4
4
|
import makeWASocket from 'baileys'
|
|
5
5
|
```
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
- [
|
|
10
|
-
- [
|
|
11
|
-
- [WhatsApp](https://whatsapp.com/channel/0029VbBcgCK1SWstUXZnLh1o)
|
|
12
|
-
- [Telegram](https://t.me/Skyzopedia)
|
|
13
|
-
|
|
14
|
-
# Index
|
|
15
|
-
|
|
16
|
-
- [Connecting Account](#connecting-account)
|
|
17
|
-
- [Connect with QR-CODE](#starting-socket-with-qr-code)
|
|
18
|
-
- [Connect with Pairing Code](#starting-socket-with-pairing-code)
|
|
19
|
-
- [Receive Full History](#receive-full-history)
|
|
20
|
-
- [Important Notes About Socket Config](#important-notes-about-socket-config)
|
|
21
|
-
- [Caching Group Metadata (Recommended)](#caching-group-metadata-recommended)
|
|
22
|
-
- [Improve Retry System & Decrypt Poll Votes](#improve-retry-system--decrypt-poll-votes)
|
|
23
|
-
- [Receive Notifications in Whatsapp App](#receive-notifications-in-whatsapp-app)
|
|
24
|
-
|
|
25
|
-
- [Save Auth Info](#saving--restoring-sessions)
|
|
26
|
-
- [Handling Events](#handling-events)
|
|
27
|
-
- [Example to Start](#example-to-start)
|
|
28
|
-
- [Decrypt Poll Votes](#decrypt-poll-votes)
|
|
29
|
-
- [Summary of Events on First Connection](#summary-of-events-on-first-connection)
|
|
30
|
-
- [Implementing a Data Store](#implementing-a-data-store)
|
|
31
|
-
- [Whatsapp IDs Explain](#whatsapp-ids-explain)
|
|
32
|
-
- [Utility Functions](#utility-functions)
|
|
33
|
-
- [Sending Messages](#sending-messages)
|
|
34
|
-
- [Non-Media Messages](#non-media-messages)
|
|
35
|
-
- [Text Message](#text-message)
|
|
36
|
-
- [Quote Message](#quote-message-works-with-all-types)
|
|
37
|
-
- [Mention User](#mention-user-works-with-most-types)
|
|
38
|
-
- [Forward Messages](#forward-messages)
|
|
39
|
-
- [Location Message](#location-message)
|
|
40
|
-
- [Contact Message](#contact-message)
|
|
41
|
-
- [Reaction Message](#reaction-message)
|
|
42
|
-
- [Pin Message](#pin-message)
|
|
43
|
-
- [Poll Message](#poll-message)
|
|
44
|
-
- [Sending with Link Preview](#sending-messages-with-link-previews)
|
|
45
|
-
- [Media Messages](#media-messages)
|
|
46
|
-
- [Gif Message](#gif-message)
|
|
47
|
-
- [Video Message](#video-message)
|
|
48
|
-
- [Audio Message](#audio-message)
|
|
49
|
-
- [Image Message](#image-message)
|
|
50
|
-
- [ViewOnce Message](#view-once-message)
|
|
51
|
-
- [Modify Messages](#modify-messages)
|
|
52
|
-
- [Delete Messages (for everyone)](#deleting-messages-for-everyone)
|
|
53
|
-
- [Edit Messages](#editing-messages)
|
|
54
|
-
- [Manipulating Media Messages](#manipulating-media-messages)
|
|
55
|
-
- [Thumbnail in Media Messages](#thumbnail-in-media-messages)
|
|
56
|
-
- [Downloading Media Messages](#downloading-media-messages)
|
|
57
|
-
- [Re-upload Media Message to Whatsapp](#re-upload-media-message-to-whatsapp)
|
|
58
|
-
- [Reject Call](#reject-call)
|
|
59
|
-
- [Send States in Chat](#send-states-in-chat)
|
|
60
|
-
- [Reading Messages](#reading-messages)
|
|
61
|
-
- [Update Presence](#update-presence)
|
|
62
|
-
- [Modifying Chats](#modifying-chats)
|
|
63
|
-
- [Archive a Chat](#archive-a-chat)
|
|
64
|
-
- [Mute/Unmute a Chat](#muteunmute-a-chat)
|
|
65
|
-
- [Mark a Chat Read/Unread](#mark-a-chat-readunread)
|
|
66
|
-
- [Delete a Message for Me](#delete-a-message-for-me)
|
|
67
|
-
- [Delete a Chat](#delete-a-chat)
|
|
68
|
-
- [Star/Unstar a Message](#starunstar-a-message)
|
|
69
|
-
- [Disappearing Messages](#disappearing-messages)
|
|
70
|
-
- [User Querys](#user-querys)
|
|
71
|
-
- [Check If ID Exists in Whatsapp](#check-if-id-exists-in-whatsapp)
|
|
72
|
-
- [Query Chat History (groups too)](#query-chat-history-groups-too)
|
|
73
|
-
- [Fetch Status](#fetch-status)
|
|
74
|
-
- [Fetch Profile Picture (groups too)](#fetch-profile-picture-groups-too)
|
|
75
|
-
- [Fetch Bussines Profile (such as description or category)](#fetch-bussines-profile-such-as-description-or-category)
|
|
76
|
-
- [Fetch Someone's Presence (if they're typing or online)](#fetch-someones-presence-if-theyre-typing-or-online)
|
|
77
|
-
- [Change Profile](#change-profile)
|
|
78
|
-
- [Change Profile Status](#change-profile-status)
|
|
79
|
-
- [Change Profile Name](#change-profile-name)
|
|
80
|
-
- [Change Display Picture (groups too)](#change-display-picture-groups-too)
|
|
81
|
-
- [Remove display picture (groups too)](#remove-display-picture-groups-too)
|
|
82
|
-
- [Groups](#groups)
|
|
83
|
-
- [Create a Group](#create-a-group)
|
|
84
|
-
- [Add/Remove or Demote/Promote](#addremove-or-demotepromote)
|
|
85
|
-
- [Change Subject (name)](#change-subject-name)
|
|
86
|
-
- [Change Description](#change-description)
|
|
87
|
-
- [Change Settings](#change-settings)
|
|
88
|
-
- [Leave a Group](#leave-a-group)
|
|
89
|
-
- [Get Invite Code](#get-invite-code)
|
|
90
|
-
- [Revoke Invite Code](#revoke-invite-code)
|
|
91
|
-
- [Join Using Invitation Code](#join-using-invitation-code)
|
|
92
|
-
- [Get Group Info by Invite Code](#get-group-info-by-invite-code)
|
|
93
|
-
- [Query Metadata (participants, name, description...)](#query-metadata-participants-name-description)
|
|
94
|
-
- [Join using groupInviteMessage](#join-using-groupinvitemessage)
|
|
95
|
-
- [Get Request Join List](#get-request-join-list)
|
|
96
|
-
- [Approve/Reject Request Join](#approvereject-request-join)
|
|
97
|
-
- [Get All Participating Groups Metadata](#get-all-participating-groups-metadata)
|
|
98
|
-
- [Toggle Ephemeral](#toggle-ephemeral)
|
|
99
|
-
- [Change Add Mode](#change-add-mode)
|
|
100
|
-
- [Privacy](#privacy)
|
|
101
|
-
- [Block/Unblock User](#blockunblock-user)
|
|
102
|
-
- [Get Privacy Settings](#get-privacy-settings)
|
|
103
|
-
- [Get BlockList](#get-blocklist)
|
|
104
|
-
- [Update LastSeen Privacy](#update-lastseen-privacy)
|
|
105
|
-
- [Update Online Privacy](#update-online-privacy)
|
|
106
|
-
- [Update Profile Picture Privacy](#update-profile-picture-privacy)
|
|
107
|
-
- [Update Status Privacy](#update-status-privacy)
|
|
108
|
-
- [Update Read Receipts Privacy](#update-read-receipts-privacy)
|
|
109
|
-
- [Update Groups Add Privacy](#update-groups-add-privacy)
|
|
110
|
-
- [Update Default Disappearing Mode](#update-default-disappearing-mode)
|
|
111
|
-
- [Broadcast Lists & Stories](#broadcast-lists--stories)
|
|
112
|
-
- [Send Broadcast & Stories](#send-broadcast--stories)
|
|
113
|
-
- [Query a Broadcast List's Recipients & Name](#query-a-broadcast-lists-recipients--name)
|
|
114
|
-
- [Writing Custom Functionality](#writing-custom-functionality)
|
|
115
|
-
- [Enabling Debug Level in Baileys Logs](#enabling-debug-level-in-baileys-logs)
|
|
116
|
-
- [How Whatsapp Communicate With Us](#how-whatsapp-communicate-with-us)
|
|
117
|
-
- [Register a Callback for Websocket Events](#register-a-callback-for-websocket-events)
|
|
7
|
+
## Links
|
|
8
|
+
|
|
9
|
+
- [WhatsApp Channel](https://whatsapp.com/channel/0029VbBcgCK1SWstUXZnLh1o)
|
|
10
|
+
- [Contact Developer](https://t.me/Skyzopedia)
|
|
118
11
|
|
|
119
12
|
## Connecting Account
|
|
120
13
|
|
|
@@ -401,13 +294,13 @@ const standardJid = sock.toPn(lidJid)
|
|
|
401
294
|
|
|
402
295
|
## Sending Messages
|
|
403
296
|
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
297
|
+
```ts
|
|
298
|
+
const jid: string
|
|
299
|
+
const content: AnyMessageContent
|
|
300
|
+
const options: MiscMessageGenerationOptions
|
|
408
301
|
|
|
409
|
-
|
|
410
|
-
|
|
302
|
+
sock.sendMessage(jid, content, options)
|
|
303
|
+
```
|
|
411
304
|
|
|
412
305
|
### Non-Media Messages
|
|
413
306
|
|
package/README.md.bak
ADDED
|
@@ -0,0 +1,1223 @@
|
|
|
1
|
+
# <div align='center'>Baileys - Typescript/Javascript WhatsApp Web API</div>
|
|
2
|
+
Then import the default function in your code:
|
|
3
|
+
```ts
|
|
4
|
+
import makeWASocket from 'baileys'
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
# Links
|
|
8
|
+
|
|
9
|
+
- [Discord](https://discord.gg/WeJM5FP9GG)
|
|
10
|
+
- [Docs](https://guide.whiskeysockets.io/)
|
|
11
|
+
- [WhatsApp](https://whatsapp.com/channel/0029VbBcgCK1SWstUXZnLh1o)
|
|
12
|
+
- [Telegram](https://t.me/Skyzopedia)
|
|
13
|
+
|
|
14
|
+
# Index
|
|
15
|
+
|
|
16
|
+
- [Connecting Account](#connecting-account)
|
|
17
|
+
- [Connect with QR-CODE](#starting-socket-with-qr-code)
|
|
18
|
+
- [Connect with Pairing Code](#starting-socket-with-pairing-code)
|
|
19
|
+
- [Receive Full History](#receive-full-history)
|
|
20
|
+
- [Important Notes About Socket Config](#important-notes-about-socket-config)
|
|
21
|
+
- [Caching Group Metadata (Recommended)](#caching-group-metadata-recommended)
|
|
22
|
+
- [Improve Retry System & Decrypt Poll Votes](#improve-retry-system--decrypt-poll-votes)
|
|
23
|
+
- [Receive Notifications in Whatsapp App](#receive-notifications-in-whatsapp-app)
|
|
24
|
+
|
|
25
|
+
- [Save Auth Info](#saving--restoring-sessions)
|
|
26
|
+
- [Handling Events](#handling-events)
|
|
27
|
+
- [Example to Start](#example-to-start)
|
|
28
|
+
- [Decrypt Poll Votes](#decrypt-poll-votes)
|
|
29
|
+
- [Summary of Events on First Connection](#summary-of-events-on-first-connection)
|
|
30
|
+
- [Implementing a Data Store](#implementing-a-data-store)
|
|
31
|
+
- [Whatsapp IDs Explain](#whatsapp-ids-explain)
|
|
32
|
+
- [Utility Functions](#utility-functions)
|
|
33
|
+
- [Sending Messages](#sending-messages)
|
|
34
|
+
- [Non-Media Messages](#non-media-messages)
|
|
35
|
+
- [Text Message](#text-message)
|
|
36
|
+
- [Quote Message](#quote-message-works-with-all-types)
|
|
37
|
+
- [Mention User](#mention-user-works-with-most-types)
|
|
38
|
+
- [Forward Messages](#forward-messages)
|
|
39
|
+
- [Location Message](#location-message)
|
|
40
|
+
- [Contact Message](#contact-message)
|
|
41
|
+
- [Reaction Message](#reaction-message)
|
|
42
|
+
- [Pin Message](#pin-message)
|
|
43
|
+
- [Poll Message](#poll-message)
|
|
44
|
+
- [Sending with Link Preview](#sending-messages-with-link-previews)
|
|
45
|
+
- [Media Messages](#media-messages)
|
|
46
|
+
- [Gif Message](#gif-message)
|
|
47
|
+
- [Video Message](#video-message)
|
|
48
|
+
- [Audio Message](#audio-message)
|
|
49
|
+
- [Image Message](#image-message)
|
|
50
|
+
- [ViewOnce Message](#view-once-message)
|
|
51
|
+
- [Modify Messages](#modify-messages)
|
|
52
|
+
- [Delete Messages (for everyone)](#deleting-messages-for-everyone)
|
|
53
|
+
- [Edit Messages](#editing-messages)
|
|
54
|
+
- [Manipulating Media Messages](#manipulating-media-messages)
|
|
55
|
+
- [Thumbnail in Media Messages](#thumbnail-in-media-messages)
|
|
56
|
+
- [Downloading Media Messages](#downloading-media-messages)
|
|
57
|
+
- [Re-upload Media Message to Whatsapp](#re-upload-media-message-to-whatsapp)
|
|
58
|
+
- [Reject Call](#reject-call)
|
|
59
|
+
- [Send States in Chat](#send-states-in-chat)
|
|
60
|
+
- [Reading Messages](#reading-messages)
|
|
61
|
+
- [Update Presence](#update-presence)
|
|
62
|
+
- [Modifying Chats](#modifying-chats)
|
|
63
|
+
- [Archive a Chat](#archive-a-chat)
|
|
64
|
+
- [Mute/Unmute a Chat](#muteunmute-a-chat)
|
|
65
|
+
- [Mark a Chat Read/Unread](#mark-a-chat-readunread)
|
|
66
|
+
- [Delete a Message for Me](#delete-a-message-for-me)
|
|
67
|
+
- [Delete a Chat](#delete-a-chat)
|
|
68
|
+
- [Star/Unstar a Message](#starunstar-a-message)
|
|
69
|
+
- [Disappearing Messages](#disappearing-messages)
|
|
70
|
+
- [User Querys](#user-querys)
|
|
71
|
+
- [Check If ID Exists in Whatsapp](#check-if-id-exists-in-whatsapp)
|
|
72
|
+
- [Query Chat History (groups too)](#query-chat-history-groups-too)
|
|
73
|
+
- [Fetch Status](#fetch-status)
|
|
74
|
+
- [Fetch Profile Picture (groups too)](#fetch-profile-picture-groups-too)
|
|
75
|
+
- [Fetch Bussines Profile (such as description or category)](#fetch-bussines-profile-such-as-description-or-category)
|
|
76
|
+
- [Fetch Someone's Presence (if they're typing or online)](#fetch-someones-presence-if-theyre-typing-or-online)
|
|
77
|
+
- [Change Profile](#change-profile)
|
|
78
|
+
- [Change Profile Status](#change-profile-status)
|
|
79
|
+
- [Change Profile Name](#change-profile-name)
|
|
80
|
+
- [Change Display Picture (groups too)](#change-display-picture-groups-too)
|
|
81
|
+
- [Remove display picture (groups too)](#remove-display-picture-groups-too)
|
|
82
|
+
- [Groups](#groups)
|
|
83
|
+
- [Create a Group](#create-a-group)
|
|
84
|
+
- [Add/Remove or Demote/Promote](#addremove-or-demotepromote)
|
|
85
|
+
- [Change Subject (name)](#change-subject-name)
|
|
86
|
+
- [Change Description](#change-description)
|
|
87
|
+
- [Change Settings](#change-settings)
|
|
88
|
+
- [Leave a Group](#leave-a-group)
|
|
89
|
+
- [Get Invite Code](#get-invite-code)
|
|
90
|
+
- [Revoke Invite Code](#revoke-invite-code)
|
|
91
|
+
- [Join Using Invitation Code](#join-using-invitation-code)
|
|
92
|
+
- [Get Group Info by Invite Code](#get-group-info-by-invite-code)
|
|
93
|
+
- [Query Metadata (participants, name, description...)](#query-metadata-participants-name-description)
|
|
94
|
+
- [Join using groupInviteMessage](#join-using-groupinvitemessage)
|
|
95
|
+
- [Get Request Join List](#get-request-join-list)
|
|
96
|
+
- [Approve/Reject Request Join](#approvereject-request-join)
|
|
97
|
+
- [Get All Participating Groups Metadata](#get-all-participating-groups-metadata)
|
|
98
|
+
- [Toggle Ephemeral](#toggle-ephemeral)
|
|
99
|
+
- [Change Add Mode](#change-add-mode)
|
|
100
|
+
- [Privacy](#privacy)
|
|
101
|
+
- [Block/Unblock User](#blockunblock-user)
|
|
102
|
+
- [Get Privacy Settings](#get-privacy-settings)
|
|
103
|
+
- [Get BlockList](#get-blocklist)
|
|
104
|
+
- [Update LastSeen Privacy](#update-lastseen-privacy)
|
|
105
|
+
- [Update Online Privacy](#update-online-privacy)
|
|
106
|
+
- [Update Profile Picture Privacy](#update-profile-picture-privacy)
|
|
107
|
+
- [Update Status Privacy](#update-status-privacy)
|
|
108
|
+
- [Update Read Receipts Privacy](#update-read-receipts-privacy)
|
|
109
|
+
- [Update Groups Add Privacy](#update-groups-add-privacy)
|
|
110
|
+
- [Update Default Disappearing Mode](#update-default-disappearing-mode)
|
|
111
|
+
- [Broadcast Lists & Stories](#broadcast-lists--stories)
|
|
112
|
+
- [Send Broadcast & Stories](#send-broadcast--stories)
|
|
113
|
+
- [Query a Broadcast List's Recipients & Name](#query-a-broadcast-lists-recipients--name)
|
|
114
|
+
- [Writing Custom Functionality](#writing-custom-functionality)
|
|
115
|
+
- [Enabling Debug Level in Baileys Logs](#enabling-debug-level-in-baileys-logs)
|
|
116
|
+
- [How Whatsapp Communicate With Us](#how-whatsapp-communicate-with-us)
|
|
117
|
+
- [Register a Callback for Websocket Events](#register-a-callback-for-websocket-events)
|
|
118
|
+
|
|
119
|
+
## Connecting Account
|
|
120
|
+
|
|
121
|
+
WhatsApp provides a multi-device API that allows Baileys to be authenticated as a second WhatsApp client by scanning a **QR code** or **Pairing Code** with WhatsApp on your phone.
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
import makeWASocket from 'baileys'
|
|
125
|
+
|
|
126
|
+
const sock = makeWASocket({
|
|
127
|
+
// can provide additional config here
|
|
128
|
+
browser: Browsers.ubuntu('My App'),
|
|
129
|
+
printQRInTerminal: true
|
|
130
|
+
})
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
If the connection is successful, you will see a QR code printed on your terminal screen, scan it with WhatsApp on your phone and you'll be logged in!
|
|
134
|
+
|
|
135
|
+
### Starting socket with **Pairing Code**
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
> [!IMPORTANT]
|
|
139
|
+
> Pairing Code isn't Mobile API, it's a method to connect Whatsapp Web without QR-CODE, you can connect only with one device, see [here](https://faq.whatsapp.com/1324084875126592/?cms_platform=web)
|
|
140
|
+
|
|
141
|
+
The phone number can't have `+` or `()` or `-`, only numbers, you must provide country code
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
import makeWASocket from 'baileys'
|
|
145
|
+
|
|
146
|
+
const sock = makeWASocket({
|
|
147
|
+
// can provide additional config here
|
|
148
|
+
printQRInTerminal: false //need to be false
|
|
149
|
+
})
|
|
150
|
+
// NOTE: WAIT TILL QR EVENT BEFORE REQUESTING THE PAIRING CODE
|
|
151
|
+
if (!sock.authState.creds.registered) {
|
|
152
|
+
const number = 'XXXXXXXXXXX'
|
|
153
|
+
const code = await sock.requestPairingCode(number)
|
|
154
|
+
console.log(code)
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Receive Full History
|
|
159
|
+
|
|
160
|
+
1. Set `syncFullHistory` as `true`
|
|
161
|
+
2. Baileys, by default, use chrome browser config
|
|
162
|
+
- If you'd like to emulate a desktop connection (and receive more message history), this browser setting to your Socket config:
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
const sock = makeWASocket({
|
|
166
|
+
...otherOpts,
|
|
167
|
+
// can use Windows, Ubuntu here too
|
|
168
|
+
browser: Browsers.macOS('Desktop'),
|
|
169
|
+
syncFullHistory: true
|
|
170
|
+
})
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Important Notes About Socket Config
|
|
174
|
+
|
|
175
|
+
### Caching Group Metadata (Recommended)
|
|
176
|
+
- If you use baileys for groups, we recommend you to set `cachedGroupMetadata` in socket config, you need to implement a cache like this:
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
const groupCache = new NodeCache({stdTTL: 5 * 60, useClones: false})
|
|
180
|
+
|
|
181
|
+
const sock = makeWASocket({
|
|
182
|
+
cachedGroupMetadata: async (jid) => groupCache.get(jid)
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
sock.ev.on('groups.update', async ([event]) => {
|
|
186
|
+
const metadata = await sock.groupMetadata(event.id)
|
|
187
|
+
groupCache.set(event.id, metadata)
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
sock.ev.on('group-participants.update', async (event) => {
|
|
191
|
+
const metadata = await sock.groupMetadata(event.id)
|
|
192
|
+
groupCache.set(event.id, metadata)
|
|
193
|
+
})
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### Improve Retry System & Decrypt Poll Votes
|
|
197
|
+
- If you want to improve sending message, retrying when error occurs and decrypt poll votes, you need to have a store and set `getMessage` config in socket like this:
|
|
198
|
+
```ts
|
|
199
|
+
const sock = makeWASocket({
|
|
200
|
+
getMessage: async (key) => await getMessageFromStore(key)
|
|
201
|
+
})
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### Receive Notifications in Whatsapp App
|
|
205
|
+
- If you want to receive notifications in whatsapp app, set `markOnlineOnConnect` to `false`
|
|
206
|
+
```ts
|
|
207
|
+
const sock = makeWASocket({
|
|
208
|
+
markOnlineOnConnect: false
|
|
209
|
+
})
|
|
210
|
+
```
|
|
211
|
+
## Saving & Restoring Sessions
|
|
212
|
+
|
|
213
|
+
You obviously don't want to keep scanning the QR code every time you want to connect.
|
|
214
|
+
|
|
215
|
+
So, you can load the credentials to log back in:
|
|
216
|
+
```ts
|
|
217
|
+
import makeWASocket, { useMultiFileAuthState } from 'baileys'
|
|
218
|
+
|
|
219
|
+
const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys')
|
|
220
|
+
|
|
221
|
+
// will use the given state to connect
|
|
222
|
+
// so if valid credentials are available -- it'll connect without QR
|
|
223
|
+
const sock = makeWASocket({ auth: state })
|
|
224
|
+
|
|
225
|
+
// this will be called as soon as the credentials are updated
|
|
226
|
+
sock.ev.on('creds.update', saveCreds)
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
> [!IMPORTANT]
|
|
230
|
+
> `useMultiFileAuthState` is a utility function to help save the auth state in a single folder, this function serves as a good guide to help write auth & key states for SQL/no-SQL databases, which I would recommend in any production grade system.
|
|
231
|
+
|
|
232
|
+
> [!NOTE]
|
|
233
|
+
> When a message is received/sent, due to signal sessions needing updating, the auth keys (`authState.keys`) will update. Whenever that happens, you must save the updated keys (`authState.keys.set()` is called). Not doing so will prevent your messages from reaching the recipient & cause other unexpected consequences. The `useMultiFileAuthState` function automatically takes care of that, but for any other serious implementation -- you will need to be very careful with the key state management.
|
|
234
|
+
|
|
235
|
+
## Handling Events
|
|
236
|
+
|
|
237
|
+
- Baileys uses the EventEmitter syntax for events.
|
|
238
|
+
They're all nicely typed up, so you shouldn't have any issues with an Intellisense editor like VS Code.
|
|
239
|
+
|
|
240
|
+
You can listen to these events like this:
|
|
241
|
+
```ts
|
|
242
|
+
const sock = makeWASocket()
|
|
243
|
+
sock.ev.on('messages.upsert', ({ messages }) => {
|
|
244
|
+
console.log('got messages', messages)
|
|
245
|
+
})
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
### Example to Start
|
|
249
|
+
|
|
250
|
+
> [!NOTE]
|
|
251
|
+
> This example includes basic auth storage too
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
import makeWASocket, { DisconnectReason, useMultiFileAuthState } from 'baileys'
|
|
255
|
+
import { Boom } from '@hapi/boom'
|
|
256
|
+
|
|
257
|
+
async function connectToWhatsApp () {
|
|
258
|
+
const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys')
|
|
259
|
+
const sock = makeWASocket({
|
|
260
|
+
// can provide additional config here
|
|
261
|
+
auth: state,
|
|
262
|
+
printQRInTerminal: true
|
|
263
|
+
})
|
|
264
|
+
sock.ev.on('connection.update', (update) => {
|
|
265
|
+
const { connection, lastDisconnect } = update
|
|
266
|
+
if(connection === 'close') {
|
|
267
|
+
const shouldReconnect = (lastDisconnect.error as Boom)?.output?.statusCode !== DisconnectReason.loggedOut
|
|
268
|
+
console.log('connection closed due to ', lastDisconnect.error, ', reconnecting ', shouldReconnect)
|
|
269
|
+
// reconnect if not logged out
|
|
270
|
+
if(shouldReconnect) {
|
|
271
|
+
connectToWhatsApp()
|
|
272
|
+
}
|
|
273
|
+
} else if(connection === 'open') {
|
|
274
|
+
console.log('opened connection')
|
|
275
|
+
}
|
|
276
|
+
})
|
|
277
|
+
sock.ev.on('messages.upsert', event => {
|
|
278
|
+
for (const m of event.messages) {
|
|
279
|
+
console.log(JSON.stringify(m, undefined, 2))
|
|
280
|
+
|
|
281
|
+
console.log('replying to', m.key.remoteJid)
|
|
282
|
+
await sock.sendMessage(m.key.remoteJid!, { text: 'Hello Word' })
|
|
283
|
+
}
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
// to storage creds (session info) when it updates
|
|
287
|
+
sock.ev.on('creds.update', saveCreds)
|
|
288
|
+
}
|
|
289
|
+
// run in main file
|
|
290
|
+
connectToWhatsApp()
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
> [!IMPORTANT]
|
|
294
|
+
> In `messages.upsert` it's recommended to use a loop like `for (const message of event.messages)` to handle all messages in array
|
|
295
|
+
|
|
296
|
+
### Decrypt Poll Votes
|
|
297
|
+
|
|
298
|
+
- By default poll votes are encrypted and handled in `messages.update`
|
|
299
|
+
- That's a simple example
|
|
300
|
+
```ts
|
|
301
|
+
sock.ev.on('messages.update', event => {
|
|
302
|
+
for(const { key, update } of event) {
|
|
303
|
+
if(update.pollUpdates) {
|
|
304
|
+
const pollCreation = await getMessage(key)
|
|
305
|
+
if(pollCreation) {
|
|
306
|
+
console.log(
|
|
307
|
+
'got poll update, aggregation: ',
|
|
308
|
+
getAggregateVotesInPollMessage({
|
|
309
|
+
message: pollCreation,
|
|
310
|
+
pollUpdates: update.pollUpdates,
|
|
311
|
+
})
|
|
312
|
+
)
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
})
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
- `getMessage` is a [store](#implementing-a-data-store) implementation (in your end)
|
|
320
|
+
|
|
321
|
+
### Summary of Events on First Connection
|
|
322
|
+
|
|
323
|
+
1. When you connect first time, `connection.update` will be fired requesting you to restart sock
|
|
324
|
+
2. Then, history messages will be received in `messaging-history.set`
|
|
325
|
+
|
|
326
|
+
## Implementing a Data Store
|
|
327
|
+
|
|
328
|
+
- Baileys does not come with a defacto storage for chats, contacts, or messages. However, a simple in-memory implementation has been provided. The store listens for chat updates, new messages, message updates, etc., to always have an up-to-date version of the data.
|
|
329
|
+
|
|
330
|
+
> [!IMPORTANT]
|
|
331
|
+
> I highly recommend building your own data store, as storing someone's entire chat history in memory is a terrible waste of RAM.
|
|
332
|
+
|
|
333
|
+
It can be used as follows:
|
|
334
|
+
|
|
335
|
+
```ts
|
|
336
|
+
import makeWASocket, { makeInMemoryStore } from 'baileys'
|
|
337
|
+
// the store maintains the data of the WA connection in memory
|
|
338
|
+
// can be written out to a file & read from it
|
|
339
|
+
const store = makeInMemoryStore({ })
|
|
340
|
+
// can be read from a file
|
|
341
|
+
store.readFromFile('./baileys_store.json')
|
|
342
|
+
// saves the state to a file every 10s
|
|
343
|
+
setInterval(() => {
|
|
344
|
+
store.writeToFile('./baileys_store.json')
|
|
345
|
+
}, 10_000)
|
|
346
|
+
|
|
347
|
+
const sock = makeWASocket({ })
|
|
348
|
+
// will listen from this socket
|
|
349
|
+
// the store can listen from a new socket once the current socket outlives its lifetime
|
|
350
|
+
store.bind(sock.ev)
|
|
351
|
+
|
|
352
|
+
sock.ev.on('chats.upsert', () => {
|
|
353
|
+
// can use 'store.chats' however you want, even after the socket dies out
|
|
354
|
+
// 'chats' => a KeyedDB instance
|
|
355
|
+
console.log('got chats', store.chats.all())
|
|
356
|
+
})
|
|
357
|
+
|
|
358
|
+
sock.ev.on('contacts.upsert', () => {
|
|
359
|
+
console.log('got contacts', Object.values(store.contacts))
|
|
360
|
+
})
|
|
361
|
+
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
The store also provides some simple functions such as `loadMessages` that utilize the store to speed up data retrieval.
|
|
365
|
+
|
|
366
|
+
## Whatsapp IDs Explain
|
|
367
|
+
|
|
368
|
+
- `id` is the WhatsApp ID, called `jid` too, of the person or group you're sending the message to.
|
|
369
|
+
- It must be in the format ```[country code][phone number]@s.whatsapp.net```
|
|
370
|
+
- Example for people: ```+19999999999@s.whatsapp.net```.
|
|
371
|
+
- For groups, it must be in the format ``` 123456789-123345@g.us ```.
|
|
372
|
+
- For broadcast lists, it's `[timestamp of creation]@broadcast`.
|
|
373
|
+
- For stories, the ID is `status@broadcast`.
|
|
374
|
+
|
|
375
|
+
## Utility Functions
|
|
376
|
+
|
|
377
|
+
- `getContentType`, returns the content type for any message
|
|
378
|
+
- `getDevice`, returns the device from message
|
|
379
|
+
- `makeCacheableSignalKeyStore`, make auth store more fast
|
|
380
|
+
- `downloadContentFromMessage`, download content from any message
|
|
381
|
+
|
|
382
|
+
## JID Conversion Utilities
|
|
383
|
+
|
|
384
|
+
Baileys provides helper methods to convert between standard JIDs (@s.whatsapp.net) and LID (Local ID) format (@lid). These are useful when working with different WhatsApp ID formats.
|
|
385
|
+
|
|
386
|
+
#### Convert to LID (@lid)
|
|
387
|
+
|
|
388
|
+
```ts
|
|
389
|
+
const standardJid = '1234567890@s.whatsapp.net'
|
|
390
|
+
const lidJid = sock.toLid(standardJid)
|
|
391
|
+
// Result: '1234567890@lid'
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
#### Convert from LID to Standard (@s.whatsapp.net)
|
|
395
|
+
|
|
396
|
+
```ts
|
|
397
|
+
const lidJid = '1234567890@lid'
|
|
398
|
+
const standardJid = sock.toPn(lidJid)
|
|
399
|
+
// Result: '1234567890@s.whatsapp.net'
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
## Sending Messages
|
|
403
|
+
|
|
404
|
+
```ts
|
|
405
|
+
const jid: string
|
|
406
|
+
const content: AnyMessageContent
|
|
407
|
+
const options: MiscMessageGenerationOptions
|
|
408
|
+
|
|
409
|
+
sock.sendMessage(jid, content, options)
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
### Non-Media Messages
|
|
413
|
+
|
|
414
|
+
#### Text Message
|
|
415
|
+
```ts
|
|
416
|
+
await sock.sendMessage(jid, { text: 'hello word' })
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
#### Quote Message (works with all types)
|
|
420
|
+
```ts
|
|
421
|
+
await sock.sendMessage(jid, { text: 'hello word' }, { quoted: message })
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
#### Mention User (works with most types)
|
|
425
|
+
- @number is to mention in text, it's optional
|
|
426
|
+
```ts
|
|
427
|
+
await sock.sendMessage(
|
|
428
|
+
jid,
|
|
429
|
+
{
|
|
430
|
+
text: '@12345678901',
|
|
431
|
+
mentions: ['12345678901@s.whatsapp.net']
|
|
432
|
+
}
|
|
433
|
+
)
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
```ts
|
|
437
|
+
const msg = getMessageFromStore() // implement this on your end
|
|
438
|
+
await sock.sendMessage(jid, { forward: msg }) // WA forward the message!
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
#### Location Message
|
|
442
|
+
```ts
|
|
443
|
+
await sock.sendMessage(
|
|
444
|
+
jid,
|
|
445
|
+
{
|
|
446
|
+
location: {
|
|
447
|
+
degreesLatitude: 24.121231,
|
|
448
|
+
degreesLongitude: 55.1121221
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
)
|
|
452
|
+
```
|
|
453
|
+
#### Contact Message
|
|
454
|
+
```ts
|
|
455
|
+
const vcard = 'BEGIN:VCARD\n' // metadata of the contact card
|
|
456
|
+
+ 'VERSION:3.0\n'
|
|
457
|
+
+ 'FN:Jeff Singh\n' // full name
|
|
458
|
+
+ 'ORG:Ashoka Uni;\n' // the organization of the contact
|
|
459
|
+
+ 'TEL;type=CELL;type=VOICE;waid=911234567890:+91 12345 67890\n' // WhatsApp ID + phone number
|
|
460
|
+
+ 'END:VCARD'
|
|
461
|
+
|
|
462
|
+
await sock.sendMessage(
|
|
463
|
+
id,
|
|
464
|
+
{
|
|
465
|
+
contacts: {
|
|
466
|
+
displayName: 'Jeff',
|
|
467
|
+
contacts: [{ vcard }]
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
)
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
```ts
|
|
474
|
+
await sock.sendMessage(
|
|
475
|
+
jid,
|
|
476
|
+
{
|
|
477
|
+
react: {
|
|
478
|
+
text: '💖', // use an empty string to remove the reaction
|
|
479
|
+
key: message.key
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
)
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
#### Pin Message
|
|
486
|
+
- You need to pass the key of message, you can retrieve from [store](#implementing-a-data-store) or use a [key](https://baileys.whiskeysockets.io/types/WAMessageKey.html) object
|
|
487
|
+
|
|
488
|
+
- Time can be:
|
|
489
|
+
|
|
490
|
+
| Time | Seconds |
|
|
491
|
+
|-------|----------------|
|
|
492
|
+
| 24h | 86.400 |
|
|
493
|
+
| 7d | 604.800 |
|
|
494
|
+
| 30d | 2.592.000 |
|
|
495
|
+
|
|
496
|
+
```ts
|
|
497
|
+
await sock.sendMessage(
|
|
498
|
+
jid,
|
|
499
|
+
{
|
|
500
|
+
pin: {
|
|
501
|
+
type: 1, // 0 to remove
|
|
502
|
+
time: 86400
|
|
503
|
+
key: message.key
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
)
|
|
507
|
+
```
|
|
508
|
+
|
|
509
|
+
#### Poll Message
|
|
510
|
+
```ts
|
|
511
|
+
await sock.sendMessage(
|
|
512
|
+
jid,
|
|
513
|
+
{
|
|
514
|
+
poll: {
|
|
515
|
+
name: 'My Poll',
|
|
516
|
+
values: ['Option 1', 'Option 2', ...],
|
|
517
|
+
selectableCount: 1,
|
|
518
|
+
toAnnouncementGroup: false // or true
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
)
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
### Sending Messages with Link Previews
|
|
525
|
+
|
|
526
|
+
1. By default, wa does not have link generation when sent from the web
|
|
527
|
+
2. Baileys has a function to generate the content for these link previews
|
|
528
|
+
3. To enable this function's usage, add `link-preview-js` as a dependency to your project with `yarn add link-preview-js`
|
|
529
|
+
4. Send a link:
|
|
530
|
+
```ts
|
|
531
|
+
await sock.sendMessage(
|
|
532
|
+
jid,
|
|
533
|
+
{
|
|
534
|
+
text: 'Hi, this was sent using https://github.com/whiskeysockets/baileys'
|
|
535
|
+
}
|
|
536
|
+
)
|
|
537
|
+
```
|
|
538
|
+
|
|
539
|
+
### Media Messages
|
|
540
|
+
|
|
541
|
+
Sending media (video, stickers, images) is easier & more efficient than ever.
|
|
542
|
+
|
|
543
|
+
> [!NOTE]
|
|
544
|
+
> In media messages, you can pass `{ stream: Stream }` or `{ url: Url }` or `Buffer` directly, you can see more [here](https://baileys.whiskeysockets.io/types/WAMediaUpload.html)
|
|
545
|
+
|
|
546
|
+
- When specifying a media url, Baileys never loads the entire buffer into memory; it even encrypts the media as a readable stream.
|
|
547
|
+
|
|
548
|
+
> [!TIP]
|
|
549
|
+
> It's recommended to use Stream or Url to save memory
|
|
550
|
+
|
|
551
|
+
#### Gif Message
|
|
552
|
+
- Whatsapp doesn't support `.gif` files, that's why we send gifs as common `.mp4` video with `gifPlayback` flag
|
|
553
|
+
```ts
|
|
554
|
+
await sock.sendMessage(
|
|
555
|
+
jid,
|
|
556
|
+
{
|
|
557
|
+
video: fs.readFileSync('Media/ma_gif.mp4'),
|
|
558
|
+
caption: 'hello word',
|
|
559
|
+
gifPlayback: true
|
|
560
|
+
}
|
|
561
|
+
)
|
|
562
|
+
```
|
|
563
|
+
|
|
564
|
+
#### Video Message
|
|
565
|
+
```ts
|
|
566
|
+
await sock.sendMessage(
|
|
567
|
+
id,
|
|
568
|
+
{
|
|
569
|
+
video: {
|
|
570
|
+
url: './Media/ma_gif.mp4'
|
|
571
|
+
},
|
|
572
|
+
caption: 'hello word',
|
|
573
|
+
ptv: false // if set to true, will send as a `video note`
|
|
574
|
+
}
|
|
575
|
+
)
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
#### Audio Message
|
|
579
|
+
- To audio message work in all devices you need to convert with some tool like `ffmpeg` with this flags:
|
|
580
|
+
```bash
|
|
581
|
+
codec: libopus //ogg file
|
|
582
|
+
ac: 1 //one channel
|
|
583
|
+
avoid_negative_ts
|
|
584
|
+
make_zero
|
|
585
|
+
```
|
|
586
|
+
- Example:
|
|
587
|
+
```bash
|
|
588
|
+
ffmpeg -i input.mp4 -avoid_negative_ts make_zero -ac 1 output.ogg
|
|
589
|
+
```
|
|
590
|
+
```ts
|
|
591
|
+
await sock.sendMessage(
|
|
592
|
+
jid,
|
|
593
|
+
{
|
|
594
|
+
audio: {
|
|
595
|
+
url: './Media/audio.mp3'
|
|
596
|
+
},
|
|
597
|
+
mimetype: 'audio/mp4'
|
|
598
|
+
}
|
|
599
|
+
)
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
#### Image Message
|
|
603
|
+
```ts
|
|
604
|
+
await sock.sendMessage(
|
|
605
|
+
id,
|
|
606
|
+
{
|
|
607
|
+
image: {
|
|
608
|
+
url: './Media/ma_img.png'
|
|
609
|
+
},
|
|
610
|
+
caption: 'hello word'
|
|
611
|
+
}
|
|
612
|
+
)
|
|
613
|
+
```
|
|
614
|
+
|
|
615
|
+
#### View Once Message
|
|
616
|
+
|
|
617
|
+
- You can send all messages above as `viewOnce`, you only need to pass `viewOnce: true` in content object
|
|
618
|
+
|
|
619
|
+
```ts
|
|
620
|
+
await sock.sendMessage(
|
|
621
|
+
id,
|
|
622
|
+
{
|
|
623
|
+
image: {
|
|
624
|
+
url: './Media/ma_img.png'
|
|
625
|
+
},
|
|
626
|
+
viewOnce: true, //works with video, audio too
|
|
627
|
+
caption: 'hello word'
|
|
628
|
+
}
|
|
629
|
+
)
|
|
630
|
+
```
|
|
631
|
+
|
|
632
|
+
## Modify Messages
|
|
633
|
+
|
|
634
|
+
### Deleting Messages (for everyone)
|
|
635
|
+
|
|
636
|
+
```ts
|
|
637
|
+
const msg = await sock.sendMessage(jid, { text: 'hello word' })
|
|
638
|
+
await sock.sendMessage(jid, { delete: msg.key })
|
|
639
|
+
```
|
|
640
|
+
|
|
641
|
+
**Note:** deleting for oneself is supported via `chatModify`, see in [this section](#modifying-chats)
|
|
642
|
+
|
|
643
|
+
### Editing Messages
|
|
644
|
+
|
|
645
|
+
- You can pass all editable contents here
|
|
646
|
+
```ts
|
|
647
|
+
await sock.sendMessage(jid, {
|
|
648
|
+
text: 'updated text goes here',
|
|
649
|
+
edit: response.key,
|
|
650
|
+
});
|
|
651
|
+
```
|
|
652
|
+
|
|
653
|
+
## Manipulating Media Messages
|
|
654
|
+
|
|
655
|
+
### Thumbnail in Media Messages
|
|
656
|
+
- For media messages, the thumbnail can be generated automatically for images & stickers provided you add `jimp` or `sharp` as a dependency in your project using `yarn add jimp` or `yarn add sharp`.
|
|
657
|
+
- Thumbnails for videos can also be generated automatically, though, you need to have `ffmpeg` installed on your system.
|
|
658
|
+
|
|
659
|
+
### Downloading Media Messages
|
|
660
|
+
|
|
661
|
+
If you want to save the media you received
|
|
662
|
+
```ts
|
|
663
|
+
import { createWriteStream } from 'fs'
|
|
664
|
+
import { downloadMediaMessage, getContentType } from 'baileys'
|
|
665
|
+
|
|
666
|
+
sock.ev.on('messages.upsert', async ({ [m] }) => {
|
|
667
|
+
if (!m.message) return // if there is no text or media message
|
|
668
|
+
const messageType = getContentType(m) // get what type of message it is (text, image, video...)
|
|
669
|
+
|
|
670
|
+
// if the message is an image
|
|
671
|
+
if (messageType === 'imageMessage') {
|
|
672
|
+
// download the message
|
|
673
|
+
const stream = await downloadMediaMessage(
|
|
674
|
+
m,
|
|
675
|
+
'stream', // can be 'buffer' too
|
|
676
|
+
{ },
|
|
677
|
+
{
|
|
678
|
+
logger,
|
|
679
|
+
// pass this so that baileys can request a reupload of media
|
|
680
|
+
// that has been deleted
|
|
681
|
+
reuploadRequest: sock.updateMediaMessage
|
|
682
|
+
}
|
|
683
|
+
)
|
|
684
|
+
// save to file
|
|
685
|
+
const writeStream = createWriteStream('./my-download.jpeg')
|
|
686
|
+
stream.pipe(writeStream)
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
```
|
|
690
|
+
|
|
691
|
+
### Re-upload Media Message to Whatsapp
|
|
692
|
+
|
|
693
|
+
- WhatsApp automatically removes old media from their servers. For the device to access said media -- a re-upload is required by another device that has it. This can be accomplished using:
|
|
694
|
+
```ts
|
|
695
|
+
await sock.updateMediaMessage(msg)
|
|
696
|
+
```
|
|
697
|
+
|
|
698
|
+
## Reject Call
|
|
699
|
+
|
|
700
|
+
- You can obtain `callId` and `callFrom` from `call` event
|
|
701
|
+
|
|
702
|
+
```ts
|
|
703
|
+
await sock.rejectCall(callId, callFrom)
|
|
704
|
+
```
|
|
705
|
+
|
|
706
|
+
## Send States in Chat
|
|
707
|
+
|
|
708
|
+
### Reading Messages
|
|
709
|
+
- A set of message [keys](https://baileys.whiskeysockets.io/types/WAMessageKey.html) must be explicitly marked read now.
|
|
710
|
+
- You cannot mark an entire 'chat' read as it were with Baileys Web.
|
|
711
|
+
This means you have to keep track of unread messages.
|
|
712
|
+
|
|
713
|
+
```ts
|
|
714
|
+
const key: WAMessageKey
|
|
715
|
+
// can pass multiple keys to read multiple messages as well
|
|
716
|
+
await sock.readMessages([key])
|
|
717
|
+
```
|
|
718
|
+
|
|
719
|
+
The message ID is the unique identifier of the message that you are marking as read.
|
|
720
|
+
On a `WAMessage`, the `messageID` can be accessed using ```messageID = message.key.id```.
|
|
721
|
+
|
|
722
|
+
### Update Presence
|
|
723
|
+
|
|
724
|
+
- ``` presence ``` can be one of [these](https://baileys.whiskeysockets.io/types/WAPresence.html)
|
|
725
|
+
- The presence expires after about 10 seconds.
|
|
726
|
+
- This lets the person/group with `jid` know whether you're online, offline, typing etc.
|
|
727
|
+
|
|
728
|
+
```ts
|
|
729
|
+
await sock.sendPresenceUpdate('available', jid)
|
|
730
|
+
```
|
|
731
|
+
|
|
732
|
+
> [!NOTE]
|
|
733
|
+
> If a desktop client is active, WA doesn't send push notifications to the device. If you would like to receive said notifications -- mark your Baileys client offline using `sock.sendPresenceUpdate('unavailable')`
|
|
734
|
+
|
|
735
|
+
## Modifying Chats
|
|
736
|
+
|
|
737
|
+
WA uses an encrypted form of communication to send chat/app updates. This has been implemented mostly and you can send the following updates:
|
|
738
|
+
|
|
739
|
+
> [!IMPORTANT]
|
|
740
|
+
> If you mess up one of your updates, WA can log you out of all your devices and you'll have to log in again.
|
|
741
|
+
|
|
742
|
+
### Archive a Chat
|
|
743
|
+
```ts
|
|
744
|
+
const lastMsgInChat = await getLastMessageInChat(jid) // implement this on your end
|
|
745
|
+
await sock.chatModify({ archive: true, lastMessages: [lastMsgInChat] }, jid)
|
|
746
|
+
```
|
|
747
|
+
### Mute/Unmute a Chat
|
|
748
|
+
|
|
749
|
+
- Supported times:
|
|
750
|
+
|
|
751
|
+
| Time | Miliseconds |
|
|
752
|
+
|-------|-----------------|
|
|
753
|
+
| Remove | null |
|
|
754
|
+
| 8h | 86.400.000 |
|
|
755
|
+
| 7d | 604.800.000 |
|
|
756
|
+
|
|
757
|
+
```ts
|
|
758
|
+
// mute for 8 hours
|
|
759
|
+
await sock.chatModify({ mute: 8 * 60 * 60 * 1000 }, jid)
|
|
760
|
+
// unmute
|
|
761
|
+
await sock.chatModify({ mute: null }, jid)
|
|
762
|
+
```
|
|
763
|
+
### Mark a Chat Read/Unread
|
|
764
|
+
```ts
|
|
765
|
+
const lastMsgInChat = await getLastMessageInChat(jid) // implement this on your end
|
|
766
|
+
// mark it unread
|
|
767
|
+
await sock.chatModify({ markRead: false, lastMessages: [lastMsgInChat] }, jid)
|
|
768
|
+
```
|
|
769
|
+
|
|
770
|
+
### Delete a Message for Me
|
|
771
|
+
```ts
|
|
772
|
+
await sock.chatModify(
|
|
773
|
+
{
|
|
774
|
+
clear: {
|
|
775
|
+
messages: [
|
|
776
|
+
{
|
|
777
|
+
id: 'ATWYHDNNWU81732J',
|
|
778
|
+
fromMe: true,
|
|
779
|
+
timestamp: '1654823909'
|
|
780
|
+
}
|
|
781
|
+
]
|
|
782
|
+
}
|
|
783
|
+
},
|
|
784
|
+
jid
|
|
785
|
+
)
|
|
786
|
+
|
|
787
|
+
```
|
|
788
|
+
### Delete a Chat
|
|
789
|
+
```ts
|
|
790
|
+
const lastMsgInChat = await getLastMessageInChat(jid) // implement this on your end
|
|
791
|
+
await sock.chatModify({
|
|
792
|
+
delete: true,
|
|
793
|
+
lastMessages: [
|
|
794
|
+
{
|
|
795
|
+
key: lastMsgInChat.key,
|
|
796
|
+
messageTimestamp: lastMsgInChat.messageTimestamp
|
|
797
|
+
}
|
|
798
|
+
]
|
|
799
|
+
},
|
|
800
|
+
jid
|
|
801
|
+
)
|
|
802
|
+
```
|
|
803
|
+
### Pin/Unpin a Chat
|
|
804
|
+
```ts
|
|
805
|
+
await sock.chatModify({
|
|
806
|
+
pin: true // or `false` to unpin
|
|
807
|
+
},
|
|
808
|
+
jid
|
|
809
|
+
)
|
|
810
|
+
```
|
|
811
|
+
### Star/Unstar a Message
|
|
812
|
+
```ts
|
|
813
|
+
await sock.chatModify({
|
|
814
|
+
star: {
|
|
815
|
+
messages: [
|
|
816
|
+
{
|
|
817
|
+
id: 'messageID',
|
|
818
|
+
fromMe: true // or `false`
|
|
819
|
+
}
|
|
820
|
+
],
|
|
821
|
+
star: true // - true: Star Message; false: Unstar Message
|
|
822
|
+
}
|
|
823
|
+
},
|
|
824
|
+
jid
|
|
825
|
+
)
|
|
826
|
+
```
|
|
827
|
+
|
|
828
|
+
### Disappearing Messages
|
|
829
|
+
|
|
830
|
+
- Ephemeral can be:
|
|
831
|
+
|
|
832
|
+
| Time | Seconds |
|
|
833
|
+
|-------|----------------|
|
|
834
|
+
| Remove | 0 |
|
|
835
|
+
| 24h | 86.400 |
|
|
836
|
+
| 7d | 604.800 |
|
|
837
|
+
| 90d | 7.776.000 |
|
|
838
|
+
|
|
839
|
+
- You need to pass in **Seconds**, default is 7 days
|
|
840
|
+
|
|
841
|
+
```ts
|
|
842
|
+
// turn on disappearing messages
|
|
843
|
+
await sock.sendMessage(
|
|
844
|
+
jid,
|
|
845
|
+
// this is 1 week in seconds -- how long you want messages to appear for
|
|
846
|
+
{ disappearingMessagesInChat: WA_DEFAULT_EPHEMERAL }
|
|
847
|
+
)
|
|
848
|
+
|
|
849
|
+
// will send as a disappearing message
|
|
850
|
+
await sock.sendMessage(jid, { text: 'hello' }, { ephemeralExpiration: WA_DEFAULT_EPHEMERAL })
|
|
851
|
+
|
|
852
|
+
// turn off disappearing messages
|
|
853
|
+
await sock.sendMessage(
|
|
854
|
+
jid,
|
|
855
|
+
{ disappearingMessagesInChat: false }
|
|
856
|
+
)
|
|
857
|
+
```
|
|
858
|
+
|
|
859
|
+
## User Querys
|
|
860
|
+
|
|
861
|
+
### Check If ID Exists in Whatsapp
|
|
862
|
+
```ts
|
|
863
|
+
const [result] = await sock.onWhatsApp(jid)
|
|
864
|
+
if (result.exists) console.log (`${jid} exists on WhatsApp, as jid: ${result.jid}`)
|
|
865
|
+
```
|
|
866
|
+
|
|
867
|
+
### Query Chat History (groups too)
|
|
868
|
+
|
|
869
|
+
- You need to have oldest message in chat
|
|
870
|
+
```ts
|
|
871
|
+
const msg = await getOldestMessageInChat(jid)
|
|
872
|
+
await sock.fetchMessageHistory(
|
|
873
|
+
50, //quantity (max: 50 per query)
|
|
874
|
+
msg.key,
|
|
875
|
+
msg.messageTimestamp
|
|
876
|
+
)
|
|
877
|
+
```
|
|
878
|
+
- Messages will be received in `messaging-history.set` event
|
|
879
|
+
|
|
880
|
+
### Fetch Status
|
|
881
|
+
```ts
|
|
882
|
+
const status = await sock.fetchStatus(jid)
|
|
883
|
+
console.log('status: ' + status)
|
|
884
|
+
```
|
|
885
|
+
|
|
886
|
+
### Fetch Profile Picture (groups too)
|
|
887
|
+
- To get the display picture of some person/group
|
|
888
|
+
```ts
|
|
889
|
+
// for low res picture
|
|
890
|
+
const ppUrl = await sock.profilePictureUrl(jid)
|
|
891
|
+
console.log(ppUrl)
|
|
892
|
+
|
|
893
|
+
// for high res picture
|
|
894
|
+
const ppUrl = await sock.profilePictureUrl(jid, 'image')
|
|
895
|
+
```
|
|
896
|
+
|
|
897
|
+
### Fetch Bussines Profile (such as description or category)
|
|
898
|
+
```ts
|
|
899
|
+
const profile = await sock.getBusinessProfile(jid)
|
|
900
|
+
console.log('business description: ' + profile.description + ', category: ' + profile.category)
|
|
901
|
+
```
|
|
902
|
+
|
|
903
|
+
### Fetch Someone's Presence (if they're typing or online)
|
|
904
|
+
```ts
|
|
905
|
+
// the presence update is fetched and called here
|
|
906
|
+
sock.ev.on('presence.update', console.log)
|
|
907
|
+
|
|
908
|
+
// request updates for a chat
|
|
909
|
+
await sock.presenceSubscribe(jid)
|
|
910
|
+
```
|
|
911
|
+
|
|
912
|
+
## Change Profile
|
|
913
|
+
|
|
914
|
+
### Change Profile Status
|
|
915
|
+
```ts
|
|
916
|
+
await sock.updateProfileStatus('Hello World!')
|
|
917
|
+
```
|
|
918
|
+
### Change Profile Name
|
|
919
|
+
```ts
|
|
920
|
+
await sock.updateProfileName('My name')
|
|
921
|
+
```
|
|
922
|
+
### Change Display Picture (groups too)
|
|
923
|
+
- To change your display picture or a group's
|
|
924
|
+
|
|
925
|
+
> [!NOTE]
|
|
926
|
+
> Like media messages, you can pass `{ stream: Stream }` or `{ url: Url }` or `Buffer` directly, you can see more [here](https://baileys.whiskeysockets.io/types/WAMediaUpload.html)
|
|
927
|
+
|
|
928
|
+
```ts
|
|
929
|
+
await sock.updateProfilePicture(jid, { url: './new-profile-picture.jpeg' })
|
|
930
|
+
```
|
|
931
|
+
### Remove display picture (groups too)
|
|
932
|
+
```ts
|
|
933
|
+
await sock.removeProfilePicture(jid)
|
|
934
|
+
```
|
|
935
|
+
|
|
936
|
+
## Groups
|
|
937
|
+
|
|
938
|
+
- To change group properties you need to be admin
|
|
939
|
+
|
|
940
|
+
### Create a Group
|
|
941
|
+
```ts
|
|
942
|
+
// title & participants
|
|
943
|
+
const group = await sock.groupCreate('My Fab Group', ['1234@s.whatsapp.net', '4564@s.whatsapp.net'])
|
|
944
|
+
console.log('created group with id: ' + group.gid)
|
|
945
|
+
await sock.sendMessage(group.id, { text: 'hello there' }) // say hello to everyone on the group
|
|
946
|
+
```
|
|
947
|
+
### Add/Remove or Demote/Promote
|
|
948
|
+
```ts
|
|
949
|
+
// id & people to add to the group (will throw error if it fails)
|
|
950
|
+
await sock.groupParticipantsUpdate(
|
|
951
|
+
jid,
|
|
952
|
+
['abcd@s.whatsapp.net', 'efgh@s.whatsapp.net'],
|
|
953
|
+
'add' // replace this parameter with 'remove' or 'demote' or 'promote'
|
|
954
|
+
)
|
|
955
|
+
```
|
|
956
|
+
### Change Subject (name)
|
|
957
|
+
```ts
|
|
958
|
+
await sock.groupUpdateSubject(jid, 'New Subject!')
|
|
959
|
+
```
|
|
960
|
+
### Change Description
|
|
961
|
+
```ts
|
|
962
|
+
await sock.groupUpdateDescription(jid, 'New Description!')
|
|
963
|
+
```
|
|
964
|
+
### Change Settings
|
|
965
|
+
```ts
|
|
966
|
+
// only allow admins to send messages
|
|
967
|
+
await sock.groupSettingUpdate(jid, 'announcement')
|
|
968
|
+
// allow everyone to send messages
|
|
969
|
+
await sock.groupSettingUpdate(jid, 'not_announcement')
|
|
970
|
+
// allow everyone to modify the group's settings -- like display picture etc.
|
|
971
|
+
await sock.groupSettingUpdate(jid, 'unlocked')
|
|
972
|
+
// only allow admins to modify the group's settings
|
|
973
|
+
await sock.groupSettingUpdate(jid, 'locked')
|
|
974
|
+
```
|
|
975
|
+
### Leave a Group
|
|
976
|
+
```ts
|
|
977
|
+
// will throw error if it fails
|
|
978
|
+
await sock.groupLeave(jid)
|
|
979
|
+
```
|
|
980
|
+
### Get Invite Code
|
|
981
|
+
- To create link with code use `'https://chat.whatsapp.com/' + code`
|
|
982
|
+
```ts
|
|
983
|
+
const code = await sock.groupInviteCode(jid)
|
|
984
|
+
console.log('group code: ' + code)
|
|
985
|
+
```
|
|
986
|
+
### Revoke Invite Code
|
|
987
|
+
```ts
|
|
988
|
+
const code = await sock.groupRevokeInvite(jid)
|
|
989
|
+
console.log('New group code: ' + code)
|
|
990
|
+
```
|
|
991
|
+
### Join Using Invitation Code
|
|
992
|
+
- Code can't have `https://chat.whatsapp.com/`, only code
|
|
993
|
+
```ts
|
|
994
|
+
const response = await sock.groupAcceptInvite(code)
|
|
995
|
+
console.log('joined to: ' + response)
|
|
996
|
+
```
|
|
997
|
+
### Get Group Info by Invite Code
|
|
998
|
+
```ts
|
|
999
|
+
const response = await sock.groupGetInviteInfo(code)
|
|
1000
|
+
console.log('group information: ' + response)
|
|
1001
|
+
```
|
|
1002
|
+
### Query Metadata (participants, name, description...)
|
|
1003
|
+
```ts
|
|
1004
|
+
const metadata = await sock.groupMetadata(jid)
|
|
1005
|
+
console.log(metadata.id + ', title: ' + metadata.subject + ', description: ' + metadata.desc)
|
|
1006
|
+
```
|
|
1007
|
+
### Join using `groupInviteMessage`
|
|
1008
|
+
```ts
|
|
1009
|
+
const response = await sock.groupAcceptInviteV4(jid, groupInviteMessage)
|
|
1010
|
+
console.log('joined to: ' + response)
|
|
1011
|
+
```
|
|
1012
|
+
### Get Request Join List
|
|
1013
|
+
```ts
|
|
1014
|
+
const response = await sock.groupRequestParticipantsList(jid)
|
|
1015
|
+
console.log(response)
|
|
1016
|
+
```
|
|
1017
|
+
### Approve/Reject Request Join
|
|
1018
|
+
```ts
|
|
1019
|
+
const response = await sock.groupRequestParticipantsUpdate(
|
|
1020
|
+
jid, // group id
|
|
1021
|
+
['abcd@s.whatsapp.net', 'efgh@s.whatsapp.net'],
|
|
1022
|
+
'approve' // or 'reject'
|
|
1023
|
+
)
|
|
1024
|
+
console.log(response)
|
|
1025
|
+
```
|
|
1026
|
+
### Get All Participating Groups Metadata
|
|
1027
|
+
```ts
|
|
1028
|
+
const response = await sock.groupFetchAllParticipating()
|
|
1029
|
+
console.log(response)
|
|
1030
|
+
```
|
|
1031
|
+
### Toggle Ephemeral
|
|
1032
|
+
|
|
1033
|
+
- Ephemeral can be:
|
|
1034
|
+
|
|
1035
|
+
| Time | Seconds |
|
|
1036
|
+
|-------|----------------|
|
|
1037
|
+
| Remove | 0 |
|
|
1038
|
+
| 24h | 86.400 |
|
|
1039
|
+
| 7d | 604.800 |
|
|
1040
|
+
| 90d | 7.776.000 |
|
|
1041
|
+
|
|
1042
|
+
```ts
|
|
1043
|
+
await sock.groupToggleEphemeral(jid, 86400)
|
|
1044
|
+
```
|
|
1045
|
+
|
|
1046
|
+
### Change Add Mode
|
|
1047
|
+
```ts
|
|
1048
|
+
await sock.groupMemberAddMode(
|
|
1049
|
+
jid,
|
|
1050
|
+
'all_member_add' // or 'admin_add'
|
|
1051
|
+
)
|
|
1052
|
+
```
|
|
1053
|
+
|
|
1054
|
+
## Privacy
|
|
1055
|
+
|
|
1056
|
+
### Block/Unblock User
|
|
1057
|
+
```ts
|
|
1058
|
+
await sock.updateBlockStatus(jid, 'block') // Block user
|
|
1059
|
+
await sock.updateBlockStatus(jid, 'unblock') // Unblock user
|
|
1060
|
+
```
|
|
1061
|
+
### Get Privacy Settings
|
|
1062
|
+
```ts
|
|
1063
|
+
const privacySettings = await sock.fetchPrivacySettings(true)
|
|
1064
|
+
console.log('privacy settings: ' + privacySettings)
|
|
1065
|
+
```
|
|
1066
|
+
### Get BlockList
|
|
1067
|
+
```ts
|
|
1068
|
+
const response = await sock.fetchBlocklist()
|
|
1069
|
+
console.log(response)
|
|
1070
|
+
```
|
|
1071
|
+
### Update LastSeen Privacy
|
|
1072
|
+
```ts
|
|
1073
|
+
const value = 'all' // 'contacts' | 'contact_blacklist' | 'none'
|
|
1074
|
+
await sock.updateLastSeenPrivacy(value)
|
|
1075
|
+
```
|
|
1076
|
+
### Update Online Privacy
|
|
1077
|
+
```ts
|
|
1078
|
+
const value = 'all' // 'match_last_seen'
|
|
1079
|
+
await sock.updateOnlinePrivacy(value)
|
|
1080
|
+
```
|
|
1081
|
+
### Update Profile Picture Privacy
|
|
1082
|
+
```ts
|
|
1083
|
+
const value = 'all' // 'contacts' | 'contact_blacklist' | 'none'
|
|
1084
|
+
await sock.updateProfilePicturePrivacy(value)
|
|
1085
|
+
```
|
|
1086
|
+
### Update Status Privacy
|
|
1087
|
+
```ts
|
|
1088
|
+
const value = 'all' // 'contacts' | 'contact_blacklist' | 'none'
|
|
1089
|
+
await sock.updateStatusPrivacy(value)
|
|
1090
|
+
```
|
|
1091
|
+
### Update Read Receipts Privacy
|
|
1092
|
+
```ts
|
|
1093
|
+
const value = 'all' // 'none'
|
|
1094
|
+
await sock.updateReadReceiptsPrivacy(value)
|
|
1095
|
+
```
|
|
1096
|
+
### Update Groups Add Privacy
|
|
1097
|
+
```ts
|
|
1098
|
+
const value = 'all' // 'contacts' | 'contact_blacklist'
|
|
1099
|
+
await sock.updateGroupsAddPrivacy(value)
|
|
1100
|
+
```
|
|
1101
|
+
### Update Default Disappearing Mode
|
|
1102
|
+
|
|
1103
|
+
- Like [this](#disappearing-messages), ephemeral can be:
|
|
1104
|
+
|
|
1105
|
+
| Time | Seconds |
|
|
1106
|
+
|-------|----------------|
|
|
1107
|
+
| Remove | 0 |
|
|
1108
|
+
| 24h | 86.400 |
|
|
1109
|
+
| 7d | 604.800 |
|
|
1110
|
+
| 90d | 7.776.000 |
|
|
1111
|
+
|
|
1112
|
+
```ts
|
|
1113
|
+
const ephemeral = 86400
|
|
1114
|
+
await sock.updateDefaultDisappearingMode(ephemeral)
|
|
1115
|
+
```
|
|
1116
|
+
|
|
1117
|
+
## Broadcast Lists & Stories
|
|
1118
|
+
|
|
1119
|
+
### Send Broadcast & Stories
|
|
1120
|
+
- Messages can be sent to broadcasts & stories. You need to add the following message options in sendMessage, like this:
|
|
1121
|
+
```ts
|
|
1122
|
+
await sock.sendMessage(
|
|
1123
|
+
jid,
|
|
1124
|
+
{
|
|
1125
|
+
image: {
|
|
1126
|
+
url: url
|
|
1127
|
+
},
|
|
1128
|
+
caption: caption
|
|
1129
|
+
},
|
|
1130
|
+
{
|
|
1131
|
+
backgroundColor: backgroundColor,
|
|
1132
|
+
font: font,
|
|
1133
|
+
statusJidList: statusJidList,
|
|
1134
|
+
broadcast: true
|
|
1135
|
+
}
|
|
1136
|
+
)
|
|
1137
|
+
```
|
|
1138
|
+
- Message body can be a `extendedTextMessage` or `imageMessage` or `videoMessage` or `voiceMessage`, see [here](https://baileys.whiskeysockets.io/types/AnyRegularMessageContent.html)
|
|
1139
|
+
- You can add `backgroundColor` and other options in the message options, see [here](https://baileys.whiskeysockets.io/types/MiscMessageGenerationOptions.html)
|
|
1140
|
+
- `broadcast: true` enables broadcast mode
|
|
1141
|
+
- `statusJidList`: a list of people that you can get which you need to provide, which are the people who will get this status message.
|
|
1142
|
+
|
|
1143
|
+
- You can send messages to broadcast lists the same way you send messages to groups & individual chats.
|
|
1144
|
+
- Right now, WA Web does not support creating broadcast lists, but you can still delete them.
|
|
1145
|
+
- Broadcast IDs are in the format `12345678@broadcast`
|
|
1146
|
+
### Query a Broadcast List's Recipients & Name
|
|
1147
|
+
```ts
|
|
1148
|
+
const bList = await sock.getBroadcastListInfo('1234@broadcast')
|
|
1149
|
+
console.log (`list name: ${bList.name}, recps: ${bList.recipients}`)
|
|
1150
|
+
```
|
|
1151
|
+
|
|
1152
|
+
## Writing Custom Functionality
|
|
1153
|
+
Baileys is written with custom functionality in mind. Instead of forking the project & re-writing the internals, you can simply write your own extensions.
|
|
1154
|
+
|
|
1155
|
+
### Enabling Debug Level in Baileys Logs
|
|
1156
|
+
First, enable the logging of unhandled messages from WhatsApp by setting:
|
|
1157
|
+
```ts
|
|
1158
|
+
const sock = makeWASocket({
|
|
1159
|
+
logger: P({ level: 'debug' }),
|
|
1160
|
+
})
|
|
1161
|
+
```
|
|
1162
|
+
This will enable you to see all sorts of messages WhatsApp sends in the console.
|
|
1163
|
+
|
|
1164
|
+
### How Whatsapp Communicate With Us
|
|
1165
|
+
|
|
1166
|
+
> [!TIP]
|
|
1167
|
+
> If you want to learn whatsapp protocol, we recommend to study about Libsignal Protocol and Noise Protocol
|
|
1168
|
+
|
|
1169
|
+
- **Example:** Functionality to track the battery percentage of your phone. You enable logging and you'll see a message about your battery pop up in the console:
|
|
1170
|
+
```
|
|
1171
|
+
{
|
|
1172
|
+
"level": 10,
|
|
1173
|
+
"fromMe": false,
|
|
1174
|
+
"frame": {
|
|
1175
|
+
"tag": "ib",
|
|
1176
|
+
"attrs": {
|
|
1177
|
+
"from": "@s.whatsapp.net"
|
|
1178
|
+
},
|
|
1179
|
+
"content": [
|
|
1180
|
+
{
|
|
1181
|
+
"tag": "edge_routing",
|
|
1182
|
+
"attrs": {},
|
|
1183
|
+
"content": [
|
|
1184
|
+
{
|
|
1185
|
+
"tag": "routing_info",
|
|
1186
|
+
"attrs": {},
|
|
1187
|
+
"content": {
|
|
1188
|
+
"type": "Buffer",
|
|
1189
|
+
"data": [8,2,8,5]
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
]
|
|
1193
|
+
}
|
|
1194
|
+
]
|
|
1195
|
+
},
|
|
1196
|
+
"msg":"communication"
|
|
1197
|
+
}
|
|
1198
|
+
```
|
|
1199
|
+
|
|
1200
|
+
The `'frame'` is what the message received is, it has three components:
|
|
1201
|
+
- `tag` -- what this frame is about (eg. message will have 'message')
|
|
1202
|
+
- `attrs` -- a string key-value pair with some metadata (contains ID of the message usually)
|
|
1203
|
+
- `content` -- the actual data (eg. a message node will have the actual message content in it)
|
|
1204
|
+
- read more about this format [here](/src/WABinary/readme.md)
|
|
1205
|
+
|
|
1206
|
+
### Register a Callback for Websocket Events
|
|
1207
|
+
|
|
1208
|
+
> [!TIP]
|
|
1209
|
+
> Recommended to see `onMessageReceived` function in `socket.ts` file to understand how websockets events are fired
|
|
1210
|
+
|
|
1211
|
+
```ts
|
|
1212
|
+
// for any message with tag 'edge_routing'
|
|
1213
|
+
sock.ws.on('CB:edge_routing', (node: BinaryNode) => { })
|
|
1214
|
+
|
|
1215
|
+
// for any message with tag 'edge_routing' and id attribute = abcd
|
|
1216
|
+
sock.ws.on('CB:edge_routing,id:abcd', (node: BinaryNode) => { })
|
|
1217
|
+
|
|
1218
|
+
// for any message with tag 'edge_routing', id attribute = abcd & first content node routing_info
|
|
1219
|
+
sock.ws.on('CB:edge_routing,id:abcd,routing_info', (node: BinaryNode) => { })
|
|
1220
|
+
```
|
|
1221
|
+
|
|
1222
|
+
> [!NOTE]
|
|
1223
|
+
> Also, this repo is now licenced under GPL 3 since it uses [libsignal-node](https://git.questbook.io/backend/service-coderunner/-/merge_requests/1)
|