Update README.md

This commit is contained in:
Adhiraj Singh
2020-06-13 12:57:25 +05:30
parent 5aedc028e0
commit b023546210

View File

@@ -14,7 +14,6 @@
const WhatsAppWeb = require('baileys') const WhatsAppWeb = require('baileys')
``` ```
## Connecting ## Connecting
``` javascript ``` javascript
const client = new WhatsAppWeb() const client = new WhatsAppWeb()
client.connect() client.connect()
@@ -28,6 +27,7 @@
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! 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!
If you don't want to wait for WhatsApp to send all your chats while connecting, you can use the following function: If you don't want to wait for WhatsApp to send all your chats while connecting, you can use the following function:
``` javascript ``` javascript
const client = new WhatsAppWeb() const client = new WhatsAppWeb()
client.connectSlim() // does not wait for chats & contacts client.connectSlim() // does not wait for chats & contacts
@@ -43,7 +43,6 @@
.catch (err => console.log("unexpected error: " + err)) .catch (err => console.log("unexpected error: " + err))
``` ```
## Handling Events ## Handling Events
Implement the following callbacks in your code: Implement the following callbacks in your code:
- Called when you have a pending unread message or recieve a new message - Called when you have a pending unread message or recieve a new message
@@ -73,10 +72,34 @@
client.setOnUnexpectedDisconnect (err => console.log ("disconnected unexpectedly: " + err) ) client.setOnUnexpectedDisconnect (err => console.log ("disconnected unexpectedly: " + err) )
``` ```
## Sending Messages ## Sending Messages
It's super simple It's super simple
- Send text messages using - Called when you have a pending unread message or recieve a new message
``` javascript
client.setOnUnreadMessage (m => {
const [notificationType, messageType] = client.getNotificationType(m) // get what type of notification it is -- message, group add notification etc.
console.log("got notification of type: " + notificationType) // message, groupAdd, groupLeave
console.log("message type: " + messageType) // conversation, imageMessage, videoMessage, contactMessage etc.
}, false) // set to `true` if you want to receive outgoing messages that may be sent from your phone
```
- Called when you recieve an update on someone's presence, they went offline or online
``` javascript
client.setOnPresenceUpdate (json => console.log(json.id + " presence is " + json.type))
```
- Called when your message gets delivered or read
``` javascript
client.setOnMessageStatusChange (json => {
let sent = json.to
if (json.participant) // participant exists when the message is from a group
sent += " ("+json.participant+")" // mention as the one sent to
// log that they acknowledged the message
console.log(sent + " acknowledged message(s) " + json.ids + " as " + json.type + " at " + json.timestamp)
})
```
- Called when the connection gets disconnected (either the server loses internet or the phone gets unpaired)
``` javascript
client.setOnUnexpectedDisconnect (err => console.log ("disconnected unexpectedly: " + err) )
```d text messages using
``` javascript ``` javascript
client.sendTextMessage(id, "oh hello there!") client.sendTextMessage(id, "oh hello there!")
``` ```
@@ -85,7 +108,35 @@
const options = {quoted: quotedMessage} const options = {quoted: quotedMessage}
client.sendTextMessage(id, "oh hello there", options) client.sendTextMessage(id, "oh hello there", options)
``` ```
``` quotedMessage ``` is a message object `Implement the following callbacks in your code:
- Called when you have a pending unread message or recieve a new message
``` javascript
client.setOnUnreadMessage (m => {
const [notificationType, messageType] = client.getNotificationType(m) // get what type of notification it is -- message, group add notification etc.
console.log("got notification of type: " + notificationType) // message, groupAdd, groupLeave
console.log("message type: " + messageType) // conversation, imageMessage, videoMessage, contactMessage etc.
}, false) // set to `true` if you want to receive outgoing messages that may be sent from your phone
```
- Called when you recieve an update on someone's presence, they went offline or online
``` javascript
client.setOnPresenceUpdate (json => console.log(json.id + " presence is " + json.type))
```
- Called when your message gets delivered or read
``` javascript
client.setOnMessageStatusChange (json => {
let sent = json.to
if (json.participant) // participant exists when the message is from a group
sent += " ("+json.participant+")" // mention as the one sent to
// log that they acknowledged the message
console.log(sent + " acknowledged message(s) " + json.ids + " as " + json.type + " at " + json.timestamp)
})
```
- Called when the connection gets disconnected (either the server loses internet or the phone gets unpaired)
``` javascript
client.setOnUnexpectedDisconnect (err => console.log ("disconnected unexpectedly: " + err) )
```
` quotedMessage ` is a message object
- Send a location using - Send a location using
``` javascript ``` javascript
client.sendLocationMessage(id, 24.121231, 55.1121221) // the latitude, longitude of the location client.sendLocationMessage(id, 24.121231, 55.1121221) // the latitude, longitude of the location
@@ -119,7 +170,7 @@
] ]
``` ```
- Tested formats: png, jpeg, webp (sticker), mp4, ogg - Tested formats: png, jpeg, webp (sticker), mp4, ogg
```options``` is a JSON object, providing some information about the message. It can have the following __optional__ values: `options` is a JSON object, providing some information about the message. It can have the following __optional__ values:
``` javascript ``` javascript
info = { info = {
caption: "hello there!", // (for media messages) the caption to send with the media (cannot be sent with stickers though) caption: "hello there!", // (for media messages) the caption to send with the media (cannot be sent with stickers though)
@@ -137,13 +188,17 @@
timestamp: Date() // optional, if you want to manually set the timestamp of the message timestamp: Date() // optional, if you want to manually set the timestamp of the message
} }
``` ```
``` id ``` is the WhatsApp id of the person or group you're sending the message to. ``` id ``` is the WhatsApp id of the person or group you're sending the message to.
It must be in the format ```[country code][phone number]@s.whatsapp.net```, for example ```+19999999999@s.whatsapp.net``` for people. For groups, it must be in the format ``` 123456789-123345@g.us ```. **Do not attach** `@c.us` for individual people IDs, It won't work It must be in the format ```[country code][phone number]@s.whatsapp.net```, for example ```+19999999999@s.whatsapp.net``` for people. For groups, it must be in the format ``` 123456789-123345@g.us ```. **Do not attach** `@c.us` for individual people IDs, It won't work
## Sending Read Receipts ## Sending Read Receipts
``` javascript ``` javascript
client.sendReadReceipt(id, messageID) 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. On a message object, it can be accessed using ```messageID = message.key.id```. 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. On a message object, it can be accessed using ```messageID = message.key.id```.
## Update Presence ## Update Presence
``` javascript ``` javascript
client.updatePresence(id, WhatsAppWeb.Presence.available) client.updatePresence(id, WhatsAppWeb.Presence.available)
@@ -157,6 +212,7 @@
WhatsAppWeb.Presence.recording // recording... WhatsAppWeb.Presence.recording // recording...
] ]
``` ```
## Decoding Media ## Decoding Media
If you want to save & process some images, videos, documents or stickers you received If you want to save & process some images, videos, documents or stickers you received
``` javascript ``` javascript
@@ -170,6 +226,7 @@
} }
} }
``` ```
## Restoring Sessions ## Restoring Sessions
Once you connect successfully, you can get your authentication credentials using Once you connect successfully, you can get your authentication credentials using
``` javascript ``` javascript
@@ -181,6 +238,7 @@
client.connect (authJSON) client.connect (authJSON)
.then (([user, chats, contacts, unread]) => console.log ("yay connected")) .then (([user, chats, contacts, unread]) => console.log ("yay connected"))
``` ```
## Querying ## Querying
- To check if a given ID is on WhatsApp - To check if a given ID is on WhatsApp
``` javascript ``` javascript
@@ -215,6 +273,7 @@
``` ```
Of course, replace ``` xyz ``` with an actual ID. Also, append ``` @c.us ``` for individuals & ``` @g.us ``` for groups. Of course, replace ``` xyz ``` with an actual ID. Also, append ``` @c.us ``` for individuals & ``` @g.us ``` for groups.
## Groups ## Groups
- To query the metadata of a group - To query the metadata of a group
``` javascript ``` javascript
@@ -249,8 +308,9 @@
client.groupInviteCode ("abcd-xyz@g.us") client.groupInviteCode ("abcd-xyz@g.us")
.then (code => console.log(code)) .then (code => console.log(code))
``` ```
## Writing Custom Functionality ## Writing Custom Functionality
Baileys is also written, keeping in mind, that you may require other functionality. Hence, instead of having to fork the project & re-write the internals, you can simply write extensions in your own code. Baileys is written, keeping in mind, that you may require other custom functionality. Hence, instead of having to fork the project & re-write the internals, you can simply write extensions in your own code.
First, enable the logging of unhandled messages from WhatsApp by setting First, enable the logging of unhandled messages from WhatsApp by setting
``` javascript ``` javascript