Takeover conflict implementation

This commit is contained in:
Adhiraj
2020-07-10 12:44:31 +05:30
parent 2dad372e75
commit 2a7d179822
4 changed files with 29 additions and 6 deletions

View File

@@ -27,7 +27,10 @@ async function example() {
fs.writeFileSync('./auth_info.json', JSON.stringify(authInfo, null, '\t')) // save this info to a file
/* Note: one can take this auth_info.json file and login again from any computer without having to scan the QR code,
and get full access to one's WhatsApp. Despite the convenience, be careful with this file */
client.setOnTakenOver (async () => {
// uncomment to reconnect whenever the connection gets taken over from somewhere else
// await client.connect ()
})
client.setOnPresenceUpdate(json => console.log(json.id + ' presence is ' + json.type))
client.setOnMessageStatusChange(json => {
const participant = json.participant ? ' (' + json.participant + ')' : '' // participant exists when the message is from a group

View File

@@ -104,6 +104,7 @@ client.connectSlim(null, 20*1000) // use loaded credentials & timeout in 20s
See the browser credentials type [here](/src/WAConnection/Constants.ts).
## Handling Events
Implement the following callbacks in your code:
- Called when you have a pending unread message or recieve a new message
@@ -136,6 +137,13 @@ Implement the following callbacks in your code:
``` ts
client.setOnUnexpectedDisconnect (err => console.log ("disconnected unexpectedly: " + err) )
```
- Called when you log into WhatsApp Web somewhere else
``` ts
client.setOnTakenOver (async () => {
// reconnect to gain connection back here
await client.connect ()
})
```
## Sending Messages
Send like, all types of messages with a single function:
@@ -183,16 +191,20 @@ To note:
}
```
## Sending Read Receipts
## Reading Messages
``` ts
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
await client.markChatUnread(id) // to mark chat as unread
await client.sendReadReceipt(id, messageID) // mark as read
await client.sendReadReceipt (id) // mark all messages in chat as read
await client.sendReadReceipt(id, null, 'unread') // mark the chat as unread
```
`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`, the `messageID` can be accessed using ```messageID = message.key.id```.
## Update Presence
``` ts

View File

@@ -13,6 +13,14 @@ import {
import { generateMessageTag } from '../WAConnection/Utils'
export default class WhatsAppWebBase extends WAConnection {
/** Set the callback for when the connection is taken over somewhere else */
setOnTakenOver(callback: (kind: 'replaced' | string | null) => void) {
this.registerCallback (['Cmd', 'type:disconnect'], json => {
this.log ('connection taken over elsewhere')
this.close ()
callback (json[1].kind)
})
}
/** Set the callback for unexpected disconnects */
setOnUnexpectedDisconnect(callback: (error: Error) => void) {
this.unexpectedDisconnect = (err) => {

View File

@@ -25,7 +25,7 @@ export default class WAConnectionBase {
/** The version of WhatsApp Web we're telling the servers we are */
version: [number, number, number] = [2, 2027, 10]
/** The Browser we're telling the WhatsApp Web servers we are */
browserDescription: [string, string, string] = Browsers.baileys ('Baileys')
browserDescription: [string, string, string] = Browsers.baileys ('Chrome')
/** Metadata like WhatsApp id, name set on WhatsApp etc. */
userMetaData: UserMetaData = { id: null, name: null, phone: null }
/** Should reconnect automatically after an unexpected disconnect */