diff --git a/README.md b/README.md index fc7c501..a9f3481 100644 --- a/README.md +++ b/README.md @@ -174,6 +174,7 @@ with: * 9 - connection lost * 10 - Invalid token value * 11 - Http Head test link not return success +* 12 - offline number, connecting.... ## Up for development diff --git a/package.json b/package.json index 262ae9c..ea1242e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "unoapi-cloud", - "version": "1.21.16", + "version": "1.21.17", "description": "Unoapi Cloud", "exports": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/src/services/client_baileys.ts b/src/services/client_baileys.ts index 22f61e1..3ca614d 100644 --- a/src/services/client_baileys.ts +++ b/src/services/client_baileys.ts @@ -478,9 +478,6 @@ export class ClientBaileys implements Client { } const r: Response = { ok } return r - } else { - logger.error('Response on sent to baileys is empty.....') - throw new SendError(5, 'Wait a moment, connecting process') } } else { throw new Error(`Unknow message type ${type}`) @@ -495,7 +492,8 @@ export class ClientBaileys implements Client { const code = e.code const title = e.title await this.onNotification(title, true) - if ([3, '3'].includes(code)) { + if ([3, '3', 12, '12'].includes(code)) { + await this.close() await this.connect(1) } const id = uuid() diff --git a/src/services/socket.ts b/src/services/socket.ts index 97cb003..a843516 100644 --- a/src/services/socket.ts +++ b/src/services/socket.ts @@ -106,6 +106,7 @@ export const connect = async ({ let sock: WASocket | undefined = undefined const msgRetryCounterCache = new NodeCache() const { dataStore, state, saveCreds, sessionStore } = store + let connectingTimeout const status: Status = { attempt: time, @@ -154,19 +155,31 @@ export const connect = async ({ } } + const verifyConnectingTimeout = async () => { + if (connectingTimeout) { + return + } + logger.info(`Connecting ${phone} set timeout to ${CONNECTING_TIMEOUT_MS} ms`) + if (await sessionStore.isStatusConnecting(phone)) { + connectingTimeout = setTimeout(async () => { + if (await sessionStore.isStatusConnecting(phone)) { + connectingTimeout = null + const message = `Connecting ${phone} timed out ${CONNECTING_TIMEOUT_MS} ms, change to disconnect` + await onNotification(message, false) + logger.warn(message) + await onDisconnected(phone, {}) + } + }, CONNECTING_TIMEOUT_MS) + } else { + connectingTimeout = null + } + } + const onConnecting = async () => { await sessionStore.setStatus(phone, 'connecting') await onNotification(`Connnecting...`, false) logger.info(`Connecting ${phone} set timeout to ${CONNECTING_TIMEOUT_MS} ms`) - setTimeout(async () => { - if (await sessionStore.isStatusConnecting(phone)) { - const message = `Connecting ${phone} timed out ${CONNECTING_TIMEOUT_MS} ms, change to disconnect` - await onNotification(message, false) - logger.info(message) - await onDisconnected(phone, {}) - await close() - } - }, CONNECTING_TIMEOUT_MS) + return verifyConnectingTimeout() } const onOpen = async () => { @@ -314,15 +327,22 @@ export const connect = async ({ const exists: exists = async (phone: string) => { await validateStatus() - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + return dataStore.loadJid(phone, sock!) } const validateStatus = async () => { if (await sessionStore.isStatusConnecting(phone)) { + await verifyConnectingTimeout() throw new SendError(5, 'Wait a moment, connecting process') - } else if (!(await sessionStore.isStatusOnline(phone))) { + } else if (await sessionStore.isStatusIsDisconnect(phone) || !sock) { throw new SendError(3, 'Disconnected number, please read qr code') + } else if (await sessionStore.isStatusOffline(phone)) { + throw new SendError(12, 'offline number, connecting....') + } + if (connectingTimeout) { + clearTimeout(connectingTimeout) + connectingTimeout = null } } @@ -331,12 +351,7 @@ export const connect = async ({ message: AnyMessageContent, options: { composing: boolean; quoted: boolean | undefined } = { composing: false, quoted: undefined }, ) => { - if (!(await sessionStore.isStatusOnline(phone))) { - if (!(await sessionStore.isStatusConnecting(phone))) { - reconnect() - } - return - } + await validateStatus() const id = isJidGroup(to) ? to : await exists(to) if (id) { @@ -364,7 +379,7 @@ export const connect = async ({ } const read: readMessages = async (keys: WAMessageKey[]) => { - if (!(await sessionStore.isStatusOnline(phone))) return false + await validateStatus() await sock?.readMessages(keys) return true @@ -376,6 +391,8 @@ export const connect = async ({ } const rejectCall: rejectCall = async (callId: string, callFrom: string) => { + await validateStatus() + return sock?.rejectCall(callId, callFrom) }