gifted-baileys 1.5.0 → 1.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +1429 -684
- package/package.json +11 -26
- package/src/Defaults/baileys-version.json +3 -0
- package/{lib → src}/Defaults/index.js +16 -8
- package/src/Defaults/index.ts +131 -0
- package/src/Defaults/phonenumber-mcc.json +223 -0
- package/src/Signal/libsignal.js +180 -0
- package/src/Signal/libsignal.ts +141 -0
- package/src/Socket/Client/abstract-socket-client.ts +19 -0
- package/src/Socket/Client/index.ts +3 -0
- package/src/Socket/Client/mobile-socket-client.js +78 -0
- package/src/Socket/Client/mobile-socket-client.ts +66 -0
- package/src/Socket/Client/web-socket-client.js +75 -0
- package/src/Socket/Client/web-socket-client.ts +57 -0
- package/{lib → src}/Socket/business.js +33 -27
- package/src/Socket/business.ts +281 -0
- package/{lib → src}/Socket/chats.js +197 -178
- package/src/Socket/chats.ts +1030 -0
- package/{lib → src}/Socket/groups.js +69 -79
- package/src/Socket/groups.ts +356 -0
- package/{lib → src}/Socket/index.js +1 -4
- package/src/Socket/index.ts +13 -0
- package/{lib → src}/Socket/messages-recv.js +160 -108
- package/src/Socket/messages-recv.ts +985 -0
- package/{lib → src}/Socket/messages-send.js +183 -100
- package/src/Socket/messages-send.ts +871 -0
- package/src/Socket/newsletter.js +227 -0
- package/src/Socket/newsletter.ts +282 -0
- package/{lib → src}/Socket/registration.js +55 -63
- package/src/Socket/registration.ts +250 -0
- package/{lib → src}/Socket/socket.js +107 -66
- package/src/Socket/socket.ts +777 -0
- package/src/Store/index.ts +3 -0
- package/{lib → src}/Store/make-cache-manager-store.js +34 -25
- package/src/Store/make-cache-manager-store.ts +100 -0
- package/{lib → src}/Store/make-in-memory-store.js +51 -61
- package/src/Store/make-in-memory-store.ts +475 -0
- package/src/Store/make-ordered-dictionary.ts +86 -0
- package/{lib → src}/Store/object-repository.js +1 -1
- package/src/Store/object-repository.ts +32 -0
- package/src/Tests/test.app-state-sync.js +204 -0
- package/src/Tests/test.app-state-sync.ts +207 -0
- package/src/Tests/test.event-buffer.js +270 -0
- package/src/Tests/test.event-buffer.ts +319 -0
- package/src/Tests/test.key-store.js +76 -0
- package/src/Tests/test.key-store.ts +92 -0
- package/src/Tests/test.libsignal.js +141 -0
- package/src/Tests/test.libsignal.ts +186 -0
- package/src/Tests/test.media-download.js +93 -0
- package/src/Tests/test.media-download.ts +76 -0
- package/src/Tests/test.messages.js +33 -0
- package/src/Tests/test.messages.ts +37 -0
- package/src/Tests/utils.js +34 -0
- package/src/Tests/utils.ts +36 -0
- package/src/Types/Auth.ts +113 -0
- package/src/Types/Call.ts +15 -0
- package/src/Types/Chat.ts +106 -0
- package/{lib/Types/Contact.d.ts → src/Types/Contact.ts} +9 -8
- package/src/Types/Events.ts +93 -0
- package/src/Types/GroupMetadata.ts +53 -0
- package/{lib → src}/Types/Label.js +1 -1
- package/src/Types/Label.ts +36 -0
- package/{lib → src}/Types/LabelAssociation.js +1 -1
- package/{lib/Types/LabelAssociation.d.ts → src/Types/LabelAssociation.ts} +22 -16
- package/src/Types/Message.ts +288 -0
- package/src/Types/Newsletter.js +32 -0
- package/src/Types/Newsletter.ts +98 -0
- package/src/Types/Product.ts +85 -0
- package/src/Types/Signal.ts +68 -0
- package/{lib/Types/Socket.d.ts → src/Types/Socket.ts} +68 -56
- package/src/Types/State.ts +29 -0
- package/{lib → src}/Types/index.js +2 -1
- package/src/Types/index.ts +59 -0
- package/{lib → src}/Utils/auth-utils.js +95 -76
- package/src/Utils/auth-utils.ts +222 -0
- package/src/Utils/baileys-event-stream.js +92 -0
- package/src/Utils/baileys-event-stream.ts +66 -0
- package/{lib → src}/Utils/business.js +45 -17
- package/src/Utils/business.ts +275 -0
- package/{lib → src}/Utils/chat-utils.js +74 -46
- package/src/Utils/chat-utils.ts +860 -0
- package/{lib → src}/Utils/crypto.js +31 -21
- package/src/Utils/crypto.ts +131 -0
- package/src/Utils/decode-wa-message.js +211 -0
- package/src/Utils/decode-wa-message.ts +228 -0
- package/{lib → src}/Utils/event-buffer.js +13 -4
- package/src/Utils/event-buffer.ts +613 -0
- package/{lib → src}/Utils/generics.js +98 -45
- package/src/Utils/generics.ts +434 -0
- package/{lib → src}/Utils/history.js +39 -10
- package/src/Utils/history.ts +112 -0
- package/src/Utils/index.ts +17 -0
- package/{lib → src}/Utils/link-preview.js +54 -17
- package/src/Utils/link-preview.ts +122 -0
- package/src/Utils/logger.ts +3 -0
- package/src/Utils/lt-hash.ts +61 -0
- package/{lib → src}/Utils/make-mutex.js +13 -4
- package/src/Utils/make-mutex.ts +44 -0
- package/{lib → src}/Utils/messages-media.js +296 -192
- package/src/Utils/messages-media.ts +847 -0
- package/{lib → src}/Utils/messages.js +124 -113
- package/src/Utils/messages.ts +956 -0
- package/{lib → src}/Utils/noise-handler.js +16 -3
- package/src/Utils/noise-handler.ts +197 -0
- package/{lib → src}/Utils/process-message.js +33 -29
- package/src/Utils/process-message.ts +414 -0
- package/{lib → src}/Utils/signal.js +23 -14
- package/src/Utils/signal.ts +177 -0
- package/{lib → src}/Utils/use-multi-file-auth-state.js +28 -19
- package/src/Utils/use-multi-file-auth-state.ts +90 -0
- package/{lib → src}/Utils/validate-connection.js +25 -42
- package/src/Utils/validate-connection.ts +238 -0
- package/src/WABinary/constants.ts +42 -0
- package/{lib → src}/WABinary/decode.js +17 -7
- package/src/WABinary/decode.ts +265 -0
- package/{lib → src}/WABinary/encode.js +17 -7
- package/src/WABinary/encode.ts +236 -0
- package/{lib → src}/WABinary/generic-utils.js +2 -2
- package/src/WABinary/generic-utils.ts +121 -0
- package/src/WABinary/index.ts +5 -0
- package/{lib → src}/WABinary/jid-utils.js +4 -1
- package/src/WABinary/jid-utils.ts +68 -0
- package/src/WABinary/types.ts +17 -0
- package/src/WAM/BinaryInfo.js +13 -0
- package/src/WAM/BinaryInfo.ts +12 -0
- package/src/WAM/constants.js +15350 -0
- package/src/WAM/constants.ts +15382 -0
- package/src/WAM/encode.js +155 -0
- package/src/WAM/encode.ts +174 -0
- package/src/WAM/index.js +19 -0
- package/src/WAM/index.ts +3 -0
- package/src/gifted +1 -0
- package/{lib → src}/index.js +1 -0
- package/src/index.ts +13 -0
- package/lib/Defaults/baileys-version.json +0 -3
- package/lib/Defaults/index.d.ts +0 -284
- package/lib/Defaults/phonenumber-mcc.json +0 -223
- package/lib/Signal/libsignal.d.ts +0 -3
- package/lib/Signal/libsignal.js +0 -152
- package/lib/Socket/Client/abstract-socket-client.d.ts +0 -17
- package/lib/Socket/Client/index.d.ts +0 -3
- package/lib/Socket/Client/mobile-socket-client.d.ts +0 -13
- package/lib/Socket/Client/mobile-socket-client.js +0 -65
- package/lib/Socket/Client/web-socket-client.d.ts +0 -12
- package/lib/Socket/Client/web-socket-client.js +0 -62
- package/lib/Socket/business.d.ts +0 -135
- package/lib/Socket/chats.d.ts +0 -79
- package/lib/Socket/groups.d.ts +0 -113
- package/lib/Socket/index.d.ts +0 -137
- package/lib/Socket/messages-recv.d.ts +0 -124
- package/lib/Socket/messages-send.d.ts +0 -119
- package/lib/Socket/registration.d.ts +0 -232
- package/lib/Socket/socket.d.ts +0 -42
- package/lib/Store/index.d.ts +0 -3
- package/lib/Store/make-cache-manager-store.d.ts +0 -13
- package/lib/Store/make-in-memory-store.d.ts +0 -117
- package/lib/Store/make-ordered-dictionary.d.ts +0 -13
- package/lib/Store/object-repository.d.ts +0 -10
- package/lib/Types/Auth.d.ts +0 -108
- package/lib/Types/Call.d.ts +0 -13
- package/lib/Types/Chat.d.ts +0 -102
- package/lib/Types/Events.d.ts +0 -157
- package/lib/Types/GroupMetadata.d.ts +0 -52
- package/lib/Types/Label.d.ts +0 -35
- package/lib/Types/Message.d.ts +0 -261
- package/lib/Types/Product.d.ts +0 -78
- package/lib/Types/Signal.d.ts +0 -57
- package/lib/Types/State.d.ts +0 -27
- package/lib/Types/index.d.ts +0 -56
- package/lib/Utils/auth-utils.d.ts +0 -18
- package/lib/Utils/baileys-event-stream.d.ts +0 -16
- package/lib/Utils/baileys-event-stream.js +0 -63
- package/lib/Utils/business.d.ts +0 -22
- package/lib/Utils/chat-utils.d.ts +0 -71
- package/lib/Utils/crypto.d.ts +0 -41
- package/lib/Utils/decode-wa-message.d.ts +0 -19
- package/lib/Utils/decode-wa-message.js +0 -174
- package/lib/Utils/event-buffer.d.ts +0 -35
- package/lib/Utils/generics.d.ts +0 -94
- package/lib/Utils/history.d.ts +0 -15
- package/lib/Utils/index.d.ts +0 -17
- package/lib/Utils/link-preview.d.ts +0 -21
- package/lib/Utils/logger.d.ts +0 -4
- package/lib/Utils/lt-hash.d.ts +0 -12
- package/lib/Utils/make-mutex.d.ts +0 -7
- package/lib/Utils/messages-media.d.ts +0 -107
- package/lib/Utils/messages.d.ts +0 -76
- package/lib/Utils/noise-handler.d.ts +0 -20
- package/lib/Utils/process-message.d.ts +0 -41
- package/lib/Utils/signal.d.ts +0 -32
- package/lib/Utils/use-multi-file-auth-state.d.ts +0 -12
- package/lib/Utils/validate-connection.d.ts +0 -11
- package/lib/WABinary/constants.d.ts +0 -27
- package/lib/WABinary/decode.d.ts +0 -7
- package/lib/WABinary/encode.d.ts +0 -3
- package/lib/WABinary/generic-utils.d.ts +0 -15
- package/lib/WABinary/index.d.ts +0 -5
- package/lib/WABinary/jid-utils.d.ts +0 -29
- package/lib/WABinary/types.d.ts +0 -18
- package/lib/index.d.ts +0 -10
- /package/{lib → src}/Socket/Client/abstract-socket-client.js +0 -0
- /package/{lib → src}/Socket/Client/index.js +0 -0
- /package/{lib → src}/Store/index.js +0 -0
- /package/{lib → src}/Store/make-ordered-dictionary.js +0 -0
- /package/{lib → src}/Types/Auth.js +0 -0
- /package/{lib → src}/Types/Call.js +0 -0
- /package/{lib → src}/Types/Chat.js +0 -0
- /package/{lib → src}/Types/Contact.js +0 -0
- /package/{lib → src}/Types/Events.js +0 -0
- /package/{lib → src}/Types/GroupMetadata.js +0 -0
- /package/{lib → src}/Types/Message.js +0 -0
- /package/{lib → src}/Types/Product.js +0 -0
- /package/{lib → src}/Types/Signal.js +0 -0
- /package/{lib → src}/Types/Socket.js +0 -0
- /package/{lib → src}/Types/State.js +0 -0
- /package/{lib → src}/Utils/index.js +0 -0
- /package/{lib → src}/Utils/logger.js +0 -0
- /package/{lib → src}/Utils/lt-hash.js +0 -0
- /package/{lib → src}/WABinary/constants.js +0 -0
- /package/{lib → src}/WABinary/index.js +0 -0
- /package/{lib → src}/WABinary/types.js +0 -0
package/README.md
CHANGED
|
@@ -1,314 +1,382 @@
|
|
|
1
|
-
# Baileys - Typescript/Javascript WhatsApp Web API
|
|
2
|
-
|
|
3
|
-
### Important Note
|
|
4
|
-
|
|
5
|
-
This library was originally a project for **CS-2362 at Ashoka University** and is in no way affiliated with or endorsed by WhatsApp. Use at your own discretion. Do not spam people with this. We discourage any stalkerware, bulk or automated messaging usage.
|
|
6
|
-
|
|
7
|
-
#### Liability and License Notice
|
|
8
|
-
Baileys and its maintainers cannot be held liable for misuse of this application, as stated in the [MIT license](https://github.com/WhiskeySockets/Baileys/blob/master/LICENSE).
|
|
9
|
-
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.
|
|
10
|
-
##
|
|
11
|
-
|
|
12
|
-
Baileys does not require Selenium or any other browser to be interface with WhatsApp Web, it does so directly using a **WebSocket**.
|
|
13
|
-
Not running Selenium or Chromimum saves you like **half a gig** of ram :/
|
|
14
|
-
Baileys supports interacting with the multi-device & web versions of WhatsApp.
|
|
15
|
-
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.
|
|
16
|
-
|
|
17
|
-
## Please Read
|
|
18
|
-
|
|
19
|
-
The original repository had to be removed by the original author - we now continue development in this repository here.
|
|
20
|
-
This is the only official repository and is maintained by the community.
|
|
21
|
-
**Join the Discord [here](https://discord.gg/WeJM5FP9GG)**
|
|
22
|
-
|
|
23
|
-
## Example
|
|
24
|
-
|
|
25
|
-
Do check out & run [example.ts](Example/example.ts) to see an example usage of the library.
|
|
26
|
-
The script covers most common use cases.
|
|
27
|
-
To run the example script, download or clone the repo and then type the following in a terminal:
|
|
28
|
-
1. ``` cd path/to/Baileys ```
|
|
29
|
-
2. ``` yarn ```
|
|
30
|
-
3. ``` yarn example ```
|
|
1
|
+
# **[Gifted-Baileys](https://www.npmjs.com/package/@giftedtech/baileys)** - Typescript/Javascript WhatsApp Web API
|
|
31
2
|
|
|
32
3
|
## Install
|
|
33
4
|
|
|
34
|
-
Use the stable version:
|
|
35
5
|
```
|
|
36
|
-
|
|
6
|
+
npm i @giftedtech/baileys
|
|
37
7
|
```
|
|
38
8
|
|
|
39
|
-
|
|
9
|
+
Using yarn
|
|
40
10
|
```
|
|
41
|
-
yarn add
|
|
11
|
+
yarn add @giftedtech/baileys@latest
|
|
42
12
|
```
|
|
43
13
|
|
|
44
14
|
Then import your code using:
|
|
15
|
+
- Commonjs:
|
|
16
|
+
```javascript
|
|
17
|
+
const { default: makeWASocket } = require("@giftedtech/baileys")
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
- Module:
|
|
45
21
|
``` ts
|
|
46
|
-
import makeWASocket from '@
|
|
22
|
+
import makeWASocket from '@giftedtech/baileys'
|
|
47
23
|
```
|
|
48
24
|
|
|
49
|
-
## Unit Tests
|
|
50
25
|
|
|
51
|
-
|
|
26
|
+
# Index
|
|
27
|
+
|
|
28
|
+
- [Connecting Account](#connecting-account)
|
|
29
|
+
- [Connect with QR-CODE](#starting-socket-with-qr-code)
|
|
30
|
+
- [Connect with Pairing Code](#starting-socket-with-pairing-code)
|
|
31
|
+
- [Receive Full History](#receive-full-history)
|
|
32
|
+
- [Important Notes About Socket Config](#important-notes-about-socket-config)
|
|
33
|
+
- [Caching Group Metadata (Recommended)](#caching-group-metadata-recommended)
|
|
34
|
+
- [Improve Retry System & Decrypt Poll Votes](#improve-retry-system--decrypt-poll-votes)
|
|
35
|
+
- [Receive Notifications in Whatsapp App](#receive-notifications-in-whatsapp-app)
|
|
36
|
+
|
|
37
|
+
- [Save Auth Info](#saving--restoring-sessions)
|
|
38
|
+
- [Handling Events](#handling-events)
|
|
39
|
+
- [Example to Start](#example-to-start)
|
|
40
|
+
- [Decrypt Poll Votes](#decrypt-poll-votes)
|
|
41
|
+
- [Summary of Events on First Connection](#summary-of-events-on-first-connection)
|
|
42
|
+
- [Implementing a Data Store](#implementing-a-data-store)
|
|
43
|
+
- [Whatsapp IDs Explain](#whatsapp-ids-explain)
|
|
44
|
+
- [Utility Functions](#utility-functions)
|
|
45
|
+
- [Sending Messages](#sending-messages)
|
|
46
|
+
- [Non-Media Messages](#non-media-messages)
|
|
47
|
+
- [Buttons Message](#buttons-message)
|
|
48
|
+
- [Buttons Flow](#buttons-flow)
|
|
49
|
+
- [Interactive Message](#interactive-message)
|
|
50
|
+
- [Text Message](#text-message)
|
|
51
|
+
- [Quote Message](#quote-message-works-with-all-types)
|
|
52
|
+
- [Mention User](#mention-user-works-with-most-types)
|
|
53
|
+
- [Mention Status](#mention-status)
|
|
54
|
+
- [Result Poll From Newsletter](#result-poll-from-newsletter)
|
|
55
|
+
- [SendAlbumMessage](#send-album-message)
|
|
56
|
+
- [Interactive Response](#interactive-response)
|
|
57
|
+
- [Request Payment](#request-payment)
|
|
58
|
+
- [Event Message](#event-message)
|
|
59
|
+
- [Interactive](#interactive)
|
|
60
|
+
- [Forward Messages](#forward-messages)
|
|
61
|
+
- [Location Message](#location-message)
|
|
62
|
+
- [Contact Message](#contact-message)
|
|
63
|
+
- [Reaction Message](#reaction-message)
|
|
64
|
+
- [Pin Message](#pin-message)
|
|
65
|
+
- [Poll Message](#poll-message)
|
|
66
|
+
- [Sending with Link Preview](#sending-messages-with-link-previews)
|
|
67
|
+
- [Media Messages](#media-messages)
|
|
68
|
+
- [Gif Message](#gif-message)
|
|
69
|
+
- [Video Message](#video-message)
|
|
70
|
+
- [Audio Message](#audio-message)
|
|
71
|
+
- [Image Message](#image-message)
|
|
72
|
+
- [ViewOnce Message](#view-once-message)
|
|
73
|
+
- [Modify Messages](#modify-messages)
|
|
74
|
+
- [Delete Messages (for everyone)](#deleting-messages-for-everyone)
|
|
75
|
+
- [Edit Messages](#editing-messages)
|
|
76
|
+
- [Manipulating Media Messages](#manipulating-media-messages)
|
|
77
|
+
- [Thumbnail in Media Messages](#thumbnail-in-media-messages)
|
|
78
|
+
- [Downloading Media Messages](#downloading-media-messages)
|
|
79
|
+
- [Re-upload Media Message to Whatsapp](#re-upload-media-message-to-whatsapp)
|
|
80
|
+
- [Reject Call](#reject-call)
|
|
81
|
+
- [Send States in Chat](#send-states-in-chat)
|
|
82
|
+
- [Reading Messages](#reading-messages)
|
|
83
|
+
- [Update Presence](#update-presence)
|
|
84
|
+
- [Modifying Chats](#modifying-chats)
|
|
85
|
+
- [Archive a Chat](#archive-a-chat)
|
|
86
|
+
- [Mute/Unmute a Chat](#muteunmute-a-chat)
|
|
87
|
+
- [Mark a Chat Read/Unread](#mark-a-chat-readunread)
|
|
88
|
+
- [Delete a Message for Me](#delete-a-message-for-me)
|
|
89
|
+
- [Delete a Chat](#delete-a-chat)
|
|
90
|
+
- [Star/Unstar a Message](#starunstar-a-message)
|
|
91
|
+
- [Disappearing Messages](#disappearing-messages)
|
|
92
|
+
- [User Querys](#user-querys)
|
|
93
|
+
- [Check If ID Exists in Whatsapp](#check-if-id-exists-in-whatsapp)
|
|
94
|
+
- [Query Chat History (groups too)](#query-chat-history-groups-too)
|
|
95
|
+
- [Fetch Status](#fetch-status)
|
|
96
|
+
- [Fetch Profile Picture (groups too)](#fetch-profile-picture-groups-too)
|
|
97
|
+
- [Fetch Bussines Profile (such as description or category)](#fetch-bussines-profile-such-as-description-or-category)
|
|
98
|
+
- [Fetch Someone's Presence (if they're typing or online)](#fetch-someones-presence-if-theyre-typing-or-online)
|
|
99
|
+
- [Change Profile](#change-profile)
|
|
100
|
+
- [Change Profile Status](#change-profile-status)
|
|
101
|
+
- [Change Profile Name](#change-profile-name)
|
|
102
|
+
- [Change Display Picture (groups too)](#change-display-picture-groups-too)
|
|
103
|
+
- [Remove display picture (groups too)](#remove-display-picture-groups-too)
|
|
104
|
+
- [Groups](#groups)
|
|
105
|
+
- [Create a Group](#create-a-group)
|
|
106
|
+
- [Add/Remove or Demote/Promote](#addremove-or-demotepromote)
|
|
107
|
+
- [Change Subject (name)](#change-subject-name)
|
|
108
|
+
- [Change Description](#change-description)
|
|
109
|
+
- [Change Settings](#change-settings)
|
|
110
|
+
- [Leave a Group](#leave-a-group)
|
|
111
|
+
- [Get Invite Code](#get-invite-code)
|
|
112
|
+
- [Revoke Invite Code](#revoke-invite-code)
|
|
113
|
+
- [Join Using Invitation Code](#join-using-invitation-code)
|
|
114
|
+
- [Get Group Info by Invite Code](#get-group-info-by-invite-code)
|
|
115
|
+
- [Query Metadata (participants, name, description...)](#query-metadata-participants-name-description)
|
|
116
|
+
- [Join using groupInviteMessage](#join-using-groupinvitemessage)
|
|
117
|
+
- [Get Request Join List](#get-request-join-list)
|
|
118
|
+
- [Approve/Reject Request Join](#approvereject-request-join)
|
|
119
|
+
- [Get All Participating Groups Metadata](#get-all-participating-groups-metadata)
|
|
120
|
+
- [Toggle Ephemeral](#toggle-ephemeral)
|
|
121
|
+
- [Change Add Mode](#change-add-mode)
|
|
122
|
+
- [Privacy](#privacy)
|
|
123
|
+
- [Block/Unblock User](#blockunblock-user)
|
|
124
|
+
- [Get Privacy Settings](#get-privacy-settings)
|
|
125
|
+
- [Get BlockList](#get-blocklist)
|
|
126
|
+
- [Update LastSeen Privacy](#update-lastseen-privacy)
|
|
127
|
+
- [Update Online Privacy](#update-online-privacy)
|
|
128
|
+
- [Update Profile Picture Privacy](#update-profile-picture-privacy)
|
|
129
|
+
- [Update Status Privacy](#update-status-privacy)
|
|
130
|
+
- [Update Read Receipts Privacy](#update-read-receipts-privacy)
|
|
131
|
+
- [Update Groups Add Privacy](#update-groups-add-privacy)
|
|
132
|
+
- [Update Default Disappearing Mode](#update-default-disappearing-mode)
|
|
133
|
+
- [Broadcast Lists & Stories](#broadcast-lists--stories)
|
|
134
|
+
- [Send Broadcast & Stories](#send-broadcast--stories)
|
|
135
|
+
- [Query a Broadcast List's Recipients & Name](#query-a-broadcast-lists-recipients--name)
|
|
136
|
+
- [Writing Custom Functionality](#writing-custom-functionality)
|
|
137
|
+
- [Enabling Debug Level in Baileys Logs](#enabling-debug-level-in-baileys-logs)
|
|
138
|
+
- [How Whatsapp Communicate With Us](#how-whatsapp-communicate-with-us)
|
|
139
|
+
- [Register a Callback for Websocket Events](#register-a-callback-for-websocket-events)
|
|
140
|
+
|
|
141
|
+
## Connecting Account
|
|
142
|
+
|
|
143
|
+
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.
|
|
144
|
+
|
|
145
|
+
### Starting socket with **QR-CODE**
|
|
146
|
+
|
|
147
|
+
> [!TIP]
|
|
148
|
+
> You can customize browser name if you connect with **QR-CODE**, with `Browser` constant, we have some browsers config, **see [here](https://baileys.whiskeysockets.io/types/BrowsersMap.html)**
|
|
149
|
+
|
|
150
|
+
```javascript
|
|
151
|
+
const { default: makeWASocket } = require("@giftedtech/baileys")
|
|
52
152
|
|
|
53
|
-
## Connecting multi device (recommended)
|
|
54
153
|
|
|
55
|
-
|
|
154
|
+
const sock = makeWASocket({
|
|
155
|
+
// can provide additional config here
|
|
156
|
+
browser: Browsers.ubuntu('Gifted'),
|
|
157
|
+
printQRInTerminal: true
|
|
158
|
+
})
|
|
159
|
+
```
|
|
56
160
|
|
|
57
|
-
|
|
58
|
-
import makeWASocket, { DisconnectReason } from '@whiskeysockets/baileys'
|
|
59
|
-
import { Boom } from '@hapi/boom'
|
|
161
|
+
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!
|
|
60
162
|
|
|
61
|
-
|
|
62
|
-
const sock = makeWASocket({
|
|
63
|
-
// can provide additional config here
|
|
64
|
-
printQRInTerminal: true
|
|
65
|
-
})
|
|
66
|
-
sock.ev.on('connection.update', (update) => {
|
|
67
|
-
const { connection, lastDisconnect } = update
|
|
68
|
-
if(connection === 'close') {
|
|
69
|
-
const shouldReconnect = (lastDisconnect.error as Boom)?.output?.statusCode !== DisconnectReason.loggedOut
|
|
70
|
-
console.log('connection closed due to ', lastDisconnect.error, ', reconnecting ', shouldReconnect)
|
|
71
|
-
// reconnect if not logged out
|
|
72
|
-
if(shouldReconnect) {
|
|
73
|
-
connectToWhatsApp()
|
|
74
|
-
}
|
|
75
|
-
} else if(connection === 'open') {
|
|
76
|
-
console.log('opened connection')
|
|
77
|
-
}
|
|
78
|
-
})
|
|
79
|
-
sock.ev.on('messages.upsert', m => {
|
|
80
|
-
console.log(JSON.stringify(m, undefined, 2))
|
|
163
|
+
### Starting socket with **Pairing Code**
|
|
81
164
|
|
|
82
|
-
console.log('replying to', m.messages[0].key.remoteJid)
|
|
83
|
-
await sock.sendMessage(m.messages[0].key.remoteJid!, { text: 'Hello there!' })
|
|
84
|
-
})
|
|
85
|
-
}
|
|
86
|
-
// run in main file
|
|
87
|
-
connectToWhatsApp()
|
|
88
|
-
```
|
|
89
165
|
|
|
90
|
-
|
|
166
|
+
> [!IMPORTANT]
|
|
167
|
+
> 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)
|
|
91
168
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
/** ping-pong interval for WS connection */
|
|
116
|
-
keepAliveIntervalMs: number
|
|
117
|
-
/** proxy agent */
|
|
118
|
-
agent?: Agent
|
|
119
|
-
/** pino logger */
|
|
120
|
-
logger: Logger
|
|
121
|
-
/** version to connect with */
|
|
122
|
-
version: WAVersion
|
|
123
|
-
/** override browser config */
|
|
124
|
-
browser: WABrowserDescription
|
|
125
|
-
/** agent used for fetch requests -- uploading/downloading media */
|
|
126
|
-
fetchAgent?: Agent
|
|
127
|
-
/** should the QR be printed in the terminal */
|
|
128
|
-
printQRInTerminal: boolean
|
|
129
|
-
/** should events be emitted for actions done by this socket connection */
|
|
130
|
-
emitOwnEvents: boolean
|
|
131
|
-
/** provide a cache to store media, so does not have to be re-uploaded */
|
|
132
|
-
mediaCache?: NodeCache
|
|
133
|
-
/** custom upload hosts to upload media to */
|
|
134
|
-
customUploadHosts: MediaConnInfo['hosts']
|
|
135
|
-
/** time to wait between sending new retry requests */
|
|
136
|
-
retryRequestDelayMs: number
|
|
137
|
-
/** max msg retry count */
|
|
138
|
-
maxMsgRetryCount: number
|
|
139
|
-
/** time to wait for the generation of the next QR in ms */
|
|
140
|
-
qrTimeout?: number;
|
|
141
|
-
/** provide an auth state object to maintain the auth state */
|
|
142
|
-
auth: AuthenticationState
|
|
143
|
-
/** manage history processing with this control; by default will sync up everything */
|
|
144
|
-
shouldSyncHistoryMessage: (msg: proto.Message.IHistorySyncNotification) => boolean
|
|
145
|
-
/** transaction capability options for SignalKeyStore */
|
|
146
|
-
transactionOpts: TransactionCapabilityOptions
|
|
147
|
-
/** provide a cache to store a user's device list */
|
|
148
|
-
userDevicesCache?: NodeCache
|
|
149
|
-
/** marks the client as online whenever the socket successfully connects */
|
|
150
|
-
markOnlineOnConnect: boolean
|
|
151
|
-
/**
|
|
152
|
-
* map to store the retry counts for failed messages;
|
|
153
|
-
* used to determine whether to retry a message or not */
|
|
154
|
-
msgRetryCounterMap?: MessageRetryMap
|
|
155
|
-
/** width for link preview images */
|
|
156
|
-
linkPreviewImageThumbnailWidth: number
|
|
157
|
-
/** Should Baileys ask the phone for full history, will be received async */
|
|
158
|
-
syncFullHistory: boolean
|
|
159
|
-
/** Should baileys fire init queries automatically, default true */
|
|
160
|
-
fireInitQueries: boolean
|
|
161
|
-
/**
|
|
162
|
-
* generate a high quality link preview,
|
|
163
|
-
* entails uploading the jpegThumbnail to WA
|
|
164
|
-
* */
|
|
165
|
-
generateHighQualityLinkPreview: boolean
|
|
166
|
-
|
|
167
|
-
/** options for axios */
|
|
168
|
-
options: AxiosRequestConfig<any>
|
|
169
|
-
/**
|
|
170
|
-
* fetch a message from your store
|
|
171
|
-
* implement this so that messages failed to send (solves the "this message can take a while" issue) can be retried
|
|
172
|
-
* */
|
|
173
|
-
getMessage: (key: proto.IMessageKey) => Promise<proto.IMessage | undefined>
|
|
169
|
+
The phone number can't have `+` or `()` or `-`, only numbers, you must provide country code
|
|
170
|
+
|
|
171
|
+
```javascript
|
|
172
|
+
const { default: makeWASocket } = require("@giftedtech/baileys")
|
|
173
|
+
|
|
174
|
+
const sock = makeWASocket({
|
|
175
|
+
// can provide additional config here
|
|
176
|
+
printQRInTerminal: false //need to be false
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
- Normal Pairing
|
|
180
|
+
if (!sock.authState.creds.registered) {
|
|
181
|
+
const number = 'XXXXXXXXXXX'
|
|
182
|
+
const code = await sock.requestPairingCode(number)
|
|
183
|
+
console.log(code)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
- Costum Pairing
|
|
187
|
+
if (!sock.authState.creds.registered) {
|
|
188
|
+
const pair = "12345678" // only 8 digit numbers or letters (no more or less)
|
|
189
|
+
const number = 'XXXXXXXXXXX'
|
|
190
|
+
const code = await sock.requestPairingCode(number, pair)
|
|
191
|
+
console.log(code)
|
|
174
192
|
}
|
|
175
193
|
```
|
|
176
194
|
|
|
177
|
-
###
|
|
195
|
+
### Receive Full History
|
|
196
|
+
|
|
197
|
+
1. Set `syncFullHistory` as `true`
|
|
198
|
+
2. Baileys, by default, use chrome browser config
|
|
199
|
+
- If you'd like to emulate a desktop connection (and receive more message history), this browser setting to your Socket config:
|
|
200
|
+
|
|
201
|
+
```javascript
|
|
202
|
+
const sock = makeWASocket({
|
|
203
|
+
...otherOpts,
|
|
204
|
+
// can use Windows, Ubuntu here too
|
|
205
|
+
browser: Browsers.macOS('Desktop'),
|
|
206
|
+
syncFullHistory: true
|
|
207
|
+
})
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Important Notes About Socket Config
|
|
211
|
+
|
|
212
|
+
### Caching Group Metadata (Recommended)
|
|
213
|
+
- If you use baileys for groups, we recommend you to set `cachedGroupMetadata` in socket config, you need to implement a cache like this:
|
|
214
|
+
|
|
215
|
+
```javascript
|
|
216
|
+
const groupCache = new NodeCache({stdTTL: 5 * 60, useClones: false})
|
|
178
217
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
218
|
+
const sock = makeWASocket({
|
|
219
|
+
cachedGroupMetadata: async (jid) => groupCache.get(jid)
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
sock.ev.on('groups.update', async ([event]) => {
|
|
223
|
+
const metadata = await sock.groupMetadata(event.id)
|
|
224
|
+
groupCache.set(event.id, metadata)
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
sock.ev.on('group-participants.update', async (event) => {
|
|
228
|
+
const metadata = await sock.groupMetadata(event.id)
|
|
229
|
+
groupCache.set(event.id, metadata)
|
|
187
230
|
})
|
|
188
231
|
```
|
|
189
232
|
|
|
233
|
+
### Improve Retry System & Decrypt Poll Votes
|
|
234
|
+
- 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:
|
|
235
|
+
```javascript
|
|
236
|
+
const sock = makeWASocket({
|
|
237
|
+
getMessage: async (key) => await getMessageFromStore(key)
|
|
238
|
+
})
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### Receive Notifications in Whatsapp App
|
|
242
|
+
- If you want to receive notifications in whatsapp app, set `markOnlineOnConnect` to `false`
|
|
243
|
+
```javascript
|
|
244
|
+
const sock = makeWASocket({
|
|
245
|
+
markOnlineOnConnect: false
|
|
246
|
+
})
|
|
247
|
+
```
|
|
190
248
|
## Saving & Restoring Sessions
|
|
191
249
|
|
|
192
|
-
You obviously don't want to keep scanning the QR code every time you want to connect.
|
|
250
|
+
You obviously don't want to keep scanning the QR code every time you want to connect.
|
|
193
251
|
|
|
194
252
|
So, you can load the credentials to log back in:
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
|
|
253
|
+
```javascript
|
|
254
|
+
const makeWASocket = require("@giftedtech/baileys").default;
|
|
255
|
+
const { useMultiFileAuthState } = require("@giftedtech/baileys");
|
|
198
256
|
|
|
199
|
-
// utility function to help save the auth state in a single folder
|
|
200
|
-
// 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
|
|
201
257
|
const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys')
|
|
258
|
+
|
|
202
259
|
// will use the given state to connect
|
|
203
260
|
// so if valid credentials are available -- it'll connect without QR
|
|
204
|
-
const
|
|
261
|
+
const sock = makeWASocket({ auth: state })
|
|
262
|
+
|
|
205
263
|
// this will be called as soon as the credentials are updated
|
|
206
|
-
|
|
207
|
-
```
|
|
208
|
-
|
|
209
|
-
**Note:** 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.
|
|
210
|
-
|
|
211
|
-
## Listening to Connection Updates
|
|
212
|
-
|
|
213
|
-
Baileys now fires the `connection.update` event to let you know something has updated in the connection. This data has the following structure:
|
|
214
|
-
``` ts
|
|
215
|
-
type ConnectionState = {
|
|
216
|
-
/** connection is now open, connecting or closed */
|
|
217
|
-
connection: WAConnectionState
|
|
218
|
-
/** the error that caused the connection to close */
|
|
219
|
-
lastDisconnect?: {
|
|
220
|
-
error: Error
|
|
221
|
-
date: Date
|
|
222
|
-
}
|
|
223
|
-
/** is this a new login */
|
|
224
|
-
isNewLogin?: boolean
|
|
225
|
-
/** the current QR code */
|
|
226
|
-
qr?: string
|
|
227
|
-
/** has the device received all pending notifications while it was offline */
|
|
228
|
-
receivedPendingNotifications?: boolean
|
|
229
|
-
}
|
|
264
|
+
sock.ev.on('creds.update', saveCreds)
|
|
230
265
|
```
|
|
231
266
|
|
|
232
|
-
|
|
267
|
+
> [!IMPORTANT]
|
|
268
|
+
> `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.
|
|
269
|
+
|
|
270
|
+
> [!NOTE]
|
|
271
|
+
> 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.
|
|
233
272
|
|
|
234
273
|
## Handling Events
|
|
235
274
|
|
|
236
|
-
Baileys uses the EventEmitter syntax for events.
|
|
275
|
+
- Baileys uses the EventEmitter syntax for events.
|
|
237
276
|
They're all nicely typed up, so you shouldn't have any issues with an Intellisense editor like VS Code.
|
|
238
277
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
``` ts
|
|
242
|
-
|
|
243
|
-
export type BaileysEventMap = {
|
|
244
|
-
/** connection state has been updated -- WS closed, opened, connecting etc. */
|
|
245
|
-
'connection.update': Partial<ConnectionState>
|
|
246
|
-
/** credentials updated -- some metadata, keys or something */
|
|
247
|
-
'creds.update': Partial<AuthenticationCreds>
|
|
248
|
-
/** history sync, everything is reverse chronologically sorted */
|
|
249
|
-
'messaging-history.set': {
|
|
250
|
-
chats: Chat[]
|
|
251
|
-
contacts: Contact[]
|
|
252
|
-
messages: WAMessage[]
|
|
253
|
-
isLatest: boolean
|
|
254
|
-
}
|
|
255
|
-
/** upsert chats */
|
|
256
|
-
'chats.upsert': Chat[]
|
|
257
|
-
/** update the given chats */
|
|
258
|
-
'chats.update': Partial<Chat>[]
|
|
259
|
-
/** delete chats with given ID */
|
|
260
|
-
'chats.delete': string[]
|
|
261
|
-
'labels.association': LabelAssociation
|
|
262
|
-
'labels.edit': Label
|
|
263
|
-
/** presence of contact in a chat updated */
|
|
264
|
-
'presence.update': { id: string, presences: { [participant: string]: PresenceData } }
|
|
265
|
-
|
|
266
|
-
'contacts.upsert': Contact[]
|
|
267
|
-
'contacts.update': Partial<Contact>[]
|
|
268
|
-
|
|
269
|
-
'messages.delete': { keys: WAMessageKey[] } | { jid: string, all: true }
|
|
270
|
-
'messages.update': WAMessageUpdate[]
|
|
271
|
-
'messages.media-update': { key: WAMessageKey, media?: { ciphertext: Uint8Array, iv: Uint8Array }, error?: Boom }[]
|
|
272
|
-
/**
|
|
273
|
-
* add/update the given messages. If they were received while the connection was online,
|
|
274
|
-
* the update will have type: "notify"
|
|
275
|
-
* */
|
|
276
|
-
'messages.upsert': { messages: WAMessage[], type: MessageUpsertType }
|
|
277
|
-
/** message was reacted to. If reaction was removed -- then "reaction.text" will be falsey */
|
|
278
|
-
'messages.reaction': { key: WAMessageKey, reaction: proto.IReaction }[]
|
|
279
|
-
|
|
280
|
-
'message-receipt.update': MessageUserReceiptUpdate[]
|
|
281
|
-
|
|
282
|
-
'groups.upsert': GroupMetadata[]
|
|
283
|
-
'groups.update': Partial<GroupMetadata>[]
|
|
284
|
-
/** apply an action to participants in a group */
|
|
285
|
-
'group-participants.update': { id: string, participants: string[], action: ParticipantAction }
|
|
286
|
-
|
|
287
|
-
'blocklist.set': { blocklist: string[] }
|
|
288
|
-
'blocklist.update': { blocklist: string[], type: 'add' | 'remove' }
|
|
289
|
-
/** Receive an update on a call, including when the call was received, rejected, accepted */
|
|
290
|
-
'call': WACallEvent[]
|
|
291
|
-
}
|
|
292
|
-
```
|
|
278
|
+
> [!IMPORTANT]
|
|
279
|
+
> **The events are [these](https://baileys.whiskeysockets.io/types/BaileysEventMap.html)**, it's important you see all events
|
|
293
280
|
|
|
294
281
|
You can listen to these events like this:
|
|
295
|
-
```
|
|
296
|
-
|
|
282
|
+
```javascript
|
|
297
283
|
const sock = makeWASocket()
|
|
298
284
|
sock.ev.on('messages.upsert', ({ messages }) => {
|
|
299
285
|
console.log('got messages', messages)
|
|
300
286
|
})
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
### Example to Start
|
|
290
|
+
|
|
291
|
+
> [!NOTE]
|
|
292
|
+
> This example includes basic auth storage too
|
|
293
|
+
|
|
294
|
+
```javascript
|
|
295
|
+
const makeWASocket = require("@giftedtech/baileys").default;
|
|
296
|
+
const { DisconnectReason, useMultiFileAuthState } = require("@giftedtech/baileys");
|
|
297
|
+
const Boom = require('@hapi/boom');
|
|
298
|
+
|
|
299
|
+
async function connectToWhatsApp () {
|
|
300
|
+
const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys')
|
|
301
|
+
const sock = makeWASocket({
|
|
302
|
+
// can provide additional config here
|
|
303
|
+
auth: state,
|
|
304
|
+
printQRInTerminal: true
|
|
305
|
+
})
|
|
306
|
+
sock.ev.on('connection.update', (update) => {
|
|
307
|
+
const { connection, lastDisconnect } = update
|
|
308
|
+
if(connection === 'close') {
|
|
309
|
+
const shouldReconnect = (lastDisconnect.error as Boom)?.output?.statusCode !== DisconnectReason.loggedOut
|
|
310
|
+
console.log('connection closed due to ', lastDisconnect.error, ', reconnecting ', shouldReconnect)
|
|
311
|
+
// reconnect if not logged out
|
|
312
|
+
if(shouldReconnect) {
|
|
313
|
+
connectToWhatsApp()
|
|
314
|
+
}
|
|
315
|
+
} else if(connection === 'open') {
|
|
316
|
+
console.log('opened connection')
|
|
317
|
+
}
|
|
318
|
+
})
|
|
319
|
+
sock.ev.on('messages.upsert', event => {
|
|
320
|
+
for (const m of event.messages) {
|
|
321
|
+
console.log(JSON.stringify(m, undefined, 2))
|
|
322
|
+
|
|
323
|
+
console.log('replying to', m.key.remoteJid)
|
|
324
|
+
await sock.sendMessage(m.key.remoteJid!, { text: 'Hello Word' })
|
|
325
|
+
}
|
|
326
|
+
})
|
|
327
|
+
|
|
328
|
+
// to storage creds (session info) when it updates
|
|
329
|
+
sock.ev.on('creds.update', saveCreds)
|
|
330
|
+
}
|
|
331
|
+
// run in main file
|
|
332
|
+
connectToWhatsApp()
|
|
333
|
+
```
|
|
301
334
|
|
|
335
|
+
> [!IMPORTANT]
|
|
336
|
+
> In `messages.upsert` it's recommended to use a loop like `for (const message of event.messages)` to handle all messages in array
|
|
337
|
+
|
|
338
|
+
### Decrypt Poll Votes
|
|
339
|
+
|
|
340
|
+
- By default poll votes are encrypted and handled in `messages.update`
|
|
341
|
+
- That's a simple example
|
|
342
|
+
```javascript
|
|
343
|
+
sock.ev.on('messages.update', event => {
|
|
344
|
+
for(const { key, update } of event) {
|
|
345
|
+
if(update.pollUpdates) {
|
|
346
|
+
const pollCreation = await getMessage(key)
|
|
347
|
+
if(pollCreation) {
|
|
348
|
+
console.log(
|
|
349
|
+
'got poll update, aggregation: ',
|
|
350
|
+
getAggregateVotesInPollMessage({
|
|
351
|
+
message: pollCreation,
|
|
352
|
+
pollUpdates: update.pollUpdates,
|
|
353
|
+
})
|
|
354
|
+
)
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
})
|
|
302
359
|
```
|
|
303
360
|
|
|
361
|
+
- `getMessage` is a [store](#implementing-a-data-store) implementation (in your end)
|
|
362
|
+
|
|
363
|
+
### Summary of Events on First Connection
|
|
364
|
+
|
|
365
|
+
1. When you connect first time, `connection.update` will be fired requesting you to restart sock
|
|
366
|
+
2. Then, history messages will be received in `messaging.history-set`
|
|
367
|
+
|
|
304
368
|
## Implementing a Data Store
|
|
305
369
|
|
|
306
|
-
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.
|
|
370
|
+
- 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.
|
|
371
|
+
|
|
372
|
+
> [!IMPORTANT]
|
|
373
|
+
> I highly recommend building your own data store, as storing someone's entire chat history in memory is a terrible waste of RAM.
|
|
307
374
|
|
|
308
375
|
It can be used as follows:
|
|
309
376
|
|
|
310
|
-
```
|
|
311
|
-
|
|
377
|
+
```javascript
|
|
378
|
+
const makeWASocket = require("@giftedtech/baileys").default;
|
|
379
|
+
const { makeInMemoryStore } = require("@giftedtech/baileys");
|
|
312
380
|
// the store maintains the data of the WA connection in memory
|
|
313
381
|
// can be written out to a file & read from it
|
|
314
382
|
const store = makeInMemoryStore({ })
|
|
@@ -324,13 +392,13 @@ const sock = makeWASocket({ })
|
|
|
324
392
|
// the store can listen from a new socket once the current socket outlives its lifetime
|
|
325
393
|
store.bind(sock.ev)
|
|
326
394
|
|
|
327
|
-
sock.ev.on('chats.
|
|
328
|
-
// can use
|
|
329
|
-
//
|
|
395
|
+
sock.ev.on('chats.upsert', () => {
|
|
396
|
+
// can use 'store.chats' however you want, even after the socket dies out
|
|
397
|
+
// 'chats' => a KeyedDB instance
|
|
330
398
|
console.log('got chats', store.chats.all())
|
|
331
399
|
})
|
|
332
400
|
|
|
333
|
-
sock.ev.on('contacts.
|
|
401
|
+
sock.ev.on('contacts.upsert', () => {
|
|
334
402
|
console.log('got contacts', Object.values(store.contacts))
|
|
335
403
|
})
|
|
336
404
|
|
|
@@ -338,197 +406,700 @@ sock.ev.on('contacts.set', () => {
|
|
|
338
406
|
|
|
339
407
|
The store also provides some simple functions such as `loadMessages` that utilize the store to speed up data retrieval.
|
|
340
408
|
|
|
341
|
-
|
|
409
|
+
## Whatsapp IDs Explain
|
|
410
|
+
|
|
411
|
+
- `id` is the WhatsApp ID, called `jid` too, of the person or group you're sending the message to.
|
|
412
|
+
- It must be in the format ```[country code][phone number]@s.whatsapp.net```
|
|
413
|
+
- Example for people: ```+19999999999@s.whatsapp.net```.
|
|
414
|
+
- For groups, it must be in the format ``` 123456789-123345@g.us ```.
|
|
415
|
+
- For broadcast lists, it's `[timestamp of creation]@broadcast`.
|
|
416
|
+
- For stories, the ID is `status@broadcast`.
|
|
417
|
+
|
|
418
|
+
## Utility Functions
|
|
419
|
+
|
|
420
|
+
- `getContentType`, returns the content type for any message
|
|
421
|
+
- `getDevice`, returns the device from message
|
|
422
|
+
- `makeCacheableSignalKeyStore`, make auth store more fast
|
|
423
|
+
- `downloadContentFromMessage`, download content from any message
|
|
342
424
|
|
|
343
425
|
## Sending Messages
|
|
344
426
|
|
|
345
|
-
|
|
427
|
+
- Send all types of messages with a single function
|
|
428
|
+
- **[Here](https://baileys.whiskeysockets.io/types/AnyMessageContent.html) you can see all message contents supported, like text message**
|
|
429
|
+
- **[Here](https://baileys.whiskeysockets.io/types/MiscMessageGenerationOptions.html) you can see all options supported, like quote message**
|
|
430
|
+
|
|
431
|
+
```javascript
|
|
432
|
+
const jid: string
|
|
433
|
+
const content: AnyMessageContent
|
|
434
|
+
const options: MiscMessageGenerationOptions
|
|
435
|
+
|
|
436
|
+
sock.sendMessage(jid, content, options)
|
|
437
|
+
```
|
|
346
438
|
|
|
347
439
|
### Non-Media Messages
|
|
348
440
|
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
441
|
+
#### Buttons Message
|
|
442
|
+
```javascript
|
|
443
|
+
// send a buttons message!
|
|
444
|
+
sock.sendMessage(jid, {
|
|
445
|
+
text: "Hello World !",
|
|
446
|
+
footer: "Gifted Tech - 2025",
|
|
447
|
+
buttons: [
|
|
448
|
+
{
|
|
449
|
+
buttonId: `🚀`,
|
|
450
|
+
buttonText: {
|
|
451
|
+
displayText: '🗿'
|
|
452
|
+
},
|
|
453
|
+
type: 1
|
|
454
|
+
}
|
|
455
|
+
],
|
|
456
|
+
headerType: 1,
|
|
457
|
+
viewOnce: true
|
|
458
|
+
},{ quoted: null })
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
#### Buttons Flow
|
|
462
|
+
```javascript
|
|
463
|
+
sock.sendMessage(jid, {
|
|
464
|
+
text: "Hello Wolrd !;",
|
|
465
|
+
footer: "© Gifted Tech",
|
|
466
|
+
buttons: [
|
|
467
|
+
{
|
|
468
|
+
buttonId: '.tes',
|
|
469
|
+
buttonText: {
|
|
470
|
+
displayText: 'TESTING BOT'
|
|
471
|
+
},
|
|
472
|
+
type: 1,
|
|
473
|
+
},
|
|
474
|
+
{
|
|
475
|
+
buttonId: ' ',
|
|
476
|
+
buttonText: {
|
|
477
|
+
displayText: 'PRIVATE SCRIPT'
|
|
478
|
+
},
|
|
479
|
+
type: 1,
|
|
480
|
+
},
|
|
481
|
+
{
|
|
482
|
+
buttonId: 'action',
|
|
483
|
+
buttonText: {
|
|
484
|
+
displayText: 'ini pesan interactiveMeta'
|
|
485
|
+
},
|
|
486
|
+
type: 4,
|
|
487
|
+
nativeFlowInfo: {
|
|
488
|
+
name: 'single_select',
|
|
489
|
+
paramsJson: JSON.stringify({
|
|
490
|
+
title: 'message',
|
|
491
|
+
sections: [
|
|
492
|
+
{
|
|
493
|
+
title: 'Gifted Tech - 2025',
|
|
494
|
+
highlight_label: '😜',
|
|
495
|
+
rows: [
|
|
496
|
+
{
|
|
497
|
+
header: 'HEADER',
|
|
498
|
+
title: 'TITLE',
|
|
499
|
+
description: 'DESCRIPTION',
|
|
500
|
+
id: 'YOUR ID',
|
|
501
|
+
},
|
|
502
|
+
{
|
|
503
|
+
header: 'HEADER',
|
|
504
|
+
title: 'TITLE',
|
|
505
|
+
description: 'DESCRIPTION',
|
|
506
|
+
id: 'YOUR ID',
|
|
507
|
+
},
|
|
508
|
+
],
|
|
509
|
+
},
|
|
510
|
+
],
|
|
511
|
+
}),
|
|
512
|
+
},
|
|
513
|
+
},
|
|
514
|
+
],
|
|
515
|
+
headerType: 1,
|
|
516
|
+
viewOnce: true
|
|
517
|
+
}, { quoted: m });
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
#### Interactive Message
|
|
521
|
+
```javascript
|
|
522
|
+
let msg = generateWAMessageFromContent(m.chat, {
|
|
523
|
+
viewOnceMessage: {
|
|
524
|
+
message: {
|
|
525
|
+
"messageContextInfo": {
|
|
526
|
+
"deviceListMetadata": {},
|
|
527
|
+
"deviceListMetadataVersion": 2
|
|
528
|
+
},
|
|
529
|
+
interactiveMessage: proto.Message.InteractiveMessage.create({
|
|
530
|
+
body: proto.Message.InteractiveMessage.Body.create({
|
|
531
|
+
text: "Gifted Tech"
|
|
532
|
+
}),
|
|
533
|
+
footer: proto.Message.InteractiveMessage.Footer.create({
|
|
534
|
+
text: "Bot"
|
|
535
|
+
}),
|
|
536
|
+
header: proto.Message.InteractiveMessage.Header.create({
|
|
537
|
+
title: "Igna",
|
|
538
|
+
subtitle: "test",
|
|
539
|
+
hasMediaAttachment: false
|
|
540
|
+
}),
|
|
541
|
+
nativeFlowMessage: proto.Message.InteractiveMessage.NativeFlowMessage.create({
|
|
542
|
+
buttons: [
|
|
543
|
+
{
|
|
544
|
+
"name": "single_select",
|
|
545
|
+
"buttonParamsJson": "{\"title\":\"title\",\"sections\":[{\".menu\":\".play dj webito\",\"highlight_label\":\"label\",\"rows\":[{\"header\":\"header\",\"title\":\"title\",\"description\":\"description\",\"id\":\"id\"},{\"header\":\"header\",\"title\":\"title\",\"description\":\"description\",\"id\":\"id\"}]}]}"
|
|
546
|
+
},
|
|
547
|
+
{
|
|
548
|
+
"name": "cta_reply",
|
|
549
|
+
"buttonParamsJson": "{\"display_text\":\"quick_reply\",\"id\":\"message\"}"
|
|
550
|
+
},
|
|
551
|
+
{
|
|
552
|
+
"name": "cta_url",
|
|
553
|
+
"buttonParamsJson": "{\"display_text\":\"url\",\"url\":\"https://www.google.com\",\"merchant_url\":\"https://www.google.com\"}"
|
|
554
|
+
},
|
|
555
|
+
{
|
|
556
|
+
"name": "cta_call",
|
|
557
|
+
"buttonParamsJson": "{\"display_text\":\"call\",\"id\":\"message\"}"
|
|
558
|
+
},
|
|
559
|
+
{
|
|
560
|
+
"name": "cta_copy",
|
|
561
|
+
"buttonParamsJson": "{\"display_text\":\"copy\",\"id\":\"123456789\",\"copy_code\":\"message\"}"
|
|
562
|
+
},
|
|
563
|
+
{
|
|
564
|
+
"name": "cta_reminder",
|
|
565
|
+
"buttonParamsJson": "{\"display_text\":\"Recordatorio\",\"id\":\"message\"}"
|
|
566
|
+
},
|
|
567
|
+
{
|
|
568
|
+
"name": "cta_cancel_reminder",
|
|
569
|
+
"buttonParamsJson": "{\"display_text\":\"cta_cancel_reminder\",\"id\":\"message\"}"
|
|
570
|
+
},
|
|
571
|
+
{
|
|
572
|
+
"name": "address_message",
|
|
573
|
+
"buttonParamsJson": "{\"display_text\":\"address_message\",\"id\":\"message\"}"
|
|
574
|
+
},
|
|
575
|
+
{
|
|
576
|
+
"name": "send_location",
|
|
577
|
+
"buttonParamsJson": ""
|
|
578
|
+
}
|
|
579
|
+
],
|
|
580
|
+
})
|
|
581
|
+
})
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}, {})
|
|
585
|
+
|
|
586
|
+
return sock.relayMessage(msg.key.remoteJid, msg.message, { messageId: msg.key.id })
|
|
587
|
+
```
|
|
588
|
+
|
|
589
|
+
#### Text Message
|
|
590
|
+
```javascript
|
|
591
|
+
await sock.sendMessage(jid, { text: 'hello word' })
|
|
592
|
+
```
|
|
593
|
+
|
|
594
|
+
#### Quote Message (works with all types)
|
|
595
|
+
```javascript
|
|
596
|
+
await sock.sendMessage(jid, { text: 'hello word' }, { quoted: message })
|
|
597
|
+
```
|
|
598
|
+
|
|
599
|
+
#### Mention User (works with most types)
|
|
600
|
+
- @number is to mention in text, it's optional
|
|
601
|
+
```javascript
|
|
602
|
+
await sock.sendMessage(
|
|
603
|
+
jid,
|
|
604
|
+
{
|
|
605
|
+
text: '@12345678901',
|
|
606
|
+
mentions: ['12345678901@s.whatsapp.net']
|
|
607
|
+
}
|
|
608
|
+
)
|
|
609
|
+
```
|
|
610
|
+
|
|
611
|
+
#### Mention Status
|
|
612
|
+
- [ jid ] If the Jid Group and Jid Private Chat are included in the JID list, try to make the JID group first starting from the Jid Private Chat or Jid Private Chat in the middle between the group Jid
|
|
613
|
+
```javascript
|
|
614
|
+
await sock.StatusMentions(
|
|
615
|
+
{
|
|
616
|
+
text: "Hello", // or image / video / audio ( url or buffer )
|
|
617
|
+
},
|
|
618
|
+
[
|
|
619
|
+
"123456789123456789@g.us",
|
|
620
|
+
"123456789@s.whatsapp.net",
|
|
621
|
+
// Enter jid chat here
|
|
622
|
+
]
|
|
623
|
+
)
|
|
624
|
+
```
|
|
625
|
+
|
|
626
|
+
#### Result Poll From Newsletter
|
|
627
|
+
```javascript
|
|
628
|
+
await client.sendMessage(
|
|
629
|
+
jid,
|
|
630
|
+
{
|
|
631
|
+
pollResult: {
|
|
632
|
+
name: "Text poll",
|
|
633
|
+
votes: [["Options 1", 10], ["Options 2", 10]], // 10 For Fake Polling Count Results
|
|
634
|
+
}
|
|
635
|
+
}, { quoted : message }
|
|
636
|
+
)
|
|
637
|
+
```
|
|
638
|
+
|
|
639
|
+
#### Send Album Message
|
|
640
|
+
- url or buffer ( image or video )
|
|
641
|
+
```javascript
|
|
642
|
+
await sock.sendAlbumMessage(
|
|
643
|
+
jid,
|
|
644
|
+
[
|
|
645
|
+
{
|
|
646
|
+
image: { url: "https://example.jpg" }, // or buffer
|
|
647
|
+
caption: "Hello World",
|
|
648
|
+
},
|
|
649
|
+
{
|
|
650
|
+
video: { url: "https://example.mp4" }, // or buffer
|
|
651
|
+
caption: "Hello World",
|
|
652
|
+
},
|
|
653
|
+
],
|
|
654
|
+
{
|
|
655
|
+
quoted : message,
|
|
656
|
+
delay : 2000 // number in seconds
|
|
657
|
+
}
|
|
658
|
+
)
|
|
659
|
+
|
|
660
|
+
```
|
|
661
|
+
|
|
662
|
+
#### Interactive Response
|
|
663
|
+
```javascript
|
|
664
|
+
await client.sendMessage(
|
|
665
|
+
jid,
|
|
666
|
+
{
|
|
667
|
+
buttonReply: {
|
|
668
|
+
text: 'Text',
|
|
669
|
+
nativeFlow: {
|
|
670
|
+
version: 3,
|
|
671
|
+
},
|
|
672
|
+
},
|
|
673
|
+
type: 'interactive',
|
|
674
|
+
ephemeral: true,
|
|
675
|
+
}
|
|
676
|
+
)
|
|
677
|
+
|
|
678
|
+
```
|
|
679
|
+
|
|
680
|
+
#### Request Payment
|
|
681
|
+
```javascript
|
|
682
|
+
- Example non media sticker
|
|
683
|
+
await client.sendMessage(
|
|
684
|
+
jid,
|
|
685
|
+
{
|
|
686
|
+
requestPayment: {
|
|
687
|
+
currency: "IDR",
|
|
688
|
+
amount: "10000000",
|
|
689
|
+
from: "123456@s.whatsapp.net",
|
|
690
|
+
note: "Hai Guys",
|
|
691
|
+
background: { ...background of the message }
|
|
692
|
+
}
|
|
693
|
+
},
|
|
694
|
+
{ quoted : message }
|
|
695
|
+
)
|
|
696
|
+
|
|
697
|
+
- with media sticker buffer
|
|
698
|
+
await client.sendMessage(
|
|
699
|
+
jid,
|
|
700
|
+
{
|
|
701
|
+
requestPayment: {
|
|
702
|
+
currency: "IDR",
|
|
703
|
+
amount: "10000000",
|
|
704
|
+
from: "123456@s.whatsapp.net",
|
|
705
|
+
sticker: Buffer,
|
|
706
|
+
background: { ...background of the message }
|
|
707
|
+
}
|
|
708
|
+
},
|
|
709
|
+
{ quoted : message }
|
|
710
|
+
)
|
|
711
|
+
|
|
712
|
+
- with media sticker url
|
|
713
|
+
await client.sendMessage(
|
|
714
|
+
jid,
|
|
715
|
+
{
|
|
716
|
+
requestPayment: {
|
|
717
|
+
currency: "IDR",
|
|
718
|
+
amount: "10000000",
|
|
719
|
+
from: "123456@s.whatsapp.net",
|
|
720
|
+
sticker: { url: Sticker Url },
|
|
721
|
+
background: { ...background of the message }
|
|
722
|
+
}
|
|
723
|
+
},
|
|
724
|
+
{ quoted : message }
|
|
725
|
+
)
|
|
726
|
+
```
|
|
727
|
+
|
|
728
|
+
#### Event Message
|
|
729
|
+
```javascript
|
|
730
|
+
await client.sendMessage(
|
|
731
|
+
jid,
|
|
732
|
+
{
|
|
733
|
+
event: {
|
|
734
|
+
isCanceled: false, // or true for cancel event
|
|
735
|
+
name: "Name Event",
|
|
736
|
+
description: "Description Event",
|
|
737
|
+
location: {
|
|
738
|
+
degressLatitude: -0,
|
|
739
|
+
degressLongitude: - 0
|
|
740
|
+
},
|
|
741
|
+
link: Call Link,
|
|
742
|
+
startTime: m.messageTimestamp.low,
|
|
743
|
+
endTime: m.messageTimestamp.low + 86400, // 86400 is day in seconds
|
|
744
|
+
extraGuestsAllowed: true // or false
|
|
745
|
+
}
|
|
746
|
+
},
|
|
747
|
+
{ quoted : message }
|
|
748
|
+
)
|
|
749
|
+
```
|
|
750
|
+
|
|
751
|
+
#### Interactive
|
|
752
|
+
```javascript
|
|
753
|
+
- Example non header media
|
|
754
|
+
await client.sendMessage(
|
|
755
|
+
jid,
|
|
756
|
+
{
|
|
757
|
+
text: "Description Of Messages", //Additional information
|
|
758
|
+
title: "Title Of Messages",
|
|
759
|
+
subtitle: "Subtitle Message",
|
|
760
|
+
footer: "Footer Messages",
|
|
761
|
+
interactiveButtons: [
|
|
762
|
+
{
|
|
763
|
+
name: "quick_reply",
|
|
764
|
+
buttonParamsJson: JSON.stringify({
|
|
765
|
+
display_text: "Display Button",
|
|
766
|
+
id: "ID"
|
|
767
|
+
})
|
|
768
|
+
},
|
|
769
|
+
{
|
|
770
|
+
name: "cta_url",
|
|
771
|
+
buttonParamsJson: JSON.stringify({
|
|
772
|
+
display_text: "Display Button",
|
|
773
|
+
url: "https://www.example.com"
|
|
774
|
+
})
|
|
775
|
+
}
|
|
776
|
+
]
|
|
777
|
+
},
|
|
778
|
+
{
|
|
779
|
+
quoted : message
|
|
780
|
+
}
|
|
781
|
+
)
|
|
782
|
+
|
|
783
|
+
- Example with media
|
|
784
|
+
await client.sendMessage(
|
|
785
|
+
jid,
|
|
786
|
+
{
|
|
787
|
+
image: { url : "https://example.jpg" }, // Can buffer
|
|
788
|
+
caption: "Description Of Messages", //Additional information
|
|
789
|
+
title: "Title Of Messages",
|
|
790
|
+
subtitle: "Subtile Message",
|
|
791
|
+
footer: "Footer Messages",
|
|
792
|
+
media: true,
|
|
793
|
+
interactiveButtons: [
|
|
794
|
+
{
|
|
795
|
+
name: "quick_reply",
|
|
796
|
+
buttonParamsJson: JSON.stringify({
|
|
797
|
+
display_text: "Display Button",
|
|
798
|
+
id: "ID"
|
|
799
|
+
})
|
|
800
|
+
},
|
|
801
|
+
{
|
|
802
|
+
name: "cta_url",
|
|
803
|
+
buttonParamsJson: JSON.stringify({
|
|
804
|
+
display_text: "Display Button",
|
|
805
|
+
url: "https://www.example.com"
|
|
806
|
+
})
|
|
807
|
+
}
|
|
808
|
+
]
|
|
809
|
+
},
|
|
810
|
+
{
|
|
811
|
+
quoted : message
|
|
812
|
+
}
|
|
813
|
+
)
|
|
814
|
+
|
|
815
|
+
- Example with header product
|
|
816
|
+
await client.sendMessage(
|
|
817
|
+
jid,
|
|
818
|
+
{
|
|
819
|
+
product: {
|
|
820
|
+
productImage: { url: "https://example.jpg }, //or buffer
|
|
821
|
+
productImageCount: 1,
|
|
822
|
+
title: "Title Product",
|
|
823
|
+
description: "Description Product",
|
|
824
|
+
priceAmount1000: 20000 * 1000,
|
|
825
|
+
currencyCode: "IDR",
|
|
826
|
+
retailerId: "Retail",
|
|
827
|
+
url: "https://example.com",
|
|
828
|
+
},
|
|
829
|
+
businessOwnerJid: "1234@s.whatsapp.net",
|
|
830
|
+
caption: "Description Of Messages", //Additional information
|
|
831
|
+
title: "Title Of Messages",
|
|
832
|
+
footer: "Footer Messages",
|
|
833
|
+
media: true,
|
|
834
|
+
interactiveButtons: [
|
|
835
|
+
{
|
|
836
|
+
name: "quick_reply",
|
|
837
|
+
buttonParamsJson: JSON.stringify({
|
|
838
|
+
display_text: "Display Button",
|
|
839
|
+
id: "ID"
|
|
840
|
+
})
|
|
841
|
+
},
|
|
842
|
+
{
|
|
843
|
+
name: "cta_url",
|
|
844
|
+
buttonParamsJson: JSON.stringify({
|
|
845
|
+
display_text: "Display Button",
|
|
846
|
+
url: "https://www.example.com"
|
|
847
|
+
})
|
|
848
|
+
}
|
|
849
|
+
]
|
|
850
|
+
},
|
|
851
|
+
{
|
|
852
|
+
quoted : message
|
|
853
|
+
}
|
|
854
|
+
)
|
|
855
|
+
```
|
|
856
|
+
|
|
857
|
+
#### Forward Messages
|
|
858
|
+
- 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
|
|
859
|
+
```javascript
|
|
860
|
+
const msg = getMessageFromStore() // implement this on your end
|
|
861
|
+
await sock.sendMessage(jid, { forward: msg }) // WA forward the message!
|
|
862
|
+
```
|
|
863
|
+
|
|
864
|
+
#### Location Message
|
|
865
|
+
```javascript
|
|
866
|
+
await sock.sendMessage(
|
|
867
|
+
jid,
|
|
868
|
+
{
|
|
869
|
+
location: {
|
|
870
|
+
degreesLatitude: 24.121231,
|
|
871
|
+
degreesLongitude: 55.1121221
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
)
|
|
875
|
+
```
|
|
876
|
+
#### Contact Message
|
|
877
|
+
```javascript
|
|
365
878
|
const vcard = 'BEGIN:VCARD\n' // metadata of the contact card
|
|
366
|
-
+ 'VERSION:3.0\n'
|
|
879
|
+
+ 'VERSION:3.0\n'
|
|
367
880
|
+ 'FN:Jeff Singh\n' // full name
|
|
368
881
|
+ 'ORG:Ashoka Uni;\n' // the organization of the contact
|
|
369
882
|
+ 'TEL;type=CELL;type=VOICE;waid=911234567890:+91 12345 67890\n' // WhatsApp ID + phone number
|
|
370
883
|
+ 'END:VCARD'
|
|
371
|
-
|
|
884
|
+
|
|
885
|
+
await sock.sendMessage(
|
|
372
886
|
id,
|
|
373
|
-
{
|
|
374
|
-
contacts: {
|
|
375
|
-
displayName: 'Jeff',
|
|
376
|
-
contacts: [{ vcard }]
|
|
887
|
+
{
|
|
888
|
+
contacts: {
|
|
889
|
+
displayName: 'Jeff',
|
|
890
|
+
contacts: [{ vcard }]
|
|
377
891
|
}
|
|
378
892
|
}
|
|
379
893
|
)
|
|
894
|
+
```
|
|
380
895
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
896
|
+
#### Reaction Message
|
|
897
|
+
- 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
|
|
898
|
+
```javascript
|
|
899
|
+
await sock.sendMessage(
|
|
900
|
+
jid,
|
|
901
|
+
{
|
|
902
|
+
react: {
|
|
903
|
+
text: '💖', // use an empty string to remove the reaction
|
|
904
|
+
key: message.key
|
|
905
|
+
}
|
|
385
906
|
}
|
|
386
|
-
|
|
907
|
+
)
|
|
908
|
+
```
|
|
909
|
+
|
|
910
|
+
#### Pin Message
|
|
911
|
+
- 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
|
|
912
|
+
|
|
913
|
+
- Time can be:
|
|
914
|
+
|
|
915
|
+
| Time | Seconds |
|
|
916
|
+
|-------|----------------|
|
|
917
|
+
| 24h | 86.400 |
|
|
918
|
+
| 7d | 604.800 |
|
|
919
|
+
| 30d | 2.592.000 |
|
|
920
|
+
|
|
921
|
+
```javascript
|
|
922
|
+
await sock.sendMessage(
|
|
923
|
+
jid,
|
|
924
|
+
{
|
|
925
|
+
pin: {
|
|
926
|
+
type: 1, // 0 to remove
|
|
927
|
+
time: 86400
|
|
928
|
+
key: message.key
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
)
|
|
932
|
+
```
|
|
387
933
|
|
|
388
|
-
|
|
934
|
+
#### Poll Message
|
|
935
|
+
```javascript
|
|
936
|
+
await sock.sendMessage(
|
|
937
|
+
jid,
|
|
938
|
+
{
|
|
939
|
+
poll: {
|
|
940
|
+
name: 'My Poll',
|
|
941
|
+
values: ['Option 1', 'Option 2', ...],
|
|
942
|
+
selectableCount: 1,
|
|
943
|
+
toAnnouncementGroup: false // or true
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
)
|
|
389
947
|
```
|
|
390
948
|
|
|
391
|
-
### Sending
|
|
949
|
+
### Sending Messages with Link Previews
|
|
392
950
|
|
|
393
|
-
1. By default,
|
|
951
|
+
1. By default, wa does not have link generation when sent from the web
|
|
394
952
|
2. Baileys has a function to generate the content for these link previews
|
|
395
953
|
3. To enable this function's usage, add `link-preview-js` as a dependency to your project with `yarn add link-preview-js`
|
|
396
954
|
4. Send a link:
|
|
397
|
-
```
|
|
398
|
-
|
|
399
|
-
|
|
955
|
+
```javascript
|
|
956
|
+
await sock.sendMessage(
|
|
957
|
+
jid,
|
|
958
|
+
{
|
|
959
|
+
text: 'Hi, this was sent using https://npmjs.com/package/@giftedtech/baileys'
|
|
960
|
+
}
|
|
961
|
+
)
|
|
400
962
|
```
|
|
401
963
|
|
|
402
964
|
### Media Messages
|
|
403
965
|
|
|
404
|
-
Sending media (video, stickers, images) is easier & more efficient than ever.
|
|
405
|
-
|
|
966
|
+
Sending media (video, stickers, images) is easier & more efficient than ever.
|
|
967
|
+
|
|
968
|
+
> [!NOTE]
|
|
969
|
+
> 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)
|
|
970
|
+
|
|
406
971
|
- When specifying a media url, Baileys never loads the entire buffer into memory; it even encrypts the media as a readable stream.
|
|
407
972
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
973
|
+
> [!TIP]
|
|
974
|
+
> It's recommended to use Stream or Url to save memory
|
|
975
|
+
|
|
976
|
+
#### Gif Message
|
|
977
|
+
- Whatsapp doesn't support `.gif` files, that's why we send gifs as common `.mp4` video with `gifPlayback` flag
|
|
978
|
+
```javascript
|
|
411
979
|
await sock.sendMessage(
|
|
412
|
-
|
|
413
|
-
{
|
|
414
|
-
video: fs.readFileSync(
|
|
415
|
-
caption:
|
|
980
|
+
jid,
|
|
981
|
+
{
|
|
982
|
+
video: fs.readFileSync('Media/ma_gif.mp4'),
|
|
983
|
+
caption: 'hello word',
|
|
416
984
|
gifPlayback: true
|
|
417
985
|
}
|
|
418
986
|
)
|
|
987
|
+
```
|
|
419
988
|
|
|
989
|
+
#### Video Message
|
|
990
|
+
```javascript
|
|
420
991
|
await sock.sendMessage(
|
|
421
|
-
id,
|
|
422
|
-
{
|
|
423
|
-
video:
|
|
424
|
-
|
|
425
|
-
|
|
992
|
+
id,
|
|
993
|
+
{
|
|
994
|
+
video: {
|
|
995
|
+
url: './Media/ma_gif.mp4'
|
|
996
|
+
},
|
|
997
|
+
caption: 'hello word',
|
|
998
|
+
ptv: false // if set to true, will send as a `video note`
|
|
426
999
|
}
|
|
427
1000
|
)
|
|
1001
|
+
```
|
|
428
1002
|
|
|
429
|
-
|
|
1003
|
+
#### Audio Message
|
|
1004
|
+
- To audio message work in all devices you need to convert with some tool like `ffmpeg` with this flags:
|
|
1005
|
+
```bash
|
|
1006
|
+
codec: libopus //ogg file
|
|
1007
|
+
ac: 1 //one channel
|
|
1008
|
+
avoid_negative_ts
|
|
1009
|
+
make_zero
|
|
1010
|
+
```
|
|
1011
|
+
- Example:
|
|
1012
|
+
```bash
|
|
1013
|
+
ffmpeg -i input.mp4 -avoid_negative_ts make_zero -ac 1 output.ogg
|
|
1014
|
+
```
|
|
1015
|
+
```javascript
|
|
430
1016
|
await sock.sendMessage(
|
|
431
|
-
|
|
432
|
-
{
|
|
433
|
-
|
|
1017
|
+
jid,
|
|
1018
|
+
{
|
|
1019
|
+
audio: {
|
|
1020
|
+
url: './Media/audio.mp3'
|
|
1021
|
+
},
|
|
1022
|
+
mimetype: 'audio/mp4'
|
|
1023
|
+
}
|
|
434
1024
|
)
|
|
435
1025
|
```
|
|
436
1026
|
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
- **MiscGenerationOptions**: some extra info about the message. It can have the following __optional__ values:
|
|
447
|
-
``` ts
|
|
448
|
-
const info: MessageOptions = {
|
|
449
|
-
quoted: quotedMessage, // the message you want to quote
|
|
450
|
-
contextInfo: { forwardingScore: 2, isForwarded: true }, // some random context info (can show a forwarded message with this too)
|
|
451
|
-
timestamp: Date(), // optional, if you want to manually set the timestamp of the message
|
|
452
|
-
caption: "hello there!", // (for media messages) the caption to send with the media (cannot be sent with stickers though)
|
|
453
|
-
jpegThumbnail: "23GD#4/==", /* (for location & media messages) has to be a base 64 encoded JPEG if you want to send a custom thumb,
|
|
454
|
-
or set to null if you don't want to send a thumbnail.
|
|
455
|
-
Do not enter this field if you want to automatically generate a thumb
|
|
456
|
-
*/
|
|
457
|
-
mimetype: Mimetype.pdf, /* (for media messages) specify the type of media (optional for all media types except documents),
|
|
458
|
-
import {Mimetype} from '@whiskeysockets/baileys'
|
|
459
|
-
*/
|
|
460
|
-
fileName: 'somefile.pdf', // (for media messages) file name for the media
|
|
461
|
-
/* will send audio messages as voice notes, if set to true */
|
|
462
|
-
ptt: true,
|
|
463
|
-
/** Should it send as a disappearing messages.
|
|
464
|
-
* By default 'chat' -- which follows the setting of the chat */
|
|
465
|
-
ephemeralExpiration: WA_DEFAULT_EPHEMERAL
|
|
1027
|
+
#### Image Message
|
|
1028
|
+
```javascript
|
|
1029
|
+
await sock.sendMessage(
|
|
1030
|
+
id,
|
|
1031
|
+
{
|
|
1032
|
+
image: {
|
|
1033
|
+
url: './Media/ma_img.png'
|
|
1034
|
+
},
|
|
1035
|
+
caption: 'hello word'
|
|
466
1036
|
}
|
|
467
|
-
|
|
468
|
-
## Forwarding Messages
|
|
469
|
-
|
|
470
|
-
``` ts
|
|
471
|
-
const msg = getMessageFromStore('455@s.whatsapp.net', 'HSJHJWH7323HSJSJ') // implement this on your end
|
|
472
|
-
await sock.sendMessage('1234@s.whatsapp.net', { forward: msg }) // WA forward the message!
|
|
1037
|
+
)
|
|
473
1038
|
```
|
|
474
1039
|
|
|
475
|
-
|
|
1040
|
+
#### View Once Message
|
|
476
1041
|
|
|
477
|
-
|
|
478
|
-
In multi-device, you cannot mark an entire "chat" read as it were with Baileys Web.
|
|
479
|
-
This means you have to keep track of unread messages.
|
|
1042
|
+
- You can send all messages above as `viewOnce`, you only need to pass `viewOnce: true` in content object
|
|
480
1043
|
|
|
481
|
-
```
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
1044
|
+
```javascript
|
|
1045
|
+
await sock.sendMessage(
|
|
1046
|
+
id,
|
|
1047
|
+
{
|
|
1048
|
+
image: {
|
|
1049
|
+
url: './Media/ma_img.png'
|
|
1050
|
+
},
|
|
1051
|
+
viewOnce: true, //works with video, audio too
|
|
1052
|
+
caption: 'hello word'
|
|
1053
|
+
}
|
|
1054
|
+
)
|
|
490
1055
|
```
|
|
491
1056
|
|
|
492
|
-
|
|
493
|
-
On a `WAMessage`, the `messageID` can be accessed using ```messageID = message.key.id```.
|
|
494
|
-
|
|
495
|
-
## Update Presence
|
|
1057
|
+
## Modify Messages
|
|
496
1058
|
|
|
497
|
-
|
|
498
|
-
await sock.sendPresenceUpdate('available', id)
|
|
1059
|
+
### Deleting Messages (for everyone)
|
|
499
1060
|
|
|
1061
|
+
```javascript
|
|
1062
|
+
const msg = await sock.sendMessage(jid, { text: 'hello word' })
|
|
1063
|
+
await sock.sendMessage(jid, { delete: msg.key })
|
|
500
1064
|
```
|
|
501
|
-
This lets the person/group with ``` id ``` know whether you're online, offline, typing etc.
|
|
502
1065
|
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
1066
|
+
**Note:** deleting for oneself is supported via `chatModify`, see in [this section](#modifying-chats)
|
|
1067
|
+
|
|
1068
|
+
### Editing Messages
|
|
1069
|
+
|
|
1070
|
+
- You can pass all editable contents here
|
|
1071
|
+
```javascript
|
|
1072
|
+
await sock.sendMessage(jid, {
|
|
1073
|
+
text: 'updated text goes here',
|
|
1074
|
+
edit: response.key,
|
|
1075
|
+
});
|
|
506
1076
|
```
|
|
507
1077
|
|
|
508
|
-
|
|
1078
|
+
## Manipulating Media Messages
|
|
509
1079
|
|
|
510
|
-
|
|
1080
|
+
### Thumbnail in Media Messages
|
|
1081
|
+
- 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`.
|
|
1082
|
+
- Thumbnails for videos can also be generated automatically, though, you need to have `ffmpeg` installed on your system.
|
|
511
1083
|
|
|
512
|
-
|
|
1084
|
+
### Downloading Media Messages
|
|
513
1085
|
|
|
514
1086
|
If you want to save the media you received
|
|
515
|
-
```
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
sock.ev.on('messages.upsert', async ({ messages }) => {
|
|
520
|
-
const m = messages[0]
|
|
1087
|
+
```javascript
|
|
1088
|
+
const { createWriteStream } = require('fs');
|
|
1089
|
+
const { downloadMediaMessage, getContentType } = require("@giftedtech/baileys");
|
|
521
1090
|
|
|
1091
|
+
sock.ev.on('messages.upsert', async ({ [m] }) => {
|
|
522
1092
|
if (!m.message) return // if there is no text or media message
|
|
523
|
-
const messageType =
|
|
1093
|
+
const messageType = getContentType(m) // get what type of message it is (text, image, video...)
|
|
1094
|
+
|
|
524
1095
|
// if the message is an image
|
|
525
1096
|
if (messageType === 'imageMessage') {
|
|
526
1097
|
// download the message
|
|
527
|
-
const
|
|
1098
|
+
const stream = await downloadMediaMessage(
|
|
528
1099
|
m,
|
|
529
|
-
'buffer'
|
|
1100
|
+
'stream', // can be 'buffer' too
|
|
530
1101
|
{ },
|
|
531
|
-
{
|
|
1102
|
+
{
|
|
532
1103
|
logger,
|
|
533
1104
|
// pass this so that baileys can request a reupload of media
|
|
534
1105
|
// that has been deleted
|
|
@@ -536,365 +1107,539 @@ sock.ev.on('messages.upsert', async ({ messages }) => {
|
|
|
536
1107
|
}
|
|
537
1108
|
)
|
|
538
1109
|
// save to file
|
|
539
|
-
|
|
1110
|
+
const writeStream = createWriteStream('./my-download.jpeg')
|
|
1111
|
+
stream.pipe(writeStream)
|
|
540
1112
|
}
|
|
541
1113
|
}
|
|
542
1114
|
```
|
|
543
1115
|
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
1116
|
+
### Re-upload Media Message to Whatsapp
|
|
1117
|
+
|
|
1118
|
+
- 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:
|
|
1119
|
+
```javascript
|
|
1120
|
+
await sock.updateMediaMessage(msg)
|
|
1121
|
+
```
|
|
1122
|
+
|
|
1123
|
+
## Reject Call
|
|
1124
|
+
|
|
1125
|
+
- You can obtain `callId` and `callFrom` from `call` event
|
|
1126
|
+
|
|
1127
|
+
```javascript
|
|
1128
|
+
await sock.rejectCall(callId, callFrom)
|
|
547
1129
|
```
|
|
548
1130
|
|
|
549
|
-
##
|
|
1131
|
+
## Send States in Chat
|
|
550
1132
|
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
1133
|
+
### Reading Messages
|
|
1134
|
+
- A set of message [keys](https://baileys.whiskeysockets.io/types/WAMessageKey.html) must be explicitly marked read now.
|
|
1135
|
+
- You cannot mark an entire 'chat' read as it were with Baileys Web.
|
|
1136
|
+
This means you have to keep track of unread messages.
|
|
1137
|
+
|
|
1138
|
+
```javascript
|
|
1139
|
+
const key: WAMessageKey
|
|
1140
|
+
// can pass multiple keys to read multiple messages as well
|
|
1141
|
+
await sock.readMessages([key])
|
|
557
1142
|
```
|
|
558
1143
|
|
|
559
|
-
|
|
1144
|
+
The message ID is the unique identifier of the message that you are marking as read.
|
|
1145
|
+
On a `WAMessage`, the `messageID` can be accessed using ```messageID = message.key.id```.
|
|
560
1146
|
|
|
561
|
-
|
|
1147
|
+
### Update Presence
|
|
562
1148
|
|
|
563
|
-
```
|
|
564
|
-
|
|
1149
|
+
- ``` presence ``` can be one of [these](https://baileys.whiskeysockets.io/types/WAPresence.html)
|
|
1150
|
+
- The presence expires after about 10 seconds.
|
|
1151
|
+
- This lets the person/group with `jid` know whether you're online, offline, typing etc.
|
|
565
1152
|
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
edit: response.key,
|
|
569
|
-
});
|
|
1153
|
+
```javascript
|
|
1154
|
+
await sock.sendPresenceUpdate('available', jid)
|
|
570
1155
|
```
|
|
571
1156
|
|
|
1157
|
+
> [!NOTE]
|
|
1158
|
+
> 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')`
|
|
1159
|
+
|
|
572
1160
|
## Modifying Chats
|
|
573
1161
|
|
|
574
1162
|
WA uses an encrypted form of communication to send chat/app updates. This has been implemented mostly and you can send the following updates:
|
|
575
1163
|
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
const lastMsgInChat = await getLastMessageInChat('123456@s.whatsapp.net') // implement this on your end
|
|
579
|
-
await sock.chatModify({ archive: true, lastMessages: [lastMsgInChat] }, '123456@s.whatsapp.net')
|
|
580
|
-
```
|
|
581
|
-
- Mute/unmute a chat
|
|
582
|
-
``` ts
|
|
583
|
-
// mute for 8 hours
|
|
584
|
-
await sock.chatModify({ mute: 8*60*60*1000 }, '123456@s.whatsapp.net', [])
|
|
585
|
-
// unmute
|
|
586
|
-
await sock.chatModify({ mute: null }, '123456@s.whatsapp.net', [])
|
|
587
|
-
```
|
|
588
|
-
- Mark a chat read/unread
|
|
589
|
-
``` ts
|
|
590
|
-
const lastMsgInChat = await getLastMessageInChat('123456@s.whatsapp.net') // implement this on your end
|
|
591
|
-
// mark it unread
|
|
592
|
-
await sock.chatModify({ markRead: false, lastMessages: [lastMsgInChat] }, '123456@s.whatsapp.net')
|
|
593
|
-
```
|
|
594
|
-
|
|
595
|
-
- Delete a message for me
|
|
596
|
-
``` ts
|
|
597
|
-
await sock.chatModify(
|
|
598
|
-
{ clear: { messages: [{ id: 'ATWYHDNNWU81732J', fromMe: true, timestamp: "1654823909" }] } },
|
|
599
|
-
'123456@s.whatsapp.net',
|
|
600
|
-
[]
|
|
601
|
-
)
|
|
602
|
-
|
|
603
|
-
```
|
|
604
|
-
|
|
605
|
-
- Delete a chat
|
|
606
|
-
``` ts
|
|
607
|
-
const lastMsgInChat = await getLastMessageInChat('123456@s.whatsapp.net') // implement this on your end
|
|
608
|
-
await sock.chatModify({
|
|
609
|
-
delete: true,
|
|
610
|
-
lastMessages: [{ key: lastMsgInChat.key, messageTimestamp: lastMsgInChat.messageTimestamp }]
|
|
611
|
-
},
|
|
612
|
-
'123456@s.whatsapp.net')
|
|
613
|
-
```
|
|
1164
|
+
> [!IMPORTANT]
|
|
1165
|
+
> 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.
|
|
614
1166
|
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
1167
|
+
### Archive a Chat
|
|
1168
|
+
```javascript
|
|
1169
|
+
const lastMsgInChat = await getLastMessageInChat(jid) // implement this on your end
|
|
1170
|
+
await sock.chatModify({ archive: true, lastMessages: [lastMsgInChat] }, jid)
|
|
1171
|
+
```
|
|
1172
|
+
### Mute/Unmute a Chat
|
|
1173
|
+
|
|
1174
|
+
- Supported times:
|
|
1175
|
+
|
|
1176
|
+
| Time | Miliseconds |
|
|
1177
|
+
|-------|-----------------|
|
|
1178
|
+
| Remove | null |
|
|
1179
|
+
| 8h | 86.400.000 |
|
|
1180
|
+
| 7d | 604.800.000 |
|
|
1181
|
+
|
|
1182
|
+
```javascript
|
|
1183
|
+
// mute for 8 hours
|
|
1184
|
+
await sock.chatModify({ mute: 8 * 60 * 60 * 1000 }, jid)
|
|
1185
|
+
// unmute
|
|
1186
|
+
await sock.chatModify({ mute: null }, jid)
|
|
1187
|
+
```
|
|
1188
|
+
### Mark a Chat Read/Unread
|
|
1189
|
+
```javascript
|
|
1190
|
+
const lastMsgInChat = await getLastMessageInChat(jid) // implement this on your end
|
|
1191
|
+
// mark it unread
|
|
1192
|
+
await sock.chatModify({ markRead: false, lastMessages: [lastMsgInChat] }, jid)
|
|
1193
|
+
```
|
|
1194
|
+
|
|
1195
|
+
### Delete a Message for Me
|
|
1196
|
+
```javascript
|
|
1197
|
+
await sock.chatModify(
|
|
1198
|
+
{
|
|
1199
|
+
clear: {
|
|
1200
|
+
messages: [
|
|
1201
|
+
{
|
|
1202
|
+
id: 'ATWYHDNNWU81732J',
|
|
1203
|
+
fromMe: true,
|
|
1204
|
+
timestamp: '1654823909'
|
|
1205
|
+
}
|
|
1206
|
+
]
|
|
1207
|
+
}
|
|
1208
|
+
},
|
|
1209
|
+
jid
|
|
1210
|
+
)
|
|
1211
|
+
|
|
1212
|
+
```
|
|
1213
|
+
### Delete a Chat
|
|
1214
|
+
```javascript
|
|
1215
|
+
const lastMsgInChat = await getLastMessageInChat(jid) // implement this on your end
|
|
1216
|
+
await sock.chatModify({
|
|
1217
|
+
delete: true,
|
|
1218
|
+
lastMessages: [
|
|
1219
|
+
{
|
|
1220
|
+
key: lastMsgInChat.key,
|
|
1221
|
+
messageTimestamp: lastMsgInChat.messageTimestamp
|
|
1222
|
+
}
|
|
1223
|
+
]
|
|
1224
|
+
},
|
|
1225
|
+
jid
|
|
1226
|
+
)
|
|
1227
|
+
```
|
|
1228
|
+
### Pin/Unpin a Chat
|
|
1229
|
+
```javascript
|
|
1230
|
+
await sock.chatModify({
|
|
1231
|
+
pin: true // or `false` to unpin
|
|
1232
|
+
},
|
|
1233
|
+
jid
|
|
1234
|
+
)
|
|
1235
|
+
```
|
|
1236
|
+
### Star/Unstar a Message
|
|
1237
|
+
```javascript
|
|
1238
|
+
await sock.chatModify({
|
|
1239
|
+
star: {
|
|
1240
|
+
messages: [
|
|
1241
|
+
{
|
|
1242
|
+
id: 'messageID',
|
|
1243
|
+
fromMe: true // or `false`
|
|
1244
|
+
}
|
|
1245
|
+
],
|
|
1246
|
+
star: true // - true: Star Message; false: Unstar Message
|
|
1247
|
+
}
|
|
1248
|
+
},
|
|
1249
|
+
jid
|
|
1250
|
+
)
|
|
1251
|
+
```
|
|
1252
|
+
|
|
1253
|
+
### Disappearing Messages
|
|
1254
|
+
|
|
1255
|
+
- Ephemeral can be:
|
|
1256
|
+
|
|
1257
|
+
| Time | Seconds |
|
|
1258
|
+
|-------|----------------|
|
|
1259
|
+
| Remove | 0 |
|
|
1260
|
+
| 24h | 86.400 |
|
|
1261
|
+
| 7d | 604.800 |
|
|
1262
|
+
| 90d | 7.776.000 |
|
|
1263
|
+
|
|
1264
|
+
- You need to pass in **Seconds**, default is 7 days
|
|
1265
|
+
|
|
1266
|
+
```javascript
|
|
638
1267
|
// turn on disappearing messages
|
|
639
1268
|
await sock.sendMessage(
|
|
640
|
-
jid,
|
|
1269
|
+
jid,
|
|
641
1270
|
// this is 1 week in seconds -- how long you want messages to appear for
|
|
642
1271
|
{ disappearingMessagesInChat: WA_DEFAULT_EPHEMERAL }
|
|
643
1272
|
)
|
|
1273
|
+
|
|
644
1274
|
// will send as a disappearing message
|
|
645
1275
|
await sock.sendMessage(jid, { text: 'hello' }, { ephemeralExpiration: WA_DEFAULT_EPHEMERAL })
|
|
1276
|
+
|
|
646
1277
|
// turn off disappearing messages
|
|
647
1278
|
await sock.sendMessage(
|
|
648
|
-
jid,
|
|
1279
|
+
jid,
|
|
649
1280
|
{ disappearingMessagesInChat: false }
|
|
650
1281
|
)
|
|
1282
|
+
```
|
|
651
1283
|
|
|
1284
|
+
## User Querys
|
|
1285
|
+
|
|
1286
|
+
### Check If ID Exists in Whatsapp
|
|
1287
|
+
```javascript
|
|
1288
|
+
const [result] = await sock.onWhatsApp(jid)
|
|
1289
|
+
if (result.exists) console.log (`${jid} exists on WhatsApp, as jid: ${result.jid}`)
|
|
652
1290
|
```
|
|
653
1291
|
|
|
654
|
-
|
|
1292
|
+
### Query Chat History (groups too)
|
|
655
1293
|
|
|
656
|
-
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
- To change your profile name
|
|
675
|
-
``` ts
|
|
676
|
-
const name = 'My name'
|
|
677
|
-
await sock.updateProfileName(name)
|
|
678
|
-
```
|
|
1294
|
+
- You need to have oldest message in chat
|
|
1295
|
+
```javascript
|
|
1296
|
+
const msg = await getOldestMessageInChat(jid)
|
|
1297
|
+
await sock.fetchMessageHistory(
|
|
1298
|
+
50, //quantity (max: 50 per query)
|
|
1299
|
+
msg.key,
|
|
1300
|
+
msg.messageTimestamp
|
|
1301
|
+
)
|
|
1302
|
+
```
|
|
1303
|
+
- Messages will be received in `messaging.history-set` event
|
|
1304
|
+
|
|
1305
|
+
### Fetch Status
|
|
1306
|
+
```javascript
|
|
1307
|
+
const status = await sock.fetchStatus(jid)
|
|
1308
|
+
console.log('status: ' + status)
|
|
1309
|
+
```
|
|
1310
|
+
|
|
1311
|
+
### Fetch Profile Picture (groups too)
|
|
679
1312
|
- To get the display picture of some person/group
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
1313
|
+
```javascript
|
|
1314
|
+
// for low res picture
|
|
1315
|
+
const ppUrl = await sock.profilePictureUrl(jid)
|
|
1316
|
+
console.log(ppUrl)
|
|
1317
|
+
|
|
1318
|
+
// for high res picture
|
|
1319
|
+
const ppUrl = await sock.profilePictureUrl(jid, 'image')
|
|
1320
|
+
```
|
|
1321
|
+
|
|
1322
|
+
### Fetch Bussines Profile (such as description or category)
|
|
1323
|
+
```javascript
|
|
1324
|
+
const profile = await sock.getBusinessProfile(jid)
|
|
1325
|
+
console.log('business description: ' + profile.description + ', category: ' + profile.category)
|
|
1326
|
+
```
|
|
1327
|
+
|
|
1328
|
+
### Fetch Someone's Presence (if they're typing or online)
|
|
1329
|
+
```javascript
|
|
1330
|
+
// the presence update is fetched and called here
|
|
1331
|
+
sock.ev.on('presence.update', console.log)
|
|
1332
|
+
|
|
1333
|
+
// request updates for a chat
|
|
1334
|
+
await sock.presenceSubscribe(jid)
|
|
1335
|
+
```
|
|
1336
|
+
|
|
1337
|
+
## Change Profile
|
|
1338
|
+
|
|
1339
|
+
### Change Profile Status
|
|
1340
|
+
```javascript
|
|
1341
|
+
await sock.updateProfileStatus('Hello World!')
|
|
1342
|
+
```
|
|
1343
|
+
### Change Profile Name
|
|
1344
|
+
```javascript
|
|
1345
|
+
await sock.updateProfileName('My name')
|
|
1346
|
+
```
|
|
1347
|
+
### Change Display Picture (groups too)
|
|
687
1348
|
- To change your display picture or a group's
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
// the presence update is fetched and called here
|
|
700
|
-
sock.ev.on('presence.update', json => console.log(json))
|
|
701
|
-
// request updates for a chat
|
|
702
|
-
await sock.presenceSubscribe("xyz@s.whatsapp.net")
|
|
703
|
-
```
|
|
704
|
-
- To block or unblock user
|
|
705
|
-
``` ts
|
|
706
|
-
await sock.updateBlockStatus("xyz@s.whatsapp.net", "block") // Block user
|
|
707
|
-
await sock.updateBlockStatus("xyz@s.whatsapp.net", "unblock") // Unblock user
|
|
708
|
-
```
|
|
709
|
-
- To get a business profile, such as description or category
|
|
710
|
-
```ts
|
|
711
|
-
const profile = await sock.getBusinessProfile("xyz@s.whatsapp.net")
|
|
712
|
-
console.log("business description: " + profile.description + ", category: " + profile.category)
|
|
713
|
-
```
|
|
714
|
-
Of course, replace ``` xyz ``` with an actual ID.
|
|
1349
|
+
|
|
1350
|
+
> [!NOTE]
|
|
1351
|
+
> 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)
|
|
1352
|
+
|
|
1353
|
+
```javascript
|
|
1354
|
+
await sock.updateProfilePicture(jid, { url: './new-profile-picture.jpeg' })
|
|
1355
|
+
```
|
|
1356
|
+
### Remove display picture (groups too)
|
|
1357
|
+
```javascript
|
|
1358
|
+
await sock.removeProfilePicture(jid)
|
|
1359
|
+
```
|
|
715
1360
|
|
|
716
1361
|
## Groups
|
|
717
|
-
- To create a group
|
|
718
|
-
``` ts
|
|
719
|
-
// title & participants
|
|
720
|
-
const group = await sock.groupCreate("My Fab Group", ["1234@s.whatsapp.net", "4564@s.whatsapp.net"])
|
|
721
|
-
console.log ("created group with id: " + group.gid)
|
|
722
|
-
sock.sendMessage(group.id, { text: 'hello there' }) // say hello to everyone on the group
|
|
723
|
-
```
|
|
724
|
-
- To add/remove people to a group or demote/promote people
|
|
725
|
-
``` ts
|
|
726
|
-
// id & people to add to the group (will throw error if it fails)
|
|
727
|
-
const response = await sock.groupParticipantsUpdate(
|
|
728
|
-
"abcd-xyz@g.us",
|
|
729
|
-
["abcd@s.whatsapp.net", "efgh@s.whatsapp.net"],
|
|
730
|
-
"add" // replace this parameter with "remove", "demote" or "promote"
|
|
731
|
-
)
|
|
732
|
-
```
|
|
733
|
-
- To change the group's subject
|
|
734
|
-
``` ts
|
|
735
|
-
await sock.groupUpdateSubject("abcd-xyz@g.us", "New Subject!")
|
|
736
|
-
```
|
|
737
|
-
- To change the group's description
|
|
738
|
-
``` ts
|
|
739
|
-
await sock.groupUpdateDescription("abcd-xyz@g.us", "New Description!")
|
|
740
|
-
```
|
|
741
|
-
- To change group settings
|
|
742
|
-
``` ts
|
|
743
|
-
// only allow admins to send messages
|
|
744
|
-
await sock.groupSettingUpdate("abcd-xyz@g.us", 'announcement')
|
|
745
|
-
// allow everyone to send messages
|
|
746
|
-
await sock.groupSettingUpdate("abcd-xyz@g.us", 'not_announcement')
|
|
747
|
-
// allow everyone to modify the group's settings -- like display picture etc.
|
|
748
|
-
await sock.groupSettingUpdate("abcd-xyz@g.us", 'unlocked')
|
|
749
|
-
// only allow admins to modify the group's settings
|
|
750
|
-
await sock.groupSettingUpdate("abcd-xyz@g.us", 'locked')
|
|
751
|
-
```
|
|
752
|
-
- To leave a group
|
|
753
|
-
``` ts
|
|
754
|
-
await sock.groupLeave("abcd-xyz@g.us") // (will throw error if it fails)
|
|
755
|
-
```
|
|
756
|
-
- To get the invite code for a group
|
|
757
|
-
``` ts
|
|
758
|
-
const code = await sock.groupInviteCode("abcd-xyz@g.us")
|
|
759
|
-
console.log("group code: " + code)
|
|
760
|
-
```
|
|
761
|
-
- To revoke the invite code in a group
|
|
762
|
-
```ts
|
|
763
|
-
const code = await sock.groupRevokeInvite("abcd-xyz@g.us")
|
|
764
|
-
console.log("New group code: " + code)
|
|
765
|
-
```
|
|
766
|
-
- To query the metadata of a group
|
|
767
|
-
``` ts
|
|
768
|
-
const metadata = await sock.groupMetadata("abcd-xyz@g.us")
|
|
769
|
-
console.log(metadata.id + ", title: " + metadata.subject + ", description: " + metadata.desc)
|
|
770
|
-
```
|
|
771
|
-
- To join the group using the invitation code
|
|
772
|
-
``` ts
|
|
773
|
-
const response = await sock.groupAcceptInvite("xxx")
|
|
774
|
-
console.log("joined to: " + response)
|
|
775
|
-
```
|
|
776
|
-
Of course, replace ``` xxx ``` with invitation code.
|
|
777
|
-
- To get group info by invite code
|
|
778
|
-
```ts
|
|
779
|
-
const response = await sock.groupGetInviteInfo("xxx")
|
|
780
|
-
console.log("group information: " + response)
|
|
781
|
-
```
|
|
782
|
-
- To join the group using groupInviteMessage
|
|
783
|
-
``` ts
|
|
784
|
-
const response = await sock.groupAcceptInviteV4("abcd@s.whatsapp.net", groupInviteMessage)
|
|
785
|
-
console.log("joined to: " + response)
|
|
786
|
-
```
|
|
787
|
-
Of course, replace ``` xxx ``` with invitation code.
|
|
788
1362
|
|
|
789
|
-
- To
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
1363
|
+
- To change group properties you need to be admin
|
|
1364
|
+
|
|
1365
|
+
### Create a Group
|
|
1366
|
+
```javascript
|
|
1367
|
+
// title & participants
|
|
1368
|
+
const group = await sock.groupCreate('My Fab Group', ['1234@s.whatsapp.net', '4564@s.whatsapp.net'])
|
|
1369
|
+
console.log('created group with id: ' + group.gid)
|
|
1370
|
+
await sock.sendMessage(group.id, { text: 'hello there' }) // say hello to everyone on the group
|
|
1371
|
+
```
|
|
1372
|
+
### Add/Remove or Demote/Promote
|
|
1373
|
+
```javascript
|
|
1374
|
+
// id & people to add to the group (will throw error if it fails)
|
|
1375
|
+
await sock.groupParticipantsUpdate(
|
|
1376
|
+
jid,
|
|
1377
|
+
['abcd@s.whatsapp.net', 'efgh@s.whatsapp.net'],
|
|
1378
|
+
'add' // replace this parameter with 'remove' or 'demote' or 'promote'
|
|
1379
|
+
)
|
|
1380
|
+
```
|
|
1381
|
+
### Change Subject (name)
|
|
1382
|
+
```javascript
|
|
1383
|
+
await sock.groupUpdateSubject(jid, 'New Subject!')
|
|
1384
|
+
```
|
|
1385
|
+
### Change Description
|
|
1386
|
+
```javascript
|
|
1387
|
+
await sock.groupUpdateDescription(jid, 'New Description!')
|
|
1388
|
+
```
|
|
1389
|
+
### Change Settings
|
|
1390
|
+
```javascript
|
|
1391
|
+
// only allow admins to send messages
|
|
1392
|
+
await sock.groupSettingUpdate(jid, 'announcement')
|
|
1393
|
+
// allow everyone to send messages
|
|
1394
|
+
await sock.groupSettingUpdate(jid, 'not_announcement')
|
|
1395
|
+
// allow everyone to modify the group's settings -- like display picture etc.
|
|
1396
|
+
await sock.groupSettingUpdate(jid, 'unlocked')
|
|
1397
|
+
// only allow admins to modify the group's settings
|
|
1398
|
+
await sock.groupSettingUpdate(jid, 'locked')
|
|
1399
|
+
```
|
|
1400
|
+
### Leave a Group
|
|
1401
|
+
```javascript
|
|
1402
|
+
// will throw error if it fails
|
|
1403
|
+
await sock.groupLeave(jid)
|
|
1404
|
+
```
|
|
1405
|
+
### Get Invite Code
|
|
1406
|
+
- To create link with code use `'https://chat.whatsapp.com/' + code`
|
|
1407
|
+
```javascript
|
|
1408
|
+
const code = await sock.groupInviteCode(jid)
|
|
1409
|
+
console.log('group code: ' + code)
|
|
1410
|
+
```
|
|
1411
|
+
### Revoke Invite Code
|
|
1412
|
+
```javascript
|
|
1413
|
+
const code = await sock.groupRevokeInvite(jid)
|
|
1414
|
+
console.log('New group code: ' + code)
|
|
1415
|
+
```
|
|
1416
|
+
### Join Using Invitation Code
|
|
1417
|
+
- Code can't have `https://chat.whatsapp.com/`, only code
|
|
1418
|
+
```javascript
|
|
1419
|
+
const response = await sock.groupAcceptInvite(code)
|
|
1420
|
+
console.log('joined to: ' + response)
|
|
1421
|
+
```
|
|
1422
|
+
### Get Group Info by Invite Code
|
|
1423
|
+
```javascript
|
|
1424
|
+
const response = await sock.groupGetInviteInfo(code)
|
|
1425
|
+
console.log('group information: ' + response)
|
|
1426
|
+
```
|
|
1427
|
+
### Query Metadata (participants, name, description...)
|
|
1428
|
+
```javascript
|
|
1429
|
+
const metadata = await sock.groupMetadata(jid)
|
|
1430
|
+
console.log(metadata.id + ', title: ' + metadata.subject + ', description: ' + metadata.desc)
|
|
1431
|
+
```
|
|
1432
|
+
### Join using `groupInviteMessage`
|
|
1433
|
+
```javascript
|
|
1434
|
+
const response = await sock.groupAcceptInviteV4(jid, groupInviteMessage)
|
|
1435
|
+
console.log('joined to: ' + response)
|
|
1436
|
+
```
|
|
1437
|
+
### Get Request Join List
|
|
1438
|
+
```javascript
|
|
1439
|
+
const response = await sock.groupRequestParticipantsList(jid)
|
|
1440
|
+
console.log(response)
|
|
1441
|
+
```
|
|
1442
|
+
### Approve/Reject Request Join
|
|
1443
|
+
```javascript
|
|
1444
|
+
const response = await sock.groupRequestParticipantsUpdate(
|
|
1445
|
+
jid, // group id
|
|
1446
|
+
['abcd@s.whatsapp.net', 'efgh@s.whatsapp.net'],
|
|
1447
|
+
'approve' // or 'reject'
|
|
1448
|
+
)
|
|
1449
|
+
console.log(response)
|
|
1450
|
+
```
|
|
1451
|
+
### Get All Participating Groups Metadata
|
|
1452
|
+
```javascript
|
|
1453
|
+
const response = await sock.groupFetchAllParticipating()
|
|
1454
|
+
console.log(response)
|
|
1455
|
+
```
|
|
1456
|
+
### Toggle Ephemeral
|
|
1457
|
+
|
|
1458
|
+
- Ephemeral can be:
|
|
1459
|
+
|
|
1460
|
+
| Time | Seconds |
|
|
1461
|
+
|-------|----------------|
|
|
1462
|
+
| Remove | 0 |
|
|
1463
|
+
| 24h | 86.400 |
|
|
1464
|
+
| 7d | 604.800 |
|
|
1465
|
+
| 90d | 7.776.000 |
|
|
1466
|
+
|
|
1467
|
+
```javascript
|
|
1468
|
+
await sock.groupToggleEphemeral(jid, 86400)
|
|
1469
|
+
```
|
|
1470
|
+
|
|
1471
|
+
### Change Add Mode
|
|
1472
|
+
```javascript
|
|
1473
|
+
await sock.groupMemberAddMode(
|
|
1474
|
+
jid,
|
|
1475
|
+
'all_member_add' // or 'admin_add'
|
|
1476
|
+
)
|
|
1477
|
+
```
|
|
803
1478
|
|
|
804
1479
|
## Privacy
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
1480
|
+
|
|
1481
|
+
### Block/Unblock User
|
|
1482
|
+
```javascript
|
|
1483
|
+
await sock.updateBlockStatus(jid, 'block') // Block user
|
|
1484
|
+
await sock.updateBlockStatus(jid, 'unblock') // Unblock user
|
|
1485
|
+
```
|
|
1486
|
+
### Get Privacy Settings
|
|
1487
|
+
```javascript
|
|
1488
|
+
const privacySettings = await sock.fetchPrivacySettings(true)
|
|
1489
|
+
console.log('privacy settings: ' + privacySettings)
|
|
1490
|
+
```
|
|
1491
|
+
### Get BlockList
|
|
1492
|
+
```javascript
|
|
1493
|
+
const response = await sock.fetchBlocklist()
|
|
1494
|
+
console.log(response)
|
|
1495
|
+
```
|
|
1496
|
+
### Update LastSeen Privacy
|
|
1497
|
+
```javascript
|
|
1498
|
+
const value = 'all' // 'contacts' | 'contact_blacklist' | 'none'
|
|
1499
|
+
await sock.updateLastSeenPrivacy(value)
|
|
1500
|
+
```
|
|
1501
|
+
### Update Online Privacy
|
|
1502
|
+
```javascript
|
|
1503
|
+
const value = 'all' // 'match_last_seen'
|
|
1504
|
+
await sock.updateOnlinePrivacy(value)
|
|
1505
|
+
```
|
|
1506
|
+
### Update Profile Picture Privacy
|
|
1507
|
+
```javascript
|
|
1508
|
+
const value = 'all' // 'contacts' | 'contact_blacklist' | 'none'
|
|
1509
|
+
await sock.updateProfilePicturePrivacy(value)
|
|
1510
|
+
```
|
|
1511
|
+
### Update Status Privacy
|
|
1512
|
+
```javascript
|
|
1513
|
+
const value = 'all' // 'contacts' | 'contact_blacklist' | 'none'
|
|
1514
|
+
await sock.updateStatusPrivacy(value)
|
|
1515
|
+
```
|
|
1516
|
+
### Update Read Receipts Privacy
|
|
1517
|
+
```javascript
|
|
1518
|
+
const value = 'all' // 'none'
|
|
1519
|
+
await sock.updateReadReceiptsPrivacy(value)
|
|
1520
|
+
```
|
|
1521
|
+
### Update Groups Add Privacy
|
|
1522
|
+
```javascript
|
|
1523
|
+
const value = 'all' // 'contacts' | 'contact_blacklist'
|
|
1524
|
+
await sock.updateGroupsAddPrivacy(value)
|
|
1525
|
+
```
|
|
1526
|
+
### Update Default Disappearing Mode
|
|
1527
|
+
|
|
1528
|
+
- Like [this](#disappearing-messages), ephemeral can be:
|
|
1529
|
+
|
|
1530
|
+
| Time | Seconds |
|
|
1531
|
+
|-------|----------------|
|
|
1532
|
+
| Remove | 0 |
|
|
1533
|
+
| 24h | 86.400 |
|
|
1534
|
+
| 7d | 604.800 |
|
|
1535
|
+
| 90d | 7.776.000 |
|
|
1536
|
+
|
|
1537
|
+
```javascript
|
|
1538
|
+
const ephemeral = 86400
|
|
1539
|
+
await sock.updateDefaultDisappearingMode(ephemeral)
|
|
1540
|
+
```
|
|
1541
|
+
|
|
845
1542
|
## Broadcast Lists & Stories
|
|
846
1543
|
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
```
|
|
850
|
-
sock.sendMessage(
|
|
1544
|
+
### Send Broadcast & Stories
|
|
1545
|
+
- Messages can be sent to broadcasts & stories. You need to add the following message options in sendMessage, like this:
|
|
1546
|
+
```javascript
|
|
1547
|
+
await sock.sendMessage(
|
|
1548
|
+
jid,
|
|
1549
|
+
{
|
|
1550
|
+
image: {
|
|
1551
|
+
url: url
|
|
1552
|
+
},
|
|
1553
|
+
caption: caption
|
|
1554
|
+
},
|
|
1555
|
+
{
|
|
1556
|
+
backgroundColor: backgroundColor,
|
|
1557
|
+
font: font,
|
|
1558
|
+
statusJidList: statusJidList,
|
|
1559
|
+
broadcast: true
|
|
1560
|
+
}
|
|
1561
|
+
)
|
|
851
1562
|
```
|
|
852
|
-
-
|
|
853
|
-
- You can add backgroundColor and other options in the message options
|
|
854
|
-
- broadcast: true enables broadcast mode
|
|
855
|
-
- statusJidList
|
|
1563
|
+
- Message body can be a `extendedTextMessage` or `imageMessage` or `videoMessage` or `voiceMessage`, see [here](https://baileys.whiskeysockets.io/types/AnyRegularMessageContent.html)
|
|
1564
|
+
- You can add `backgroundColor` and other options in the message options, see [here](https://baileys.whiskeysockets.io/types/MiscMessageGenerationOptions.html)
|
|
1565
|
+
- `broadcast: true` enables broadcast mode
|
|
1566
|
+
- `statusJidList`: a list of people that you can get which you need to provide, which are the people who will get this status message.
|
|
856
1567
|
|
|
857
1568
|
- You can send messages to broadcast lists the same way you send messages to groups & individual chats.
|
|
858
1569
|
- Right now, WA Web does not support creating broadcast lists, but you can still delete them.
|
|
859
1570
|
- Broadcast IDs are in the format `12345678@broadcast`
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
1571
|
+
### Query a Broadcast List's Recipients & Name
|
|
1572
|
+
```javascript
|
|
1573
|
+
const bList = await sock.getBroadcastListInfo('1234@broadcast')
|
|
1574
|
+
console.log (`list name: ${bList.name}, recps: ${bList.recipients}`)
|
|
1575
|
+
```
|
|
865
1576
|
|
|
866
1577
|
## Writing Custom Functionality
|
|
867
1578
|
Baileys is written with custom functionality in mind. Instead of forking the project & re-writing the internals, you can simply write your own extensions.
|
|
868
1579
|
|
|
1580
|
+
### Enabling Debug Level in Baileys Logs
|
|
869
1581
|
First, enable the logging of unhandled messages from WhatsApp by setting:
|
|
870
|
-
```
|
|
1582
|
+
```javascript
|
|
871
1583
|
const sock = makeWASocket({
|
|
872
1584
|
logger: P({ level: 'debug' }),
|
|
873
1585
|
})
|
|
874
1586
|
```
|
|
875
|
-
This will enable you to see all sorts of messages WhatsApp sends in the console.
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
1587
|
+
This will enable you to see all sorts of messages WhatsApp sends in the console.
|
|
1588
|
+
|
|
1589
|
+
### How Whatsapp Communicate With Us
|
|
1590
|
+
|
|
1591
|
+
> [!TIP]
|
|
1592
|
+
> If you want to learn whatsapp protocol, we recommend to study about Libsignal Protocol and Noise Protocol
|
|
1593
|
+
|
|
1594
|
+
- **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:
|
|
1595
|
+
```
|
|
1596
|
+
{
|
|
1597
|
+
"level": 10,
|
|
1598
|
+
"fromMe": false,
|
|
1599
|
+
"frame": {
|
|
1600
|
+
"tag": "ib",
|
|
1601
|
+
"attrs": {
|
|
1602
|
+
"from": "@s.whatsapp.net"
|
|
1603
|
+
},
|
|
1604
|
+
"content": [
|
|
1605
|
+
{
|
|
1606
|
+
"tag": "edge_routing",
|
|
1607
|
+
"attrs": {},
|
|
1608
|
+
"content": [
|
|
1609
|
+
{
|
|
1610
|
+
"tag": "routing_info",
|
|
1611
|
+
"attrs": {},
|
|
1612
|
+
"content": {
|
|
1613
|
+
"type": "Buffer",
|
|
1614
|
+
"data": [8,2,8,5]
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
]
|
|
1618
|
+
}
|
|
1619
|
+
]
|
|
1620
|
+
},
|
|
1621
|
+
"msg":"communication"
|
|
1622
|
+
}
|
|
897
1623
|
```
|
|
898
|
-
Also, this repo is now licenced under GPL 3 since it uses [libsignal-node](https://git.questbook.io/backend/service-coderunner/-/merge_requests/1)
|
|
899
1624
|
|
|
900
|
-
|
|
1625
|
+
The `'frame'` is what the message received is, it has three components:
|
|
1626
|
+
- `tag` -- what this frame is about (eg. message will have 'message')
|
|
1627
|
+
- `attrs` -- a string key-value pair with some metadata (contains ID of the message usually)
|
|
1628
|
+
- `content` -- the actual data (eg. a message node will have the actual message content in it)
|
|
1629
|
+
- read more about this format [here](/src/WABinary/readme.md)
|
|
1630
|
+
|
|
1631
|
+
### Register a Callback for Websocket Events
|
|
1632
|
+
|
|
1633
|
+
> [!TIP]
|
|
1634
|
+
> Recommended to see `onMessageReceived` function in `socket.ts` file to understand how websockets events are fired
|
|
1635
|
+
|
|
1636
|
+
```javascript
|
|
1637
|
+
// for any message with tag 'edge_routing'
|
|
1638
|
+
sock.ws.on('CB:edge_routing', (node: BinaryNode) => { })
|
|
1639
|
+
|
|
1640
|
+
// for any message with tag 'edge_routing' and id attribute = abcd
|
|
1641
|
+
sock.ws.on('CB:edge_routing,id:abcd', (node: BinaryNode) => { })
|
|
1642
|
+
|
|
1643
|
+
// for any message with tag 'edge_routing', id attribute = abcd & first content node routing_info
|
|
1644
|
+
sock.ws.on('CB:edge_routing,id:abcd,routing_info', (node: BinaryNode) => { })
|
|
1645
|
+
```
|