mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Remove depracations + add chat ping loop
This commit is contained in:
@@ -46,7 +46,8 @@ export class WAConnection extends EventEmitter {
|
|||||||
maxRetries: 10,
|
maxRetries: 10,
|
||||||
connectCooldownMs: 4000,
|
connectCooldownMs: 4000,
|
||||||
phoneResponseTime: 15_000,
|
phoneResponseTime: 15_000,
|
||||||
alwaysUseTakeover: true
|
alwaysUseTakeover: true,
|
||||||
|
queryChatsTillReceived: true
|
||||||
}
|
}
|
||||||
/** When to auto-reconnect */
|
/** When to auto-reconnect */
|
||||||
autoReconnect = ReconnectMode.onConnectionLost
|
autoReconnect = ReconnectMode.onConnectionLost
|
||||||
@@ -62,28 +63,23 @@ export class WAConnection extends EventEmitter {
|
|||||||
messageLog: { tag: string, json: string, fromMe: boolean, binaryTags?: any[] }[] = []
|
messageLog: { tag: string, json: string, fromMe: boolean, binaryTags?: any[] }[] = []
|
||||||
|
|
||||||
maxCachedMessages = 50
|
maxCachedMessages = 50
|
||||||
/**
|
|
||||||
* @deprecated
|
|
||||||
* does not do anything
|
|
||||||
* */
|
|
||||||
loadProfilePicturesForChatsAutomatically = false
|
|
||||||
|
|
||||||
lastChatsReceived: Date
|
lastChatsReceived: Date
|
||||||
chats = new KeyedDB (Utils.waChatKey(false), value => value.jid)
|
chats = new KeyedDB (Utils.waChatKey(false), value => value.jid)
|
||||||
contacts: { [k: string]: WAContact } = {}
|
contacts: { [k: string]: WAContact } = {}
|
||||||
blocklist: string[] = [];
|
blocklist: string[] = []
|
||||||
|
|
||||||
/** Data structure of tokens & IDs used to establish one's identiy to WhatsApp Web */
|
/** Data structure of tokens & IDs used to establish one's identiy to WhatsApp Web */
|
||||||
protected authInfo: AuthenticationCredentials = null
|
protected authInfo: AuthenticationCredentials
|
||||||
/** Curve keys to initially authenticate */
|
/** Curve keys to initially authenticate */
|
||||||
protected curveKeys: { private: Uint8Array; public: Uint8Array }
|
protected curveKeys: { private: Uint8Array; public: Uint8Array }
|
||||||
/** The websocket connection */
|
/** The websocket connection */
|
||||||
protected conn: WS = null
|
protected conn: WS
|
||||||
protected msgCount = 0
|
protected msgCount = 0
|
||||||
protected keepAliveReq: NodeJS.Timeout
|
protected keepAliveReq: NodeJS.Timeout
|
||||||
protected encoder = new Encoder()
|
protected encoder = new Encoder()
|
||||||
protected decoder = new Decoder()
|
protected decoder = new Decoder()
|
||||||
protected phoneCheckInterval = undefined
|
protected phoneCheckInterval
|
||||||
protected phoneCheckListeners = 0
|
protected phoneCheckListeners = 0
|
||||||
|
|
||||||
protected referenceDate = new Date () // used for generating tags
|
protected referenceDate = new Date () // used for generating tags
|
||||||
@@ -99,6 +95,7 @@ export class WAConnection extends EventEmitter {
|
|||||||
() => this.state === 'connecting' && this.endConnection(DisconnectReason.timedOut)
|
() => this.state === 'connecting' && this.endConnection(DisconnectReason.timedOut)
|
||||||
)
|
)
|
||||||
protected messagesDebounceTimeout = Utils.debouncedTimeout(2000)
|
protected messagesDebounceTimeout = Utils.debouncedTimeout(2000)
|
||||||
|
protected chatsDebounceTimeout = Utils.debouncedTimeout(10_000)
|
||||||
/**
|
/**
|
||||||
* Connect to WhatsAppWeb
|
* Connect to WhatsAppWeb
|
||||||
* @param options the connect options
|
* @param options the connect options
|
||||||
@@ -432,6 +429,7 @@ export class WAConnection extends EventEmitter {
|
|||||||
this.initTimeout && clearTimeout (this.initTimeout)
|
this.initTimeout && clearTimeout (this.initTimeout)
|
||||||
this.connectionDebounceTimeout.cancel()
|
this.connectionDebounceTimeout.cancel()
|
||||||
this.messagesDebounceTimeout.cancel()
|
this.messagesDebounceTimeout.cancel()
|
||||||
|
this.chatsDebounceTimeout.cancel()
|
||||||
this.keepAliveReq && clearInterval(this.keepAliveReq)
|
this.keepAliveReq && clearInterval(this.keepAliveReq)
|
||||||
this.phoneCheckListeners = 0
|
this.phoneCheckListeners = 0
|
||||||
this.clearPhoneCheckInterval ()
|
this.clearPhoneCheckInterval ()
|
||||||
|
|||||||
@@ -107,12 +107,26 @@ export class WAConnection extends Base {
|
|||||||
*/
|
*/
|
||||||
sendPostConnectQueries () {
|
sendPostConnectQueries () {
|
||||||
this.sendBinary (['query', {type: 'contacts', epoch: '1'}, null], [ WAMetric.queryContact, WAFlag.ignore ])
|
this.sendBinary (['query', {type: 'contacts', epoch: '1'}, null], [ WAMetric.queryContact, WAFlag.ignore ])
|
||||||
this.sendBinary (['query', {type: 'chat', epoch: '1'}, null], [ WAMetric.queryChat, WAFlag.ignore ])
|
|
||||||
this.sendBinary (['query', {type: 'status', epoch: '1'}, null], [ WAMetric.queryStatus, WAFlag.ignore ])
|
this.sendBinary (['query', {type: 'status', epoch: '1'}, null], [ WAMetric.queryStatus, WAFlag.ignore ])
|
||||||
this.sendBinary (['query', {type: 'quick_reply', epoch: '1'}, null], [ WAMetric.queryQuickReply, WAFlag.ignore ])
|
this.sendBinary (['query', {type: 'quick_reply', epoch: '1'}, null], [ WAMetric.queryQuickReply, WAFlag.ignore ])
|
||||||
this.sendBinary (['query', {type: 'label', epoch: '1'}, null], [ WAMetric.queryLabel, WAFlag.ignore ])
|
this.sendBinary (['query', {type: 'label', epoch: '1'}, null], [ WAMetric.queryLabel, WAFlag.ignore ])
|
||||||
this.sendBinary (['query', {type: 'emoji', epoch: '1'}, null], [ WAMetric.queryEmoji, WAFlag.ignore ])
|
this.sendBinary (['query', {type: 'emoji', epoch: '1'}, null], [ WAMetric.queryEmoji, WAFlag.ignore ])
|
||||||
this.sendBinary (['action', {type: 'set', epoch: '1'}, [['presence', {type: Presence.available}, null]] ], [ WAMetric.presence, WAFlag.available ])
|
this.sendBinary (['action', {type: 'set', epoch: '1'}, [['presence', {type: Presence.available}, null]] ], [ WAMetric.presence, WAFlag.available ])
|
||||||
|
|
||||||
|
if(this.connectOptions.queryChatsTillReceived) {
|
||||||
|
this.chatsDebounceTimeout.start(
|
||||||
|
undefined,
|
||||||
|
() => {
|
||||||
|
this.logger.debug('pinging with chats query')
|
||||||
|
this.sendChatsQuery(this.msgCount)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
this.sendChatsQuery(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
protected sendChatsQuery(epoch: number) {
|
||||||
|
return this.sendBinary(['query', {type: 'chat', epoch: epoch.toString()}, null], [ WAMetric.queryChat, WAFlag.ignore ])
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Refresh QR Code
|
* Refresh QR Code
|
||||||
|
|||||||
@@ -94,12 +94,13 @@ export class WAConnection extends Base {
|
|||||||
this.logger.info(`connected to WhatsApp Web server, authenticating via ${reconnectID ? 'reconnect' : 'takeover'}`)
|
this.logger.info(`connected to WhatsApp Web server, authenticating via ${reconnectID ? 'reconnect' : 'takeover'}`)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
this.connectionDebounceTimeout.setInterval(this.connectOptions.maxIdleTimeMs)
|
||||||
const authResult = await this.authenticate(reconnectID)
|
const authResult = await this.authenticate(reconnectID)
|
||||||
|
|
||||||
this.conn
|
this.conn
|
||||||
.removeAllListeners('error')
|
.removeAllListeners('error')
|
||||||
.removeAllListeners('close')
|
.removeAllListeners('close')
|
||||||
this.connectionDebounceTimeout.start(this.connectOptions.maxIdleTimeMs)
|
this.connectionDebounceTimeout.start()
|
||||||
resolve(authResult as WAOpenResult)
|
resolve(authResult as WAOpenResult)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject(error)
|
reject(error)
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ export class WAConnection extends Base {
|
|||||||
// chats received
|
// chats received
|
||||||
this.on('CB:response,type:chat', json => {
|
this.on('CB:response,type:chat', json => {
|
||||||
if (json[1].duplicate || !json[2]) return
|
if (json[1].duplicate || !json[2]) return
|
||||||
|
|
||||||
|
this.chatsDebounceTimeout.cancel()
|
||||||
const chats = new KeyedDB(this.chatOrderingKey, c => c.jid)
|
const chats = new KeyedDB(this.chatOrderingKey, c => c.jid)
|
||||||
|
|
||||||
json[2].forEach(([item, chat]: [any, WAChat]) => {
|
json[2].forEach(([item, chat]: [any, WAChat]) => {
|
||||||
|
|||||||
@@ -85,11 +85,6 @@ export enum ReconnectMode {
|
|||||||
export type WALoadChatOptions = {
|
export type WALoadChatOptions = {
|
||||||
searchString?: string
|
searchString?: string
|
||||||
custom?: (c: WAChat) => boolean
|
custom?: (c: WAChat) => boolean
|
||||||
/**
|
|
||||||
* @deprecated
|
|
||||||
* does not do anything now
|
|
||||||
*/
|
|
||||||
loadProfilePicture?: boolean
|
|
||||||
}
|
}
|
||||||
export type WAConnectOptions = {
|
export type WAConnectOptions = {
|
||||||
/** fails the connection if no data is received for X seconds */
|
/** fails the connection if no data is received for X seconds */
|
||||||
@@ -105,6 +100,11 @@ export type WAConnectOptions = {
|
|||||||
fetchAgent?: Agent
|
fetchAgent?: Agent
|
||||||
/** Always uses takeover for connections */
|
/** Always uses takeover for connections */
|
||||||
alwaysUseTakeover?: boolean
|
alwaysUseTakeover?: boolean
|
||||||
|
/**
|
||||||
|
* Sometimes WA does not send the chats,
|
||||||
|
* this keeps pinging the phone to send the chats over
|
||||||
|
* */
|
||||||
|
queryChatsTillReceived?: boolean
|
||||||
}
|
}
|
||||||
/** from: https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url */
|
/** from: https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url */
|
||||||
export const URL_REGEX = /[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)?/gi
|
export const URL_REGEX = /[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)?/gi
|
||||||
|
|||||||
Reference in New Issue
Block a user