@chatbridge-it/baileys 0.0.0-stage → 1.0.1
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/LICENSE +24 -0
- package/README.md +689 -2
- package/WAProto/index.js +169661 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/index.js +147 -0
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Signal/Group/ciphertext-message.js +15 -0
- package/lib/Signal/Group/group-session-builder.js +64 -0
- package/lib/Signal/Group/group_cipher.js +96 -0
- package/lib/Signal/Group/index.js +57 -0
- package/lib/Signal/Group/keyhelper.js +55 -0
- package/lib/Signal/Group/queue-job.js +57 -0
- package/lib/Signal/Group/sender-chain-key.js +34 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
- package/lib/Signal/Group/sender-key-message.js +69 -0
- package/lib/Signal/Group/sender-key-name.js +51 -0
- package/lib/Signal/Group/sender-key-record.js +53 -0
- package/lib/Signal/Group/sender-key-state.js +99 -0
- package/lib/Signal/Group/sender-message-key.js +29 -0
- package/lib/Signal/libsignal.js +174 -0
- package/lib/Socket/Client/abstract-socket-client.js +13 -0
- package/lib/Socket/Client/index.js +19 -0
- package/lib/Socket/Client/mobile-socket-client.js +65 -0
- package/lib/Socket/Client/web-socket-client.js +62 -0
- package/lib/Socket/business.js +260 -0
- package/lib/Socket/chats.js +987 -0
- package/lib/Socket/dugong.js +637 -0
- package/lib/Socket/groups.js +317 -0
- package/lib/Socket/index.js +11 -0
- package/lib/Socket/messages-recv.js +1110 -0
- package/lib/Socket/messages-send.js +831 -0
- package/lib/Socket/newsletter.js +424 -0
- package/lib/Socket/registration.js +166 -0
- package/lib/Socket/socket.js +661 -0
- package/lib/Socket/usync.js +70 -0
- package/lib/Store/index.js +10 -0
- package/lib/Store/make-cache-manager-store.js +83 -0
- package/lib/Store/make-in-memory-store.js +427 -0
- package/lib/Store/make-ordered-dictionary.js +81 -0
- package/lib/Store/object-repository.js +27 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.js +4 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.js +27 -0
- package/lib/Types/LabelAssociation.js +9 -0
- package/lib/Types/Message.js +9 -0
- package/lib/Types/Newsletter.js +38 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.js +2 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +42 -0
- package/lib/Utils/auth-utils.js +206 -0
- package/lib/Utils/baileys-event-stream.js +63 -0
- package/lib/Utils/business.js +234 -0
- package/lib/Utils/chat-utils.js +729 -0
- package/lib/Utils/crypto.js +151 -0
- package/lib/Utils/decode-wa-message.js +198 -0
- package/lib/Utils/event-buffer.js +514 -0
- package/lib/Utils/generics.js +498 -0
- package/lib/Utils/history.js +96 -0
- package/lib/Utils/index.js +33 -0
- package/lib/Utils/link-preview.js +93 -0
- package/lib/Utils/logger.js +7 -0
- package/lib/Utils/lt-hash.js +51 -0
- package/lib/Utils/make-mutex.js +43 -0
- package/lib/Utils/messages-media.js +819 -0
- package/lib/Utils/messages.js +787 -0
- package/lib/Utils/noise-handler.js +155 -0
- package/lib/Utils/process-message.js +321 -0
- package/lib/Utils/signal.js +153 -0
- package/lib/Utils/use-multi-file-auth-state.js +122 -0
- package/lib/Utils/validate-connection.js +229 -0
- package/lib/WABinary/constants.js +40 -0
- package/lib/WABinary/decode.js +252 -0
- package/lib/WABinary/encode.js +265 -0
- package/lib/WABinary/generic-utils.js +198 -0
- package/lib/WABinary/index.js +21 -0
- package/lib/WABinary/jid-utils.js +62 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.js +13 -0
- package/lib/WAM/constants.js +15350 -0
- package/lib/WAM/encode.js +155 -0
- package/lib/WAM/index.js +19 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
- package/lib/WAUSync/Protocols/index.js +20 -0
- package/lib/WAUSync/USyncQuery.js +89 -0
- package/lib/WAUSync/USyncUser.js +26 -0
- package/lib/WAUSync/index.js +19 -0
- package/lib/index.js +33 -0
- package/package.json +98 -4
package/README.md
CHANGED
|
@@ -1,3 +1,690 @@
|
|
|
1
|
-
#
|
|
1
|
+
# chatbridge Baileys
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A WhatsApp Web Multi-Device API library for Node.js. It connects to WhatsApp directly over WebSocket, so no browser (Puppeteer, Selenium, etc.) is required.
|
|
4
|
+
|
|
5
|
+
- Package: [@chatbridge-it/baileys](https://www.npmjs.com/package/@chatbridge-it/baileys)
|
|
6
|
+
- Maintainer: chatbridge
|
|
7
|
+
- Base project: [Baileys](https://github.com/WhiskeySockets/Baileys), with additional helpers and message types
|
|
8
|
+
|
|
9
|
+
> **Disclaimer.** This is an unofficial library. It is not affiliated with, authorized by, or endorsed by WhatsApp LLC or Meta. Using it may violate WhatsApp's Terms of Service and can lead to account restrictions or bans. Do not use it for spam, unsolicited bulk messaging, or harassment. You are solely responsible for how you use this software.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Table of Contents
|
|
14
|
+
|
|
15
|
+
- [Features](#features)
|
|
16
|
+
- [Requirements](#requirements)
|
|
17
|
+
- [Installation](#installation)
|
|
18
|
+
- [Quick Start](#quick-start)
|
|
19
|
+
- [Authentication](#authentication)
|
|
20
|
+
- [Sending Messages](#sending-messages)
|
|
21
|
+
- [Interactive Messages](#interactive-messages)
|
|
22
|
+
- [Groups](#groups)
|
|
23
|
+
- [Newsletters (Channels)](#newsletters-channels)
|
|
24
|
+
- [Contacts, Profiles and Presence](#contacts-profiles-and-presence)
|
|
25
|
+
- [Events](#events)
|
|
26
|
+
- [Connection Options](#connection-options)
|
|
27
|
+
- [Disconnect Reasons](#disconnect-reasons)
|
|
28
|
+
- [Session Storage and Security](#session-storage-and-security)
|
|
29
|
+
- [Project Structure](#project-structure)
|
|
30
|
+
- [Known Limitations](#known-limitations)
|
|
31
|
+
- [Troubleshooting](#troubleshooting)
|
|
32
|
+
- [Contributing](#contributing)
|
|
33
|
+
- [License](#license)
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## Features
|
|
38
|
+
|
|
39
|
+
- WebSocket-based connection, no browser required
|
|
40
|
+
- Multi-device login with **QR code** or **pairing code**
|
|
41
|
+
- Built-in multi-file session storage (`useMultiFileAuthState`) and support for custom auth state
|
|
42
|
+
- Send text, images, videos, audio and voice notes, documents, stickers, locations, contacts, polls, reactions, forwards, edits, deletes and view-once media
|
|
43
|
+
- Interactive messages with buttons (URL, copy code, quick reply and native flow buttons)
|
|
44
|
+
- Group management: create, metadata, subject, description, participants, invite codes, join requests, member labels
|
|
45
|
+
- Channel (newsletter) management: create, follow, mute, metadata, reactions, resolve a channel from its URL
|
|
46
|
+
- Contacts, profile picture, profile name and status, privacy settings, block list, presence
|
|
47
|
+
- Fully event-driven API through `sock.ev`
|
|
48
|
+
- Configurable timeouts, retry behavior, logging (pino) and history sync
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Requirements
|
|
53
|
+
|
|
54
|
+
- **Node.js 20 or newer.** The `preinstall` script (`engine-requirements.js`) stops the installation on older versions.
|
|
55
|
+
- A WhatsApp account that can link a companion device (QR code or pairing code).
|
|
56
|
+
|
|
57
|
+
Optional peer dependencies. Install only what you need:
|
|
58
|
+
|
|
59
|
+
| Package | Used for | Notes |
|
|
60
|
+
|---|---|---|
|
|
61
|
+
| `qrcode-terminal` | Printing QR codes in the terminal | Used in the quick start below |
|
|
62
|
+
| `sharp` | Image and video thumbnails | Declared peer range is `^0.32.2`, install that version |
|
|
63
|
+
| `jimp` | Thumbnails if `sharp` is not available | Alternative to `sharp` |
|
|
64
|
+
| `link-preview-js` | Link previews in text messages | Optional |
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
npm install qrcode-terminal
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## Installation
|
|
73
|
+
|
|
74
|
+
Install the library from npm:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
npm install @chatbridge-it/baileys
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Import it in CommonJS:
|
|
81
|
+
|
|
82
|
+
```js
|
|
83
|
+
const {
|
|
84
|
+
makeWASocket,
|
|
85
|
+
useMultiFileAuthState,
|
|
86
|
+
fetchLatestBaileysVersion,
|
|
87
|
+
DisconnectReason
|
|
88
|
+
} = require("@chatbridge-it/baileys");
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
In ES modules, import the default export and destructure it:
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
import baileys from "@chatbridge-it/baileys";
|
|
95
|
+
const { makeWASocket, useMultiFileAuthState, fetchLatestBaileysVersion, DisconnectReason } = baileys;
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## Quick Start
|
|
101
|
+
|
|
102
|
+
This example logs in with a QR code, replies `pong` to `ping`, and reconnects automatically unless the session was logged out.
|
|
103
|
+
|
|
104
|
+
```js
|
|
105
|
+
const {
|
|
106
|
+
makeWASocket,
|
|
107
|
+
useMultiFileAuthState,
|
|
108
|
+
fetchLatestBaileysVersion,
|
|
109
|
+
DisconnectReason
|
|
110
|
+
} = require("@chatbridge-it/baileys");
|
|
111
|
+
const qrcode = require("qrcode-terminal");
|
|
112
|
+
|
|
113
|
+
async function start() {
|
|
114
|
+
const { state, saveCreds } = await useMultiFileAuthState("auth_info");
|
|
115
|
+
const { version } = await fetchLatestBaileysVersion();
|
|
116
|
+
|
|
117
|
+
const sock = makeWASocket({
|
|
118
|
+
version,
|
|
119
|
+
auth: state,
|
|
120
|
+
printQRInTerminal: false
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
sock.ev.on("creds.update", saveCreds);
|
|
124
|
+
|
|
125
|
+
sock.ev.on("connection.update", ({ connection, lastDisconnect, qr }) => {
|
|
126
|
+
if (qr) {
|
|
127
|
+
qrcode.generate(qr, { small: true });
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (connection === "open") {
|
|
131
|
+
console.log("Connected");
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (connection === "close") {
|
|
135
|
+
const statusCode = lastDisconnect?.error?.output?.statusCode;
|
|
136
|
+
|
|
137
|
+
if (statusCode === DisconnectReason.loggedOut) {
|
|
138
|
+
console.log("Logged out. Delete the auth_info folder and pair again.");
|
|
139
|
+
} else {
|
|
140
|
+
start();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
sock.ev.on("messages.upsert", async ({ messages, type }) => {
|
|
146
|
+
if (type !== "notify") return;
|
|
147
|
+
|
|
148
|
+
for (const msg of messages) {
|
|
149
|
+
if (!msg.message || msg.key.fromMe) continue;
|
|
150
|
+
|
|
151
|
+
const text = msg.message.conversation || msg.message.extendedTextMessage?.text;
|
|
152
|
+
|
|
153
|
+
if (text === "ping") {
|
|
154
|
+
await sock.sendMessage(msg.key.remoteJid, { text: "pong" }, { quoted: msg });
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
start().catch(console.error);
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Run it with `node index.js`, then scan the QR code from WhatsApp on your phone: **Settings > Linked devices > Link a device**.
|
|
164
|
+
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
## Authentication
|
|
168
|
+
|
|
169
|
+
### QR code
|
|
170
|
+
|
|
171
|
+
The library emits a `qr` value through `connection.update`. Render it however you like. The example above uses `qrcode-terminal`.
|
|
172
|
+
|
|
173
|
+
### Pairing code
|
|
174
|
+
|
|
175
|
+
Pairing codes let you link a device by typing a code on your phone instead of scanning a QR code. Request it only when the account is not registered yet:
|
|
176
|
+
|
|
177
|
+
```js
|
|
178
|
+
if (!sock.authState.creds.registered) {
|
|
179
|
+
const phoneNumber = "15551234567"; // digits only, including the country code
|
|
180
|
+
|
|
181
|
+
setTimeout(async () => {
|
|
182
|
+
// Pass null to get a random code, or pass your own code as the second argument
|
|
183
|
+
const code = await sock.requestPairingCode(phoneNumber, null);
|
|
184
|
+
console.log("Pairing code:", code);
|
|
185
|
+
}, 3000);
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
On your phone: **Settings > Linked devices > Link a device > Link with phone number instead**, then enter the code.
|
|
190
|
+
|
|
191
|
+
- A custom code is converted to uppercase by the library.
|
|
192
|
+
- **Always pass the second argument explicitly.** If you omit it, the library uses a fixed default code that is defined in its source code, so anyone can know it. Pass `null` for a random code, or a code that is not predictable.
|
|
193
|
+
|
|
194
|
+
### Persisting the session
|
|
195
|
+
|
|
196
|
+
Save credentials every time they change, otherwise you will have to pair again after restarting:
|
|
197
|
+
|
|
198
|
+
```js
|
|
199
|
+
sock.ev.on("creds.update", saveCreds);
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
---
|
|
203
|
+
|
|
204
|
+
## Sending Messages
|
|
205
|
+
|
|
206
|
+
Every message is sent with `sock.sendMessage(jid, content, options)`. A JID is a WhatsApp address: `15551234567@s.whatsapp.net` for a user, `120363...@g.us` for a group, and `...@newsletter` for a channel.
|
|
207
|
+
|
|
208
|
+
### Text
|
|
209
|
+
|
|
210
|
+
```js
|
|
211
|
+
await sock.sendMessage(jid, { text: "Hello" });
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### Reply (quote) a message
|
|
215
|
+
|
|
216
|
+
```js
|
|
217
|
+
await sock.sendMessage(jid, { text: "Got it" }, { quoted: msg });
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### Mentions
|
|
221
|
+
|
|
222
|
+
```js
|
|
223
|
+
await sock.sendMessage(groupJid, {
|
|
224
|
+
text: "@15551234567 welcome!",
|
|
225
|
+
mentions: ["15551234567@s.whatsapp.net"]
|
|
226
|
+
});
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### Image, video and view-once
|
|
230
|
+
|
|
231
|
+
`image`, `video` and `document` accept a Buffer, a local file read with `fs.readFileSync`, or a `{ url }` object.
|
|
232
|
+
|
|
233
|
+
```js
|
|
234
|
+
const fs = require("fs");
|
|
235
|
+
|
|
236
|
+
await sock.sendMessage(jid, { image: { url: "https://example.com/photo.jpg" }, caption: "Look" });
|
|
237
|
+
await sock.sendMessage(jid, { video: fs.readFileSync("./clip.mp4"), caption: "Clip" });
|
|
238
|
+
await sock.sendMessage(jid, { image: fs.readFileSync("./photo.jpg"), viewOnce: true });
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### Audio and voice note
|
|
242
|
+
|
|
243
|
+
```js
|
|
244
|
+
await sock.sendMessage(jid, { audio: { url: "./song.mp3" }, mimetype: "audio/mp4" });
|
|
245
|
+
|
|
246
|
+
// Voice note (push-to-talk), Opus in an OGG container
|
|
247
|
+
await sock.sendMessage(jid, {
|
|
248
|
+
audio: fs.readFileSync("./note.ogg"),
|
|
249
|
+
mimetype: "audio/ogg; codecs=opus",
|
|
250
|
+
ptt: true
|
|
251
|
+
});
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### Document
|
|
255
|
+
|
|
256
|
+
```js
|
|
257
|
+
await sock.sendMessage(jid, {
|
|
258
|
+
document: fs.readFileSync("./report.pdf"),
|
|
259
|
+
mimetype: "application/pdf",
|
|
260
|
+
fileName: "report.pdf",
|
|
261
|
+
caption: "Monthly report"
|
|
262
|
+
});
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### Sticker
|
|
266
|
+
|
|
267
|
+
Stickers must be WebP files.
|
|
268
|
+
|
|
269
|
+
```js
|
|
270
|
+
await sock.sendMessage(jid, { sticker: fs.readFileSync("./sticker.webp") });
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
### Location
|
|
274
|
+
|
|
275
|
+
```js
|
|
276
|
+
await sock.sendMessage(jid, {
|
|
277
|
+
location: {
|
|
278
|
+
degreesLatitude: 41.9028,
|
|
279
|
+
degreesLongitude: 12.4964,
|
|
280
|
+
name: "Rome",
|
|
281
|
+
address: "Piazza Venezia"
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
### Contact card
|
|
287
|
+
|
|
288
|
+
```js
|
|
289
|
+
const vcard = [
|
|
290
|
+
"BEGIN:VCARD",
|
|
291
|
+
"VERSION:3.0",
|
|
292
|
+
"FN:Jane Doe",
|
|
293
|
+
"TEL;type=CELL;waid=15551234567:+1 555 123 4567",
|
|
294
|
+
"END:VCARD"
|
|
295
|
+
].join("\n");
|
|
296
|
+
|
|
297
|
+
await sock.sendMessage(jid, { contacts: { displayName: "Jane Doe", contacts: [{ vcard }] } });
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
### Poll
|
|
301
|
+
|
|
302
|
+
```js
|
|
303
|
+
await sock.sendMessage(jid, {
|
|
304
|
+
poll: {
|
|
305
|
+
name: "Lunch?",
|
|
306
|
+
values: ["Pizza", "Sushi", "Salad"],
|
|
307
|
+
selectableCount: 1
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
### Reaction
|
|
313
|
+
|
|
314
|
+
```js
|
|
315
|
+
await sock.sendMessage(jid, { react: { text: "👍", key: msg.key } });
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
Send an empty `text` to remove the reaction.
|
|
319
|
+
|
|
320
|
+
### Edit, delete and forward
|
|
321
|
+
|
|
322
|
+
`sendMessage` returns the sent message, including its `key`, which you need to edit or delete it later.
|
|
323
|
+
|
|
324
|
+
```js
|
|
325
|
+
const sent = await sock.sendMessage(jid, { text: "Draft" });
|
|
326
|
+
|
|
327
|
+
await sock.sendMessage(jid, { text: "Final version", edit: sent.key });
|
|
328
|
+
await sock.sendMessage(jid, { delete: sent.key });
|
|
329
|
+
|
|
330
|
+
await sock.sendMessage(jid, { forward: msg, force: true });
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
Edits only work on messages sent by your account. WhatsApp limits how long after sending a message edits and deletes are still allowed.
|
|
334
|
+
|
|
335
|
+
### Read receipts and typing indicator
|
|
336
|
+
|
|
337
|
+
```js
|
|
338
|
+
await sock.readMessages([msg.key]);
|
|
339
|
+
|
|
340
|
+
await sock.sendPresenceUpdate("composing", jid); // typing
|
|
341
|
+
await sock.sendPresenceUpdate("paused", jid); // stopped typing
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
Supported presence types: `available`, `unavailable`, `composing`, `recording`, `paused`.
|
|
345
|
+
|
|
346
|
+
---
|
|
347
|
+
|
|
348
|
+
## Interactive Messages
|
|
349
|
+
|
|
350
|
+
Interactive messages add a text body, an optional header with media, a footer and buttons.
|
|
351
|
+
|
|
352
|
+
```js
|
|
353
|
+
await sock.sendMessage(jid, {
|
|
354
|
+
interactiveMessage: {
|
|
355
|
+
header: "Order update",
|
|
356
|
+
title: "Your order has shipped.",
|
|
357
|
+
footer: "chatbridge",
|
|
358
|
+
buttons: [
|
|
359
|
+
{
|
|
360
|
+
name: "cta_url",
|
|
361
|
+
buttonParamsJson: JSON.stringify({
|
|
362
|
+
display_text: "Track order",
|
|
363
|
+
url: "https://example.com/track"
|
|
364
|
+
})
|
|
365
|
+
},
|
|
366
|
+
{
|
|
367
|
+
name: "cta_copy",
|
|
368
|
+
buttonParamsJson: JSON.stringify({
|
|
369
|
+
display_text: "Copy code",
|
|
370
|
+
id: "copy-1",
|
|
371
|
+
copy_code: "ABC123"
|
|
372
|
+
})
|
|
373
|
+
},
|
|
374
|
+
{
|
|
375
|
+
name: "quick_reply",
|
|
376
|
+
buttonParamsJson: JSON.stringify({
|
|
377
|
+
display_text: "Contact support",
|
|
378
|
+
id: "support"
|
|
379
|
+
})
|
|
380
|
+
}
|
|
381
|
+
]
|
|
382
|
+
}
|
|
383
|
+
}, { quoted: msg });
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
### Fields
|
|
387
|
+
|
|
388
|
+
| Field | Description |
|
|
389
|
+
|---|---|
|
|
390
|
+
| `header` | Title shown above the body |
|
|
391
|
+
| `title` | Body text |
|
|
392
|
+
| `footer` | Small text at the bottom |
|
|
393
|
+
| `image` | Header image: `{ url }` or a Buffer |
|
|
394
|
+
| `video` | Header video: `{ url }` or a Buffer |
|
|
395
|
+
| `document` | Header document. **Buffer only.** Use with `mimetype`, `fileName` and optionally `jpegThumbnail` |
|
|
396
|
+
| `buttons` | Array of `{ name, buttonParamsJson }` objects |
|
|
397
|
+
| `nativeFlowMessage` | Advanced: raw native flow payload (`buttons`, `messageParamsJson`) |
|
|
398
|
+
| `contextInfo` | Optional context such as `mentionedJid` |
|
|
399
|
+
|
|
400
|
+
### Document header example
|
|
401
|
+
|
|
402
|
+
```js
|
|
403
|
+
await sock.sendMessage(jid, {
|
|
404
|
+
interactiveMessage: {
|
|
405
|
+
header: "Invoice",
|
|
406
|
+
title: "Your invoice is ready.",
|
|
407
|
+
footer: "chatbridge",
|
|
408
|
+
document: fs.readFileSync("./invoice.pdf"),
|
|
409
|
+
mimetype: "application/pdf",
|
|
410
|
+
fileName: "invoice.pdf",
|
|
411
|
+
buttons: [
|
|
412
|
+
{
|
|
413
|
+
name: "cta_url",
|
|
414
|
+
buttonParamsJson: JSON.stringify({
|
|
415
|
+
display_text: "Pay online",
|
|
416
|
+
url: "https://example.com/pay"
|
|
417
|
+
})
|
|
418
|
+
}
|
|
419
|
+
]
|
|
420
|
+
}
|
|
421
|
+
}, { quoted: msg });
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
Button rendering depends on the recipient's WhatsApp app version and account type, and WhatsApp may change or restrict these message types at any time. Test with your target accounts before relying on them in production.
|
|
425
|
+
|
|
426
|
+
---
|
|
427
|
+
|
|
428
|
+
## Groups
|
|
429
|
+
|
|
430
|
+
```js
|
|
431
|
+
// Create a group
|
|
432
|
+
const group = await sock.groupCreate("Team", ["15551234567@s.whatsapp.net"]);
|
|
433
|
+
|
|
434
|
+
// Metadata
|
|
435
|
+
const meta = await sock.groupMetadata(group.id);
|
|
436
|
+
|
|
437
|
+
// Subject and description
|
|
438
|
+
await sock.groupUpdateSubject(group.id, "Team (renamed)");
|
|
439
|
+
await sock.groupUpdateDescription(group.id, "Project discussions");
|
|
440
|
+
|
|
441
|
+
// Participants: action is "add", "remove", "promote" or "demote"
|
|
442
|
+
await sock.groupParticipantsUpdate(group.id, ["15557654321@s.whatsapp.net"], "add");
|
|
443
|
+
|
|
444
|
+
// Invite link code
|
|
445
|
+
const inviteCode = await sock.groupInviteCode(group.id);
|
|
446
|
+
|
|
447
|
+
// Groups the account participates in
|
|
448
|
+
const groups = await sock.groupFetchAllParticipating();
|
|
449
|
+
|
|
450
|
+
// Member label shown in the group (trimmed to 30 characters)
|
|
451
|
+
await sock.setLabelGroup(group.id, "Admin");
|
|
452
|
+
|
|
453
|
+
// Leave
|
|
454
|
+
await sock.groupLeave(group.id);
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
Pending join requests can be handled with `groupRequestParticipantsUpdate(jid, participants, action)`.
|
|
458
|
+
|
|
459
|
+
---
|
|
460
|
+
|
|
461
|
+
## Newsletters (Channels)
|
|
462
|
+
|
|
463
|
+
```js
|
|
464
|
+
// Resolve a channel from its link or bare invite key
|
|
465
|
+
const channel = await sock.newsletterFromUrl("https://whatsapp.com/channel/XXXXXXXXXXXX");
|
|
466
|
+
// -> { id, name, description, subscribers, verification, creation_time, ... }
|
|
467
|
+
|
|
468
|
+
// Create a channel. The third argument is the reaction setting string, converted to uppercase
|
|
469
|
+
const created = await sock.newsletterCreate("Updates", "Official updates", "all");
|
|
470
|
+
|
|
471
|
+
// Follow and mute
|
|
472
|
+
await sock.newsletterFollow(channel.id);
|
|
473
|
+
await sock.newsletterMute(channel.id);
|
|
474
|
+
|
|
475
|
+
// React to a channel post
|
|
476
|
+
await sock.newsletterReactMessage(channel.id, serverId, "👍");
|
|
477
|
+
|
|
478
|
+
// Remove the reaction
|
|
479
|
+
await sock.newsletterReactMessage(channel.id, serverId);
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
`newsletterMetadata(type, key, role)` gives direct access to channel metadata when you already have the channel key.
|
|
483
|
+
|
|
484
|
+
---
|
|
485
|
+
|
|
486
|
+
## Contacts, Profiles and Presence
|
|
487
|
+
|
|
488
|
+
```js
|
|
489
|
+
// Check which numbers have a WhatsApp account. Returns an array with jid and exists fields.
|
|
490
|
+
const results = await sock.onWhatsApp("15551234567");
|
|
491
|
+
|
|
492
|
+
// Profile picture: type is "preview" (default) or "image"
|
|
493
|
+
const picUrl = await sock.profilePictureUrl(jid, "image");
|
|
494
|
+
|
|
495
|
+
// Status text of a contact
|
|
496
|
+
const status = await sock.fetchStatus(jid);
|
|
497
|
+
|
|
498
|
+
// Your profile
|
|
499
|
+
await sock.updateProfileName("chatbridge Bot");
|
|
500
|
+
await sock.updateProfileStatus("Available 24/7");
|
|
501
|
+
await sock.updateProfilePicture(sock.user.id, fs.readFileSync("./avatar.jpg"));
|
|
502
|
+
|
|
503
|
+
// Privacy and block list
|
|
504
|
+
await sock.updateLastSeenPrivacy("contacts");
|
|
505
|
+
await sock.updateBlockStatus(jid, "block"); // "block" or "unblock"
|
|
506
|
+
const blocked = await sock.fetchBlocklist();
|
|
507
|
+
|
|
508
|
+
// Presence: subscribe to a contact's online status
|
|
509
|
+
await sock.presenceSubscribe(jid);
|
|
510
|
+
```
|
|
511
|
+
|
|
512
|
+
---
|
|
513
|
+
|
|
514
|
+
## Events
|
|
515
|
+
|
|
516
|
+
Listen to events through `sock.ev.on(eventName, handler)`.
|
|
517
|
+
|
|
518
|
+
| Event | When it fires | Payload |
|
|
519
|
+
|---|---|---|
|
|
520
|
+
| `connection.update` | Connection state, QR code or login state changes | `{ connection, lastDisconnect, qr, isNewLogin, receivedPendingNotifications }` |
|
|
521
|
+
| `creds.update` | Credentials changed. Persist them | Partial credentials object |
|
|
522
|
+
| `messages.upsert` | New messages arrive or history is loaded | `{ messages, type }`, where `type` is `notify` (new) or `append` (history) |
|
|
523
|
+
| `messages.update` | Message status, content or edits change | Array of `{ key, update }` |
|
|
524
|
+
| `messages.delete` | Messages were deleted | `{ keys }` or `{ jid, all: true }` |
|
|
525
|
+
| `messages.reaction` | Reactions on messages change | Array of `{ key, reaction }` |
|
|
526
|
+
| `message-receipt.update` | Delivery or read receipts change | Array of receipt objects |
|
|
527
|
+
| `messages.media-update` | Media was re-fetched after a retry | Array of update objects |
|
|
528
|
+
| `chats.upsert` / `chats.update` / `chats.delete` | Chat list changes | Chat objects or chat IDs |
|
|
529
|
+
| `contacts.upsert` / `contacts.update` | Contacts change | Array of contact objects |
|
|
530
|
+
| `groups.upsert` / `groups.update` | Group metadata changes | Group objects or partial updates |
|
|
531
|
+
| `group-participants.update` | Members are added, removed, promoted or demoted | `{ id, author, participants, action }` |
|
|
532
|
+
| `presence.update` | A contact changes presence | `{ id, presences }` |
|
|
533
|
+
| `blocklist.update` | Block list changes | `{ blocklist, type }` |
|
|
534
|
+
| `call` | Incoming or updated call | Array of call objects |
|
|
535
|
+
| `newsletter.reaction` / `newsletter.view` | Channel post reactions or view counts change | `{ id, server_id, ... }` |
|
|
536
|
+
| `newsletter-participants.update` / `newsletter-settings.update` | Channel roles or settings change | Update objects |
|
|
537
|
+
| `labels.edit` / `labels.association` | Business labels change | Label objects |
|
|
538
|
+
|
|
539
|
+
Example:
|
|
540
|
+
|
|
541
|
+
```js
|
|
542
|
+
sock.ev.on("group-participants.update", ({ id, participants, action }) => {
|
|
543
|
+
console.log(`Group ${id}: ${action} ${participants.join(", ")}`);
|
|
544
|
+
});
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
---
|
|
548
|
+
|
|
549
|
+
## Connection Options
|
|
550
|
+
|
|
551
|
+
Pass options to `makeWASocket({ ... })`. The defaults come from `lib/Defaults/index.js`.
|
|
552
|
+
|
|
553
|
+
| Option | Default | Description |
|
|
554
|
+
|---|---|---|
|
|
555
|
+
| `auth` | `undefined` | Auth state, usually from `useMultiFileAuthState(folder)`. Required |
|
|
556
|
+
| `version` | Bundled WhatsApp Web version | Use `fetchLatestBaileysVersion()` to track the current version |
|
|
557
|
+
| `browser` | `Browsers("Chrome")` | Browser name shown on the linked device list |
|
|
558
|
+
| `logger` | pino logger | Any pino-compatible logger |
|
|
559
|
+
| `printQRInTerminal` | `false` | Deprecated. Print the `qr` value yourself |
|
|
560
|
+
| `markOnlineOnConnect` | `true` | Mark the account as online when connected |
|
|
561
|
+
| `syncFullHistory` | `false` | Request the full message history on first login |
|
|
562
|
+
| `shouldSyncHistoryMessage` | `() => true` | Filter which history sync messages are processed |
|
|
563
|
+
| `getMessage` | `async () => undefined` | Return a stored message by key. Needed to resend messages after decryption retries. Implement it with your own store |
|
|
564
|
+
| `cachedGroupMetadata` | `async () => undefined` | Cache for group metadata lookups |
|
|
565
|
+
| `generateHighQualityLinkPreview` | `false` | Generate higher-quality link preview images |
|
|
566
|
+
| `linkPreviewImageThumbnailWidth` | `192` | Width of link preview thumbnails |
|
|
567
|
+
| `emitOwnEvents` | `true` | Emit events for messages sent by your account |
|
|
568
|
+
| `shouldIgnoreJid` | `() => false` | Return `true` to ignore events for a JID |
|
|
569
|
+
| `connectTimeoutMs` | `20000` | Connection timeout in milliseconds |
|
|
570
|
+
| `keepAliveIntervalMs` | `30000` | Keep-alive ping interval |
|
|
571
|
+
| `defaultQueryTimeoutMs` | `60000` | Timeout for query responses |
|
|
572
|
+
| `retryRequestDelayMs` | `250` | Delay between retry requests |
|
|
573
|
+
| `maxMsgRetryCount` | `5` | Maximum retries for a message |
|
|
574
|
+
| `waWebSocketUrl` | `wss://web.whatsapp.com/ws/chat` | WebSocket endpoint |
|
|
575
|
+
| `patchMessageBeforeSending` | identity function | Hook to modify a message just before sending |
|
|
576
|
+
|
|
577
|
+
Example:
|
|
578
|
+
|
|
579
|
+
```js
|
|
580
|
+
const sock = makeWASocket({
|
|
581
|
+
version,
|
|
582
|
+
auth: state,
|
|
583
|
+
logger: require("pino")({ level: "silent" }),
|
|
584
|
+
syncFullHistory: false,
|
|
585
|
+
getMessage: async (key) => {
|
|
586
|
+
// Return the message from your own store, or undefined
|
|
587
|
+
return undefined;
|
|
588
|
+
}
|
|
589
|
+
});
|
|
590
|
+
```
|
|
591
|
+
|
|
592
|
+
---
|
|
593
|
+
|
|
594
|
+
## Disconnect Reasons
|
|
595
|
+
|
|
596
|
+
When `connection.update` reports `connection: "close"`, read the status code from `lastDisconnect?.error?.output?.statusCode` and compare it with `DisconnectReason`.
|
|
597
|
+
|
|
598
|
+
| Reason | Code | Recommended action |
|
|
599
|
+
|---|---|---|
|
|
600
|
+
| `loggedOut` | 401 | The device was unlinked. Delete the auth folder and pair again |
|
|
601
|
+
| `badSession` | 500 | The session is corrupted. Delete the auth folder and pair again |
|
|
602
|
+
| `connectionReplaced` | 440 | Another instance opened the same session. Run one instance per session |
|
|
603
|
+
| `restartRequired` | 515 | Reconnect. This can happen once right after a new login |
|
|
604
|
+
| `connectionClosed` / `connectionLost` / `timedOut` | 428 / 408 | Reconnect with a short delay |
|
|
605
|
+
| `multideviceMismatch` | 411 | The linked session is incompatible. Pair the device again if it persists |
|
|
606
|
+
| `forbidden` | 403 | The account may be restricted. Check the WhatsApp app |
|
|
607
|
+
| `unavailableService` | 503 | WhatsApp is temporarily unavailable. Reconnect later |
|
|
608
|
+
|
|
609
|
+
---
|
|
610
|
+
|
|
611
|
+
## Session Storage and Security
|
|
612
|
+
|
|
613
|
+
- `useMultiFileAuthState(folder)` stores credentials and encryption keys as JSON files inside `folder`.
|
|
614
|
+
- **Treat the auth folder like a password.** Anyone with it can access the WhatsApp account. Do not commit it to Git. Add it to `.gitignore`.
|
|
615
|
+
- In production, store credentials in a database or secret manager by writing a custom auth state with `{ creds, keys: { get, set } }`. Use `makeCacheableSignalKeyStore(state.keys, logger)` to add caching.
|
|
616
|
+
- Keep only one running instance per session to avoid `connectionReplaced` loops.
|
|
617
|
+
|
|
618
|
+
---
|
|
619
|
+
|
|
620
|
+
## Project Structure
|
|
621
|
+
|
|
622
|
+
```
|
|
623
|
+
baileys/
|
|
624
|
+
├── lib/
|
|
625
|
+
│ ├── index.js Entry point: exports makeWASocket and all modules
|
|
626
|
+
│ ├── Socket/ Socket implementation: messages, groups, chats, newsletters, business, registration
|
|
627
|
+
│ ├── Utils/ Auth state, message generation, media, crypto, event buffer, link previews
|
|
628
|
+
│ ├── Types/ Constants and type definitions (JavaScript)
|
|
629
|
+
│ ├── Store/ Optional in-memory store for chats, contacts and messages
|
|
630
|
+
│ ├── Signal/ Signal protocol (one-to-one and group encryption)
|
|
631
|
+
│ ├── WABinary/ WhatsApp binary XML encoder and decoder
|
|
632
|
+
│ ├── WAM/ WhatsApp metrics encoder
|
|
633
|
+
│ ├── WAUSync/ USync query protocols
|
|
634
|
+
│ └── Defaults/ Default connection configuration
|
|
635
|
+
├── WAProto/ Protobuf definitions and generated code
|
|
636
|
+
├── engine-requirements.js Node.js version check run at install time
|
|
637
|
+
└── package.json
|
|
638
|
+
```
|
|
639
|
+
|
|
640
|
+
---
|
|
641
|
+
|
|
642
|
+
## Known Limitations
|
|
643
|
+
|
|
644
|
+
- There is no test suite in the repository.
|
|
645
|
+
- The `types` field in `package.json` points to `lib/index.d.ts`, which is not included. Type definitions are not currently shipped.
|
|
646
|
+
- WhatsApp changes its protocol regularly. Some features can break without notice until the library is updated.
|
|
647
|
+
|
|
648
|
+
---
|
|
649
|
+
|
|
650
|
+
## Troubleshooting
|
|
651
|
+
|
|
652
|
+
**Installation fails with a Node.js version error.**
|
|
653
|
+
Upgrade to Node.js 20 or newer.
|
|
654
|
+
|
|
655
|
+
**No QR code appears.**
|
|
656
|
+
`printQRInTerminal` is `false` by default. Render the `qr` value from `connection.update` yourself, as in the quick start.
|
|
657
|
+
|
|
658
|
+
**The connection closes with `loggedOut` (401).**
|
|
659
|
+
The device was unlinked from WhatsApp. Delete the auth folder and pair again.
|
|
660
|
+
|
|
661
|
+
**The connection closes with `connectionReplaced` (440).**
|
|
662
|
+
Another process is using the same credentials. Stop the extra instance.
|
|
663
|
+
|
|
664
|
+
**Messages fail to decrypt or retries never succeed.**
|
|
665
|
+
Implement `getMessage` and return recently sent messages from your store.
|
|
666
|
+
|
|
667
|
+
**Image or video thumbnails are missing.**
|
|
668
|
+
Install `sharp@^0.32.2` or `jimp`.
|
|
669
|
+
|
|
670
|
+
**The pairing code is rejected.**
|
|
671
|
+
Use digits only with the country code for the phone number. Enter the code on the "Link with phone number instead" screen. Request one code at a time for the same number.
|
|
672
|
+
|
|
673
|
+
---
|
|
674
|
+
|
|
675
|
+
## Contributing
|
|
676
|
+
|
|
677
|
+
Suggestions and bug reports are welcome. Please describe what changed, why, and how you tested it.
|
|
678
|
+
|
|
679
|
+
---
|
|
680
|
+
|
|
681
|
+
## License
|
|
682
|
+
|
|
683
|
+
Released under the MIT License. See [LICENSE](./LICENSE).
|
|
684
|
+
|
|
685
|
+
---
|
|
686
|
+
|
|
687
|
+
## Credits
|
|
688
|
+
|
|
689
|
+
- [Baileys](https://github.com/WhiskeySockets/Baileys), the original WhatsApp Web API library this project is based on
|
|
690
|
+
- chatbridge, maintainer of this fork
|