mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
69 lines
3.3 KiB
Markdown
69 lines
3.3 KiB
Markdown
# Baileys
|
|
Reverse Engineered WhatsApp Web API in Node.js. Baileys does not require Selenium or any other browser to be interface with WhatsApp Web, it does so directly using WebSockets.
|
|
|
|
Thank you to [Sigalor](https://github.com/sigalor/whatsapp-web-reveng) for writing the guide reverse engineering WhatsApp Web and to the go reimplementation written by [Rhymen](https://github.com/Rhymen/go-whatsapp/tree/484cfe758705761d76724e01839d6fc473dc10c4)
|
|
|
|
Baileys is super easy to use:
|
|
1. Import using
|
|
``` javascript
|
|
const WhatsAppWeb = require('Baileys')
|
|
```
|
|
2. Create an instance of Baileys & connect using
|
|
``` javascript
|
|
let client = new WhatsAppWeb()
|
|
client.connect()
|
|
```
|
|
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!
|
|
3. Implement the following event handlers in your code:
|
|
1. ``` javascript
|
|
client.handlers.onConnected = () => { /* when you're successfully authenticated with the WhatsApp Web servers */ }
|
|
```
|
|
2. ``` javascript
|
|
client.handlers.onUnreadMessage = (message) => { /* called when you have a pending unread message or recieve a new message */ }
|
|
```
|
|
3. ``` javascript
|
|
client.handlers.onError = (error) => { /* called when there was an error */ }
|
|
```
|
|
4. ``` javascript
|
|
client.handlers.onDisconnect = () => { /* called when internet gets disconnected */ }
|
|
```
|
|
4. Send a text message using
|
|
``` javascript
|
|
client.sendTextMessage(id, txtMessage)
|
|
```
|
|
The id is the phone number of the person the message is being sent to, it must be in the format '[country code][phone number]@s.whatsapp.net', for example '+19999999999@s.whatsapp.net'
|
|
5. Send a read reciept using
|
|
``` javascript
|
|
client.sendReadReceipt(id, messageID)
|
|
```
|
|
The id is in the same format as mentioned earlier. The message ID is the unique identifier of the message that you are marking as read
|
|
6. Tell someone what your status is right now by using
|
|
``` javascript
|
|
client.updatePresence(id, presence)
|
|
```
|
|
Presence can be one of the following:
|
|
``` javascript
|
|
static Presence = {
|
|
available: "available", // "online"
|
|
unavailable: "unavailable", // offline
|
|
composing: "composing", // "typing..."
|
|
recording: "recording", // "recording..."
|
|
paused: "paused" // I have no clue
|
|
}
|
|
```
|
|
7. Once you want to close your session, you can get your authentication credentials using:
|
|
``` javascript
|
|
const authJSON = client.base64EncodedAuthInfo()
|
|
```
|
|
and then save this JSON to a file
|
|
8. If you want to restore your session (i.e. log back in without having to scan the QR code), simply retreive your previously saved credentials and use
|
|
``` javascript
|
|
const authJSON = JSON.parse( fs.readFileSync("auth_info.json") )
|
|
client.login( authJSON )
|
|
```
|
|
This will use the credentials to connect & log back in. No need to call ``` connect() ``` after calling this function
|
|
|
|
Do check out test.js to see example usage of all these functions.
|
|
|
|
# Note
|
|
I am in no way affiliated with WhatsApp. This was written for educational purposes. Use at your own discretion. |