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

@ -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' },
}); });
@ -141,10 +141,20 @@ 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"]
@ -152,11 +162,9 @@ export const initWbot = async (whatsapp: Whatsapp, backupSessionRestore: boolean
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
} }
@ -167,7 +175,7 @@ export const initWbot = async (whatsapp: Whatsapp, backupSessionRestore: boolean
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);
} }
@ -63,7 +63,7 @@ const wbotMonitor = async (
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);
} }