mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Update README.md
This commit is contained in:
74
README.md
74
README.md
@@ -14,7 +14,6 @@
|
||||
const WhatsAppWeb = require('baileys')
|
||||
```
|
||||
## Connecting
|
||||
|
||||
``` javascript
|
||||
const client = new WhatsAppWeb()
|
||||
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 you don't want to wait for WhatsApp to send all your chats while connecting, you can use the following function:
|
||||
|
||||
``` javascript
|
||||
const client = new WhatsAppWeb()
|
||||
client.connectSlim() // does not wait for chats & contacts
|
||||
@@ -43,7 +43,6 @@
|
||||
.catch (err => console.log("unexpected error: " + err))
|
||||
```
|
||||
## Handling Events
|
||||
|
||||
Implement the following callbacks in your code:
|
||||
|
||||
- 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) )
|
||||
```
|
||||
## Sending Messages
|
||||
|
||||
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
|
||||
client.sendTextMessage(id, "oh hello there!")
|
||||
```
|
||||
@@ -85,7 +108,35 @@
|
||||
const options = {quoted: quotedMessage}
|
||||
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
|
||||
``` javascript
|
||||
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
|
||||
```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
|
||||
info = {
|
||||
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
|
||||
}
|
||||
```
|
||||
|
||||
``` 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
|
||||
|
||||
## Sending Read Receipts
|
||||
``` 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. On a message object, it can be accessed using ```messageID = message.key.id```.
|
||||
|
||||
## Update Presence
|
||||
``` javascript
|
||||
client.updatePresence(id, WhatsAppWeb.Presence.available)
|
||||
@@ -157,6 +212,7 @@
|
||||
WhatsAppWeb.Presence.recording // recording...
|
||||
]
|
||||
```
|
||||
|
||||
## Decoding Media
|
||||
If you want to save & process some images, videos, documents or stickers you received
|
||||
``` javascript
|
||||
@@ -170,6 +226,7 @@
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Restoring Sessions
|
||||
Once you connect successfully, you can get your authentication credentials using
|
||||
``` javascript
|
||||
@@ -181,6 +238,7 @@
|
||||
client.connect (authJSON)
|
||||
.then (([user, chats, contacts, unread]) => console.log ("yay connected"))
|
||||
```
|
||||
|
||||
## Querying
|
||||
- To check if a given ID is on WhatsApp
|
||||
``` javascript
|
||||
@@ -215,6 +273,7 @@
|
||||
```
|
||||
|
||||
Of course, replace ``` xyz ``` with an actual ID. Also, append ``` @c.us ``` for individuals & ``` @g.us ``` for groups.
|
||||
|
||||
## Groups
|
||||
- To query the metadata of a group
|
||||
``` javascript
|
||||
@@ -249,8 +308,9 @@
|
||||
client.groupInviteCode ("abcd-xyz@g.us")
|
||||
.then (code => console.log(code))
|
||||
```
|
||||
|
||||
## 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
|
||||
``` javascript
|
||||
|
||||
Reference in New Issue
Block a user