@blurose/baileys 1.1.16 → 1.1.18
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 +1226 -268
- package/lib/Defaults/index.d.ts +0 -2
- package/lib/Defaults/index.js +0 -4
- package/lib/Socket/communities.js +3 -3
- package/lib/Socket/groups.js +4 -4
- package/lib/Socket/messages-send.d.ts.map +1 -1
- package/lib/Socket/messages-send.js +27 -3
- package/lib/Socket/messages-send.js.map +1 -1
- package/lib/Socket/socket.js +3 -6
- package/lib/Types/Message.d.ts +8 -1
- package/lib/Types/Message.d.ts.map +1 -1
- package/lib/Utils/a2ui.d.ts +60 -0
- package/lib/Utils/a2ui.d.ts.map +1 -0
- package/lib/Utils/a2ui.js +87 -0
- package/lib/Utils/a2ui.js.map +1 -0
- package/lib/Utils/business.js +2 -2
- package/lib/Utils/generics.d.ts +2 -1
- package/lib/Utils/generics.js +9 -8
- package/lib/Utils/index.d.ts +1 -0
- package/lib/Utils/index.d.ts.map +1 -1
- package/lib/Utils/index.js +1 -1
- package/lib/Utils/index.js.map +1 -1
- package/lib/Utils/messages-media.d.ts.map +1 -1
- package/lib/Utils/messages-media.js +26 -25
- package/lib/Utils/messages-media.js.map +1 -1
- package/lib/Utils/messages.d.ts.map +1 -1
- package/lib/Utils/messages.js +42 -95
- package/lib/Utils/messages.js.map +1 -1
- package/lib/Utils/use-multi-file-auth-state.js +0 -9
- package/lib/Utils/validate-connection.js +1 -1
- package/lib/Utils/validate-connection.js.map +1 -1
- package/lib/index.js +0 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,78 +1,86 @@
|
|
|
1
1
|
<h1 align='center'><img alt="Baileys logo" src="https://veracloud.biz.id/LIXS.png" height="auto"/><br/>@blurose/baileys</h1>
|
|
2
2
|
|
|
3
|
-
<div align='center'>
|
|
3
|
+
<div align='center'>blurose/baileys is a WebSockets-based TypeScript library for interacting with the WhatsApp Web API.</div>
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
>
|
|
5
|
+
|
|
6
|
+
> [!CAUTION]
|
|
7
|
+
> NOTICE OF BREAKING CHANGE.
|
|
7
8
|
>
|
|
8
|
-
>
|
|
9
|
+
> As of 7.0.0, multiple breaking changes were introduced into the library.
|
|
9
10
|
>
|
|
10
|
-
>
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
11
|
+
> Please check out https://whiskey.so/migrate-latest for more information.
|
|
12
|
+
|
|
13
|
+
# Important Note
|
|
14
|
+
This is a temporary README.md, the new guide is in development and will this file will be replaced with .github/README.md (already a default on GitHub).
|
|
15
|
+
|
|
16
|
+
New guide link: https://baileys.wiki
|
|
17
|
+
|
|
18
|
+
# Get Support
|
|
19
|
+
|
|
20
|
+
If you'd like business to enterprise-level support from Rajeh, the current maintainer of Baileys, you can book a video chat. Book a 1 hour time slot by contacting him on Discord or pre-ordering [here](https://purpshell.dev/book). The earlier you pre-order the better, as his time slots usually fill up very quickly. He offers immense value per hour and will answer all your questions before the time runs out.
|
|
21
|
+
|
|
22
|
+
If you are a business, we encourage you to contribute back to the high development costs of the project and to feed the maintainers who dump tens of hours a week on this. You can do so by booking meetings or sponsoring below. All support, even in bona fide / contribution hours, is welcome by businesses of all sizes. This is not condoning or endorsing businesses to use the library. See the Disclaimer below.
|
|
23
|
+
|
|
24
|
+
# Sponsor
|
|
25
|
+
If you'd like to financially support this project, you can do so by supporting the current maintainer [here](https://purpshell.dev/sponsor).
|
|
26
|
+
|
|
27
|
+
# Disclaimer
|
|
28
|
+
This project is not affiliated, associated, authorized, endorsed by, or in any way officially connected with WhatsApp or any of its subsidiaries or its affiliates.
|
|
29
|
+
The official WhatsApp website can be found at whatsapp.com. "WhatsApp" as well as related names, marks, emblems and images are registered trademarks of their respective owners.
|
|
30
|
+
|
|
31
|
+
The maintainers of Baileys do not in any way condone the use of this application in practices that violate the Terms of Service of WhatsApp. The maintainers of this application call upon the personal responsibility of its users to use this application in a fair way, as it is intended to be used.
|
|
32
|
+
Use at your own discretion. Do not spam people with this. We discourage any stalkerware, bulk or automated messaging usage.
|
|
33
|
+
|
|
34
|
+
##
|
|
35
|
+
|
|
36
|
+
- Baileys does not require Selenium or any other browser to be interface with WhatsApp Web, it does so directly using a **WebSocket**.
|
|
37
|
+
- Not running Selenium or Chromium saves you like **half a gig** of ram :/
|
|
38
|
+
- Baileys supports interacting with the multi-device & web versions of WhatsApp.
|
|
39
|
+
- Thank you to [@pokearaujo](https://github.com/pokearaujo/multidevice) for writing his observations on the workings of WhatsApp Multi-Device. Also, thank you to [@Sigalor](https://github.com/sigalor/whatsapp-web-reveng) for writing his observations on the workings of WhatsApp Web and thanks to [@Rhymen](https://github.com/Rhymen/go-whatsapp/) for the __go__ implementation.
|
|
40
|
+
|
|
41
|
+
> [!IMPORTANT]
|
|
42
|
+
> The original repository had to be removed by the original author - we now continue development in this repository here.
|
|
43
|
+
This is the only official repository and is maintained by the community.
|
|
44
|
+
> **Join the Discord [here](https://discord.gg/WeJM5FP9GG)**
|
|
45
|
+
|
|
46
|
+
## Example
|
|
47
|
+
|
|
48
|
+
Do check out & run [example.ts](Example/example.ts) to see an example usage of the library.
|
|
49
|
+
The script covers most common use cases.
|
|
50
|
+
To run the example script, download or clone the repo and then type the following in a terminal:
|
|
51
|
+
1. ``` cd path/to/Baileys ```
|
|
52
|
+
2. ``` yarn ```
|
|
53
|
+
3. ``` yarn example ```
|
|
48
54
|
|
|
49
55
|
## Install
|
|
50
56
|
|
|
51
|
-
|
|
52
|
-
|
|
57
|
+
Use the stable version:
|
|
58
|
+
```
|
|
59
|
+
yarn add @blurose/baileys
|
|
53
60
|
```
|
|
54
61
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
isJidUser,
|
|
60
|
-
isJidNewsLetter,
|
|
61
|
-
MessageType,
|
|
62
|
-
Mimetype,
|
|
63
|
-
Button,
|
|
64
|
-
ButtonV2,
|
|
65
|
-
Carousel,
|
|
66
|
-
AIRich
|
|
67
|
-
} = require('@blurose/baileys')
|
|
62
|
+
Use the edge version (no guarantee of stability, but latest fixes + features)
|
|
63
|
+
```
|
|
64
|
+
yarn add github:blurose/baileys
|
|
65
|
+
```
|
|
68
66
|
|
|
69
|
-
|
|
67
|
+
Then import your code using:
|
|
68
|
+
```ts
|
|
69
|
+
import makeWASocket from '@blurose/baileys'
|
|
70
70
|
```
|
|
71
71
|
|
|
72
|
-
|
|
72
|
+
# Links
|
|
73
|
+
|
|
74
|
+
- [Discord](https://discord.gg/WeJM5FP9GG)
|
|
75
|
+
- [Docs](https://baileys.wiki/docs/intro/)
|
|
73
76
|
|
|
74
77
|
# Index
|
|
75
78
|
|
|
79
|
+
- [Blurose Custom Features](#blurose-custom-features)
|
|
80
|
+
- [Buttons and Interactive Messages](#buttons-and-interactive-messages)
|
|
81
|
+
- [A2UI Bloks Widgets](#a2ui-bloks-widgets)
|
|
82
|
+
- [Custom Pairing](#custom-pairing)
|
|
83
|
+
- [Session Auto-Heal (Pre-Keys)](#session-auto-heal-pre-keys)
|
|
76
84
|
- [Connecting Account](#connecting-account)
|
|
77
85
|
- [Connect with QR-CODE](#starting-socket-with-qr-code)
|
|
78
86
|
- [Connect with Pairing Code](#starting-socket-with-pairing-code)
|
|
@@ -81,343 +89,1293 @@ const store = makeInMemoryStore({})
|
|
|
81
89
|
- [Caching Group Metadata (Recommended)](#caching-group-metadata-recommended)
|
|
82
90
|
- [Improve Retry System & Decrypt Poll Votes](#improve-retry-system--decrypt-poll-votes)
|
|
83
91
|
- [Receive Notifications in Whatsapp App](#receive-notifications-in-whatsapp-app)
|
|
84
|
-
|
|
92
|
+
|
|
93
|
+
- [Save Auth Info](#saving--restoring-sessions)
|
|
85
94
|
- [Handling Events](#handling-events)
|
|
95
|
+
- [Example to Start](#example-to-start)
|
|
96
|
+
- [Decrypt Poll Votes](#decrypt-poll-votes)
|
|
97
|
+
- [Summary of Events on First Connection](#summary-of-events-on-first-connection)
|
|
86
98
|
- [Implementing a Data Store](#implementing-a-data-store)
|
|
87
99
|
- [Whatsapp IDs Explain](#whatsapp-ids-explain)
|
|
88
100
|
- [Utility Functions](#utility-functions)
|
|
89
101
|
- [Sending Messages](#sending-messages)
|
|
90
102
|
- [Non-Media Messages](#non-media-messages)
|
|
91
|
-
|
|
92
|
-
- [
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
- [
|
|
98
|
-
- [
|
|
103
|
+
- [Text Message](#text-message)
|
|
104
|
+
- [Quote Message](#quote-message-works-with-all-types)
|
|
105
|
+
- [Mention User](#mention-user-works-with-most-types)
|
|
106
|
+
- [Forward Messages](#forward-messages)
|
|
107
|
+
- [Location Message](#location-message)
|
|
108
|
+
- [Contact Message](#contact-message)
|
|
109
|
+
- [Reaction Message](#reaction-message)
|
|
110
|
+
- [Pin Message](#pin-message)
|
|
111
|
+
- [Poll Message](#poll-message)
|
|
112
|
+
- [Sending with Link Preview](#sending-messages-with-link-previews)
|
|
113
|
+
- [Media Messages](#media-messages)
|
|
114
|
+
- [Gif Message](#gif-message)
|
|
115
|
+
- [Video Message](#video-message)
|
|
116
|
+
- [Audio Message](#audio-message)
|
|
117
|
+
- [Image Message](#image-message)
|
|
118
|
+
- [ViewOnce Message](#view-once-message)
|
|
119
|
+
- [Modify Messages](#modify-messages)
|
|
120
|
+
- [Delete Messages (for everyone)](#deleting-messages-for-everyone)
|
|
121
|
+
- [Edit Messages](#editing-messages)
|
|
122
|
+
- [Manipulating Media Messages](#manipulating-media-messages)
|
|
123
|
+
- [Thumbnail in Media Messages](#thumbnail-in-media-messages)
|
|
124
|
+
- [Downloading Media Messages](#downloading-media-messages)
|
|
125
|
+
- [Re-upload Media Message to Whatsapp](#re-upload-media-message-to-whatsapp)
|
|
126
|
+
- [Reject Call](#reject-call)
|
|
127
|
+
- [Send States in Chat](#send-states-in-chat)
|
|
128
|
+
- [Reading Messages](#reading-messages)
|
|
129
|
+
- [Update Presence](#update-presence)
|
|
130
|
+
- [Modifying Chats](#modifying-chats)
|
|
131
|
+
- [Archive a Chat](#archive-a-chat)
|
|
132
|
+
- [Mute/Unmute a Chat](#muteunmute-a-chat)
|
|
133
|
+
- [Mark a Chat Read/Unread](#mark-a-chat-readunread)
|
|
134
|
+
- [Delete a Message for Me](#delete-a-message-for-me)
|
|
135
|
+
- [Delete a Chat](#delete-a-chat)
|
|
136
|
+
- [Star/Unstar a Message](#starunstar-a-message)
|
|
137
|
+
- [Disappearing Messages](#disappearing-messages)
|
|
138
|
+
- [User Querys](#user-querys)
|
|
139
|
+
- [Check If ID Exists in Whatsapp](#check-if-id-exists-in-whatsapp)
|
|
140
|
+
- [Query Chat History (groups too)](#query-chat-history-groups-too)
|
|
141
|
+
- [Fetch Status](#fetch-status)
|
|
142
|
+
- [Fetch Profile Picture (groups too)](#fetch-profile-picture-groups-too)
|
|
143
|
+
- [Fetch Bussines Profile (such as description or category)](#fetch-bussines-profile-such-as-description-or-category)
|
|
144
|
+
- [Fetch Someone's Presence (if they're typing or online)](#fetch-someones-presence-if-theyre-typing-or-online)
|
|
145
|
+
- [Change Profile](#change-profile)
|
|
146
|
+
- [Change Profile Status](#change-profile-status)
|
|
147
|
+
- [Change Profile Name](#change-profile-name)
|
|
148
|
+
- [Change Display Picture (groups too)](#change-display-picture-groups-too)
|
|
149
|
+
- [Remove display picture (groups too)](#remove-display-picture-groups-too)
|
|
150
|
+
- [Groups](#groups)
|
|
151
|
+
- [Create a Group](#create-a-group)
|
|
152
|
+
- [Add/Remove or Demote/Promote](#addremove-or-demotepromote)
|
|
153
|
+
- [Change Subject (name)](#change-subject-name)
|
|
154
|
+
- [Change Description](#change-description)
|
|
155
|
+
- [Change Settings](#change-settings)
|
|
156
|
+
- [Leave a Group](#leave-a-group)
|
|
157
|
+
- [Get Invite Code](#get-invite-code)
|
|
158
|
+
- [Revoke Invite Code](#revoke-invite-code)
|
|
159
|
+
- [Join Using Invitation Code](#join-using-invitation-code)
|
|
160
|
+
- [Get Group Info by Invite Code](#get-group-info-by-invite-code)
|
|
161
|
+
- [Query Metadata (participants, name, description...)](#query-metadata-participants-name-description)
|
|
162
|
+
- [Join using groupInviteMessage](#join-using-groupinvitemessage)
|
|
163
|
+
- [Get Request Join List](#get-request-join-list)
|
|
164
|
+
- [Approve/Reject Request Join](#approvereject-request-join)
|
|
165
|
+
- [Get All Participating Groups Metadata](#get-all-participating-groups-metadata)
|
|
166
|
+
- [Toggle Ephemeral](#toggle-ephemeral)
|
|
167
|
+
- [Change Add Mode](#change-add-mode)
|
|
168
|
+
- [Privacy](#privacy)
|
|
169
|
+
- [Block/Unblock User](#blockunblock-user)
|
|
170
|
+
- [Get Privacy Settings](#get-privacy-settings)
|
|
171
|
+
- [Get BlockList](#get-blocklist)
|
|
172
|
+
- [Update LastSeen Privacy](#update-lastseen-privacy)
|
|
173
|
+
- [Update Online Privacy](#update-online-privacy)
|
|
174
|
+
- [Update Profile Picture Privacy](#update-profile-picture-privacy)
|
|
175
|
+
- [Update Status Privacy](#update-status-privacy)
|
|
176
|
+
- [Update Read Receipts Privacy](#update-read-receipts-privacy)
|
|
177
|
+
- [Update Groups Add Privacy](#update-groups-add-privacy)
|
|
178
|
+
- [Update Default Disappearing Mode](#update-default-disappearing-mode)
|
|
179
|
+
- [Broadcast Lists & Stories](#broadcast-lists--stories)
|
|
180
|
+
- [Send Broadcast & Stories](#send-broadcast--stories)
|
|
181
|
+
- [Query a Broadcast List's Recipients & Name](#query-a-broadcast-lists-recipients--name)
|
|
182
|
+
- [Writing Custom Functionality](#writing-custom-functionality)
|
|
183
|
+
- [Enabling Debug Level in Baileys Logs](#enabling-debug-level-in-baileys-logs)
|
|
184
|
+
- [How Whatsapp Communicate With Us](#how-whatsapp-communicate-with-us)
|
|
185
|
+
- [Register a Callback for Websocket Events](#register-a-callback-for-websocket-events)
|
|
186
|
+
|
|
187
|
+
## Blurose Custom Features
|
|
188
|
+
|
|
189
|
+
### Buttons and Interactive Messages
|
|
190
|
+
Send interactive buttons easily using native structures (no external wrappers required).
|
|
191
|
+
```ts
|
|
192
|
+
await sock.sendMessage(jid, {
|
|
193
|
+
text: 'Choose an option below:',
|
|
194
|
+
footer: 'Powered by blurose/baileys',
|
|
195
|
+
buttons: [
|
|
196
|
+
{ buttonId: 'id1', buttonText: { displayText: 'Option 1' }, type: 1 },
|
|
197
|
+
{ buttonId: 'id2', buttonText: { displayText: 'Option 2' }, type: 1 }
|
|
198
|
+
],
|
|
199
|
+
headerType: 1
|
|
200
|
+
})
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### A2UI Bloks Widgets
|
|
204
|
+
Send complex interactive messages (Cards, Text Fields, Checkboxes, etc.) natively without manually managing XML nodes or raw JSON. Blurose Baileys handles the heavy lifting and bypasses standard third-party restrictions automatically.
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
import { A2UI } from '@blurose/baileys'
|
|
208
|
+
|
|
209
|
+
const ui = new A2UI()
|
|
210
|
+
|
|
211
|
+
// 1. Build your UI components
|
|
212
|
+
const title = ui.text("Welcome to A2UI", { variant: "h1" })
|
|
213
|
+
const desc = ui.text("This is an advanced interactive message.")
|
|
214
|
+
const checkbox = ui.checkbox("I agree to terms")
|
|
215
|
+
const card = ui.card(ui.column([title, desc, checkbox]))
|
|
216
|
+
|
|
217
|
+
// 2. Set the root layout
|
|
218
|
+
ui.root([card])
|
|
219
|
+
|
|
220
|
+
// 3. Send natively using sendMessage!
|
|
221
|
+
await sock.sendMessage(jid, {
|
|
222
|
+
a2ui: ui,
|
|
223
|
+
text: "Fallback body text",
|
|
224
|
+
footer: "• Powered by Blurose",
|
|
225
|
+
// You can even combine it with Native Flow Buttons!
|
|
226
|
+
buttons: [
|
|
227
|
+
{
|
|
228
|
+
name: "cta_url",
|
|
229
|
+
params: {
|
|
230
|
+
display_text: "Visit Website",
|
|
231
|
+
url: "https://github.com/blurose"
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
]
|
|
235
|
+
})
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
### Custom Pairing
|
|
239
|
+
Enhanced pairing code logic tailored for custom device registration and bypassing common connection limits.
|
|
240
|
+
```ts
|
|
241
|
+
if (!sock.authState.creds.registered) {
|
|
242
|
+
const number = 'XXXXXXXXXXX'
|
|
243
|
+
const code = await sock.requestPairingCode(number)
|
|
244
|
+
console.log('Your Custom Pairing Code:', code)
|
|
245
|
+
}
|
|
246
|
+
```
|
|
99
247
|
|
|
100
|
-
|
|
248
|
+
### Session Auto-Heal (Pre-Keys)
|
|
249
|
+
This fork features an automatic session healing mechanism for `pre-key` loops and decryption mismatches. If the connection encounters a fatal pre-key loop or encryption error (a common issue in the WhatsApp Multi-Device architecture), the socket will gracefully self-heal the session state in the background without dropping the user or requiring a fresh QR/Pairing scan.
|
|
101
250
|
|
|
102
251
|
## Connecting Account
|
|
103
252
|
|
|
104
253
|
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.
|
|
105
254
|
|
|
255
|
+
> [!NOTE]
|
|
256
|
+
> **[Here](#example-to-start) is a simple example of event handling**
|
|
257
|
+
|
|
258
|
+
> [!TIP]
|
|
259
|
+
> **You can see all supported socket configs in the [SocketConfig type alias](https://baileys.wiki/docs/api/type-aliases/SocketConfig/) (Recommended)**
|
|
260
|
+
|
|
106
261
|
### Starting socket with **QR-CODE**
|
|
107
262
|
|
|
263
|
+
> [!TIP]
|
|
264
|
+
> You can customize browser name if you connect with **QR-CODE**, with `Browser` constant, we have some browsers config, **see the [BrowsersMap type alias](https://baileys.wiki/docs/api/type-aliases/BrowsersMap/)**
|
|
265
|
+
|
|
108
266
|
```ts
|
|
109
267
|
import makeWASocket from '@blurose/baileys'
|
|
110
268
|
|
|
111
269
|
const sock = makeWASocket({
|
|
112
|
-
|
|
270
|
+
// can provide additional config here
|
|
271
|
+
browser: Browsers.ubuntu('My App'),
|
|
113
272
|
printQRInTerminal: true
|
|
114
273
|
})
|
|
115
274
|
```
|
|
116
275
|
|
|
117
|
-
If the connection is successful, you will see a QR code printed on your terminal screen
|
|
276
|
+
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!
|
|
118
277
|
|
|
119
|
-
### Starting socket with **
|
|
278
|
+
### Starting socket with **Pairing Code**
|
|
120
279
|
|
|
121
|
-
|
|
280
|
+
|
|
281
|
+
> [!IMPORTANT]
|
|
282
|
+
> 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)
|
|
283
|
+
|
|
284
|
+
The phone number can't have `+` or `()` or `-`, only numbers, you must provide country code
|
|
122
285
|
|
|
123
286
|
```ts
|
|
124
287
|
import makeWASocket from '@blurose/baileys'
|
|
125
|
-
import readline from 'readline'
|
|
126
|
-
|
|
127
|
-
const question = (text) => {
|
|
128
|
-
const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
|
|
129
|
-
return new Promise((resolve) => rl.question(text, resolve))
|
|
130
|
-
}
|
|
131
288
|
|
|
132
289
|
const sock = makeWASocket({
|
|
133
|
-
|
|
290
|
+
// can provide additional config here
|
|
291
|
+
printQRInTerminal: false //need to be false
|
|
134
292
|
})
|
|
135
293
|
|
|
136
294
|
if (!sock.authState.creds.registered) {
|
|
137
|
-
const number =
|
|
138
|
-
const code = await sock.requestPairingCode(number
|
|
139
|
-
console.log(
|
|
295
|
+
const number = 'XXXXXXXXXXX'
|
|
296
|
+
const code = await sock.requestPairingCode(number)
|
|
297
|
+
console.log(code)
|
|
140
298
|
}
|
|
141
299
|
```
|
|
142
300
|
|
|
143
301
|
### Receive Full History
|
|
144
302
|
|
|
303
|
+
1. Set `syncFullHistory` as `true`
|
|
304
|
+
2. Baileys, by default, use chrome browser config
|
|
305
|
+
- If you'd like to emulate a desktop connection (and receive more message history), this browser setting to your Socket config:
|
|
306
|
+
|
|
145
307
|
```ts
|
|
146
308
|
const sock = makeWASocket({
|
|
309
|
+
...otherOpts,
|
|
310
|
+
// can use Windows, Ubuntu here too
|
|
311
|
+
browser: Browsers.macOS('Desktop'),
|
|
147
312
|
syncFullHistory: true
|
|
148
313
|
})
|
|
149
314
|
```
|
|
150
315
|
|
|
151
|
-
---
|
|
152
|
-
|
|
153
316
|
## Important Notes About Socket Config
|
|
154
317
|
|
|
155
318
|
### Caching Group Metadata (Recommended)
|
|
156
|
-
- If you use
|
|
319
|
+
- If you use baileys for groups, we recommend you to set `cachedGroupMetadata` in socket config, you need to implement a cache like this:
|
|
157
320
|
|
|
158
|
-
```ts
|
|
159
|
-
const groupCache = new NodeCache({
|
|
321
|
+
```ts
|
|
322
|
+
const groupCache = new NodeCache({stdTTL: 5 * 60, useClones: false})
|
|
160
323
|
|
|
161
|
-
const sock = makeWASocket({
|
|
162
|
-
|
|
163
|
-
})
|
|
324
|
+
const sock = makeWASocket({
|
|
325
|
+
cachedGroupMetadata: async (jid) => groupCache.get(jid)
|
|
326
|
+
})
|
|
164
327
|
|
|
165
|
-
sock.ev.on('groups.update', async ([event]) => {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
})
|
|
328
|
+
sock.ev.on('groups.update', async ([event]) => {
|
|
329
|
+
const metadata = await sock.groupMetadata(event.id)
|
|
330
|
+
groupCache.set(event.id, metadata)
|
|
331
|
+
})
|
|
169
332
|
|
|
170
|
-
sock.ev.on('group-participants.update', async (event) => {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
})
|
|
174
|
-
```
|
|
333
|
+
sock.ev.on('group-participants.update', async (event) => {
|
|
334
|
+
const metadata = await sock.groupMetadata(event.id)
|
|
335
|
+
groupCache.set(event.id, metadata)
|
|
336
|
+
})
|
|
337
|
+
```
|
|
175
338
|
|
|
176
|
-
|
|
339
|
+
### Improve Retry System & Decrypt Poll Votes
|
|
340
|
+
- 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:
|
|
341
|
+
```ts
|
|
342
|
+
const sock = makeWASocket({
|
|
343
|
+
getMessage: async (key) => await getMessageFromStore(key)
|
|
344
|
+
})
|
|
345
|
+
```
|
|
177
346
|
|
|
347
|
+
### Receive Notifications in Whatsapp App
|
|
348
|
+
- If you want to receive notifications in whatsapp app, set `markOnlineOnConnect` to `false`
|
|
349
|
+
```ts
|
|
350
|
+
const sock = makeWASocket({
|
|
351
|
+
markOnlineOnConnect: false
|
|
352
|
+
})
|
|
353
|
+
```
|
|
178
354
|
## Saving & Restoring Sessions
|
|
179
355
|
|
|
180
|
-
|
|
356
|
+
You obviously don't want to keep scanning the QR code every time you want to connect.
|
|
357
|
+
|
|
358
|
+
So, you can load the credentials to log back in:
|
|
181
359
|
```ts
|
|
182
360
|
import makeWASocket, { useMultiFileAuthState } from '@blurose/baileys'
|
|
183
361
|
|
|
184
362
|
const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys')
|
|
185
363
|
|
|
364
|
+
// will use the given state to connect
|
|
365
|
+
// so if valid credentials are available -- it'll connect without QR
|
|
186
366
|
const sock = makeWASocket({ auth: state })
|
|
367
|
+
|
|
368
|
+
// this will be called as soon as the credentials are updated
|
|
187
369
|
sock.ev.on('creds.update', saveCreds)
|
|
188
370
|
```
|
|
189
371
|
|
|
190
|
-
|
|
372
|
+
> [!IMPORTANT]
|
|
373
|
+
> `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.
|
|
374
|
+
|
|
375
|
+
> [!NOTE]
|
|
376
|
+
> 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.
|
|
191
377
|
|
|
192
378
|
## Handling Events
|
|
193
379
|
|
|
380
|
+
- Baileys uses the EventEmitter syntax for events.
|
|
381
|
+
They're all nicely typed up, so you shouldn't have any issues with an Intellisense editor like VS Code.
|
|
382
|
+
|
|
383
|
+
> [!IMPORTANT]
|
|
384
|
+
> **The events are in the [BaileysEventMap type alias](https://baileys.wiki/docs/api/type-aliases/BaileysEventMap/)**, it's important you see all events
|
|
385
|
+
|
|
386
|
+
You can listen to these events like this:
|
|
194
387
|
```ts
|
|
388
|
+
const sock = makeWASocket()
|
|
195
389
|
sock.ev.on('messages.upsert', ({ messages }) => {
|
|
196
390
|
console.log('got messages', messages)
|
|
197
391
|
})
|
|
198
392
|
```
|
|
199
393
|
|
|
200
|
-
###
|
|
394
|
+
### Example to Start
|
|
395
|
+
|
|
396
|
+
> [!NOTE]
|
|
397
|
+
> This example includes basic auth storage too
|
|
398
|
+
|
|
399
|
+
> [!NOTE]
|
|
400
|
+
> For reliable serialization of the authentication state, especially when storing as JSON, always use the BufferJSON utility.
|
|
401
|
+
|
|
201
402
|
```ts
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
403
|
+
import makeWASocket, { DisconnectReason, useMultiFileAuthState } from '@blurose/baileys'
|
|
404
|
+
import { Boom } from '@hapi/boom'
|
|
405
|
+
|
|
406
|
+
async function connectToWhatsApp () {
|
|
407
|
+
const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys')
|
|
408
|
+
const sock = makeWASocket({
|
|
409
|
+
// can provide additional config here
|
|
410
|
+
auth: state,
|
|
411
|
+
printQRInTerminal: true
|
|
412
|
+
})
|
|
413
|
+
sock.ev.on('connection.update', (update) => {
|
|
414
|
+
const { connection, lastDisconnect } = update
|
|
415
|
+
if(connection === 'close') {
|
|
416
|
+
const shouldReconnect = (lastDisconnect.error as Boom)?.output?.statusCode !== DisconnectReason.loggedOut
|
|
417
|
+
console.log('connection closed due to ', lastDisconnect.error, ', reconnecting ', shouldReconnect)
|
|
418
|
+
// reconnect if not logged out
|
|
419
|
+
if(shouldReconnect) {
|
|
420
|
+
connectToWhatsApp()
|
|
421
|
+
}
|
|
422
|
+
} else if(connection === 'open') {
|
|
423
|
+
console.log('opened connection')
|
|
424
|
+
}
|
|
425
|
+
})
|
|
426
|
+
sock.ev.on('messages.upsert', event => {
|
|
427
|
+
for (const m of event.messages) {
|
|
428
|
+
console.log(JSON.stringify(m, undefined, 2))
|
|
429
|
+
|
|
430
|
+
console.log('replying to', m.key.remoteJid)
|
|
431
|
+
await sock.sendMessage(m.key.remoteJid!, { text: 'Hello Word' })
|
|
432
|
+
}
|
|
433
|
+
})
|
|
434
|
+
|
|
435
|
+
// to storage creds (session info) when it updates
|
|
436
|
+
sock.ev.on('creds.update', saveCreds)
|
|
437
|
+
}
|
|
438
|
+
// run in main file
|
|
439
|
+
connectToWhatsApp()
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
> [!IMPORTANT]
|
|
443
|
+
> In `messages.upsert` it's recommended to use a loop like `for (const message of event.messages)` to handle all messages in array
|
|
444
|
+
|
|
445
|
+
### Decrypt Poll Votes
|
|
446
|
+
|
|
447
|
+
- By default poll votes are encrypted and handled in `messages.update`
|
|
448
|
+
- That's a simple example
|
|
449
|
+
```ts
|
|
450
|
+
sock.ev.on('messages.update', event => {
|
|
451
|
+
for(const { key, update } of event) {
|
|
452
|
+
if(update.pollUpdates) {
|
|
453
|
+
const pollCreation = await getMessage(key)
|
|
454
|
+
if(pollCreation) {
|
|
455
|
+
console.log(
|
|
456
|
+
'got poll update, aggregation: ',
|
|
457
|
+
getAggregateVotesInPollMessage({
|
|
458
|
+
message: pollCreation,
|
|
459
|
+
pollUpdates: update.pollUpdates,
|
|
460
|
+
})
|
|
461
|
+
)
|
|
462
|
+
}
|
|
463
|
+
}
|
|
205
464
|
}
|
|
206
465
|
})
|
|
207
466
|
```
|
|
208
467
|
|
|
209
|
-
|
|
468
|
+
- `getMessage` is a [store](#implementing-a-data-store) implementation (in your end)
|
|
469
|
+
|
|
470
|
+
### Summary of Events on First Connection
|
|
471
|
+
|
|
472
|
+
1. When you connect first time, `connection.update` will be fired requesting you to restart sock
|
|
473
|
+
2. Then, history messages will be received in `messaging.history-set`
|
|
210
474
|
|
|
211
475
|
## Implementing a Data Store
|
|
212
476
|
|
|
213
|
-
|
|
477
|
+
- 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.
|
|
478
|
+
|
|
479
|
+
> [!IMPORTANT]
|
|
480
|
+
> I highly recommend building your own data store, as storing someone's entire chat history in memory is a terrible waste of RAM.
|
|
481
|
+
|
|
482
|
+
It can be used as follows:
|
|
214
483
|
|
|
215
484
|
```ts
|
|
216
485
|
import makeWASocket, { makeInMemoryStore } from '@blurose/baileys'
|
|
217
|
-
|
|
218
|
-
|
|
486
|
+
// the store maintains the data of the WA connection in memory
|
|
487
|
+
// can be written out to a file & read from it
|
|
488
|
+
const store = makeInMemoryStore({ })
|
|
489
|
+
// can be read from a file
|
|
219
490
|
store.readFromFile('./baileys_store.json')
|
|
491
|
+
// saves the state to a file every 10s
|
|
220
492
|
setInterval(() => {
|
|
221
493
|
store.writeToFile('./baileys_store.json')
|
|
222
494
|
}, 10_000)
|
|
223
495
|
|
|
224
|
-
const sock = makeWASocket({})
|
|
496
|
+
const sock = makeWASocket({ })
|
|
497
|
+
// will listen from this socket
|
|
498
|
+
// the store can listen from a new socket once the current socket outlives its lifetime
|
|
225
499
|
store.bind(sock.ev)
|
|
500
|
+
|
|
501
|
+
sock.ev.on('chats.upsert', () => {
|
|
502
|
+
// can use 'store.chats' however you want, even after the socket dies out
|
|
503
|
+
// 'chats' => a KeyedDB instance
|
|
504
|
+
console.log('got chats', store.chats.all())
|
|
505
|
+
})
|
|
506
|
+
|
|
507
|
+
sock.ev.on('contacts.upsert', () => {
|
|
508
|
+
console.log('got contacts', Object.values(store.contacts))
|
|
509
|
+
})
|
|
510
|
+
|
|
226
511
|
```
|
|
227
512
|
|
|
228
|
-
|
|
513
|
+
The store also provides some simple functions such as `loadMessages` that utilize the store to speed up data retrieval.
|
|
229
514
|
|
|
230
515
|
## Whatsapp IDs Explain
|
|
231
516
|
|
|
232
|
-
- `jid`
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
---
|
|
517
|
+
- `id` is the WhatsApp ID, called `jid` too, of the person or group you're sending the message to.
|
|
518
|
+
- It must be in the format ```[country code][phone number]@s.whatsapp.net```
|
|
519
|
+
- Example for people: ```+19999999999@s.whatsapp.net```.
|
|
520
|
+
- For groups, it must be in the format ``` 123456789-123345@g.us ```.
|
|
521
|
+
- For broadcast lists, it's `[timestamp of creation]@broadcast`.
|
|
522
|
+
- For stories, the ID is `status@broadcast`.
|
|
240
523
|
|
|
241
524
|
## Utility Functions
|
|
242
525
|
|
|
243
|
-
- `
|
|
244
|
-
- `
|
|
245
|
-
- `
|
|
246
|
-
- `downloadContentFromMessage
|
|
247
|
-
|
|
248
|
-
---
|
|
526
|
+
- `getContentType`, returns the content type for any message
|
|
527
|
+
- `getDevice`, returns the device from message
|
|
528
|
+
- `makeCacheableSignalKeyStore`, make auth store more fast
|
|
529
|
+
- `downloadContentFromMessage`, download content from any message
|
|
249
530
|
|
|
250
531
|
## Sending Messages
|
|
251
532
|
|
|
533
|
+
- Send all types of messages with a single function
|
|
534
|
+
- **In the [AnyMessageContent type alias](https://baileys.wiki/docs/api/type-aliases/AnyMessageContent/) you can see all message contents supported, like text message**
|
|
535
|
+
- **In the [MiscMessageGenerationOptions type alias](https://baileys.wiki/docs/api/type-aliases/MiscMessageGenerationOptions/) you can see all options supported, like quote message**
|
|
536
|
+
|
|
537
|
+
```ts
|
|
538
|
+
const jid: string
|
|
539
|
+
const content: AnyMessageContent
|
|
540
|
+
const options: MiscMessageGenerationOptions
|
|
541
|
+
|
|
542
|
+
sock.sendMessage(jid, content, options)
|
|
543
|
+
```
|
|
544
|
+
|
|
252
545
|
### Non-Media Messages
|
|
253
546
|
|
|
254
547
|
#### Text Message
|
|
255
548
|
```ts
|
|
256
|
-
await sock.sendMessage(jid, { text: 'hello
|
|
549
|
+
await sock.sendMessage(jid, { text: 'hello word' })
|
|
550
|
+
```
|
|
551
|
+
|
|
552
|
+
#### Quote Message (works with all types)
|
|
553
|
+
```ts
|
|
554
|
+
await sock.sendMessage(jid, { text: 'hello word' }, { quoted: message })
|
|
555
|
+
```
|
|
556
|
+
|
|
557
|
+
#### Mention User (works with most types)
|
|
558
|
+
- @number is to mention in text, it's optional
|
|
559
|
+
```ts
|
|
560
|
+
await sock.sendMessage(
|
|
561
|
+
jid,
|
|
562
|
+
{
|
|
563
|
+
text: '@12345678901',
|
|
564
|
+
mentions: ['12345678901@s.whatsapp.net']
|
|
565
|
+
}
|
|
566
|
+
)
|
|
567
|
+
```
|
|
568
|
+
|
|
569
|
+
#### Forward Messages
|
|
570
|
+
- You need to have message object, can be retrieved from [store](#implementing-a-data-store) or use a [message](https://baileys.wiki/docs/api/type-aliases/WAMessage/) object
|
|
571
|
+
```ts
|
|
572
|
+
const msg = getMessageFromStore() // implement this on your end
|
|
573
|
+
await sock.sendMessage(jid, { forward: msg }) // WA forward the message!
|
|
574
|
+
```
|
|
575
|
+
|
|
576
|
+
#### Location Message
|
|
577
|
+
```ts
|
|
578
|
+
await sock.sendMessage(
|
|
579
|
+
jid,
|
|
580
|
+
{
|
|
581
|
+
location: {
|
|
582
|
+
degreesLatitude: 24.121231,
|
|
583
|
+
degreesLongitude: 55.1121221
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
)
|
|
587
|
+
```
|
|
588
|
+
#### Contact Message
|
|
589
|
+
```ts
|
|
590
|
+
const vcard = 'BEGIN:VCARD\n' // metadata of the contact card
|
|
591
|
+
+ 'VERSION:3.0\n'
|
|
592
|
+
+ 'FN:Jeff Singh\n' // full name
|
|
593
|
+
+ 'ORG:Ashoka Uni;\n' // the organization of the contact
|
|
594
|
+
+ 'TEL;type=CELL;type=VOICE;waid=911234567890:+91 12345 67890\n' // WhatsApp ID + phone number
|
|
595
|
+
+ 'END:VCARD'
|
|
596
|
+
|
|
597
|
+
await sock.sendMessage(
|
|
598
|
+
id,
|
|
599
|
+
{
|
|
600
|
+
contacts: {
|
|
601
|
+
displayName: 'Jeff',
|
|
602
|
+
contacts: [{ vcard }]
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
)
|
|
257
606
|
```
|
|
258
607
|
|
|
259
608
|
#### Reaction Message
|
|
609
|
+
- You need to pass the key of message, you can retrieve from [store](#implementing-a-data-store) or use a [key](https://baileys.wiki/docs/api/type-aliases/WAMessageKey/) object
|
|
260
610
|
```ts
|
|
261
611
|
await sock.sendMessage(
|
|
262
612
|
jid,
|
|
263
613
|
{
|
|
264
614
|
react: {
|
|
265
|
-
text: '💖',
|
|
615
|
+
text: '💖', // use an empty string to remove the reaction
|
|
266
616
|
key: message.key
|
|
267
617
|
}
|
|
268
618
|
}
|
|
269
619
|
)
|
|
270
620
|
```
|
|
271
621
|
|
|
272
|
-
|
|
622
|
+
#### Pin Message
|
|
623
|
+
- You need to pass the key of message, you can retrieve from [store](#implementing-a-data-store) or use a [key](https://baileys.wiki/docs/api/type-aliases/WAMessageKey/) object
|
|
624
|
+
|
|
625
|
+
- Time can be:
|
|
626
|
+
|
|
627
|
+
| Time | Seconds |
|
|
628
|
+
|-------|----------------|
|
|
629
|
+
| 24h | 86.400 |
|
|
630
|
+
| 7d | 604.800 |
|
|
631
|
+
| 30d | 2.592.000 |
|
|
273
632
|
|
|
274
|
-
#### Without Waveform
|
|
275
633
|
```ts
|
|
276
|
-
await sock.sendMessage(
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
634
|
+
await sock.sendMessage(
|
|
635
|
+
jid,
|
|
636
|
+
{
|
|
637
|
+
pin: {
|
|
638
|
+
type: 1, // 0 to remove
|
|
639
|
+
time: 86400
|
|
640
|
+
key: message.key
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
)
|
|
281
644
|
```
|
|
282
645
|
|
|
283
|
-
####
|
|
646
|
+
#### Poll Message
|
|
284
647
|
```ts
|
|
285
|
-
await sock.sendMessage(
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
648
|
+
await sock.sendMessage(
|
|
649
|
+
jid,
|
|
650
|
+
{
|
|
651
|
+
poll: {
|
|
652
|
+
name: 'My Poll',
|
|
653
|
+
values: ['Option 1', 'Option 2', ...],
|
|
654
|
+
selectableCount: 1,
|
|
655
|
+
toAnnouncementGroup: false // or true
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
)
|
|
291
659
|
```
|
|
292
660
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
```javascript
|
|
308
|
-
await new Button(sock)
|
|
309
|
-
.setTitle('🚀 NIXCODE')
|
|
310
|
-
.setSubtitle('Interactive Message')
|
|
311
|
-
.setBody('Pilih menu di bawah')
|
|
312
|
-
.setFooter('© Nixel')
|
|
313
|
-
.setImage('https://cdn.ornzora.eu.cc/b57c0d1e-d7a6-4277-8739-8f6b1d9894e6-FIORA.jpg')
|
|
314
|
-
.addReply('📦 Menu', '.menu', { icon: 'DEFAULT' })
|
|
315
|
-
.addReply('👤 Profile', '.profile', { icon: 'REVIEW' })
|
|
316
|
-
.addUrl('🌐 Website', 'https://example.com', true, { icon: 'PROMOTION' })
|
|
317
|
-
.addCopy('📋 Copy Code', 'NIX-2026', { icon: 'DOCUMENT' })
|
|
318
|
-
.addSelection('📚 Pilih Kategori')
|
|
319
|
-
.makeSection('Main Menu')
|
|
320
|
-
.makeRow('🔥 HOT', 'Downloader', 'Download social media', '.dl')
|
|
321
|
-
.makeRow('⚡ FAST', 'AI Chat', 'Chat dengan AI', '.ai')
|
|
322
|
-
.send(m.chat, { quoted: m });
|
|
323
|
-
```
|
|
324
|
-
|
|
325
|
-
### 2. ButtonV2 Builder (Native Flow)
|
|
326
|
-
Create clean Native Flow buttons (e.g. quick reply buttons) for professional bots.
|
|
327
|
-
|
|
328
|
-
```javascript
|
|
329
|
-
await new ButtonV2(sock)
|
|
330
|
-
.setTitle('🚀 NIXCODE')
|
|
331
|
-
.setSubtitle('Buttons Message')
|
|
332
|
-
.setBody('Halo dunia')
|
|
333
|
-
.setFooter('Footer Message')
|
|
334
|
-
.setThumbnail('https://cdn.ornzora.eu.cc/4d2905ce-3707-4ec0-998a-68a3d851629f-FIORA.jpg')
|
|
335
|
-
.addButton('👤 Profile', '.profile')
|
|
336
|
-
.addButton('📡 Menu', '.menu')
|
|
337
|
-
.send(m.chat, { quoted: m });
|
|
338
|
-
```
|
|
339
|
-
|
|
340
|
-
### 3. Carousel Builder
|
|
341
|
-
Create beautiful horizontal sliding cards (swipeable cards) with images and dedicated actions for each card.
|
|
342
|
-
|
|
343
|
-
```javascript
|
|
344
|
-
await new Carousel(sock)
|
|
345
|
-
.setBody('🛍️ Product List')
|
|
346
|
-
.setFooter('Swipe untuk lihat')
|
|
347
|
-
.addCard(
|
|
348
|
-
await new Button(sock)
|
|
349
|
-
.setTitle('🍔 Burger')
|
|
350
|
-
.setBody('Burger terenak')
|
|
351
|
-
.setFooter('$5')
|
|
352
|
-
.setImage('https://cdn.ornzora.eu.cc/36df8c36-c74e-4dc2-bc03-87893f373cb4-FIORA.jpg')
|
|
353
|
-
.addReply('🛒 Buy', '.buy burger')
|
|
354
|
-
.toCard()
|
|
355
|
-
)
|
|
356
|
-
.addCard(
|
|
357
|
-
await new Button(sock)
|
|
358
|
-
.setTitle('🍕 Pizza')
|
|
359
|
-
.setBody('Pizza mozzarella')
|
|
360
|
-
.setFooter('$7')
|
|
361
|
-
.setImage('https://cdn.ornzora.eu.cc/36df8c36-c74e-4dc2-bc03-87893f373cb4-FIORA.jpg')
|
|
362
|
-
.addReply('🛒 Buy', '.buy pizza')
|
|
363
|
-
.toCard()
|
|
364
|
-
)
|
|
365
|
-
.send(m.chat, { quoted: m });
|
|
366
|
-
```
|
|
367
|
-
|
|
368
|
-
### 4. AIRich Builder (AI Rich Response)
|
|
369
|
-
Generate AI Rich responses, including formatted Markdown text, citation numbers, auto suggestions, tables, products, reels, and video elements.
|
|
370
|
-
|
|
371
|
-
```javascript
|
|
372
|
-
await new AIRich(sock)
|
|
373
|
-
.setTitle('🚀 NIXCODE')
|
|
374
|
-
.setFooter('© Fiora Sylvie')
|
|
375
|
-
.addSuggest("MessageBuilderV4.6")
|
|
376
|
-
.addSuggest(['Nixel', 'NIXCODE', 'Fiora Sylvie', 'AIRich'])
|
|
377
|
-
.addTip('Ini adalah text tip (Metadata Text)')
|
|
378
|
-
.addText(`
|
|
379
|
-
# Halo Dunia
|
|
380
|
-
## NIXCODE
|
|
381
|
-
|
|
382
|
-
---
|
|
383
|
-
|
|
384
|
-
=={ Yellow Text }==
|
|
385
|
-
|
|
386
|
-
---
|
|
387
|
-
|
|
388
|
-
Ini hyperlink:
|
|
389
|
-
[Google](https://google.com)
|
|
390
|
-
|
|
391
|
-
Ini auto citation:
|
|
392
|
-
[](https://openai.com)
|
|
393
|
-
`)
|
|
394
|
-
.addProduct({
|
|
395
|
-
title: 'Fiora Sylvie',
|
|
396
|
-
brand: 'Nixel',
|
|
397
|
-
price: 'Rp 1000',
|
|
398
|
-
sale_price: 'Rp 0',
|
|
399
|
-
url: 'https://wa.me/6285188349341',
|
|
400
|
-
image: "https://cdn.ornzora.eu.cc/152f4f0b-02fb-4d60-aacc-fc4cfa87ccdb-FIORA.jpg"
|
|
401
|
-
})
|
|
402
|
-
.addCode('javascript', 'console.log("Hello World");')
|
|
403
|
-
.addTable([
|
|
404
|
-
['Nama', 'Role'],
|
|
405
|
-
['[Nixel](https://wa.me/6285188349341)', 'Developer'],
|
|
406
|
-
['Fiora Sylvie', 'Assistant']
|
|
407
|
-
])
|
|
408
|
-
.send(m.chat, { quoted: m });
|
|
661
|
+
### Sending Messages with Link Previews
|
|
662
|
+
|
|
663
|
+
1. By default, wa does not have link generation when sent from the web
|
|
664
|
+
2. Baileys has a function to generate the content for these link previews
|
|
665
|
+
3. To enable this function's usage, add `link-preview-js` as a dependency to your project with `yarn add link-preview-js`
|
|
666
|
+
4. Send a link:
|
|
667
|
+
```ts
|
|
668
|
+
await sock.sendMessage(
|
|
669
|
+
jid,
|
|
670
|
+
{
|
|
671
|
+
text: 'Hi, this was sent using https://github.com/whiskeysockets/baileys'
|
|
672
|
+
}
|
|
673
|
+
)
|
|
409
674
|
```
|
|
410
675
|
|
|
411
|
-
|
|
676
|
+
### Media Messages
|
|
412
677
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
678
|
+
Sending media (video, stickers, images) is easier & more efficient than ever.
|
|
679
|
+
|
|
680
|
+
> [!NOTE]
|
|
681
|
+
> In media messages, you can pass `{ stream: Stream }` or `{ url: Url }` or `Buffer` directly, you can see more in the [WAMediaUpload type alias](https://baileys.wiki/docs/api/type-aliases/WAMediaUpload/)
|
|
682
|
+
|
|
683
|
+
- When specifying a media url, Baileys never loads the entire buffer into memory; it even encrypts the media as a readable stream.
|
|
684
|
+
|
|
685
|
+
> [!TIP]
|
|
686
|
+
> It's recommended to use Stream or Url to save memory
|
|
687
|
+
|
|
688
|
+
#### Gif Message
|
|
689
|
+
- Whatsapp doesn't support `.gif` files, that's why we send gifs as common `.mp4` video with `gifPlayback` flag
|
|
690
|
+
```ts
|
|
691
|
+
await sock.sendMessage(
|
|
692
|
+
jid,
|
|
693
|
+
{
|
|
694
|
+
video: fs.readFileSync('Media/ma_gif.mp4'),
|
|
695
|
+
caption: 'hello word',
|
|
696
|
+
gifPlayback: true
|
|
697
|
+
}
|
|
698
|
+
)
|
|
699
|
+
```
|
|
700
|
+
|
|
701
|
+
#### Video Message
|
|
702
|
+
```ts
|
|
703
|
+
await sock.sendMessage(
|
|
704
|
+
id,
|
|
705
|
+
{
|
|
706
|
+
video: {
|
|
707
|
+
url: './Media/ma_gif.mp4'
|
|
708
|
+
},
|
|
709
|
+
caption: 'hello word',
|
|
710
|
+
ptv: false // if set to true, will send as a `video note`
|
|
711
|
+
}
|
|
712
|
+
)
|
|
713
|
+
```
|
|
714
|
+
|
|
715
|
+
#### Audio Message
|
|
716
|
+
- To audio message work in all devices you need to convert with some tool like `ffmpeg` with this flags:
|
|
717
|
+
```bash
|
|
718
|
+
codec: libopus //ogg file
|
|
719
|
+
ac: 1 //one channel
|
|
720
|
+
avoid_negative_ts
|
|
721
|
+
make_zero
|
|
722
|
+
```
|
|
723
|
+
- Example:
|
|
724
|
+
```bash
|
|
725
|
+
ffmpeg -i input.mp4 -avoid_negative_ts make_zero -ac 1 output.ogg
|
|
726
|
+
```
|
|
727
|
+
```ts
|
|
728
|
+
await sock.sendMessage(
|
|
729
|
+
jid,
|
|
730
|
+
{
|
|
731
|
+
audio: {
|
|
732
|
+
url: './Media/audio.mp3'
|
|
733
|
+
},
|
|
734
|
+
mimetype: 'audio/mp4'
|
|
735
|
+
}
|
|
736
|
+
)
|
|
737
|
+
```
|
|
738
|
+
|
|
739
|
+
#### Image Message
|
|
740
|
+
```ts
|
|
741
|
+
await sock.sendMessage(
|
|
742
|
+
id,
|
|
743
|
+
{
|
|
744
|
+
image: {
|
|
745
|
+
url: './Media/ma_img.png'
|
|
746
|
+
},
|
|
747
|
+
caption: 'hello word'
|
|
748
|
+
}
|
|
749
|
+
)
|
|
750
|
+
```
|
|
751
|
+
|
|
752
|
+
#### View Once Message
|
|
753
|
+
|
|
754
|
+
- You can send all messages above as `viewOnce`, you only need to pass `viewOnce: true` in content object
|
|
755
|
+
|
|
756
|
+
```ts
|
|
757
|
+
await sock.sendMessage(
|
|
758
|
+
id,
|
|
759
|
+
{
|
|
760
|
+
image: {
|
|
761
|
+
url: './Media/ma_img.png'
|
|
762
|
+
},
|
|
763
|
+
viewOnce: true, //works with video, audio too
|
|
764
|
+
caption: 'hello word'
|
|
765
|
+
}
|
|
766
|
+
)
|
|
767
|
+
```
|
|
768
|
+
|
|
769
|
+
## Modify Messages
|
|
770
|
+
|
|
771
|
+
### Deleting Messages (for everyone)
|
|
772
|
+
|
|
773
|
+
```ts
|
|
774
|
+
const msg = await sock.sendMessage(jid, { text: 'hello word' })
|
|
775
|
+
await sock.sendMessage(jid, { delete: msg.key })
|
|
776
|
+
```
|
|
777
|
+
|
|
778
|
+
**Note:** deleting for oneself is supported via `chatModify`, see in [this section](#modifying-chats)
|
|
779
|
+
|
|
780
|
+
### Editing Messages
|
|
781
|
+
|
|
782
|
+
- You can pass all editable contents here
|
|
783
|
+
```ts
|
|
784
|
+
await sock.sendMessage(jid, {
|
|
785
|
+
text: 'updated text goes here',
|
|
786
|
+
edit: response.key,
|
|
787
|
+
});
|
|
788
|
+
```
|
|
789
|
+
|
|
790
|
+
## Manipulating Media Messages
|
|
791
|
+
|
|
792
|
+
### Thumbnail in Media Messages
|
|
793
|
+
- 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`.
|
|
794
|
+
- Thumbnails for videos can also be generated automatically, though, you need to have `ffmpeg` installed on your system.
|
|
795
|
+
|
|
796
|
+
### Downloading Media Messages
|
|
797
|
+
|
|
798
|
+
If you want to save the media you received
|
|
799
|
+
```ts
|
|
800
|
+
import { createWriteStream } from 'fs'
|
|
801
|
+
import { downloadMediaMessage, getContentType } from '@blurose/baileys'
|
|
802
|
+
|
|
803
|
+
sock.ev.on('messages.upsert', async ({ [m] }) => {
|
|
804
|
+
if (!m.message) return // if there is no text or media message
|
|
805
|
+
const messageType = getContentType(m) // get what type of message it is (text, image, video...)
|
|
806
|
+
|
|
807
|
+
// if the message is an image
|
|
808
|
+
if (messageType === 'imageMessage') {
|
|
809
|
+
// download the message
|
|
810
|
+
const stream = await downloadMediaMessage(
|
|
811
|
+
m,
|
|
812
|
+
'stream', // can be 'buffer' too
|
|
813
|
+
{ },
|
|
814
|
+
{
|
|
815
|
+
logger,
|
|
816
|
+
// pass this so that baileys can request a reupload of media
|
|
817
|
+
// that has been deleted
|
|
818
|
+
reuploadRequest: sock.updateMediaMessage
|
|
819
|
+
}
|
|
820
|
+
)
|
|
821
|
+
// save to file
|
|
822
|
+
const writeStream = createWriteStream('./my-download.jpeg')
|
|
823
|
+
stream.pipe(writeStream)
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
```
|
|
827
|
+
|
|
828
|
+
### Re-upload Media Message to Whatsapp
|
|
829
|
+
|
|
830
|
+
- 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:
|
|
831
|
+
```ts
|
|
832
|
+
await sock.updateMediaMessage(msg)
|
|
833
|
+
```
|
|
834
|
+
|
|
835
|
+
## Reject Call
|
|
836
|
+
|
|
837
|
+
- You can obtain `callId` and `callFrom` from `call` event
|
|
838
|
+
|
|
839
|
+
```ts
|
|
840
|
+
await sock.rejectCall(callId, callFrom)
|
|
841
|
+
```
|
|
842
|
+
|
|
843
|
+
## Send States in Chat
|
|
844
|
+
|
|
845
|
+
### Reading Messages
|
|
846
|
+
- A set of message [keys](https://baileys.wiki/docs/api/type-aliases/WAMessageKey/) must be explicitly marked read now.
|
|
847
|
+
- You cannot mark an entire 'chat' read as it were with Baileys Web.
|
|
848
|
+
This means you have to keep track of unread messages.
|
|
849
|
+
|
|
850
|
+
```ts
|
|
851
|
+
const key: WAMessageKey
|
|
852
|
+
// can pass multiple keys to read multiple messages as well
|
|
853
|
+
await sock.readMessages([key])
|
|
854
|
+
```
|
|
855
|
+
|
|
856
|
+
The message ID is the unique identifier of the message that you are marking as read.
|
|
857
|
+
On a `WAMessage`, the `messageID` can be accessed using ```messageID = message.key.id```.
|
|
858
|
+
|
|
859
|
+
### Update Presence
|
|
860
|
+
|
|
861
|
+
- ``` presence ``` can be one of the values in the [WAPresence type alias](https://baileys.wiki/docs/api/type-aliases/WAPresence/)
|
|
862
|
+
- The presence expires after about 10 seconds.
|
|
863
|
+
- This lets the person/group with `jid` know whether you're online, offline, typing etc.
|
|
864
|
+
|
|
865
|
+
```ts
|
|
866
|
+
await sock.sendPresenceUpdate('available', jid)
|
|
867
|
+
```
|
|
868
|
+
|
|
869
|
+
> [!NOTE]
|
|
870
|
+
> 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')`
|
|
871
|
+
|
|
872
|
+
## Modifying Chats
|
|
873
|
+
|
|
874
|
+
WA uses an encrypted form of communication to send chat/app updates. This has been implemented mostly and you can send the following updates:
|
|
875
|
+
|
|
876
|
+
> [!IMPORTANT]
|
|
877
|
+
> 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.
|
|
878
|
+
|
|
879
|
+
### Archive a Chat
|
|
880
|
+
```ts
|
|
881
|
+
const lastMsgInChat = await getLastMessageInChat(jid) // implement this on your end
|
|
882
|
+
await sock.chatModify({ archive: true, lastMessages: [lastMsgInChat] }, jid)
|
|
883
|
+
```
|
|
884
|
+
### Mute/Unmute a Chat
|
|
885
|
+
|
|
886
|
+
- Supported times:
|
|
887
|
+
|
|
888
|
+
| Time | Miliseconds |
|
|
889
|
+
|-------|-----------------|
|
|
890
|
+
| Remove | null |
|
|
891
|
+
| 8h | 86.400.000 |
|
|
892
|
+
| 7d | 604.800.000 |
|
|
893
|
+
|
|
894
|
+
```ts
|
|
895
|
+
// mute for 8 hours
|
|
896
|
+
await sock.chatModify({ mute: 8 * 60 * 60 * 1000 }, jid)
|
|
897
|
+
// unmute
|
|
898
|
+
await sock.chatModify({ mute: null }, jid)
|
|
899
|
+
```
|
|
900
|
+
### Mark a Chat Read/Unread
|
|
901
|
+
```ts
|
|
902
|
+
const lastMsgInChat = await getLastMessageInChat(jid) // implement this on your end
|
|
903
|
+
// mark it unread
|
|
904
|
+
await sock.chatModify({ markRead: false, lastMessages: [lastMsgInChat] }, jid)
|
|
905
|
+
```
|
|
906
|
+
|
|
907
|
+
### Delete a Message for Me
|
|
908
|
+
```ts
|
|
909
|
+
await sock.chatModify(
|
|
910
|
+
{
|
|
911
|
+
clear: {
|
|
912
|
+
messages: [
|
|
913
|
+
{
|
|
914
|
+
id: 'ATWYHDNNWU81732J',
|
|
915
|
+
fromMe: true,
|
|
916
|
+
timestamp: '1654823909'
|
|
917
|
+
}
|
|
918
|
+
]
|
|
919
|
+
}
|
|
920
|
+
},
|
|
921
|
+
jid
|
|
922
|
+
)
|
|
923
|
+
|
|
924
|
+
```
|
|
925
|
+
### Delete a Chat
|
|
926
|
+
```ts
|
|
927
|
+
const lastMsgInChat = await getLastMessageInChat(jid) // implement this on your end
|
|
928
|
+
await sock.chatModify({
|
|
929
|
+
delete: true,
|
|
930
|
+
lastMessages: [
|
|
931
|
+
{
|
|
932
|
+
key: lastMsgInChat.key,
|
|
933
|
+
messageTimestamp: lastMsgInChat.messageTimestamp
|
|
934
|
+
}
|
|
935
|
+
]
|
|
936
|
+
},
|
|
937
|
+
jid
|
|
938
|
+
)
|
|
939
|
+
```
|
|
940
|
+
### Pin/Unpin a Chat
|
|
941
|
+
```ts
|
|
942
|
+
await sock.chatModify({
|
|
943
|
+
pin: true // or `false` to unpin
|
|
944
|
+
},
|
|
945
|
+
jid
|
|
946
|
+
)
|
|
947
|
+
```
|
|
948
|
+
### Star/Unstar a Message
|
|
949
|
+
```ts
|
|
950
|
+
await sock.chatModify({
|
|
951
|
+
star: {
|
|
952
|
+
messages: [
|
|
953
|
+
{
|
|
954
|
+
id: 'messageID',
|
|
955
|
+
fromMe: true // or `false`
|
|
956
|
+
}
|
|
957
|
+
],
|
|
958
|
+
star: true // - true: Star Message; false: Unstar Message
|
|
959
|
+
}
|
|
960
|
+
},
|
|
961
|
+
jid
|
|
962
|
+
)
|
|
963
|
+
```
|
|
964
|
+
|
|
965
|
+
### Disappearing Messages
|
|
966
|
+
|
|
967
|
+
- Ephemeral can be:
|
|
968
|
+
|
|
969
|
+
| Time | Seconds |
|
|
970
|
+
|-------|----------------|
|
|
971
|
+
| Remove | 0 |
|
|
972
|
+
| 24h | 86.400 |
|
|
973
|
+
| 7d | 604.800 |
|
|
974
|
+
| 90d | 7.776.000 |
|
|
975
|
+
|
|
976
|
+
- You need to pass in **Seconds**, default is 7 days
|
|
977
|
+
|
|
978
|
+
```ts
|
|
979
|
+
// turn on disappearing messages
|
|
980
|
+
await sock.sendMessage(
|
|
981
|
+
jid,
|
|
982
|
+
// this is 1 week in seconds -- how long you want messages to appear for
|
|
983
|
+
{ disappearingMessagesInChat: WA_DEFAULT_EPHEMERAL }
|
|
984
|
+
)
|
|
985
|
+
|
|
986
|
+
// will send as a disappearing message
|
|
987
|
+
await sock.sendMessage(jid, { text: 'hello' }, { ephemeralExpiration: WA_DEFAULT_EPHEMERAL })
|
|
988
|
+
|
|
989
|
+
// turn off disappearing messages
|
|
990
|
+
await sock.sendMessage(
|
|
991
|
+
jid,
|
|
992
|
+
{ disappearingMessagesInChat: false }
|
|
993
|
+
)
|
|
994
|
+
```
|
|
995
|
+
|
|
996
|
+
## User Querys
|
|
997
|
+
|
|
998
|
+
### Check If ID Exists in Whatsapp
|
|
999
|
+
```ts
|
|
1000
|
+
const [result] = await sock.onWhatsApp(jid)
|
|
1001
|
+
if (result.exists) console.log (`${jid} exists on WhatsApp, as jid: ${result.jid}`)
|
|
1002
|
+
```
|
|
1003
|
+
|
|
1004
|
+
### Query Chat History (groups too)
|
|
1005
|
+
|
|
1006
|
+
- You need to have oldest message in chat
|
|
1007
|
+
```ts
|
|
1008
|
+
const msg = await getOldestMessageInChat(jid) // implement this on your end
|
|
1009
|
+
await sock.fetchMessageHistory(
|
|
1010
|
+
50, //quantity (max: 50 per query)
|
|
1011
|
+
msg.key,
|
|
1012
|
+
msg.messageTimestamp
|
|
1013
|
+
)
|
|
1014
|
+
```
|
|
1015
|
+
- Messages will be received in `messaging.history-set` event
|
|
1016
|
+
|
|
1017
|
+
### Fetch Status
|
|
1018
|
+
```ts
|
|
1019
|
+
const status = await sock.fetchStatus(jid)
|
|
1020
|
+
console.log('status: ' + status)
|
|
1021
|
+
```
|
|
1022
|
+
|
|
1023
|
+
### Fetch Profile Picture (groups too)
|
|
1024
|
+
- To get the display picture of some person/group
|
|
1025
|
+
```ts
|
|
1026
|
+
// for low res picture
|
|
1027
|
+
const ppUrl = await sock.profilePictureUrl(jid)
|
|
1028
|
+
console.log(ppUrl)
|
|
1029
|
+
|
|
1030
|
+
// for high res picture
|
|
1031
|
+
const ppUrl = await sock.profilePictureUrl(jid, 'image')
|
|
1032
|
+
```
|
|
1033
|
+
|
|
1034
|
+
### Fetch Bussines Profile (such as description or category)
|
|
1035
|
+
```ts
|
|
1036
|
+
const profile = await sock.getBusinessProfile(jid)
|
|
1037
|
+
console.log('business description: ' + profile.description + ', category: ' + profile.category)
|
|
1038
|
+
```
|
|
1039
|
+
|
|
1040
|
+
### Fetch Someone's Presence (if they're typing or online)
|
|
1041
|
+
```ts
|
|
1042
|
+
// the presence update is fetched and called here
|
|
1043
|
+
sock.ev.on('presence.update', console.log)
|
|
1044
|
+
|
|
1045
|
+
// request updates for a chat
|
|
1046
|
+
await sock.presenceSubscribe(jid)
|
|
1047
|
+
```
|
|
1048
|
+
|
|
1049
|
+
## Change Profile
|
|
1050
|
+
|
|
1051
|
+
### Change Profile Status
|
|
1052
|
+
```ts
|
|
1053
|
+
await sock.updateProfileStatus('Hello World!')
|
|
1054
|
+
```
|
|
1055
|
+
### Change Profile Name
|
|
1056
|
+
```ts
|
|
1057
|
+
await sock.updateProfileName('My name')
|
|
1058
|
+
```
|
|
1059
|
+
### Change Display Picture (groups too)
|
|
1060
|
+
- To change your display picture or a group's
|
|
1061
|
+
|
|
1062
|
+
> [!NOTE]
|
|
1063
|
+
> Like media messages, you can pass `{ stream: Stream }` or `{ url: Url }` or `Buffer` directly, you can see more in the [WAMediaUpload type alias](https://baileys.wiki/docs/api/type-aliases/WAMediaUpload/)
|
|
416
1064
|
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
1065
|
+
```ts
|
|
1066
|
+
await sock.updateProfilePicture(jid, { url: './new-profile-picture.jpeg' })
|
|
1067
|
+
```
|
|
1068
|
+
### Remove display picture (groups too)
|
|
1069
|
+
```ts
|
|
1070
|
+
await sock.removeProfilePicture(jid)
|
|
1071
|
+
```
|
|
1072
|
+
|
|
1073
|
+
## Groups
|
|
1074
|
+
|
|
1075
|
+
- To change group properties you need to be admin
|
|
1076
|
+
|
|
1077
|
+
### Create a Group
|
|
1078
|
+
```ts
|
|
1079
|
+
// title & participants
|
|
1080
|
+
const group = await sock.groupCreate('My Fab Group', ['1234@s.whatsapp.net', '4564@s.whatsapp.net'])
|
|
1081
|
+
console.log('created group with id: ' + group.gid)
|
|
1082
|
+
await sock.sendMessage(group.id, { text: 'hello there' }) // say hello to everyone on the group
|
|
1083
|
+
```
|
|
1084
|
+
### Add/Remove or Demote/Promote
|
|
1085
|
+
```ts
|
|
1086
|
+
// id & people to add to the group (will throw error if it fails)
|
|
1087
|
+
await sock.groupParticipantsUpdate(
|
|
1088
|
+
jid,
|
|
1089
|
+
['abcd@s.whatsapp.net', 'efgh@s.whatsapp.net'],
|
|
1090
|
+
'add' // replace this parameter with 'remove' or 'demote' or 'promote'
|
|
1091
|
+
)
|
|
1092
|
+
```
|
|
1093
|
+
### Change Subject (name)
|
|
1094
|
+
```ts
|
|
1095
|
+
await sock.groupUpdateSubject(jid, 'New Subject!')
|
|
1096
|
+
```
|
|
1097
|
+
### Change Description
|
|
1098
|
+
```ts
|
|
1099
|
+
await sock.groupUpdateDescription(jid, 'New Description!')
|
|
1100
|
+
```
|
|
1101
|
+
### Change Settings
|
|
1102
|
+
```ts
|
|
1103
|
+
// only allow admins to send messages
|
|
1104
|
+
await sock.groupSettingUpdate(jid, 'announcement')
|
|
1105
|
+
// allow everyone to send messages
|
|
1106
|
+
await sock.groupSettingUpdate(jid, 'not_announcement')
|
|
1107
|
+
// allow everyone to modify the group's settings -- like display picture etc.
|
|
1108
|
+
await sock.groupSettingUpdate(jid, 'unlocked')
|
|
1109
|
+
// only allow admins to modify the group's settings
|
|
1110
|
+
await sock.groupSettingUpdate(jid, 'locked')
|
|
1111
|
+
```
|
|
1112
|
+
### Leave a Group
|
|
1113
|
+
```ts
|
|
1114
|
+
// will throw error if it fails
|
|
1115
|
+
await sock.groupLeave(jid)
|
|
1116
|
+
```
|
|
1117
|
+
### Get Invite Code
|
|
1118
|
+
- To create link with code use `'https://chat.whatsapp.com/' + code`
|
|
1119
|
+
```ts
|
|
1120
|
+
const code = await sock.groupInviteCode(jid)
|
|
1121
|
+
console.log('group code: ' + code)
|
|
1122
|
+
```
|
|
1123
|
+
### Revoke Invite Code
|
|
1124
|
+
```ts
|
|
1125
|
+
const code = await sock.groupRevokeInvite(jid)
|
|
1126
|
+
console.log('New group code: ' + code)
|
|
1127
|
+
```
|
|
1128
|
+
### Join Using Invitation Code
|
|
1129
|
+
- Code can't have `https://chat.whatsapp.com/`, only code
|
|
1130
|
+
```ts
|
|
1131
|
+
const response = await sock.groupAcceptInvite(code)
|
|
1132
|
+
console.log('joined to: ' + response)
|
|
1133
|
+
```
|
|
1134
|
+
### Get Group Info by Invite Code
|
|
1135
|
+
```ts
|
|
1136
|
+
const response = await sock.groupGetInviteInfo(code)
|
|
1137
|
+
console.log('group information: ' + response)
|
|
1138
|
+
```
|
|
1139
|
+
### Query Metadata (participants, name, description...)
|
|
1140
|
+
```ts
|
|
1141
|
+
const metadata = await sock.groupMetadata(jid)
|
|
1142
|
+
console.log(metadata.id + ', title: ' + metadata.subject + ', description: ' + metadata.desc)
|
|
1143
|
+
```
|
|
1144
|
+
### Join using `groupInviteMessage`
|
|
1145
|
+
```ts
|
|
1146
|
+
const response = await sock.groupAcceptInviteV4(jid, groupInviteMessage)
|
|
1147
|
+
console.log('joined to: ' + response)
|
|
1148
|
+
```
|
|
1149
|
+
### Get Request Join List
|
|
1150
|
+
```ts
|
|
1151
|
+
const response = await sock.groupRequestParticipantsList(jid)
|
|
1152
|
+
console.log(response)
|
|
1153
|
+
```
|
|
1154
|
+
### Approve/Reject Request Join
|
|
1155
|
+
```ts
|
|
1156
|
+
const response = await sock.groupRequestParticipantsUpdate(
|
|
1157
|
+
jid, // group id
|
|
1158
|
+
['abcd@s.whatsapp.net', 'efgh@s.whatsapp.net'],
|
|
1159
|
+
'approve' // or 'reject'
|
|
1160
|
+
)
|
|
1161
|
+
console.log(response)
|
|
1162
|
+
```
|
|
1163
|
+
### Get All Participating Groups Metadata
|
|
1164
|
+
```ts
|
|
1165
|
+
const response = await sock.groupFetchAllParticipating()
|
|
1166
|
+
console.log(response)
|
|
1167
|
+
```
|
|
1168
|
+
### Toggle Ephemeral
|
|
1169
|
+
|
|
1170
|
+
- Ephemeral can be:
|
|
1171
|
+
|
|
1172
|
+
| Time | Seconds |
|
|
1173
|
+
|-------|----------------|
|
|
1174
|
+
| Remove | 0 |
|
|
1175
|
+
| 24h | 86.400 |
|
|
1176
|
+
| 7d | 604.800 |
|
|
1177
|
+
| 90d | 7.776.000 |
|
|
1178
|
+
|
|
1179
|
+
```ts
|
|
1180
|
+
await sock.groupToggleEphemeral(jid, 86400)
|
|
1181
|
+
```
|
|
1182
|
+
|
|
1183
|
+
### Change Add Mode
|
|
1184
|
+
```ts
|
|
1185
|
+
await sock.groupMemberAddMode(
|
|
1186
|
+
jid,
|
|
1187
|
+
'all_member_add' // or 'admin_add'
|
|
1188
|
+
)
|
|
1189
|
+
```
|
|
1190
|
+
|
|
1191
|
+
## Privacy
|
|
1192
|
+
|
|
1193
|
+
### Block/Unblock User
|
|
1194
|
+
```ts
|
|
1195
|
+
await sock.updateBlockStatus(jid, 'block') // Block user
|
|
1196
|
+
await sock.updateBlockStatus(jid, 'unblock') // Unblock user
|
|
1197
|
+
```
|
|
1198
|
+
### Get Privacy Settings
|
|
1199
|
+
```ts
|
|
1200
|
+
const privacySettings = await sock.fetchPrivacySettings(true)
|
|
1201
|
+
console.log('privacy settings: ' + privacySettings)
|
|
1202
|
+
```
|
|
1203
|
+
### Get BlockList
|
|
1204
|
+
```ts
|
|
1205
|
+
const response = await sock.fetchBlocklist()
|
|
1206
|
+
console.log(response)
|
|
1207
|
+
```
|
|
1208
|
+
### Update LastSeen Privacy
|
|
1209
|
+
```ts
|
|
1210
|
+
const value = 'all' // 'contacts' | 'contact_blacklist' | 'none'
|
|
1211
|
+
await sock.updateLastSeenPrivacy(value)
|
|
1212
|
+
```
|
|
1213
|
+
### Update Online Privacy
|
|
1214
|
+
```ts
|
|
1215
|
+
const value = 'all' // 'match_last_seen'
|
|
1216
|
+
await sock.updateOnlinePrivacy(value)
|
|
1217
|
+
```
|
|
1218
|
+
### Update Profile Picture Privacy
|
|
1219
|
+
```ts
|
|
1220
|
+
const value = 'all' // 'contacts' | 'contact_blacklist' | 'none'
|
|
1221
|
+
await sock.updateProfilePicturePrivacy(value)
|
|
1222
|
+
```
|
|
1223
|
+
### Update Status Privacy
|
|
1224
|
+
```ts
|
|
1225
|
+
const value = 'all' // 'contacts' | 'contact_blacklist' | 'none'
|
|
1226
|
+
await sock.updateStatusPrivacy(value)
|
|
1227
|
+
```
|
|
1228
|
+
### Update Read Receipts Privacy
|
|
1229
|
+
```ts
|
|
1230
|
+
const value = 'all' // 'none'
|
|
1231
|
+
await sock.updateReadReceiptsPrivacy(value)
|
|
1232
|
+
```
|
|
1233
|
+
### Update Groups Add Privacy
|
|
1234
|
+
```ts
|
|
1235
|
+
const value = 'all' // 'contacts' | 'contact_blacklist'
|
|
1236
|
+
await sock.updateGroupsAddPrivacy(value)
|
|
1237
|
+
```
|
|
1238
|
+
### Update Default Disappearing Mode
|
|
1239
|
+
|
|
1240
|
+
- Like [this](#disappearing-messages), ephemeral can be:
|
|
1241
|
+
|
|
1242
|
+
| Time | Seconds |
|
|
1243
|
+
|-------|----------------|
|
|
1244
|
+
| Remove | 0 |
|
|
1245
|
+
| 24h | 86.400 |
|
|
1246
|
+
| 7d | 604.800 |
|
|
1247
|
+
| 90d | 7.776.000 |
|
|
1248
|
+
|
|
1249
|
+
```ts
|
|
1250
|
+
const ephemeral = 86400
|
|
1251
|
+
await sock.updateDefaultDisappearingMode(ephemeral)
|
|
1252
|
+
```
|
|
1253
|
+
|
|
1254
|
+
## Broadcast Lists & Stories
|
|
1255
|
+
|
|
1256
|
+
### Send Broadcast & Stories
|
|
1257
|
+
- Messages can be sent to broadcasts & stories. You need to add the following message options in sendMessage, like this:
|
|
1258
|
+
```ts
|
|
1259
|
+
await sock.sendMessage(
|
|
1260
|
+
jid,
|
|
1261
|
+
{
|
|
1262
|
+
image: {
|
|
1263
|
+
url: url
|
|
1264
|
+
},
|
|
1265
|
+
caption: caption
|
|
1266
|
+
},
|
|
1267
|
+
{
|
|
1268
|
+
backgroundColor: backgroundColor,
|
|
1269
|
+
font: font,
|
|
1270
|
+
statusJidList: statusJidList,
|
|
1271
|
+
broadcast: true
|
|
1272
|
+
}
|
|
1273
|
+
)
|
|
1274
|
+
```
|
|
1275
|
+
- Message body can be a `extendedTextMessage` or `imageMessage` or `videoMessage` or `voiceMessage`, see the [AnyRegularMessageContent type alias](https://baileys.wiki/docs/api/type-aliases/AnyRegularMessageContent/)
|
|
1276
|
+
- You can add `backgroundColor` and other options in the message options, see the [MiscMessageGenerationOptions type alias](https://baileys.wiki/docs/api/type-aliases/MiscMessageGenerationOptions/)
|
|
1277
|
+
- `broadcast: true` enables broadcast mode
|
|
1278
|
+
- `statusJidList`: a list of people that you can get which you need to provide, which are the people who will get this status message.
|
|
1279
|
+
|
|
1280
|
+
- You can send messages to broadcast lists the same way you send messages to groups & individual chats.
|
|
1281
|
+
- Right now, WA Web does not support creating broadcast lists, but you can still delete them.
|
|
1282
|
+
- Broadcast IDs are in the format `12345678@broadcast`
|
|
1283
|
+
### Query a Broadcast List's Recipients & Name
|
|
1284
|
+
```ts
|
|
1285
|
+
const bList = await sock.getBroadcastListInfo('1234@broadcast')
|
|
1286
|
+
console.log (`list name: ${bList.name}, recps: ${bList.recipients}`)
|
|
1287
|
+
```
|
|
1288
|
+
|
|
1289
|
+
## Writing Custom Functionality
|
|
1290
|
+
Baileys is written with custom functionality in mind. Instead of forking the project & re-writing the internals, you can simply write your own extensions.
|
|
1291
|
+
|
|
1292
|
+
### Enabling Debug Level in Baileys Logs
|
|
1293
|
+
First, enable the logging of unhandled messages from WhatsApp by setting:
|
|
1294
|
+
```ts
|
|
1295
|
+
const sock = makeWASocket({
|
|
1296
|
+
logger: P({ level: 'debug' }),
|
|
1297
|
+
})
|
|
1298
|
+
```
|
|
1299
|
+
This will enable you to see all sorts of messages WhatsApp sends in the console.
|
|
1300
|
+
|
|
1301
|
+
### How Whatsapp Communicate With Us
|
|
1302
|
+
|
|
1303
|
+
> [!TIP]
|
|
1304
|
+
> If you want to learn whatsapp protocol, we recommend to study about Libsignal Protocol and Noise Protocol
|
|
1305
|
+
|
|
1306
|
+
- **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:
|
|
1307
|
+
```
|
|
1308
|
+
{
|
|
1309
|
+
"level": 10,
|
|
1310
|
+
"fromMe": false,
|
|
1311
|
+
"frame": {
|
|
1312
|
+
"tag": "ib",
|
|
1313
|
+
"attrs": {
|
|
1314
|
+
"from": "@s.whatsapp.net"
|
|
1315
|
+
},
|
|
1316
|
+
"content": [
|
|
1317
|
+
{
|
|
1318
|
+
"tag": "edge_routing",
|
|
1319
|
+
"attrs": {},
|
|
1320
|
+
"content": [
|
|
1321
|
+
{
|
|
1322
|
+
"tag": "routing_info",
|
|
1323
|
+
"attrs": {},
|
|
1324
|
+
"content": {
|
|
1325
|
+
"type": "Buffer",
|
|
1326
|
+
"data": [8,2,8,5]
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
]
|
|
1330
|
+
}
|
|
1331
|
+
]
|
|
1332
|
+
},
|
|
1333
|
+
"msg":"communication"
|
|
1334
|
+
}
|
|
1335
|
+
```
|
|
1336
|
+
|
|
1337
|
+
The `'frame'` is what the message received is, it has three components:
|
|
1338
|
+
- `tag` -- what this frame is about (eg. message will have 'message')
|
|
1339
|
+
- `attrs` -- a string key-value pair with some metadata (contains ID of the message usually)
|
|
1340
|
+
- `content` -- the actual data (eg. a message node will have the actual message content in it)
|
|
1341
|
+
- read more about this format [here](/src/WABinary/readme.md)
|
|
1342
|
+
|
|
1343
|
+
### Register a Callback for Websocket Events
|
|
1344
|
+
|
|
1345
|
+
> [!TIP]
|
|
1346
|
+
> Recommended to see `onMessageReceived` function in `socket.ts` file to understand how websockets events are fired
|
|
1347
|
+
|
|
1348
|
+
```ts
|
|
1349
|
+
// for any message with tag 'edge_routing'
|
|
1350
|
+
sock.ws.on('CB:edge_routing', (node: BinaryNode) => { })
|
|
1351
|
+
|
|
1352
|
+
// for any message with tag 'edge_routing' and id attribute = abcd
|
|
1353
|
+
sock.ws.on('CB:edge_routing,id:abcd', (node: BinaryNode) => { })
|
|
1354
|
+
|
|
1355
|
+
// for any message with tag 'edge_routing', id attribute = abcd & first content node routing_info
|
|
1356
|
+
sock.ws.on('CB:edge_routing,id:abcd,routing_info', (node: BinaryNode) => { })
|
|
1357
|
+
```
|
|
421
1358
|
|
|
422
1359
|
# License
|
|
423
|
-
Copyright (c) 2025
|
|
1360
|
+
Copyright (c) 2025 blurose
|
|
1361
|
+
|
|
1362
|
+
Licensed under the MIT License:
|
|
1363
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
1364
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
1365
|
+
in the Software without restriction, including without limitation the rights
|
|
1366
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
1367
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
1368
|
+
furnished to do so, subject to the following conditions:
|
|
1369
|
+
|
|
1370
|
+
The above copyright notice and this permission notice shall be included in all
|
|
1371
|
+
copies or substantial portions of the Software.
|
|
1372
|
+
|
|
1373
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
1374
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
1375
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
1376
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
1377
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
1378
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
1379
|
+
SOFTWARE.
|
|
1380
|
+
|
|
1381
|
+
Thus, the maintainers of the project can't be held liable for any potential misuse of this project.
|