mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Takeover conflict implementation
This commit is contained in:
@@ -27,7 +27,10 @@ async function example() {
|
|||||||
fs.writeFileSync('./auth_info.json', JSON.stringify(authInfo, null, '\t')) // save this info to a file
|
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,
|
/* 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 */
|
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.setOnPresenceUpdate(json => console.log(json.id + ' presence is ' + json.type))
|
||||||
client.setOnMessageStatusChange(json => {
|
client.setOnMessageStatusChange(json => {
|
||||||
const participant = json.participant ? ' (' + json.participant + ')' : '' // participant exists when the message is from a group
|
const participant = json.participant ? ' (' + json.participant + ')' : '' // participant exists when the message is from a group
|
||||||
|
|||||||
20
README.md
20
README.md
@@ -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).
|
See the browser credentials type [here](/src/WAConnection/Constants.ts).
|
||||||
|
|
||||||
## 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
|
||||||
@@ -136,6 +137,13 @@ Implement the following callbacks in your code:
|
|||||||
``` ts
|
``` ts
|
||||||
client.setOnUnexpectedDisconnect (err => console.log ("disconnected unexpectedly: " + err) )
|
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
|
## Sending Messages
|
||||||
|
|
||||||
Send like, all types of messages with a single function:
|
Send like, all types of messages with a single function:
|
||||||
@@ -183,16 +191,20 @@ To note:
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Sending Read Receipts
|
## Reading Messages
|
||||||
``` ts
|
``` ts
|
||||||
const id = '1234-123@g.us'
|
const id = '1234-123@g.us'
|
||||||
const messageID = 'AHASHH123123AHGA' // id of the message you want to read
|
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
|
## Update Presence
|
||||||
``` ts
|
``` ts
|
||||||
|
|||||||
@@ -13,6 +13,14 @@ import {
|
|||||||
import { generateMessageTag } from '../WAConnection/Utils'
|
import { generateMessageTag } from '../WAConnection/Utils'
|
||||||
|
|
||||||
export default class WhatsAppWebBase extends WAConnection {
|
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 */
|
/** Set the callback for unexpected disconnects */
|
||||||
setOnUnexpectedDisconnect(callback: (error: Error) => void) {
|
setOnUnexpectedDisconnect(callback: (error: Error) => void) {
|
||||||
this.unexpectedDisconnect = (err) => {
|
this.unexpectedDisconnect = (err) => {
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export default class WAConnectionBase {
|
|||||||
/** The version of WhatsApp Web we're telling the servers we are */
|
/** The version of WhatsApp Web we're telling the servers we are */
|
||||||
version: [number, number, number] = [2, 2027, 10]
|
version: [number, number, number] = [2, 2027, 10]
|
||||||
/** The Browser we're telling the WhatsApp Web servers we are */
|
/** 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. */
|
/** Metadata like WhatsApp id, name set on WhatsApp etc. */
|
||||||
userMetaData: UserMetaData = { id: null, name: null, phone: null }
|
userMetaData: UserMetaData = { id: null, name: null, phone: null }
|
||||||
/** Should reconnect automatically after an unexpected disconnect */
|
/** Should reconnect automatically after an unexpected disconnect */
|
||||||
|
|||||||
Reference in New Issue
Block a user