@elrayyxml/baileys 1.0.2 → 1.0.6

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 ADDED
@@ -0,0 +1,2485 @@
1
+ [![-----------------------------------------------------](https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/colored.png)](#table-of-contents)
2
+
3
+ <div align="center">
4
+
5
+ ### @elrayyxml/baileys
6
+
7
+ </div>
8
+
9
+ [![-----------------------------------------------------](https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/colored.png)](#table-of-contents)
10
+
11
+ <div align="center">
12
+
13
+ <p><em>Baileys Modified</em></p>
14
+
15
+ <img src="https://files.catbox.moe/7m4cuw.jpg" width="300" alt="Cover Banner" />
16
+
17
+ </div>
18
+
19
+ ---
20
+
21
+ <div align="center">
22
+
23
+ [![npm version](https://img.shields.io/npm/v/@elrayyxml/baileys.svg)](https://www.npmjs.com/package/@elrayyxml/baileys)
24
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
25
+ [![WhatsApp](https://img.shields.io/badge/WhatsApp-API-green.svg?logo=whatsapp)](https://whatsapp.com)
26
+ [![Node.js](https://img.shields.io/badge/Node.js-20%2B-green.svg)](https://nodejs.org)
27
+ [![npm downloads](https://img.shields.io/npm/dt/@elrayyxml/baileys.svg?color=blueviolet&label=Downloads&logo=npm)](https://www.npmjs.com/package/@elrayyxml/baileys)
28
+ [![REST API](https://img.shields.io/badge/REST_API-green.svg)](https://api.elrayyxml.web.id)
29
+
30
+ </div>
31
+
32
+ ### Installation
33
+
34
+ ```bash
35
+ npm install @elrayyxml/baileys
36
+ # or
37
+ yarn add @elrayyxml/baileys
38
+ ```
39
+
40
+ ### `package.json` Fork Baileys
41
+
42
+ ```json
43
+ "dependencies": {
44
+ "baileys": "npm:@elrayyxml/baileys"
45
+ }
46
+ ```
47
+
48
+ ### Contact
49
+
50
+ <div align="center">
51
+
52
+ [![Instagram](https://img.shields.io/badge/Instagram-E4405F?style=for-the-badge&logo=instagram&logoColor=white)](https://instagram.com/elrayyxml)
53
+ [![TikTok](https://img.shields.io/badge/TikTok-000000?style=for-the-badge&logo=tiktok&logoColor=white)](https://tik-tok.com/@elrayyxml)
54
+ [![Email](https://img.shields.io/badge/Email-elrayy68@gmail.com-D14836?style=for-the-badge&logo=gmail&logoColor=white)](mailto:elrayy68@gmail.com)
55
+ [![WhatsApp](https://img.shields.io/badge/WhatsApp-6289526377530-25D366?style=for-the-badge&logo=whatsapp&logoColor=white)](https://wa.me/6289526377530)
56
+
57
+ </div>
58
+
59
+ ### Official Channel
60
+
61
+ <div align="center">
62
+ <a href="https://whatsapp.com/channel/0029Vb69z8n1dAvztHQTDu3r">
63
+ <img src="https://img.shields.io/badge/Join-WhatsApp%20Channel-25D366?logo=whatsapp&logoColor=white" alt="WhatsApp Channel" />
64
+ </a>
65
+
66
+ </div>
67
+
68
+
69
+ ## Example
70
+
71
+ ## Install
72
+
73
+ Use the stable version:
74
+ ```
75
+ yarn add @elrayyxml/baileys
76
+ ```
77
+
78
+ Then import your code using:
79
+ ```ts
80
+ import makeWASocket from '@elrayyxml/baileys'
81
+ ```
82
+
83
+ ```ts
84
+ import makeWASocket from '@elrayyxml/baileys'
85
+
86
+ const sock = makeWASocket({
87
+ // can provide additional config here
88
+ browser: Browsers.ubuntu('My App'),
89
+ printQRInTerminal: true
90
+ })
91
+ ```
92
+
93
+ 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!
94
+
95
+ ### Starting socket with **Pairing Code**
96
+
97
+
98
+ > [!IMPORTANT]
99
+ > 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)
100
+
101
+ The phone number can't have `+` or `()` or `-`, only numbers, you must provide country code
102
+
103
+ ```ts
104
+ import makeWASocket from '@elrayyxml/baileys'
105
+
106
+ const sock = makeWASocket({
107
+ // can provide additional config here
108
+ printQRInTerminal: false //need to be false
109
+ })
110
+
111
+ if (!sock.authState.creds.registered) {
112
+ const number = 'XXXXXXXXXXX'
113
+ const code = await sock.requestPairingCode(number) // or await sock.requestPairingCode(number, 'CODEOTPS') custom your pairing code
114
+ console.log(code)
115
+ }
116
+ ```
117
+
118
+ ### Receive Full History
119
+
120
+ 1. Set `syncFullHistory` as `true`
121
+ 2. Baileys, by default, use chrome browser config
122
+ - If you'd like to emulate a desktop connection (and receive more message history), this browser setting to your Socket config:
123
+
124
+ ```ts
125
+ const sock = makeWASocket({
126
+ ...otherOpts,
127
+ // can use Windows, Ubuntu here too
128
+ browser: Browsers.macOS('Desktop'),
129
+ syncFullHistory: true
130
+ })
131
+ ```
132
+
133
+ ## Important Notes About Socket Config
134
+
135
+ ### Caching Group Metadata (Recommended)
136
+ - If you use baileys for groups, we recommend you to set `cachedGroupMetadata` in socket config, you need to implement a cache like this:
137
+
138
+ ```ts
139
+ const groupCache = new NodeCache({stdTTL: 5 * 60, useClones: false})
140
+
141
+ const sock = makeWASocket({
142
+ cachedGroupMetadata: async (jid) => groupCache.get(jid)
143
+ })
144
+
145
+ sock.ev.on('groups.update', async ([event]) => {
146
+ const metadata = await sock.groupMetadata(event.id)
147
+ groupCache.set(event.id, metadata)
148
+ })
149
+
150
+ sock.ev.on('group-participants.update', async (event) => {
151
+ const metadata = await sock.groupMetadata(event.id)
152
+ groupCache.set(event.id, metadata)
153
+ })
154
+ ```
155
+
156
+ ### Improve Retry System & Decrypt Poll Votes
157
+ - 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:
158
+ ```ts
159
+ const sock = makeWASocket({
160
+ getMessage: async (key) => await getMessageFromStore(key)
161
+ })
162
+ ```
163
+
164
+ ### Receive Notifications in Whatsapp App
165
+ - If you want to receive notifications in whatsapp app, set `markOnlineOnConnect` to `false`
166
+ ```ts
167
+ const sock = makeWASocket({
168
+ markOnlineOnConnect: false
169
+ })
170
+ ```
171
+ ## Saving & Restoring Sessions
172
+
173
+ You obviously don't want to keep scanning the QR code every time you want to connect.
174
+
175
+ So, you can load the credentials to log back in:
176
+ ```ts
177
+ import makeWASocket, { useMultiFileAuthState } from '@elrayyxml/baileys'
178
+
179
+ const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys')
180
+
181
+ // will use the given state to connect
182
+ // so if valid credentials are available -- it'll connect without QR
183
+ const sock = makeWASocket({ auth: state })
184
+
185
+ // this will be called as soon as the credentials are updated
186
+ sock.ev.on('creds.update', saveCreds)
187
+ ```
188
+
189
+ > [!IMPORTANT]
190
+ > `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.
191
+
192
+ > [!NOTE]
193
+ > 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.
194
+
195
+ ## Handling Events
196
+
197
+ - Baileys uses the EventEmitter syntax for events.
198
+ They're all nicely typed up, so you shouldn't have any issues with an Intellisense editor like VS Code.
199
+
200
+ > [!IMPORTANT]
201
+ > **The events are [these](https://baileys.whiskeysockets.io/types/BaileysEventMap.html)**, it's important you see all events
202
+
203
+ You can listen to these events like this:
204
+ ```ts
205
+ const sock = makeWASocket()
206
+ sock.ev.on('messages.upsert', ({ messages }) => {
207
+ console.log('got messages', messages)
208
+ })
209
+ ```
210
+
211
+ ### Example to Start
212
+
213
+ > [!NOTE]
214
+ > This example includes basic auth storage too
215
+
216
+ ```ts
217
+ import makeWASocket, { DisconnectReason, useMultiFileAuthState } from '@elrayyxml/baileys'
218
+ import { Boom } from '@hapi/boom'
219
+
220
+ async function connectToWhatsApp () {
221
+ const { state, saveCreds } = await useMultiFileAuthState('./auth_info_baileys')
222
+ const sock = makeWASocket({
223
+ // can provide additional config here
224
+ auth: state,
225
+ printQRInTerminal: true
226
+ })
227
+ sock.ev.on('connection.update', (update) => {
228
+ const { connection, lastDisconnect } = update
229
+ if(connection === 'close') {
230
+ const shouldReconnect = (lastDisconnect.error as Boom)?.output?.statusCode !== DisconnectReason.loggedOut
231
+ console.log('connection closed due to ', lastDisconnect.error, ', reconnecting ', shouldReconnect)
232
+ // reconnect if not logged out
233
+ if(shouldReconnect) {
234
+ connectToWhatsApp()
235
+ }
236
+ } else if(connection === 'open') {
237
+ console.log('opened connection')
238
+ }
239
+ })
240
+ sock.ev.on('messages.upsert', event => {
241
+ for (const m of event.messages) {
242
+ console.log(JSON.stringify(m, undefined, 2))
243
+
244
+ console.log('replying to', m.key.remoteJid)
245
+ await sock.sendMessage(m.key.remoteJid!, { text: 'Hello Word' })
246
+ }
247
+ })
248
+
249
+ // to storage creds (session info) when it updates
250
+ sock.ev.on('creds.update', saveCreds)
251
+ }
252
+ // run in main file
253
+ connectToWhatsApp()
254
+ ```
255
+
256
+ ### For example if you use useSingleFileAuthState and useMongoFileAuthState
257
+ ```ts
258
+ import makeWASocket, { useSingleFileAuthState, useMongoFileAuthState } from '@elrayyxml/baileys'
259
+
260
+ // Single Auth
261
+ const { state, saveState } = await useSingleFileAuthState('./auth_info_baileys.json')
262
+ const sock = makeWASocket({
263
+ auth: state,
264
+ printQRInTerminal: true
265
+ })
266
+
267
+ sock.ev.on('creds.update', saveState)
268
+
269
+ // Mongo Auth
270
+ import { MongoClient } from "mongodb"
271
+
272
+ const connectAuth = async() => {
273
+ global.client = new MongoClient('mongoURL')
274
+ global.client.connect(err => {
275
+ if (err) {
276
+ console.warn("Warning: MongoDB link is invalid or cannot be connected.")
277
+ } else {
278
+ console.log('Successfully Connected To MongoDB Server')
279
+ }
280
+ })
281
+ }
282
+ await client.connect()
283
+ const collection = client.db("@itsockchann").collection("sessions")
284
+ return collection
285
+ }
286
+
287
+ const Authentication = await connectAuth()
288
+ const { state, saveCreds } = await useMongoFileAuthState(Authentication)
289
+ const sock = makeWASocket({
290
+ auth: state,
291
+ printQRInTerminal: true
292
+ })
293
+
294
+ sock.ev.on('creds.update', saveCreds)
295
+ ```
296
+
297
+ > [!IMPORTANT]
298
+ > In `messages.upsert` it's recommended to use a loop like `for (const message of event.messages)` to handle all messages in array
299
+
300
+ ### Decrypt Poll Votes
301
+
302
+ - By default poll votes are encrypted and handled in `messages.update`
303
+ ```ts
304
+ import pino from "pino"
305
+ import { makeInMemoryStore, getAggregateVotesInPollMessage } from '@elrayyxml/baileys'
306
+
307
+ const logger = pino({ timestamp: () => `,"time":"${new Date().toJSON()}"` }).child({ class: "@Itsockchann" })
308
+ logger.level = "fatal"
309
+ const store = makeInMemoryStore({ logger })
310
+
311
+ async function getMessage(key){
312
+ if (store) {
313
+ const msg = await store.loadMessage(key.remoteJid, key.id)
314
+ return msg?.message
315
+ }
316
+ return {
317
+ conversation: "Itsocki Kawaiii"
318
+ }
319
+ }
320
+
321
+ sock.ev.on("messages.update", async (chatUpdate) => {
322
+ for(const { key, update } of chatUpdate) {
323
+ if(update.pollUpdates && key.fromMe) {
324
+ const pollCreation = await getMessage(key)
325
+ if(pollCreation) {
326
+ const pollUpdate = await getAggregateVotesInPollMessage({
327
+ message: pollCreation,
328
+ pollUpdates: update.pollUpdates,
329
+ })
330
+ const toCmd = pollUpdate.filter(v => v.voters.length !== 0)[0]?.name
331
+ if (toCmd == undefined) return
332
+ console.log(toCmd)
333
+ }
334
+ }
335
+ }
336
+ })
337
+ ```
338
+
339
+ ### Decrypt Event Response
340
+
341
+ - By default event response are encrypted and handled in `messages.update`
342
+ ```ts
343
+ import { jidNormalizedUser, getAggregateResponsesInEventMessage } from '@elrayyxml/baileys'
344
+
345
+ sock.ev.on("messages.update", async ([chatUpdate]) => {
346
+ const eventResponses = chatUpdate.update?.eventResponses
347
+ const agregate = getAggregateResponsesInEventMessage({ eventResponses }, jidNormalizedUser(sock.user.lid))
348
+ console.log(agregate)
349
+ })
350
+ ```
351
+
352
+ ### Summary of Events on First Connection
353
+
354
+ 1. When you connect first time, `connection.update` will be fired requesting you to restart sock
355
+ 2. Then, history messages will be received in `messaging.history-set`
356
+
357
+ ## Implementing a Data Store
358
+
359
+ - 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.
360
+
361
+ > [!IMPORTANT]
362
+ > I highly recommend building your own data store, as storing someone's entire chat history in memory is a terrible waste of RAM.
363
+
364
+ It can be used as follows:
365
+
366
+ ```ts
367
+ import makeWASocket, { makeInMemoryStore } from '@elrayyxml/baileys'
368
+ // the store maintains the data of the WA connection in memory
369
+ // can be written out to a file & read from it
370
+ const store = makeInMemoryStore({ })
371
+ // can be read from a file
372
+ store.readFromFile('./baileys_store.json')
373
+ // saves the state to a file every 10s
374
+ setInterval(() => {
375
+ store.writeToFile('./baileys_store.json')
376
+ }, 10_000)
377
+
378
+ const sock = makeWASocket({ })
379
+ // will listen from this socket
380
+ // the store can listen from a new socket once the current socket outlives its lifetime
381
+ store.bind(sock.ev)
382
+
383
+ sock.ev.on('chats.upsert', () => {
384
+ // can use 'store.chats' however you want, even after the socket dies out
385
+ // 'chats' => a KeyedDB instance
386
+ console.log('got chats', store.chats.all())
387
+ })
388
+
389
+ sock.ev.on('contacts.upsert', () => {
390
+ console.log('got contacts', Object.values(store.contacts))
391
+ })
392
+
393
+ ```
394
+
395
+ The store also provides some simple functions such as `loadMessages` that utilize the store to speed up data retrieval.
396
+
397
+ ## Whatsapp IDs Explain
398
+
399
+ - `id` is the WhatsApp ID, called `jid` too, of the person or group you're sending the message to.
400
+ - It must be in the format ```[country code][phone number]@s.whatsapp.net```
401
+ - Example for people: ```+19999999999@s.whatsapp.net```.
402
+ - For groups, it must be in the format ``` 123456789-123345@g.us ```.
403
+ - For broadcast lists, it's `[timestamp of creation]@broadcast`.
404
+ - For stories, the ID is `status@broadcast`.
405
+
406
+ ## Utility Functions
407
+
408
+ - `getContentType`, returns the content type for any message
409
+ - `getDevice`, returns the device from message
410
+ - `makeCacheableSignalKeyStore`, make auth store more fast
411
+ - `downloadContentFromMessage`, download content from any message
412
+
413
+ ## Sending Messages
414
+
415
+ - Send all types of messages with a single function
416
+ - **[Here](https://baileys.whiskeysockets.io/types/AnyMessageContent.html) you can see all message contents supported, like text message**
417
+ - **[Here](https://baileys.whiskeysockets.io/types/MiscMessageGenerationOptions.html) you can see all options supported, like quote message**
418
+
419
+ ```ts
420
+ const jid: string
421
+ const content: AnyMessageContent
422
+ const options: MiscMessageGenerationOptions
423
+
424
+ sock.sendMessage(jid, content, options)
425
+ ```
426
+
427
+ ### Non-Media Messages
428
+
429
+ #### Text Message
430
+ ```ts
431
+ await sock.sendMessage(jid, { text: 'hello word' })
432
+ ```
433
+
434
+ #### Quote Message (works with all types)
435
+ ```ts
436
+ await sock.sendMessage(jid, { text: 'hello word' }, { quoted: message })
437
+ ```
438
+
439
+ #### Mention User (works with most types)
440
+ - @number is to mention in text, it's optional
441
+ ```ts
442
+ await sock.sendMessage(
443
+ jid,
444
+ {
445
+ text: '@12345678901',
446
+ mentions: ['12345678901@s.whatsapp.net']
447
+ }
448
+ )
449
+ ```
450
+
451
+ #### Forward Messages
452
+ - You need to have message object, can be retrieved from [store](#implementing-a-data-store) or use a [message](https://baileys.whiskeysockets.io/types/WAMessage.html) object
453
+ ```ts
454
+ const msg = getMessageFromStore() // implement this on your end
455
+ await sock.sendMessage(jid, { forward: msg, force: true or number }) // WA forward the message!
456
+ ```
457
+
458
+ #### Location Message
459
+ ```ts
460
+ await sock.sendMessage(
461
+ jid,
462
+ {
463
+ location: {
464
+ degreesLatitude: 24.121231,
465
+ degreesLongitude: 55.1121221
466
+ }
467
+ }
468
+ )
469
+ ```
470
+
471
+ #### Live Location Message
472
+ ```ts
473
+ await sock.sendMessage(
474
+ jid,
475
+ {
476
+ location: {
477
+ degreesLatitude: 24.121231,
478
+ degreesLongitude: 55.1121221
479
+ },
480
+ live: true
481
+ }
482
+ )
483
+ ```
484
+ #### Contact Message
485
+ ```ts
486
+ const vcard = 'BEGIN:VCARD\n' // metadata of the contact card
487
+ + 'VERSION:3.0\n'
488
+ + 'FN:Jeff Singh\n' // full name
489
+ + 'ORG:Ashoka Uni\n' // the organization of the contact
490
+ + 'TELtype=CELLtype=VOICEwaid=911234567890:+91 12345 67890\n' // WhatsApp ID + phone number
491
+ + 'END:VCARD'
492
+
493
+ await sock.sendMessage(
494
+ jid,
495
+ {
496
+ contacts: {
497
+ displayName: 'ElrayyXml',
498
+ contacts: [{ vcard }]
499
+ }
500
+ }
501
+ )
502
+ ```
503
+
504
+ #### Reaction Message
505
+ - You need to pass the key of message, you can retrieve from [store](#implementing-a-data-store) or use a [key](https://baileys.whiskeysockets.io/types/WAMessageKey.html) object
506
+ ```ts
507
+ await sock.sendMessage(
508
+ jid,
509
+ {
510
+ react: {
511
+ text: '💖', // use an empty string to remove the reaction
512
+ key: message.key
513
+ }
514
+ }
515
+ )
516
+ ```
517
+
518
+ #### Pin Message
519
+ - You need to pass the key of message, you can retrieve from [store](#implementing-a-data-store) or use a [key](https://baileys.whiskeysockets.io/types/WAMessageKey.html) object
520
+
521
+ - Time can be:
522
+
523
+ | Time | Seconds |
524
+ |-------|----------------|
525
+ | 24h | 86.400 |
526
+ | 7d | 604.800 |
527
+ | 30d | 2.592.000 |
528
+
529
+ ```ts
530
+ await sock.sendMessage(
531
+ jid,
532
+ {
533
+ pin: {
534
+ type: 1, // 2 to remove
535
+ time: 86400,
536
+ key: Key
537
+ }
538
+ }
539
+ )
540
+ ```
541
+
542
+ ### Keep Message
543
+ ```ts
544
+ await sock.sendMessage(
545
+ jid,
546
+ {
547
+ keep: {
548
+ key: Key,
549
+ type: 1 // or 2
550
+ }
551
+ }
552
+ )
553
+ ```
554
+
555
+ #### Poll Message
556
+ ```ts
557
+ await sock.sendMessage(
558
+ jid,
559
+ {
560
+ poll: {
561
+ name: 'My Poll',
562
+ values: ['Option 1', 'Option 2', ...],
563
+ selectableCount: 1,
564
+ toAnnouncementGroup: false // or true
565
+ }
566
+ }
567
+ )
568
+ ```
569
+
570
+ #### Poll Result Message
571
+ ```ts
572
+ await sock.sendMessage(
573
+ jid,
574
+ {
575
+ pollResult: {
576
+ name: 'Hi',
577
+ values: [
578
+ [
579
+ 'Option 1',
580
+ 1000
581
+ ],
582
+ [
583
+ 'Option 2',
584
+ 2000
585
+ ]
586
+ ]
587
+ }
588
+ }
589
+ )
590
+ ```
591
+
592
+ ### Call Message
593
+ ```ts
594
+ await sock.sendMessage(
595
+ jid,
596
+ {
597
+ call: {
598
+ name: 'Hay',
599
+ type: 1 // 2 for video
600
+ }
601
+ }
602
+ )
603
+ ```
604
+
605
+ ### Event Message
606
+ ```ts
607
+ await sock.sendMessage(
608
+ jid,
609
+ {
610
+ event: {
611
+ isCanceled: false, // or true
612
+ name: 'holiday together!',
613
+ description: 'who wants to come along?',
614
+ location: {
615
+ degreesLatitude: 24.121231,
616
+ degreesLongitude: 55.1121221,
617
+ name: 'name'
618
+ },
619
+ call: 'audio', // or 'video'
620
+ startTime: number,
621
+ endTime: number,
622
+ extraGuestsAllowed: true // or false
623
+ }
624
+ }
625
+ )
626
+ ```
627
+
628
+ ### Order Message
629
+ ```ts
630
+ await sock.sendMessage(
631
+ jid,
632
+ {
633
+ order: {
634
+ orderId: '574xxx',
635
+ thumbnail: 'your_thumbnail',
636
+ itemCount: 'your_count',
637
+ status: 'your_status', // INQUIRY || ACCEPTED || DECLINED
638
+ surface: 'CATALOG',
639
+ message: 'your_caption',
640
+ orderTitle: "your_title",
641
+ sellerJid: 'your_jid'',
642
+ token: 'your_token',
643
+ totalAmount1000: 'your_amount',
644
+ totalCurrencyCode: 'IDR'
645
+ }
646
+ }
647
+ )
648
+ ```
649
+
650
+ ### Product Message
651
+ ```ts
652
+ await sock.sendMessage(
653
+ jid,
654
+ {
655
+ product: {
656
+ productImage: { // for using buffer >> productImage: your_buffer
657
+ url: your_url
658
+ },
659
+ productId: 'your_id',
660
+ title: 'your_title',
661
+ description: 'your_description',
662
+ currencyCode: 'IDR',
663
+ priceAmount1000: 'your_amount',
664
+ retailerId: 'your_reid', // optional use if needed
665
+ url: 'your_url', // optional use if needed
666
+ productImageCount: 'your_imageCount',
667
+ firstImageId: 'your_image', // optional use if needed
668
+ salePriceAmount1000: 'your_priceSale',
669
+ signedUrl: 'your_url' // optional use if needed
670
+ },
671
+ businessOwnerJid: 'your_jid'
672
+ }
673
+ )
674
+ ```
675
+
676
+ ### Payment Message
677
+ ```ts
678
+ await sock.sendMessage(
679
+ jid,
680
+ {
681
+ payment: {
682
+ note: 'Hi!',
683
+ currency: 'IDR', // optional
684
+ offset: 0, // optional
685
+ amount: '10000', // optional
686
+ expiry: 0, // optional
687
+ from: '628xxxx@s.whatsapp.net', // optional
688
+ image: { // optional
689
+ placeholderArgb: "your_background", // optional
690
+ textArgb: "your_text", // optional
691
+ subtextArgb: "your_subtext" // optional
692
+ }
693
+ }
694
+ }
695
+ )
696
+ ```
697
+
698
+ #### Payment Invite Message
699
+ ```ts
700
+ await sock.sendMessage(
701
+ id,
702
+ {
703
+ paymentInvite: {
704
+ type: number, // 1 || 2 || 3
705
+ expiry: 0
706
+ }
707
+ }
708
+ )
709
+ ```
710
+
711
+ ### Admin Invite Message
712
+ ```ts
713
+ await sock.sendMessage(
714
+ jid,
715
+ {
716
+ adminInvite: {
717
+ jid: '123xxx@newsletter',
718
+ name: 'newsletter_name',
719
+ caption: 'Please be my channel admin',
720
+ expiration: 86400,
721
+ jpegThumbnail: Buffer // optional
722
+ }
723
+ }
724
+ )
725
+ ```
726
+
727
+ ### Group Invite Message
728
+ ```ts
729
+ await sock.sendMessage(
730
+ jid,
731
+ {
732
+ groupInvite: {
733
+ jid: '123xxx@g.us',
734
+ name: 'group_name',
735
+ caption: 'Please Join My Whatsapp Group',
736
+ code: 'code_invite',
737
+ expiration: 86400,
738
+ jpegThumbnail: Buffer, // optional
739
+ }
740
+ }
741
+ )
742
+ ```
743
+
744
+ ### Sticker Pack Message
745
+ ```ts
746
+ // I don't know why the sticker doesn't appear
747
+ await sock.sendMessage(
748
+ jid,
749
+ {
750
+ stickerPack: {
751
+ name: 'Hiii',
752
+ publisher: 'By Itsockchann',
753
+ description: 'Hello',
754
+ cover: Buffer, // Image buffer
755
+ stickers: [{
756
+ sticker: { url: 'https://example.com/1234kjd.webp' },
757
+ emojis: ['❤'], // optional
758
+ accessibilityLabel: '', // optional
759
+ isLottie: Boolean, // optional
760
+ isAnimated: Boolean // optional
761
+ },
762
+ {
763
+ sticker: Buffer,
764
+ emojis: ['❤'], // optional
765
+ accessibilityLabel: '', // optional
766
+ isLottie: Boolean, // optional
767
+ isAnimated: Boolean // optional
768
+ }]
769
+ }
770
+ }
771
+ )
772
+ ```
773
+
774
+ ### Share Phone Number Message
775
+ ```ts
776
+ await sock.sendMessage(
777
+ jid,
778
+ {
779
+ sharePhoneNumber: {
780
+ }
781
+ }
782
+ )
783
+ ```
784
+
785
+ ### Request Phone Number Message
786
+ ```ts
787
+ await sock.sendMessage(
788
+ jid,
789
+ {
790
+ requestPhoneNumber: {
791
+ }
792
+ }
793
+ )
794
+ ```
795
+
796
+ ### Buttons Reply Message
797
+ ```ts
798
+ // List
799
+ await sock.sendMessage(
800
+ jid,
801
+ {
802
+ buttonReply: {
803
+ name: 'Hii',
804
+ description: 'description',
805
+ rowId: 'ID'
806
+ },
807
+ type: 'list'
808
+ }
809
+ )
810
+ // Plain
811
+ await sock.sendMessage(
812
+ jid,
813
+ {
814
+ buttonReply: {
815
+ displayText: 'Hii',
816
+ id: 'ID'
817
+ },
818
+ type: 'plain'
819
+ }
820
+ )
821
+
822
+ // Template
823
+ await sock.sendMessage(
824
+ jid,
825
+ {
826
+ buttonReply: {
827
+ displayText: 'Hii',
828
+ id: 'ID',
829
+ index: 'number'
830
+ },
831
+ type: 'template'
832
+ }
833
+ )
834
+
835
+ // Interactive
836
+ await sock.sendMessage(
837
+ jid,
838
+ {
839
+ buttonReply: {
840
+ body: 'Hii',
841
+ nativeFlows: {
842
+ name: 'menu_options',
843
+ paramsJson: JSON.stringify({ id: 'ID', description: 'description' })
844
+ version: 1 // 2 | 3
845
+ }
846
+ },
847
+ type: 'interactive'
848
+ }
849
+ )
850
+ ```
851
+
852
+ ### Buttons Message
853
+ ```ts
854
+ await sock.sendMessage(
855
+ jid,
856
+ {
857
+ text: 'This is a button message!', // image: buffer or // image: { url: url } If you want to use images
858
+ caption: 'caption', // Use this if you are using an image or video
859
+ footer: 'Hello World!',
860
+ buttons: [{
861
+ buttonId: 'Id1',
862
+ buttonText: {
863
+ displayText: 'Button 1'
864
+ }
865
+ },
866
+ {
867
+ buttonId: 'Id2',
868
+ buttonText: {
869
+ displayText: 'Button 2'
870
+ }
871
+ },
872
+ {
873
+ buttonId: 'Id3',
874
+ buttonText: {
875
+ displayText: 'Button 3'
876
+ }
877
+ }]
878
+ }
879
+ )
880
+ ```
881
+
882
+ ### Buttons List Message
883
+ ```ts
884
+ // Just working in a private chat
885
+ await sock.sendMessage(
886
+ jid,
887
+ {
888
+ text: 'This is a list!',
889
+ footer: 'Hello World!',
890
+ title: 'Amazing boldfaced list title',
891
+ buttonText: 'Required, text on the button to view the list',
892
+ sections: [
893
+ {
894
+ title: 'Section 1',
895
+ rows: [{
896
+ title: 'Option 1',
897
+ rowId: 'option1'
898
+ },
899
+ {
900
+ title: 'Option 2',
901
+ rowId: 'option2',
902
+ description: 'This is a description'
903
+ }]
904
+ },
905
+ {
906
+ title: 'Section 2',
907
+ rows: [{
908
+ title: 'Option 3',
909
+ rowId: 'option3'
910
+ },
911
+ {
912
+ title: 'Option 4',
913
+ rowId: 'option4',
914
+ description: 'This is a description V2'
915
+ }]
916
+ }]
917
+ }
918
+ )
919
+ ```
920
+
921
+ ### Buttons Product List Message
922
+ ```ts
923
+ // Just working in a private chat
924
+ await sock.sendMessage(
925
+ jid,
926
+ {
927
+ text: 'This is a list!',
928
+ footer: 'Hello World!',
929
+ title: 'Amazing boldfaced list title',
930
+ buttonText: 'Required, text on the button to view the list',
931
+ productList: [{
932
+ title: 'This is a title',
933
+ products: [
934
+ {
935
+ productId: '1234'
936
+ },
937
+ {
938
+ productId: '5678'
939
+ }
940
+ ]
941
+ }],
942
+ businessOwnerJid: '628xxx@s.whatsapp.net',
943
+ thumbnail: 'https://example.com/jdbenkksjs.jpg' // or buffer
944
+ }
945
+ )
946
+ ```
947
+
948
+ ### Buttons Cards Message
949
+ ```ts
950
+ await sock.sendMessage(
951
+ jid,
952
+ {
953
+ text: 'Body Message',
954
+ title: 'Title Message',
955
+ subtile: 'Subtitle Message',
956
+ footer: 'Footer Message',
957
+ cards: [
958
+ {
959
+ image: { url: 'https://example.com/jdbenkksjs.jpg' }, // or buffer
960
+ title: 'Title Cards',
961
+ body: 'Body Cards',
962
+ footer: 'Footer Cards',
963
+ buttons: [
964
+ {
965
+ name: 'quick_reply',
966
+ buttonParamsJson: JSON.stringify({
967
+ display_text: 'Display Button',
968
+ id: 'ID'
969
+ })
970
+ },
971
+ {
972
+ name: 'cta_url',
973
+ buttonParamsJson: JSON.stringify({
974
+ display_text: 'Display Button',
975
+ url: 'https://www.example.com'
976
+ })
977
+ }
978
+ ]
979
+ },
980
+ {
981
+ video: { url: 'https://example.com/jdbenkksjs.mp4' }, // or buffer
982
+ title: 'Title Cards',
983
+ body: 'Body Cards',
984
+ footer: 'Footer Cards',
985
+ buttons: [
986
+ {
987
+ name: 'quick_reply',
988
+ buttonParamsJson: JSON.stringify({
989
+ display_text: 'Display Button',
990
+ id: 'ID'
991
+ })
992
+ },
993
+ {
994
+ name: 'cta_url',
995
+ buttonParamsJson: JSON.stringify({
996
+ display_text: 'Display Button',
997
+ url: 'https://www.example.com'
998
+ })
999
+ }
1000
+ ]
1001
+ }
1002
+ ]
1003
+ }
1004
+ )
1005
+ ```
1006
+
1007
+ ### Buttons Template Message
1008
+ ```ts
1009
+ // This no longer works
1010
+ await sock.sendMessage(
1011
+ jid,
1012
+ {
1013
+ text: 'This is a template message!',
1014
+ footer: 'Hello World!',
1015
+ templateButtons: [{
1016
+ index: 1,
1017
+ urlButton: {
1018
+ displayText: 'Follow Me',
1019
+ url: 'https://whatsapp.com/channel/0029Vag9VSI2ZjCocqa2lB1y'
1020
+ },
1021
+ },
1022
+ {
1023
+ index: 2,
1024
+ callButton: {
1025
+ displayText: 'Call Me!',
1026
+ phoneNumber: '628xxx'
1027
+ },
1028
+ },
1029
+ {
1030
+ index: 3,
1031
+ quickReplyButton: {
1032
+ displayText: 'This is a reply, just like normal buttons!',
1033
+ id: 'id-like-buttons-message'
1034
+ },
1035
+ }]
1036
+ }
1037
+ )
1038
+ ```
1039
+
1040
+ ### Buttons Interactive Message
1041
+ ```ts
1042
+ await sock.sendMessage(
1043
+ jid,
1044
+ {
1045
+ text: 'This is an Interactive message!',
1046
+ title: 'Hiii',
1047
+ subtitle: 'There is a subtitle',
1048
+ footer: 'Hello World!',
1049
+ interactiveButtons: [
1050
+ {
1051
+ name: 'quick_reply',
1052
+ buttonParamsJson: JSON.stringify({
1053
+ display_text: 'Click Me!',
1054
+ id: 'your_id'
1055
+ })
1056
+ },
1057
+ {
1058
+ name: 'cta_url',
1059
+ buttonParamsJson: JSON.stringify({
1060
+ display_text: 'Follow Me',
1061
+ url: 'https://whatsapp.com/channel/0029Vag9VSI2ZjCocqa2lB1y',
1062
+ merchant_url: 'https://whatsapp.com/channel/0029Vag9VSI2ZjCocqa2lB1y'
1063
+ })
1064
+ },
1065
+ {
1066
+ name: 'cta_copy',
1067
+ buttonParamsJson: JSON.stringify({
1068
+ display_text: 'Click Me!',
1069
+ copy_code: 'https://whatsapp.com/channel/0029Vag9VSI2ZjCocqa2lB1y'
1070
+ })
1071
+ },
1072
+ {
1073
+ name: 'cta_call',
1074
+ buttonParamsJson: JSON.stringify({
1075
+ display_text: 'Call Me!',
1076
+ phone_number: '628xxx'
1077
+ })
1078
+ },
1079
+ {
1080
+ name: 'cta_catalog',
1081
+ buttonParamsJson: JSON.stringify({
1082
+ business_phone_number: '628xxx'
1083
+ })
1084
+ },
1085
+ {
1086
+ name: 'cta_reminder',
1087
+ buttonParamsJson: JSON.stringify({
1088
+ display_text: '...'
1089
+ })
1090
+ },
1091
+ {
1092
+ name: 'cta_cancel_reminder',
1093
+ buttonParamsJson: JSON.stringify({
1094
+ display_text: '...'
1095
+ })
1096
+ },
1097
+ {
1098
+ name: 'address_message',
1099
+ buttonParamsJson: JSON.stringify({
1100
+ display_text: '...'
1101
+ })
1102
+ },
1103
+ {
1104
+ name: 'send_location',
1105
+ buttonParamsJson: JSON.stringify({
1106
+ display_text: '...'
1107
+ })
1108
+ },
1109
+ {
1110
+ name: 'open_webview',
1111
+ buttonParamsJson: JSON.stringify({
1112
+ title: 'Follow Me!',
1113
+ link: {
1114
+ in_app_webview: true, // or false
1115
+ url: 'https://whatsapp.com/channel/0029Vag9VSI2ZjCocqa2lB1y'
1116
+ }
1117
+ })
1118
+ },
1119
+ {
1120
+ name: 'mpm',
1121
+ buttonParamsJson: JSON.stringify({
1122
+ product_id: '8816262248471474'
1123
+ })
1124
+ },
1125
+ {
1126
+ name: 'wa_payment_transaction_details',
1127
+ buttonParamsJson: JSON.stringify({
1128
+ transaction_id: '12345848'
1129
+ })
1130
+ },
1131
+ {
1132
+ name: 'automated_greeting_message_view_catalog',
1133
+ buttonParamsJson: JSON.stringify({
1134
+ business_phone_number: '628xxx',
1135
+ catalog_product_id: '12345'
1136
+ })
1137
+ },
1138
+ {
1139
+ name: 'galaxy_message',
1140
+ buttonParamsJson: JSON.stringify({
1141
+ mode: 'published',
1142
+ flow_message_version: '3',
1143
+ flow_token: '1:1307913409923914:293680f87029f5a13d1ec5e35e718af3',
1144
+ flow_id: '1307913409923914',
1145
+ flow_cta: 'Itsockchann kawaii >\\<',
1146
+ flow_action: 'navigate',
1147
+ flow_action_payload: {
1148
+ screen: 'QUESTION_ONE',
1149
+ params: {
1150
+ user_id: '123456789',
1151
+ referral: 'campaign_xyz'
1152
+ }
1153
+ },
1154
+ flow_metadata: {
1155
+ flow_json_version: '201',
1156
+ data_api_protocol: 'v2',
1157
+ flow_name: 'Lead Qualification [en]',
1158
+ data_api_version: 'v2',
1159
+ categories: ['Lead Generation', 'Sales']
1160
+ }
1161
+ })
1162
+ },
1163
+ {
1164
+ name: 'single_select',
1165
+ buttonParamsJson: JSON.stringify({
1166
+ title: 'Click Me!',
1167
+ sections: [
1168
+ {
1169
+ title: 'Title 1',
1170
+ highlight_label: 'Highlight label 1',
1171
+ rows: [
1172
+ {
1173
+ header: 'Header 1',
1174
+ title: 'Title 1',
1175
+ description: 'Description 1',
1176
+ id: 'Id 1'
1177
+ },
1178
+ {
1179
+ header: 'Header 2',
1180
+ title: 'Title 2',
1181
+ description: 'Description 2',
1182
+ id: 'Id 2'
1183
+ }
1184
+ ]
1185
+ }
1186
+ ]
1187
+ })
1188
+ }
1189
+ ]
1190
+ }
1191
+ )
1192
+
1193
+ // If you want to use an image
1194
+ await sock.sendMessage(
1195
+ jid,
1196
+ {
1197
+ image: {
1198
+ url: 'https://example.com/jdbenkksjs.jpg'
1199
+ },
1200
+ caption: 'Body',
1201
+ title: 'Title',
1202
+ subtitle: 'Subtitle',
1203
+ footer: 'Footer',
1204
+ interactiveButtons: [
1205
+ {
1206
+ name: 'quick_reply',
1207
+ buttonParamsJson: JSON.stringify({
1208
+ display_text: 'DisplayText',
1209
+ id: 'ID1'
1210
+ })
1211
+ }
1212
+ ],
1213
+ hasMediaAttachment: false // or true
1214
+ }
1215
+ )
1216
+
1217
+ // If you want to use an video
1218
+ await sock.sendMessage(
1219
+ jid,
1220
+ {
1221
+ video: {
1222
+ url: 'https://example.com/jdbenkksjs.mp4'
1223
+ },
1224
+ caption: 'Body',
1225
+ title: 'Title',
1226
+ subtitle: 'Subtitle',
1227
+ footer: 'Footer',
1228
+ interactiveButtons: [
1229
+ {
1230
+ name: 'quick_reply',
1231
+ buttonParamsJson: JSON.stringify({
1232
+ display_text: 'DisplayText',
1233
+ id: 'ID1'
1234
+ })
1235
+ }
1236
+ ],
1237
+ hasMediaAttachment: false // or true
1238
+ }
1239
+ )
1240
+
1241
+ // If you want to use an document
1242
+ await sock.sendMessage(
1243
+ jid,
1244
+ {
1245
+ document: {
1246
+ url: 'https://example.com/jdbenkksjs.jpg'
1247
+ },
1248
+ mimetype: 'image/jpeg',
1249
+ jpegThumbnail: await sock.resize('https://example.com/jdbenkksjs.jpg', 320, 320),
1250
+ caption: 'Body',
1251
+ title: 'Title',
1252
+ subtitle: 'Subtitle',
1253
+ footer: 'Footer',
1254
+ interactiveButtons: [
1255
+ {
1256
+ name: 'quick_reply',
1257
+ buttonParamsJson: JSON.stringify({
1258
+ display_text: 'DisplayText',
1259
+ id: 'ID1'
1260
+ })
1261
+ }
1262
+ ],
1263
+ hasMediaAttachment: false // or true
1264
+ }
1265
+ )
1266
+
1267
+ // If you want to use an location
1268
+ await sock.sendMessage(
1269
+ jid,
1270
+ {
1271
+ location: {
1272
+ degressLatitude: -0,
1273
+ degressLongitude: 0,
1274
+ name: 'Hi'
1275
+ },
1276
+ caption: 'Body',
1277
+ title: 'Title',
1278
+ subtitle: 'Subtitle',
1279
+ footer: 'Footer',
1280
+ interactiveButtons: [
1281
+ {
1282
+ name: 'quick_reply',
1283
+ buttonParamsJson: JSON.stringify({
1284
+ display_text: 'DisplayText',
1285
+ id: 'ID1'
1286
+ })
1287
+ }
1288
+ ],
1289
+ hasMediaAttachment: false // or true
1290
+ }
1291
+ )
1292
+
1293
+ // if you want to use an product
1294
+ await sock.sendMessage(
1295
+ jid,
1296
+ {
1297
+ product: {
1298
+ productImage: {
1299
+ url: 'https://example.com/jdbenkksjs.jpg'
1300
+ },
1301
+ productId: '836xxx',
1302
+ title: 'Title',
1303
+ description: 'Description',
1304
+ currencyCode: 'IDR',
1305
+ priceAmount1000: '283xxx',
1306
+ retailerId: 'Itsockchann',
1307
+ url: 'https://example.com',
1308
+ productImageCount: 1
1309
+ },
1310
+ businessOwnerJid: '628xxx@s.whatsapp.net',
1311
+ caption: 'Body',
1312
+ title: 'Title',
1313
+ subtitle: 'Subtitle',
1314
+ footer: 'Footer',
1315
+ interactiveButtons: [
1316
+ {
1317
+ name: 'quick_reply',
1318
+ buttonParamsJson: JSON.stringify({
1319
+ display_text: 'DisplayText',
1320
+ id: 'ID1'
1321
+ })
1322
+ }
1323
+ ],
1324
+ hasMediaAttachment: false // or true
1325
+ }
1326
+ )
1327
+ ```
1328
+
1329
+ ### Buttons Interactive Message PIX
1330
+ ```ts
1331
+ await sock.sendMessage(
1332
+ jid,
1333
+ {
1334
+ text: '', // This string is required. Even it's empty.
1335
+ interactiveButtons: [
1336
+ {
1337
+ name: 'payment_info',
1338
+ buttonParamsJson: JSON.stringify({
1339
+ payment_settings: [{
1340
+ type: "pix_static_code",
1341
+ pix_static_code: {
1342
+ merchant_name: 'itsockchann kawaii >\\\\\\<',
1343
+ key: 'example@itsockchan.com',
1344
+ key_type: 'EMAIL' // PHONE || EMAIL || CPF || EVP
1345
+ }
1346
+ }]
1347
+ })
1348
+ }
1349
+ ],
1350
+ }
1351
+ )
1352
+ ```
1353
+
1354
+ ### Buttons Interactive Message PAY
1355
+ ```ts
1356
+ await sock.sendMessage(
1357
+ jid,
1358
+ {
1359
+ text: '', // This string is required. Even it's empty.
1360
+ interactiveButtons: [
1361
+ {
1362
+ name: 'review_and_pay',
1363
+ buttonParamsJson: JSON.stringify({
1364
+ currency: 'IDR',
1365
+ payment_configuration: '',
1366
+ payment_type: '',
1367
+ total_amount: {
1368
+ value: '999999999',
1369
+ offset: '100'
1370
+ },
1371
+ reference_id: '45XXXXX',
1372
+ type: 'physical-goods',
1373
+ payment_method: 'confirm',
1374
+ payment_status: 'captured',
1375
+ payment_timestamp: Math.floor(Date.now() / 1000),
1376
+ order: {
1377
+ status: 'completed',
1378
+ description: '',
1379
+ subtotal: {
1380
+ value: '0',
1381
+ offset: '100'
1382
+ },
1383
+ order_type: 'PAYMENT_REQUEST',
1384
+ items: [{
1385
+ retailer_id: 'your_retailer_id',
1386
+ name: 'Itsockchann Kawaii >\\\<',
1387
+ amount: {
1388
+ value: '999999999',
1389
+ offset: '100'
1390
+ },
1391
+ quantity: '1',
1392
+ }]
1393
+ },
1394
+ additional_note: 'Itsockchann Kawaii >\\\<',
1395
+ native_payment_methods: [],
1396
+ share_payment_status: false
1397
+ })
1398
+ }
1399
+ ],
1400
+ }
1401
+ )
1402
+ ```
1403
+
1404
+ ### Status Mentions Message
1405
+ ```ts
1406
+ const jidat = [
1407
+ '123451679@g.us',
1408
+ '124848899@g.us',
1409
+ '111384848@g.us',
1410
+ '62689xxxx@s.whatsapp.net',
1411
+ '62xxxxxxx@s.whatsapp.net'
1412
+ ]
1413
+ // Text
1414
+ await sock.sendStatusMentions(
1415
+ {
1416
+ text: 'Hello Everyone :3',
1417
+ font: 2, // optional
1418
+ textColor: 'FF0000', // optional
1419
+ backgroundColor: '#000000' // optional
1420
+ },
1421
+ jids // Limit to 5 mentions per status
1422
+ )
1423
+
1424
+ // Image
1425
+ await sock.sendStatusMentions(
1426
+ {
1427
+ Image: { url: 'https://example.com/ruriooe.jpg' }, or image buffer
1428
+ caption: 'Hello Everyone :3' // optional
1429
+ },
1430
+ jids // Limit to 5 mentions per status
1431
+ )
1432
+
1433
+ // Video
1434
+ await sock.sendStatusMentions(
1435
+ {
1436
+ video: { url: 'https://example.com/ruriooe.mp4' }, or video buffer
1437
+ caption: 'Hello Everyone :3' // optional
1438
+ },
1439
+ jids // Limit to 5 mentions per status
1440
+ )
1441
+
1442
+ // Audio
1443
+ await sock.sendStatusMentions(
1444
+ {
1445
+ audio: { url: 'https://example.com/ruriooe.mp3' }, or audio buffer
1446
+ backgroundColor: '#000000', // optional
1447
+ mimetype: 'audio/mp4',
1448
+ ppt: true
1449
+ },
1450
+ jids // Limit to 5 mentions per status
1451
+ )
1452
+ ```
1453
+
1454
+ ### Shop Message
1455
+ ```ts
1456
+ await sock.sendMessage(
1457
+ jid,
1458
+ {
1459
+ text: 'Body',
1460
+ title: 'Title',
1461
+ subtitle: 'Subtitle',
1462
+ footer: 'Footer',
1463
+ shop: {
1464
+ surface: 1, // 2 | 3 | 4
1465
+ id: 'https://example.com'
1466
+ },
1467
+ viewOnce: true
1468
+ }
1469
+ )
1470
+
1471
+ // Image
1472
+ await sock.sendMessage(
1473
+ jid,
1474
+ {
1475
+ image: {
1476
+ url: 'https://example.com/jdbenkksjs.jpg'
1477
+ },
1478
+ caption: 'Body',
1479
+ title: 'Title',
1480
+ subtitle: 'Subtitle',
1481
+ footer: 'Footer',
1482
+ shop: {
1483
+ surface: 1, // 2 | 3 | 4
1484
+ id: 'https://example.com'
1485
+ },
1486
+ hasMediaAttachment: false, // or true
1487
+ viewOnce: true
1488
+ }
1489
+ )
1490
+
1491
+ // Video
1492
+ await sock.sendMessage(
1493
+ jid,
1494
+ {
1495
+ video: {
1496
+ url: 'https://example.com/jdbenkksjs.jpg'
1497
+ },
1498
+ caption: 'Body',
1499
+ title: 'Title',
1500
+ subtitle: 'Subtitle',
1501
+ footer: 'Footer',
1502
+ shop: {
1503
+ surface: 1, // 2 | 3 | 4
1504
+ id: 'https://example.com'
1505
+ },
1506
+ hasMediaAttachment: false, // or true
1507
+ viewOnce: true
1508
+ }
1509
+ )
1510
+
1511
+ // Document
1512
+ await sock.sendMessage(
1513
+ jid,
1514
+ {
1515
+ document: {
1516
+ url: 'https://example.com/jdbenkksjs.jpg'
1517
+ },
1518
+ mimetype: 'image/jpeg',
1519
+ jpegThumbnail: await sock.resize('https://example.com/jdbenkksjs.jpg', 320, 320),
1520
+ caption: 'Body',
1521
+ title: 'Title',
1522
+ subtitle: 'Subtitle',
1523
+ footer: 'Footer',
1524
+ shop: {
1525
+ surface: 1, // 2 | 3 | 4
1526
+ id: 'https://example.com'
1527
+ },
1528
+ hasMediaAttachment: false, // or true,
1529
+ viewOnce: true
1530
+ }
1531
+ )
1532
+
1533
+ // Location
1534
+ await sock.sendMessage(
1535
+ jid,
1536
+ {
1537
+ location: {
1538
+ degressLatitude: -0,
1539
+ degressLongitude: 0,
1540
+ name: 'Hi'
1541
+ },
1542
+ caption: 'Body',
1543
+ title: 'Title',
1544
+ subtitle: 'Subtitle',
1545
+ footer: 'Footer',
1546
+ shop: {
1547
+ surface: 1, // 2 | 3 | 4
1548
+ id: 'https://example.com'
1549
+ },
1550
+ hasMediaAttachment: false, // or true
1551
+ viewOnce: true
1552
+ }
1553
+ )
1554
+
1555
+ // Product
1556
+ await sock.sendMessage(
1557
+ jid,
1558
+ {
1559
+ product: {
1560
+ productImage: {
1561
+ url: 'https://example.com/jdbenkksjs.jpg'
1562
+ },
1563
+ productId: '836xxx',
1564
+ title: 'Title',
1565
+ description: 'Description',
1566
+ currencyCode: 'IDR',
1567
+ priceAmount1000: '283xxx',
1568
+ retailerId: 'Itsockchann',
1569
+ url: 'https://example.com',
1570
+ productImageCount: 1
1571
+ },
1572
+ businessOwnerJid: '628xxx@s.whatsapp.net',
1573
+ caption: 'Body',
1574
+ title: 'Title',
1575
+ subtitle: 'Subtitle',
1576
+ footer: 'Footer',
1577
+ shop: {
1578
+ surface: 1, // 2 | 3 | 4
1579
+ id: 'https://example.com'
1580
+ },
1581
+ hasMediaAttachment: false, // or true
1582
+ viewOnce: true
1583
+ }
1584
+ )
1585
+ ```
1586
+ ### Collection Message
1587
+ ```ts
1588
+ await sock.sendMessage(
1589
+ jid,
1590
+ {
1591
+ text: 'Body',
1592
+ title: 'Title',
1593
+ subtitle: 'Subtitle',
1594
+ footer: 'Footer',
1595
+ collection: {
1596
+ bizJid: 'jid',
1597
+ id: 'https://example.com',
1598
+ version: 1
1599
+ },
1600
+ viewOnce: true
1601
+ }
1602
+ )
1603
+
1604
+ // Image
1605
+ await sock.sendMessage(
1606
+ jid,
1607
+ {
1608
+ image: {
1609
+ url: 'https://example.com/jdbenkksjs.jpg'
1610
+ },
1611
+ caption: 'Body',
1612
+ title: 'Title',
1613
+ subtitle: 'Subtitle',
1614
+ footer: 'Footer',
1615
+ collection: {
1616
+ bizJid: 'jid',
1617
+ id: 'https://example.com',
1618
+ version: 1
1619
+ },
1620
+ hasMediaAttachment: false, // or true
1621
+ viewOnce: true
1622
+ }
1623
+ )
1624
+
1625
+ // Video
1626
+ await sock.sendMessage(
1627
+ jid,
1628
+ {
1629
+ video: {
1630
+ url: 'https://example.com/jdbenkksjs.jpg'
1631
+ },
1632
+ caption: 'Body',
1633
+ title: 'Title',
1634
+ subtitle: 'Subtitle',
1635
+ footer: 'Footer',
1636
+ collection: {
1637
+ bizJid: 'jid',
1638
+ id: 'https://example.com',
1639
+ version: 1
1640
+ },
1641
+ hasMediaAttachment: false, // or true
1642
+ viewOnce: true
1643
+ }
1644
+ )
1645
+
1646
+ // Document
1647
+ await sock.sendMessage(
1648
+ jid,
1649
+ {
1650
+ document: {
1651
+ url: 'https://example.com/jdbenkksjs.jpg'
1652
+ },
1653
+ mimetype: 'image/jpeg',
1654
+ jpegThumbnail: await sock.resize('https://example.com/jdbenkksjs.jpg', 320, 320),
1655
+ caption: 'Body',
1656
+ title: 'Title',
1657
+ subtitle: 'Subtitle',
1658
+ footer: 'Footer',
1659
+ collection: {
1660
+ bizJid: 'jid',
1661
+ id: 'https://example.com',
1662
+ version: 1
1663
+ },
1664
+ hasMediaAttachment: false, // or true,
1665
+ viewOnce: true
1666
+ }
1667
+ )
1668
+
1669
+ // Location
1670
+ await sock.sendMessage(
1671
+ jid,
1672
+ {
1673
+ location: {
1674
+ degressLatitude: -0,
1675
+ degressLongitude: 0,
1676
+ name: 'Hi'
1677
+ },
1678
+ caption: 'Body',
1679
+ title: 'Title',
1680
+ subtitle: 'Subtitle',
1681
+ footer: 'Footer',
1682
+ collection: {
1683
+ bizJid: 'jid',
1684
+ id: 'https://example.com',
1685
+ version: 1
1686
+ },
1687
+ hasMediaAttachment: false, // or true
1688
+ viewOnce: true
1689
+ }
1690
+ )
1691
+
1692
+ // Product
1693
+ await sock.sendMessage(
1694
+ jid,
1695
+ {
1696
+ product: {
1697
+ productImage: {
1698
+ url: 'https://example.com/jdbenkksjs.jpg'
1699
+ },
1700
+ productId: '836xxx',
1701
+ title: 'Title',
1702
+ description: 'Description',
1703
+ currencyCode: 'IDR',
1704
+ priceAmount1000: '283xxx',
1705
+ retailerId: 'Itsockchann',
1706
+ url: 'https://example.com',
1707
+ productImageCount: 1
1708
+ },
1709
+ businessOwnerJid: '628xxx@s.whatsapp.net',
1710
+ caption: 'Body',
1711
+ title: 'Title',
1712
+ subtitle: 'Subtitle',
1713
+ footer: 'Footer',
1714
+ collection: {
1715
+ bizJid: 'jid',
1716
+ id: 'https://example.com',
1717
+ version: 1
1718
+ },
1719
+ hasMediaAttachment: false, // or true
1720
+ viewOnce: true
1721
+ }
1722
+ )
1723
+ ```
1724
+
1725
+ ### AI Icon Feature
1726
+ ```ts
1727
+ await sock.sendMessage(
1728
+ jid,
1729
+ {
1730
+ text: 'Hi'
1731
+ }, {
1732
+ ai: true // Add ai usage and change it to true
1733
+ }
1734
+ )
1735
+
1736
+ // If using relay
1737
+ await sock.relayMessage(
1738
+ jid,
1739
+ {
1740
+ extendedTextMessage: {
1741
+ text: 'Hi'
1742
+ }
1743
+ }, {
1744
+ AI: true // Use capital letters
1745
+ }
1746
+ )
1747
+ ```
1748
+
1749
+ ### Sending Messages with Link Previews
1750
+
1751
+ 1. By default, wa does not have link generation when sent from the web
1752
+ 2. Baileys has a function to generate the content for these link previews
1753
+ 3. To enable this function's usage, add `link-preview-js` as a dependency to your project with `yarn add link-preview-js`
1754
+ 4. Send a link:
1755
+ ```ts
1756
+ await sock.sendMessage(
1757
+ jid,
1758
+ {
1759
+ text: 'Hi, this was sent using https://github.com/whiskeysockets/baileys'
1760
+ }
1761
+ )
1762
+ ```
1763
+
1764
+ ### Media Messages
1765
+
1766
+ Sending media (video, stickers, images) is easier & more efficient than ever.
1767
+
1768
+ > [!NOTE]
1769
+ > In media messages, you can pass `{ stream: Stream }` or `{ url: Url }` or `Buffer` directly, you can see more [here](https://baileys.whiskeysockets.io/types/WAMediaUpload.html)
1770
+
1771
+ - When specifying a media url, Baileys never loads the entire buffer into memory it even encrypts the media as a readable stream.
1772
+
1773
+ > [!TIP]
1774
+ > It's recommended to use Stream or Url to save memory
1775
+
1776
+ #### Gif Message
1777
+ - Whatsapp doesn't support `.gif` files, that's why we send gifs as common `.mp4` video with `gifPlayback` flag
1778
+ ```ts
1779
+ await sock.sendMessage(
1780
+ jid,
1781
+ {
1782
+ video: fs.readFileSync('Media/ma_gif.mp4'),
1783
+ caption: 'hello word',
1784
+ gifPlayback: true
1785
+ }
1786
+ )
1787
+ ```
1788
+
1789
+ #### Video Message
1790
+ ```ts
1791
+ await sock.sendMessage(
1792
+ id,
1793
+ {
1794
+ video: {
1795
+ url: './Media/ma_gif.mp4'
1796
+ },
1797
+ caption: 'hello word',
1798
+ }
1799
+ )
1800
+ ```
1801
+
1802
+ #### Video Ptv Message
1803
+ ```ts
1804
+ await sock.sendMessage(
1805
+ id,
1806
+ {
1807
+ video: {
1808
+ url: './Media/ma_gif.mp4'
1809
+ },
1810
+ ptv: true
1811
+ }
1812
+ )
1813
+ ```
1814
+
1815
+ #### Audio Message
1816
+ - To audio message work in all devices you need to convert with some tool like `ffmpeg` with this flags:
1817
+ ```bash
1818
+ codec: libopus //ogg file
1819
+ ac: 1 //one channel
1820
+ avoid_negative_ts
1821
+ make_zero
1822
+ ```
1823
+ - Example:
1824
+ ```bash
1825
+ ffmpeg -i input.mp4 -avoid_negative_ts make_zero -ac 1 output.ogg
1826
+ ```
1827
+ ```ts
1828
+ await sock.sendMessage(
1829
+ jid,
1830
+ {
1831
+ audio: {
1832
+ url: './Media/audio.mp3'
1833
+ },
1834
+ mimetype: 'audio/mp4'
1835
+ }
1836
+ )
1837
+ ```
1838
+
1839
+ #### Image Message
1840
+ ```ts
1841
+ await sock.sendMessage(
1842
+ id,
1843
+ {
1844
+ image: {
1845
+ url: './Media/ma_img.png'
1846
+ },
1847
+ caption: 'hello word'
1848
+ }
1849
+ )
1850
+ ```
1851
+
1852
+ ### Album Message
1853
+ ```ts
1854
+ await sock.sendMessage(
1855
+ id,
1856
+ {
1857
+ album: [{
1858
+ image: {
1859
+ url: 'https://example.com/itsockchan.jpg'
1860
+ },
1861
+ caption: 'Hay'
1862
+ }, {
1863
+ image: Buffer,
1864
+ caption: 'Hay'
1865
+ }, {
1866
+ video: {
1867
+ url: 'https://example.com/itsockchan.mp4'
1868
+ },
1869
+ caption: 'Hay'
1870
+ }, {
1871
+ video: Buffer,
1872
+ caption: 'Hay'
1873
+ }
1874
+ }
1875
+ )
1876
+ ```
1877
+
1878
+ #### View Once Message
1879
+
1880
+ - You can send all messages above as `viewOnce`, you only need to pass `viewOnce: true` in content object
1881
+
1882
+ ```ts
1883
+ await sock.sendMessage(
1884
+ id,
1885
+ {
1886
+ image: {
1887
+ url: './Media/ma_img.png'
1888
+ },
1889
+ viewOnce: true, //works with video, audio too
1890
+ caption: 'hello word'
1891
+ }
1892
+ )
1893
+ ```
1894
+
1895
+ ## Modify Messages
1896
+
1897
+ ### Deleting Messages (for everyone)
1898
+
1899
+ ```ts
1900
+ const msg = await sock.sendMessage(jid, { text: 'hello word' })
1901
+ await sock.sendMessage(jid, { delete: msg.key })
1902
+ ```
1903
+
1904
+ **Note:** deleting for oneself is supported via `chatModify`, see in [this section](#modifying-chats)
1905
+
1906
+ ### Editing Messages
1907
+
1908
+ - You can pass all editable contents here
1909
+ ```ts
1910
+ await sock.sendMessage(jid, {
1911
+ text: 'updated text goes here',
1912
+ edit: response.key,
1913
+ })
1914
+ ```
1915
+
1916
+ ## Manipulating Media Messages
1917
+
1918
+ ### Thumbnail in Media Messages
1919
+ - 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`.
1920
+ - Thumbnails for videos can also be generated automatically, though, you need to have `ffmpeg` installed on your system.
1921
+
1922
+ ### Downloading Media Messages
1923
+
1924
+ If you want to save the media you received
1925
+ ```ts
1926
+ import { createWriteStream } from 'fs'
1927
+ import { downloadMediaMessage, getContentType } from '@elrayyxml/baileys'
1928
+
1929
+ sock.ev.on('messages.upsert', async ({ [m] }) => {
1930
+ if (!m.message) return // if there is no text or media message
1931
+ const messageType = getContentType(m) // get what type of message it is (text, image, video...)
1932
+
1933
+ // if the message is an image
1934
+ if (messageType === 'imageMessage') {
1935
+ // download the message
1936
+ const stream = await downloadMediaMessage(
1937
+ m,
1938
+ 'stream', // can be 'buffer' too
1939
+ { },
1940
+ {
1941
+ logger,
1942
+ // pass this so that baileys can request a reupload of media
1943
+ // that has been deleted
1944
+ reuploadRequest: sock.updateMediaMessage
1945
+ }
1946
+ )
1947
+ // save to file
1948
+ const writeStream = createWriteStream('./my-download.jpeg')
1949
+ stream.pipe(writeStream)
1950
+ }
1951
+ }
1952
+ ```
1953
+
1954
+ ### Re-upload Media Message to Whatsapp
1955
+
1956
+ - 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:
1957
+ ```ts
1958
+ await sock.updateMediaMessage(msg)
1959
+ ```
1960
+
1961
+ ## Reject Call
1962
+
1963
+ - You can obtain `callId` and `callFrom` from `call` event
1964
+
1965
+ ```ts
1966
+ await sock.rejectCall(callId, callFrom)
1967
+ ```
1968
+
1969
+ ## Send States in Chat
1970
+
1971
+ ### Reading Messages
1972
+ - A set of message [keys](https://baileys.whiskeysockets.io/types/WAMessageKey.html) must be explicitly marked read now.
1973
+ - You cannot mark an entire 'chat' read as it were with Baileys Web.
1974
+ This means you have to keep track of unread messages.
1975
+
1976
+ ```ts
1977
+ const key: WAMessageKey
1978
+ // can pass multiple keys to read multiple messages as well
1979
+ await sock.readMessages([key])
1980
+ ```
1981
+
1982
+ The message ID is the unique identifier of the message that you are marking as read.
1983
+ On a `WAMessage`, the `messageID` can be accessed using ```messageID = message.key.id```.
1984
+
1985
+ ### Update Presence
1986
+
1987
+ - ``` presence ``` can be one of [these](https://baileys.whiskeysockets.io/types/WAPresence.html)
1988
+ - The presence expires after about 10 seconds.
1989
+ - This lets the person/group with `jid` know whether you're online, offline, typing etc.
1990
+
1991
+ ```ts
1992
+ await sock.sendPresenceUpdate('available', jid)
1993
+ ```
1994
+
1995
+ > [!NOTE]
1996
+ > 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')`
1997
+
1998
+ ## Modifying Chats
1999
+
2000
+ WA uses an encrypted form of communication to send chat/app updates. This has been implemented mostly and you can send the following updates:
2001
+
2002
+ > [!IMPORTANT]
2003
+ > 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.
2004
+
2005
+ ### Archive a Chat
2006
+ ```ts
2007
+ const lastMsgInChat = await getLastMessageInChat(jid) // implement this on your end
2008
+ await sock.chatModify({ archive: true, lastMessages: [lastMsgInChat] }, jid)
2009
+ ```
2010
+ ### Mute/Unmute a Chat
2011
+
2012
+ - Supported times:
2013
+
2014
+ | Time | Miliseconds |
2015
+ |-------|-----------------|
2016
+ | Remove | null |
2017
+ | 8h | 86.400.000 |
2018
+ | 7d | 604.800.000 |
2019
+
2020
+ ```ts
2021
+ // mute for 8 hours
2022
+ await sock.chatModify({ mute: 8 * 60 * 60 * 1000 }, jid)
2023
+ // unmute
2024
+ await sock.chatModify({ mute: null }, jid)
2025
+ ```
2026
+ ### Mark a Chat Read/Unread
2027
+ ```ts
2028
+ const lastMsgInChat = await getLastMessageInChat(jid) // implement this on your end
2029
+ // mark it unread
2030
+ await sock.chatModify({ markRead: false, lastMessages: [lastMsgInChat] }, jid)
2031
+ ```
2032
+
2033
+ ### Delete a Message for Me
2034
+ ```ts
2035
+ await sock.chatModify(
2036
+ {
2037
+ clear: {
2038
+ messages: [
2039
+ {
2040
+ id: 'ATWYHDNNWU81732J',
2041
+ fromMe: true,
2042
+ timestamp: '1654823909'
2043
+ }
2044
+ ]
2045
+ }
2046
+ },
2047
+ jid
2048
+ )
2049
+
2050
+ ```
2051
+ ### Delete a Chat
2052
+ ```ts
2053
+ const lastMsgInChat = await getLastMessageInChat(jid) // implement this on your end
2054
+ await sock.chatModify({
2055
+ delete: true,
2056
+ lastMessages: [
2057
+ {
2058
+ key: lastMsgInChat.key,
2059
+ messageTimestamp: lastMsgInChat.messageTimestamp
2060
+ }
2061
+ ]
2062
+ },
2063
+ jid
2064
+ )
2065
+ ```
2066
+ ### Pin/Unpin a Chat
2067
+ ```ts
2068
+ await sock.chatModify({
2069
+ pin: true // or `false` to unpin
2070
+ },
2071
+ jid
2072
+ )
2073
+ ```
2074
+ ### Star/Unstar a Message
2075
+ ```ts
2076
+ await sock.chatModify({
2077
+ star: {
2078
+ messages: [
2079
+ {
2080
+ id: 'messageID',
2081
+ fromMe: true // or `false`
2082
+ }
2083
+ ],
2084
+ star: true // - true: Star Message false: Unstar Message
2085
+ }
2086
+ },
2087
+ jid
2088
+ )
2089
+ ```
2090
+
2091
+ ### Disappearing Messages
2092
+
2093
+ - Ephemeral can be:
2094
+
2095
+ | Time | Seconds |
2096
+ |-------|----------------|
2097
+ | Remove | 0 |
2098
+ | 24h | 86.400 |
2099
+ | 7d | 604.800 |
2100
+ | 90d | 7.776.000 |
2101
+
2102
+ - You need to pass in **Seconds**, default is 7 days
2103
+
2104
+ ```ts
2105
+ // turn on disappearing messages
2106
+ await sock.sendMessage(
2107
+ jid,
2108
+ // this is 1 week in seconds -- how long you want messages to appear for
2109
+ { disappearingMessagesInChat: WA_DEFAULT_EPHEMERAL }
2110
+ )
2111
+
2112
+ // will send as a disappearing message
2113
+ await sock.sendMessage(jid, { text: 'hello' }, { ephemeralExpiration: WA_DEFAULT_EPHEMERAL })
2114
+
2115
+ // turn off disappearing messages
2116
+ await sock.sendMessage(
2117
+ jid,
2118
+ { disappearingMessagesInChat: false }
2119
+ )
2120
+ ```
2121
+
2122
+ ### Clear Messages
2123
+ ```ts
2124
+ await sock.clearMessage(jid, key, timestamps)
2125
+ ```
2126
+
2127
+ ## User Querys
2128
+
2129
+ ### Check If ID Exists in Whatsapp
2130
+ ```ts
2131
+ const [result] = await sock.onWhatsApp(jid)
2132
+ if (result.exists) console.log (`${jid} exists on WhatsApp, as jid: ${result.jid}`)
2133
+ ```
2134
+
2135
+ ### Query Chat History (groups too)
2136
+
2137
+ - You need to have oldest message in chat
2138
+ ```ts
2139
+ const msg = await getOldestMessageInChat(jid)
2140
+ await sock.fetchMessageHistory(
2141
+ 50, //quantity (max: 50 per query)
2142
+ msg.key,
2143
+ msg.messageTimestamp
2144
+ )
2145
+ ```
2146
+ - Messages will be received in `messaging.history-set` event
2147
+
2148
+ ### Fetch Status
2149
+ ```ts
2150
+ const status = await sock.fetchStatus(jid)
2151
+ console.log('status: ' + status)
2152
+ ```
2153
+
2154
+ ### Fetch Profile Picture
2155
+ - To get the display picture of some person, group and channel
2156
+ ```ts
2157
+ // for low res picture
2158
+ const ppUrl = await sock.profilePictureUrl(jid)
2159
+ console.log(ppUrl)
2160
+ ```
2161
+
2162
+ ### Fetch Bussines Profile (such as description or category)
2163
+ ```ts
2164
+ const profile = await sock.getBusinessProfile(jid)
2165
+ console.log('business description: ' + profile.description + ', category: ' + profile.category)
2166
+ ```
2167
+
2168
+ ### Fetch Someone's Presence (if they're typing or online)
2169
+ ```ts
2170
+ // the presence update is fetched and called here
2171
+ sock.ev.on('presence.update', console.log)
2172
+
2173
+ // request updates for a chat
2174
+ await sock.presenceSubscribe(jid)
2175
+ ```
2176
+
2177
+ ## Change Profile
2178
+
2179
+ ### Change Profile Status
2180
+ ```ts
2181
+ await sock.updateProfileStatus('Hello World!')
2182
+ ```
2183
+ ### Change Profile Name
2184
+ ```ts
2185
+ await sock.updateProfileName('My name')
2186
+ ```
2187
+ ### Change Display Picture (groups too)
2188
+ - To change your display picture or a group's
2189
+
2190
+ > [!NOTE]
2191
+ > Like media messages, you can pass `{ stream: Stream }` or `{ url: Url }` or `Buffer` directly, you can see more [here](https://baileys.whiskeysockets.io/types/WAMediaUpload.html)
2192
+
2193
+ ```ts
2194
+ await sock.updateProfilePicture(jid, { url: './new-profile-picture.jpeg' })
2195
+ ```
2196
+ ### Remove display picture (groups too)
2197
+ ```ts
2198
+ await sock.removeProfilePicture(jid)
2199
+ ```
2200
+
2201
+ ## Groups
2202
+
2203
+ - To change group properties you need to be admin
2204
+
2205
+ ### Create a Group
2206
+ ```ts
2207
+ // title & participants
2208
+ const group = await sock.groupCreate('My Fab Group', ['1234@s.whatsapp.net', '4564@s.whatsapp.net'])
2209
+ console.log('created group with id: ' + group.gid)
2210
+ await sock.sendMessage(group.id, { text: 'hello there' }) // say hello to everyone on the group
2211
+ ```
2212
+ ### Add/Remove or Demote/Promote
2213
+ ```ts
2214
+ // id & people to add to the group (will throw error if it fails)
2215
+ await sock.groupParticipantsUpdate(
2216
+ jid,
2217
+ ['abcd@s.whatsapp.net', 'efgh@s.whatsapp.net'],
2218
+ 'add' // replace this parameter with 'remove' or 'demote' or 'promote'
2219
+ )
2220
+ ```
2221
+ ### Change Subject (name)
2222
+ ```ts
2223
+ await sock.groupUpdateSubject(jid, 'New Subject!')
2224
+ ```
2225
+ ### Change Description
2226
+ ```ts
2227
+ await sock.groupUpdateDescription(jid, 'New Description!')
2228
+ ```
2229
+ ### Change Settings
2230
+ ```ts
2231
+ // only allow admins to send messages
2232
+ await sock.groupSettingUpdate(jid, 'announcement')
2233
+ // allow everyone to send messages
2234
+ await sock.groupSettingUpdate(jid, 'not_announcement')
2235
+ // allow everyone to modify the group's settings -- like display picture etc.
2236
+ await sock.groupSettingUpdate(jid, 'unlocked')
2237
+ // only allow admins to modify the group's settings
2238
+ await sock.groupSettingUpdate(jid, 'locked')
2239
+ ```
2240
+ ### Leave a Group
2241
+ ```ts
2242
+ // will throw error if it fails
2243
+ await sock.groupLeave(jid)
2244
+ ```
2245
+ ### Get Invite Code
2246
+ - To create link with code use `'https://chat.whatsapp.com/' + code`
2247
+ ```ts
2248
+ const code = await sock.groupInviteCode(jid)
2249
+ console.log('group code: ' + code)
2250
+ ```
2251
+ ### Revoke Invite Code
2252
+ ```ts
2253
+ const code = await sock.groupRevokeInvite(jid)
2254
+ console.log('New group code: ' + code)
2255
+ ```
2256
+ ### Join Using Invitation Code
2257
+ - Code can't have `https://chat.whatsapp.com/`, only code
2258
+ ```ts
2259
+ const response = await sock.groupAcceptInvite(code)
2260
+ console.log('joined to: ' + response)
2261
+ ```
2262
+ ### Get Group Info by Invite Code
2263
+ ```ts
2264
+ const response = await sock.groupGetInviteInfo(code)
2265
+ console.log('group information: ' + response)
2266
+ ```
2267
+ ### Query Metadata (participants, name, description...)
2268
+ ```ts
2269
+ const metadata = await sock.groupMetadata(jid)
2270
+ console.log(metadata.id + ', title: ' + metadata.subject + ', description: ' + metadata.desc)
2271
+ ```
2272
+ ### Join using `groupInviteMessage`
2273
+ ```ts
2274
+ const response = await sock.groupAcceptInviteV4(jid, groupInviteMessage)
2275
+ console.log('joined to: ' + response)
2276
+ ```
2277
+ ### Get Request Join List
2278
+ ```ts
2279
+ const response = await sock.groupRequestParticipantsList(jid)
2280
+ console.log(response)
2281
+ ```
2282
+ ### Approve/Reject Request Join
2283
+ ```ts
2284
+ const response = await sock.groupRequestParticipantsUpdate(
2285
+ jid, // group id
2286
+ ['abcd@s.whatsapp.net', 'efgh@s.whatsapp.net'],
2287
+ 'approve' // or 'reject'
2288
+ )
2289
+ console.log(response)
2290
+ ```
2291
+ ### Get All Participating Groups Metadata
2292
+ ```ts
2293
+ const response = await sock.groupFetchAllParticipating()
2294
+ console.log(response)
2295
+ ```
2296
+ ### Toggle Ephemeral
2297
+
2298
+ - Ephemeral can be:
2299
+
2300
+ | Time | Seconds |
2301
+ |-------|----------------|
2302
+ | Remove | 0 |
2303
+ | 24h | 86.400 |
2304
+ | 7d | 604.800 |
2305
+ | 90d | 7.776.000 |
2306
+
2307
+ ```ts
2308
+ await sock.groupToggleEphemeral(jid, 86400)
2309
+ ```
2310
+
2311
+ ### Change Add Mode
2312
+ ```ts
2313
+ await sock.groupMemberAddMode(
2314
+ jid,
2315
+ 'all_member_add' // or 'admin_add'
2316
+ )
2317
+ ```
2318
+
2319
+ ## Privacy
2320
+
2321
+ ### Block/Unblock User
2322
+ ```ts
2323
+ await sock.updateBlockStatus(jid, 'block') // Block user
2324
+ await sock.updateBlockStatus(jid, 'unblock') // Unblock user
2325
+ ```
2326
+ ### Get Privacy Settings
2327
+ ```ts
2328
+ const privacySettings = await sock.fetchPrivacySettings(true)
2329
+ console.log('privacy settings: ' + privacySettings)
2330
+ ```
2331
+ ### Get BlockList
2332
+ ```ts
2333
+ const response = await sock.fetchBlocklist()
2334
+ console.log(response)
2335
+ ```
2336
+ ### Update LastSeen Privacy
2337
+ ```ts
2338
+ const value = 'all' // 'contacts' | 'contact_blacklist' | 'none'
2339
+ await sock.updateLastSeenPrivacy(value)
2340
+ ```
2341
+ ### Update Online Privacy
2342
+ ```ts
2343
+ const value = 'all' // 'match_last_seen'
2344
+ await sock.updateOnlinePrivacy(value)
2345
+ ```
2346
+ ### Update Profile Picture Privacy
2347
+ ```ts
2348
+ const value = 'all' // 'contacts' | 'contact_blacklist' | 'none'
2349
+ await sock.updateProfilePicturePrivacy(value)
2350
+ ```
2351
+ ### Update Status Privacy
2352
+ ```ts
2353
+ const value = 'all' // 'contacts' | 'contact_blacklist' | 'none'
2354
+ await sock.updateStatusPrivacy(value)
2355
+ ```
2356
+ ### Update Read Receipts Privacy
2357
+ ```ts
2358
+ const value = 'all' // 'none'
2359
+ await sock.updateReadReceiptsPrivacy(value)
2360
+ ```
2361
+ ### Update Groups Add Privacy
2362
+ ```ts
2363
+ const value = 'all' // 'contacts' | 'contact_blacklist'
2364
+ await sock.updateGroupsAddPrivacy(value)
2365
+ ```
2366
+ ### Update Default Disappearing Mode
2367
+
2368
+ - Like [this](#disappearing-messages), ephemeral can be:
2369
+
2370
+ | Time | Seconds |
2371
+ |-------|----------------|
2372
+ | Remove | 0 |
2373
+ | 24h | 86.400 |
2374
+ | 7d | 604.800 |
2375
+ | 90d | 7.776.000 |
2376
+
2377
+ ```ts
2378
+ const ephemeral = 86400
2379
+ await sock.updateDefaultDisappearingMode(ephemeral)
2380
+ ```
2381
+
2382
+ ## Broadcast Lists & Stories
2383
+
2384
+ ### Send Broadcast & Stories
2385
+ - Messages can be sent to broadcasts & stories. You need to add the following message options in sendMessage, like this:
2386
+ ```ts
2387
+ await sock.sendMessage(
2388
+ jid,
2389
+ {
2390
+ image: {
2391
+ url: url
2392
+ },
2393
+ caption: caption
2394
+ },
2395
+ {
2396
+ backgroundColor: backgroundColor,
2397
+ font: font,
2398
+ statusJidList: statusJidList,
2399
+ broadcast: true
2400
+ }
2401
+ )
2402
+ ```
2403
+ - Message body can be a `extendedTextMessage` or `imageMessage` or `videoMessage` or `voiceMessage`, see [here](https://baileys.whiskeysockets.io/types/AnyRegularMessageContent.html)
2404
+ - You can add `backgroundColor` and other options in the message options, see [here](https://baileys.whiskeysockets.io/types/MiscMessageGenerationOptions.html)
2405
+ - `broadcast: true` enables broadcast mode
2406
+ - `statusJidList`: a list of people that you can get which you need to provide, which are the people who will get this status message.
2407
+
2408
+ - You can send messages to broadcast lists the same way you send messages to groups & individual chats.
2409
+ - Right now, WA Web does not support creating broadcast lists, but you can still delete them.
2410
+ - Broadcast IDs are in the format `12345678@broadcast`
2411
+ ### Query a Broadcast List's Recipients & Name
2412
+ ```ts
2413
+ const bList = await sock.getBroadcastListInfo('1234@broadcast')
2414
+ console.log (`list name: ${bList.name}, recps: ${bList.recipients}`)
2415
+ ```
2416
+
2417
+ ## Writing Custom Functionality
2418
+ Baileys is written with custom functionality in mind. Instead of forking the project & re-writing the internals, you can simply write your own extensions.
2419
+
2420
+ ### Enabling Debug Level in Baileys Logs
2421
+ First, enable the logging of unhandled messages from WhatsApp by setting:
2422
+ ```ts
2423
+ const sock = makeWASocket({
2424
+ logger: P({ level: 'debug' }),
2425
+ })
2426
+ ```
2427
+ This will enable you to see all sorts of messages WhatsApp sends in the console.
2428
+
2429
+ ### How Whatsapp Communicate With Us
2430
+
2431
+ > [!TIP]
2432
+ > If you want to learn whatsapp protocol, we recommend to study about Libsignal Protocol and Noise Protocol
2433
+
2434
+ - **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:
2435
+ ```
2436
+ {
2437
+ "level": 10,
2438
+ "fromMe": false,
2439
+ "frame": {
2440
+ "tag": "ib",
2441
+ "attrs": {
2442
+ "from": "@s.whatsapp.net"
2443
+ },
2444
+ "content": [
2445
+ {
2446
+ "tag": "edge_routing",
2447
+ "attrs": {},
2448
+ "content": [
2449
+ {
2450
+ "tag": "routing_info",
2451
+ "attrs": {},
2452
+ "content": {
2453
+ "type": "Buffer",
2454
+ "data": [8,2,8,5]
2455
+ }
2456
+ }
2457
+ ]
2458
+ }
2459
+ ]
2460
+ },
2461
+ "msg":"communication"
2462
+ }
2463
+ ```
2464
+
2465
+ The `'frame'` is what the message received is, it has three components:
2466
+ - `tag` -- what this frame is about (eg. message will have 'message')
2467
+ - `attrs` -- a string key-value pair with some metadata (contains ID of the message usually)
2468
+ - `content` -- the actual data (eg. a message node will have the actual message content in it)
2469
+ - read more about this format [here](/src/WABinary/readme.md)
2470
+
2471
+ ### Register a Callback for Websocket Events
2472
+
2473
+ > [!TIP]
2474
+ > Recommended to see `onMessageReceived` function in `socket.ts` file to understand how websockets events are fired
2475
+
2476
+ ```ts
2477
+ // for any message with tag 'edge_routing'
2478
+ sock.ws.on('CB:edge_routing', (node: BinaryNode) => { })
2479
+
2480
+ // for any message with tag 'edge_routing' and id attribute = abcd
2481
+ sock.ws.on('CB:edge_routing,id:abcd', (node: BinaryNode) => { })
2482
+
2483
+ // for any message with tag 'edge_routing', id attribute = abcd & first content node routing_info
2484
+ sock.ws.on('CB:edge_routing,id:abcd,routing_info', (node: BinaryNode) => { })
2485
+ ```