Update README.md

This commit is contained in:
Adhiraj Singh
2020-07-08 12:44:25 +05:30
parent b9df538764
commit 9907f54bcd

161
README.md
View File

@@ -29,38 +29,38 @@ Create and cd to your NPM project directory and then in terminal, write:
Then import in your code using: Then import in your code using:
``` ts ``` ts
import { WAClient } from '@adiwajshing/baileys' import { WAClient } from '@adiwajshing/baileys'
``` ```
## Connecting ## Connecting
``` ts ``` ts
const client = new WAClient() const client = new WAClient()
client.connect() client.connect()
.then (([user, chats, contacts, unread]) => { .then (([user, chats, contacts, unread]) => {
console.log ("oh hello " + user.name + " (" + user.id + ")") console.log ("oh hello " + user.name + " (" + user.id + ")")
console.log ("you have " + unread.length + " unread messages") console.log ("you have " + unread.length + " unread messages")
console.log ("you have " + chats.length + " chats") console.log ("you have " + chats.length + " chats")
}) })
.catch (err => console.log("unexpected error: " + err) ) .catch (err => console.log("unexpected error: " + err) )
``` ```
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:
``` ts ``` ts
const client = new WAClient() const client = new WAClient()
client.connectSlim() // does not wait for chats & contacts client.connectSlim() // does not wait for chats & contacts
.then (user => { .then (user => {
console.log ("oh hello " + user.name + " (" + user.id + ")") console.log ("oh hello " + user.name + " (" + user.id + ")")
client.receiveChatsAndContacts () // wait for chats & contacts in the background client.receiveChatsAndContacts () // wait for chats & contacts in the background
.then (([chats, contacts, unread]) => { .then (([chats, contacts, unread]) => {
console.log ("you have " + unread.length + " unread messages") console.log ("you have " + unread.length + " unread messages")
console.log ("you have " + chats.length + " chats") console.log ("you have " + chats.length + " chats")
})
}) })
.catch (err => console.log("unexpected error: " + err)) })
.catch (err => console.log("unexpected error: " + err))
``` ```
## Saving & Restoring Sessions ## Saving & Restoring Sessions
@@ -69,23 +69,23 @@ You obviously don't want to keep scanning the QR code every time you want to con
So, do the following the first time you connect: So, do the following the first time you connect:
``` ts ``` ts
import * as fs from 'fs' import * as fs from 'fs'
const client = new WAClient() const client = new WAClient()
client.connectSlim() // connect first client.connectSlim() // connect first
.then (user => { .then (user => {
const creds = client.base64EncodedAuthInfo () // contains all the keys you need to restore a session const creds = client.base64EncodedAuthInfo () // contains all the keys you need to restore a session
fs.writeFileSync('./auth_info.json', JSON.stringify(creds, null, '\t')) // save JSON to file fs.writeFileSync('./auth_info.json', JSON.stringify(creds, null, '\t')) // save JSON to file
}) })
``` ```
Then, to restore a session: Then, to restore a session:
``` ts ``` ts
const client = new WAClient() const client = new WAClient()
client.connectSlim('./auth_info.json') // will load JSON credentials from file client.connectSlim('./auth_info.json') // will load JSON credentials from file
.then (user => { .then (user => {
// yay connected without scanning QR // yay connected without scanning QR
}) })
``` ```
Optionally, you can load the credentials yourself from somewhere & pass in the JSON object as well. Optionally, you can load the credentials yourself from somewhere & pass in the JSON object as well.
@@ -127,25 +127,25 @@ Implement the following callbacks in your code:
Send like, all types of messages with a single function: Send like, all types of messages with a single function:
``` ts ``` ts
import { MessageType, MessageOptions, Mimetype } from '@adiwajshing/baileys' import { MessageType, MessageOptions, Mimetype } from '@adiwajshing/baileys'
const id = 'abcd@s.whatsapp.net' // the WhatsApp ID const id = 'abcd@s.whatsapp.net' // the WhatsApp ID
// send a simple text! // send a simple text!
client.sendMessage (id, 'oh hello there', MessageType.text) client.sendMessage (id, 'oh hello there', MessageType.text)
// send a location! // send a location!
client.sendMessage(id, {degreeslatitude: 24.121231, degreesLongitude: 55.1121221}, MessageType.location) client.sendMessage(id, {degreeslatitude: 24.121231, degreesLongitude: 55.1121221}, MessageType.location)
// send a contact! // send a contact!
const vcard = 'BEGIN:VCARD\n' // metadata of the contact card const vcard = 'BEGIN:VCARD\n' // metadata of the contact card
+ 'VERSION:3.0\n' + 'VERSION:3.0\n'
+ 'FN:Jeff Singh\n' // full name + 'FN:Jeff Singh\n' // full name
+ 'ORG:Ashoka Uni;\n' // the organization of the contact + 'ORG:Ashoka Uni;\n' // the organization of the contact
+ 'TEL;type=CELL;type=VOICE;waid=911234567890:+91 12345 67890\n' // WhatsApp ID + phone number + 'TEL;type=CELL;type=VOICE;waid=911234567890:+91 12345 67890\n' // WhatsApp ID + phone number
+ 'END:VCARD' + 'END:VCARD'
client.sendMessage(id, {displayname: "Jeff", vcard: vcard}, MessageType.contact) client.sendMessage(id, {displayname: "Jeff", vcard: vcard}, MessageType.contact)
// send a gif // send a gif
const buffer = fs.readFileSync("Media/ma_gif.mp4") // load some gif const buffer = fs.readFileSync("Media/ma_gif.mp4") // load some gif
const options: MessageOptions = {mimetype: Mimetype.gif, caption: "hello!"} // some metadata & caption const options: MessageOptions = {mimetype: Mimetype.gif, caption: "hello!"} // some metadata & caption
client.sendMessage(id, buffer, MessageType.video, options) client.sendMessage(id, buffer, MessageType.video, options)
``` ```
To note: To note:
- `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.
@@ -154,7 +154,7 @@ To note:
- Please do not explicitly disable ID validation (in `MessageOptions`) because then your messages may fail for no apparent reason. - Please do not explicitly disable ID validation (in `MessageOptions`) because then your messages may fail for no apparent reason.
- For media messages, the thumbnail can be generated automatically for images & stickers. Thumbnails for videos can also be generated automatically, though, you need to have `ffmpeg` installed on your system. - For media messages, the thumbnail can be generated automatically for images & stickers. Thumbnails for videos can also be generated automatically, though, you need to have `ffmpeg` installed on your system.
- **MessageOptions**: some extra info about the message. It can have the following __optional__ values: - **MessageOptions**: some extra info about the message. It can have the following __optional__ values:
``` ts ``` ts
const info: MessageOptions = { const info: MessageOptions = {
quoted: quotedMessage, // the message you want to quote quoted: quotedMessage, // the message you want to quote
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
@@ -168,61 +168,58 @@ To note:
import {Mimetype} from '@adiwajshing/baileys' import {Mimetype} from '@adiwajshing/baileys'
*/ */
} }
``` ```
## Sending Read Receipts ## Sending Read Receipts
``` ts ``` ts
client.sendReadReceipt(id, messageID) const id = '1234-123@g.us'
``` const messageID = 'AHASHH123123AHGA' // id of the message you want to read
await client.sendReadReceipt(id, messageID) // mark as read
Also, to mark a chat unread: await client.markChatUnread(id) // to mark chat as unread
``` ts
client.markChatUnread(id)
``` ```
`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 `WAMessage`, it can be accessed using ```messageID = message.key.id```. `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 `WAMessage`, it can be accessed using ```messageID = message.key.id```.
## Update Presence ## Update Presence
``` ts ``` ts
client.updatePresence(id, WhatsAppWeb.Presence.available) client.updatePresence(id, WhatsAppWeb.Presence.available)
``` ```
This lets the person/group with ``` id ``` know whether you're online, offline, typing etc. where ``` presence ``` can be one of the following: This lets the person/group with ``` id ``` know whether you're online, offline, typing etc. where ``` presence ``` can be one of the following:
``` ts ``` ts
// call: import { Presence } from '@adiwajshing/baileys' // call: import { Presence } from '@adiwajshing/baileys'
export enum Presence { export enum Presence {
available = 'available', // "online" available = 'available', // "online"
unavailable = 'unavailable', // "offline" unavailable = 'unavailable', // "offline"
composing = 'composing', // "typing..." composing = 'composing', // "typing..."
recording = 'recording', // "recording..." recording = 'recording', // "recording..."
paused = 'paused', // I have no clue paused = 'paused', // I have no clue
} }
``` ```
## 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
``` ts ``` ts
import { getNotificationType, MessageType } from '@adiwajshing/baileys' import { getNotificationType, MessageType } from '@adiwajshing/baileys'
client.setOnUnreadMessage (false, async m => { client.setOnUnreadMessage (false, async m => {
const messageType = getNotificationType(m.message) // get what type of message it is -- text, image, video const messageType = getNotificationType(m.message) // get what type of message it is -- text, image, video
// if the message is not a text message // if the message is not a text message
if (messageType !== MessageType.text && messageType !== MessageType.extendedText) { if (messageType !== MessageType.text && messageType !== MessageType.extendedText) {
const savedFilename = await client.decodeMediaMessage(m.message, "filename") // extension applied automatically const savedFilename = await client.decodeMediaMessage(m.message, "filename") // extension applied automatically
console.log(m.key.remoteJid + " sent media, saved at: " + savedFilename) console.log(m.key.remoteJid + " sent media, saved at: " + savedFilename)
}
} }
}
``` ```
## Deleting Messages ## Deleting Messages
``` ts ``` ts
const jid = '1234@s.whatsapp.net' // can also be a group const jid = '1234@s.whatsapp.net' // can also be a group
const response = await client.sendMessage (jid, 'hello!', MessageType.text) // send a message const response = await client.sendMessage (jid, 'hello!', MessageType.text) // send a message
await client.deleteMessage (jid, {id: response.messageID, remoteJid: jid, fromMe: true}) // will delete the sent message! await client.deleteMessage (jid, {id: response.messageID, remoteJid: jid, fromMe: true}) // will delete the sent message!
```
You can also archive a chat using: // You can also archive a chat using:
``` ts await client.archiveChat(jid)
await client.archiveChat(jid)
``` ```
## Querying ## Querying