Implementação parcial do recurso para pre enviar mensagens quando é lido o qrcode

pull/21/head
adriano 2022-12-30 02:07:13 -03:00
parent 95b02299ef
commit 963422a7a0
3 changed files with 31 additions and 23 deletions

View File

@ -146,8 +146,8 @@ export const _restore = async (whatsapp: Whatsapp, msg_file_title: string) => {
console.log('THE WHATSAAP ID: ', whatsapp.id, ' WILL BE RESTORED SOON!') console.log('THE WHATSAAP ID: ', whatsapp.id, ' WILL BE RESTORED SOON!')
await whatsapp.update({ status: "RESTORING", }); await whatsapp.update({ status: "RESTORING", });
const io = getIO(); const io = getIO();
io.emit("whatsappSession", { io.emit("whatsappSession", {

View File

@ -38,7 +38,7 @@ const syncUnreadMessages = async (wbot: Session) => {
}); });
for (const msg of unreadMessages) { for (const msg of unreadMessages) {
// console.log('--BACKEND MSG: ', msg) // console.log('--BACKEND MSG: ', msg)
await handleMessage(msg, wbot); await handleMessage(msg, wbot);
@ -72,7 +72,7 @@ export const initWbot = async (whatsapp: Whatsapp, backupSessionRestore: boolean
// usando instancia do chrome // usando instancia do chrome
const wbot: Session = new Client({ const wbot: Session = new Client({
session: sessionCfg, authStrategy: new LocalAuth({ clientId: 'bd_' + whatsapp.id }), session: sessionCfg, authStrategy: new LocalAuth({ clientId: 'bd_' + whatsapp.id }),
puppeteer: { args: ['--no-sandbox', '--disable-setuid-sandbox'], executablePath: process.env.CHROME_BIN || undefined }, puppeteer: { args: ['--no-sandbox', '--disable-setuid-sandbox'], executablePath: process.env.CHROME_BIN || undefined },
// puppeteer: { args: ['--no-sandbox', '--disable-setuid-sandbox'], executablePath: process.env.CHROME_BIN || '/usr/bin/google-chrome-stable' }, // puppeteer: { args: ['--no-sandbox', '--disable-setuid-sandbox'], executablePath: process.env.CHROME_BIN || '/usr/bin/google-chrome-stable' },
}); });
@ -139,26 +139,34 @@ export const initWbot = async (whatsapp: Whatsapp, backupSessionRestore: boolean
}); });
wbot.on("ready", async () => { wbot.on("ready", async () => {
logger.info(`Session: ${sessionName} READY`); logger.info(`Session: ${sessionName} READY`);
if(whatsapp.name.includes(wbot.info["wid"]["user"])){ if (whatsapp.name.includes(wbot.info["wid"]["user"])) {
console.log('-----------------> THIS IS THE RIGHT NUMBER') console.log('-----------------> THIS IS THE RIGHT NUMBER')
for (let i = 0; i < 12; i++) {
await wbot.sendMessage(`5517988325936@c.us`, `*@!ping*<>${whatsapp.name}`);
console.log('Send: ', i, ' | (Math.floor(Math.random() * 3)+2): ', (Math.floor(Math.random() * 2)+3))
await new Promise(r => setTimeout(r,(Math.floor(Math.random() * 3)+3)));
}
} }
else{ else {
console.log('-----------------> THIS IS THE WRONG NUMBER') console.log('-----------------> THIS IS THE WRONG NUMBER')
let read_number = wbot.info["wid"]["user"] let read_number = wbot.info["wid"]["user"]
await wbot.logout() await wbot.logout()
io.emit("whatsappSession", { io.emit("whatsappSession", {
action: "error", action: "error",
msg: `Numero lido: ${read_number} \nEssa sessão de whatsapp foi desconectada porque o numero que esta descrito no nome dessa sessão não corresponde ao numero lido!` msg: `Numero lido: ${read_number} \nEssa sessão de whatsapp foi desconectada porque o numero que esta descrito no nome dessa sessão não corresponde ao numero lido!`
}); });
// restartWhatsSession(whatsapp) return
}
return
}
await whatsapp.update({ await whatsapp.update({
@ -166,8 +174,8 @@ export const initWbot = async (whatsapp: Whatsapp, backupSessionRestore: boolean
qrcode: "", qrcode: "",
retries: 0, retries: 0,
number: wbot.info["wid"]["user"] number: wbot.info["wid"]["user"]
}); });
await insertOrUpeateWhatsCache(`whatsapp:${whatsapp.id}`,whatsapp) await insertOrUpeateWhatsCache(`whatsapp:${whatsapp.id}`, whatsapp)
io.emit("whatsappSession", { io.emit("whatsappSession", {
action: "update", action: "update",
@ -223,7 +231,7 @@ export const initWbot = async (whatsapp: Whatsapp, backupSessionRestore: boolean
}, 90000); }, 90000);
console.log(' PASSOU NO TIMEOUT whatsapp.id: ',whatsapp.id) console.log(' PASSOU NO TIMEOUT whatsapp.id: ', whatsapp.id)
} }

View File

@ -22,12 +22,12 @@ const wbotMonitor = async (
wbot.on("change_state", async newState => { wbot.on("change_state", async newState => {
logger.info(`Monitor session: ${sessionName}, ${newState}`); logger.info(`Monitor session: ${sessionName}, ${newState}`);
// console.log('>>>>>>>>>>>>>> change_state wbotMonitor.ts MOBILE NUMBER: ', wbot.info["wid"]["user"]) // console.log('>>>>>>>>>>>>>> change_state wbotMonitor.ts MOBILE NUMBER: ', wbot.info["wid"]["user"])
try { try {
await whatsapp.update({ status: newState }); await whatsapp.update({ status: newState });
await insertOrUpeateWhatsCache(`whatsapp:${whatsapp.id}`, { status: newState }) await insertOrUpeateWhatsCache(`whatsapp:${whatsapp.id}`, { status: newState })
} catch (err) { } catch (err: any) {
Sentry.captureException(err); Sentry.captureException(err);
logger.error(err); logger.error(err);
} }
@ -46,7 +46,7 @@ const wbotMonitor = async (
try { try {
await whatsapp.update({ battery, plugged }); await whatsapp.update({ battery, plugged });
} catch (err) { } catch (err: any) {
Sentry.captureException(err); Sentry.captureException(err);
logger.error(err); logger.error(err);
} }
@ -60,10 +60,10 @@ const wbotMonitor = async (
wbot.on("disconnected", async reason => { wbot.on("disconnected", async reason => {
logger.info(`Disconnected session: ${sessionName}, reason: ${reason}`); logger.info(`Disconnected session: ${sessionName}, reason: ${reason}`);
try { try {
await whatsapp.update({ status: "OPENING", session: "" }); await whatsapp.update({ status: "OPENING", session: "" });
await insertOrUpeateWhatsCache(`whatsapp:${whatsapp.id}`, { status: "OPENING", session: "" }) await insertOrUpeateWhatsCache(`whatsapp:${whatsapp.id}`, { status: "OPENING", session: "" })
} catch (err) { } catch (err: any) {
Sentry.captureException(err); Sentry.captureException(err);
logger.error(err); logger.error(err);
} }
@ -75,7 +75,7 @@ const wbotMonitor = async (
setTimeout(() => StartWhatsAppSession(whatsapp), 2000); setTimeout(() => StartWhatsAppSession(whatsapp), 2000);
}); });
} catch (err) { } catch (err: any) {
Sentry.captureException(err); Sentry.captureException(err);
logger.error(err); logger.error(err);
} }