mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Move msgCount increment to generateMessageTag
Prevents duplicate tags from being sent
This commit is contained in:
@@ -15,7 +15,7 @@ import * as fs from 'fs'
|
|||||||
async function example() {
|
async function example() {
|
||||||
const conn = new WAConnection() // instantiate
|
const conn = new WAConnection() // instantiate
|
||||||
conn.autoReconnect = ReconnectMode.onConnectionLost // only automatically reconnect when the connection breaks
|
conn.autoReconnect = ReconnectMode.onConnectionLost // only automatically reconnect when the connection breaks
|
||||||
conn.logger.level = 'trace' // set to 'debug' to see what kind of stuff you can implement
|
conn.logger.level = 'debug' // set to 'debug' to see what kind of stuff you can implement
|
||||||
// attempt to reconnect at most 10 times in a row
|
// attempt to reconnect at most 10 times in a row
|
||||||
conn.connectOptions.maxRetries = 10
|
conn.connectOptions.maxRetries = 10
|
||||||
conn.chatOrderingKey = waChatKey(true) // order chats such that pinned chats are on top
|
conn.chatOrderingKey = waChatKey(true) // order chats such that pinned chats are on top
|
||||||
@@ -38,7 +38,6 @@ 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
|
||||||
|
|
||||||
console.log('oh hello ' + conn.user.name + ' (' + conn.user.jid + ')')
|
console.log('oh hello ' + conn.user.name + ' (' + conn.user.jid + ')')
|
||||||
|
|
||||||
// uncomment to load all unread messages
|
// uncomment to load all unread messages
|
||||||
//const unread = await conn.loadAllUnreadMessages ()
|
//const unread = await conn.loadAllUnreadMessages ()
|
||||||
//console.log ('you have ' + unread.length + ' unread messages')
|
//console.log ('you have ' + unread.length + ' unread messages')
|
||||||
|
|||||||
@@ -75,6 +75,26 @@ WAConnectionTest('Misc', conn => {
|
|||||||
it('should return the stories', async () => {
|
it('should return the stories', async () => {
|
||||||
await conn.getStories()
|
await conn.getStories()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should return the profile picture correctly', async () => {
|
||||||
|
// wait for chats
|
||||||
|
await new Promise(resolve => (
|
||||||
|
conn.once('initial-data-received', resolve)
|
||||||
|
))
|
||||||
|
const pictures = await Promise.all(
|
||||||
|
conn.chats.all().slice(0, 15).map(({ jid }) => (
|
||||||
|
conn.getProfilePicture(jid)
|
||||||
|
.catch(err => '')
|
||||||
|
))
|
||||||
|
)
|
||||||
|
// pictures should return correctly
|
||||||
|
const truePictures = pictures.filter(pp => !!pp)
|
||||||
|
assert.strictEqual(
|
||||||
|
new Set(truePictures).size,
|
||||||
|
truePictures.length
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
it('should change the profile picture', async () => {
|
it('should change the profile picture', async () => {
|
||||||
await delay (5000)
|
await delay (5000)
|
||||||
|
|
||||||
@@ -88,11 +108,6 @@ WAConnectionTest('Misc', conn => {
|
|||||||
|
|
||||||
await conn.updateProfilePicture (conn.user.jid, oldPP) // revert back
|
await conn.updateProfilePicture (conn.user.jid, oldPP) // revert back
|
||||||
})
|
})
|
||||||
it('should return the profile picture', async () => {
|
|
||||||
const response = await conn.getProfilePicture(testJid)
|
|
||||||
assert.ok(response)
|
|
||||||
assert.rejects(conn.getProfilePicture('abcd@s.whatsapp.net'))
|
|
||||||
})
|
|
||||||
it('should send typing indicator', async () => {
|
it('should send typing indicator', async () => {
|
||||||
const response = await conn.updatePresence(testJid, Presence.composing)
|
const response = await conn.updatePresence(testJid, Presence.composing)
|
||||||
assert.ok(response)
|
assert.ok(response)
|
||||||
|
|||||||
@@ -223,17 +223,17 @@ export class WAConnection extends EventEmitter {
|
|||||||
requiresPhoneConnection = requiresPhoneConnection !== false
|
requiresPhoneConnection = requiresPhoneConnection !== false
|
||||||
waitForOpen = waitForOpen !== false
|
waitForOpen = waitForOpen !== false
|
||||||
let triesLeft = maxRetries || 2
|
let triesLeft = maxRetries || 2
|
||||||
tag = tag || this.generateMessageTag (longTag)
|
tag = tag || this.generateMessageTag(longTag)
|
||||||
|
|
||||||
while (triesLeft >= 0) {
|
while (triesLeft >= 0) {
|
||||||
if (waitForOpen) await this.waitForConnection()
|
if (waitForOpen) await this.waitForConnection()
|
||||||
|
|
||||||
const promise = this.waitForMessage(tag, requiresPhoneConnection, timeoutMs)
|
const promise = this.waitForMessage(tag, requiresPhoneConnection, timeoutMs)
|
||||||
|
|
||||||
if (this.logger.level === 'trace') {
|
if (this.logger.level === 'trace') {
|
||||||
this.logger.trace ({ fromMe: true },`${tag},${JSON.stringify(json)}`)
|
this.logger.trace ({ fromMe: true },`${tag},${JSON.stringify(json)}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (binaryTags) tag = await this.sendBinary(json as WANode, binaryTags, tag)
|
if (binaryTags) tag = await this.sendBinary(json as WANode, binaryTags, tag)
|
||||||
else tag = await this.sendJSON(json, tag)
|
else tag = await this.sendJSON(json, tag)
|
||||||
|
|
||||||
@@ -369,7 +369,6 @@ export class WAConnection extends EventEmitter {
|
|||||||
}
|
}
|
||||||
/** Send some message to the WhatsApp servers */
|
/** Send some message to the WhatsApp servers */
|
||||||
protected async send(m) {
|
protected async send(m) {
|
||||||
this.msgCount += 1 // increment message count, it makes the 'epoch' field when sending binary messages
|
|
||||||
this.conn.send(m)
|
this.conn.send(m)
|
||||||
}
|
}
|
||||||
protected async waitForConnection () {
|
protected async waitForConnection () {
|
||||||
@@ -477,6 +476,8 @@ export class WAConnection extends EventEmitter {
|
|||||||
)
|
)
|
||||||
generateMessageTag (longTag: boolean = false) {
|
generateMessageTag (longTag: boolean = false) {
|
||||||
const seconds = Utils.unixTimestampSeconds(this.referenceDate)
|
const seconds = Utils.unixTimestampSeconds(this.referenceDate)
|
||||||
return `${longTag ? seconds : (seconds%1000)}.--${this.msgCount}`
|
const tag = `${longTag ? seconds : (seconds%1000)}.--${this.msgCount}`
|
||||||
|
this.msgCount += 1 // increment message count, it makes the 'epoch' field when sending binary messages
|
||||||
|
return tag
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ export class WAConnection extends Base {
|
|||||||
}
|
}
|
||||||
const canLogin = this.canLogin()
|
const canLogin = this.canLogin()
|
||||||
this.referenceDate = new Date () // refresh reference date
|
this.referenceDate = new Date () // refresh reference date
|
||||||
let isNewUser = false
|
|
||||||
|
|
||||||
this.startDebouncedTimeout ()
|
this.startDebouncedTimeout ()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user