added media send/recieve, extended text messages send/recieve

This commit is contained in:
Adhiraj
2020-04-16 16:20:48 +05:30
parent 6629c08c70
commit 9ea4b0b381
13 changed files with 987 additions and 61 deletions

View File

@@ -1,22 +1,23 @@
# 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.
Reverse Engineered WhatsApp Web API in pure Node.js. Baileys does not require Selenium or any other browser to be interface with WhatsApp Web, it does so directly using a WebSocket.
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)
Thank you to [Sigalor](https://github.com/sigalor/whatsapp-web-reveng) for writing the guide to reverse engineering WhatsApp Web and thanks to [Rhymen](https://github.com/Rhymen/go-whatsapp/tree/484cfe758705761d76724e01839d6fc473dc10c4) for the __go__ reimplementation.
Baileys is super easy to use:
1. Install from npm using
* Install from npm using
``` npm install github:adiwajshing/Baileys ```
2. Then import in your code using
* Then import in your code using
``` javascript
const WhatsAppWeb = require('Baileys')
```
3. Create an instance of Baileys & connect using
* 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!
4. Implement the following event handlers in your code:
* Implement the following event handlers in your code:
``` javascript
client.handlers.onConnected = () => { /* when you're successfully authenticated with the WhatsApp Web servers */ }
```
@@ -29,21 +30,77 @@ Baileys is super easy to use:
``` javascript
client.handlers.onDisconnect = () => { /* called when internet gets disconnected */ }
```
5. Send a text message using
* Get the type of message using
``` javascript
client.handlers.onUnreadMessage = (m) => {
const messageType = client.getMessageType(m.message) // get what type of message it is -- text, image, video
}
```
* Decode a media message using
``` javascript
client.handlers.onUnreadMessage = (m) => {
const messageType = client.getMessageType(m.message) // get what type of message it is -- text, image, video
// if the message is not a text message
if (messageType !== WhatsAppWeb.MessageType.text && messageType !== WhatsAppWeb.MessageType.extendedText) {
client.decodeMediaMessage(m.message, "filename") // extension applied automatically
.then (meta => console.log(m.key.remoteJid + " sent media, saved at: " + meta.fileName))
.catch (err => console.log("error in decoding message: " + err))
}
}
```
* Send a text message using
``` javascript
client.sendTextMessage(id, txtMessage)
client.sendTextMessage(id, txtMessage)
```
Or if you want to quote another message:
``` javascript
client.sendTextMessage(id, txtMessage, quotedMessage)
```
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'
6. Send a read reciept using
* Send a media (image, video, sticker, pdf) message using
``` javascript
client.sendMediaMessage(id, mediaBuffer, mediaType, info)
```
- The thumbnail can be generated automatically for images & stickers.
- ```mediaBuffer``` is just a Buffer containing the contents of the media you want to send
- ```mediaType``` represents the type of message you are sending. This can be one of the following:
``` javascript
[
WhatsAppWeb.MessageType.image, // an image message
WhatsAppWeb.MessageType.video, // a video message
WhatsAppWeb.MessageType.audio, // an audio message
WhatsAppWeb.MessageType.sticker // a sticker message
]
```
- ```info``` is a JSON object, providing some information about the media. It can have the following __optional__ values:
``` javascript
info = {
caption: "hello there!", // the caption to send with the media (cannot be sent with stickers though)
thumbnail: null, /* has to be a base 64 encoded JPEG if you want to send a custom thumb,
or set to null if you don't want to send a thumbnail.
Do not enter this field if you want to automatically generate a thumb
*/
mimetype: "application/pdf", /* specify the type of media (optional for all media types except documents),
for pdf files => set to "application/pdf",
for txt files => set to "application/txt"
etc.
*/
gif: true // only applicable to video messages, if the video should be treated as a GIF
}
```
- Tested formats: png, jpeg, webp (sticker), mp4, ogg
- To automatically generate thumbnails for videos, you need to have ``` ffmpeg ``` installed on your system
* 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
7. Tell someone what your status is right now by using
* Update your status by using
``` javascript
client.updatePresence(id, presence)
```
Presence can be one of the following:
This lets the person with ``` id ``` know your status. where ``` presence ``` can be one of the following:
``` javascript
static Presence = {
available: "available", // "online"
@@ -53,20 +110,22 @@ Baileys is super easy to use:
paused: "paused" // I have no clue
}
```
8. Once you want to close your session, you can get your authentication credentials using:
* 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
9. 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
* 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 )
client.login(authJSON)
```
This will use the credentials to connect & log back in. No need to call ``` connect() ``` after calling this function
10. If you want to query whether a number is registered on WhatsApp, use:
* If you want to query whether a number is registered on WhatsApp, use:
``` javascript
client.isOnWhatsApp ("[countrycode][some10digitnumber]@s.whatsapp.net", (exists, id) => {
client.isOnWhatsApp ("[countrycode][some10digitnumber]@s.whatsapp.net")
.then ((exists, id) => {
if (exists) {
console.log(id + " is on WhatsApp")
} else {
@@ -74,10 +133,13 @@ Baileys is super easy to use:
}
})
```
Of course, replace ``` [countrycode][some10digitnumber] ``` with an actual country code & number
Of course, replace ``` [countrycode][some10digitnumber] ``` with an actual country code & number. ``` isOnWhatsApp ``` returns a promise.
Do check out test.js to see example usage of all these functions.
Do check out & run [example.js](example/example.js) to see example usage of all these functions.
To run the example script, download or clone the repo and then type the following in terminal:
1. ``` cd path/to/Baileys/example ```
2. ``` node example.js ```
# Note
I am in no way affiliated with WhatsApp. This was written for educational purposes. Use at your own discretion.