Correção no diretorio semente das sessões para atualizar a sessao quando estiver conectado

pull/21/head
adriano 2023-05-02 09:47:41 -03:00
parent 34d6be3978
commit 7358e314ba
2 changed files with 71 additions and 34 deletions

View File

@ -19,8 +19,8 @@ const express = require('express');
const FormData = require('form-data'); const FormData = require('form-data');
// const { MessageMedia } = require('./node_modules/whatsapp-web.js/src/structures'); // const { MessageMedia } = require('./node_modules/whatsapp-web.js/src/structures');
let whatsappWebGlobalPath = path.join(process.env.NODE_PATH, 'whatsapp-web.js', '/src/structures'); let whatsappWebGlobalPath = path.join(process.env.NODE_PATH, 'whatsapp-web.js', '/src/structures');
whatsappWebGlobalPath = whatsappWebGlobalPath.replace(':','') whatsappWebGlobalPath = whatsappWebGlobalPath.replace(':', '')
console.log('whatsappWebGlobalPath: ', whatsappWebGlobalPath) console.log('whatsappWebGlobalPath: ', whatsappWebGlobalPath)
console.log('process.env.NODE_PATH: ', process.env.NODE_PATH) console.log('process.env.NODE_PATH: ', process.env.NODE_PATH)
@ -176,20 +176,25 @@ client.on("qr", async qr => {
// omnihit.qrcode(process.env.MOBILEUID, process.env.MOBILENAME, qr); // omnihit.qrcode(process.env.MOBILEUID, process.env.MOBILENAME, qr);
// omnihit.monitor(process.env.MOBILEUID, process.env.MOBILENAME, "STARTUP"); // omnihit.monitor(process.env.MOBILEUID, process.env.MOBILENAME, "STARTUP");
asking_qrcode = true asking_qrcode = true
await new Promise((resolve, reject) => {
dbcc.query("UPDATE Whatsapps SET qrcode = ?, status = ?, retries = ? where id = ?", [qr, 'qrcode', 0, process.env.WHATSAPP_ID], dbcc.query("UPDATE Whatsapps SET qrcode = ?, status = ?, retries = ? where id = ?", [qr, 'qrcode', 0, process.env.WHATSAPP_ID],
function (err, result) { function (err, result) {
if (err)
console.log("ERROR: " + err);
// else
// console.log('myslq result: ', result);
});
if (err) {
console.log("ERROR: " + err);
reject(err)
}
else {
resolve(result)
}
});
})
let url = process.env.CLIENT_URL + '/whatsapp/connection/qrcode' let url = process.env.CLIENT_URL + '/whatsapp/connection/qrcode'
try { try {
@ -271,15 +276,25 @@ client.on("ready", async () => {
dbcc.query("UPDATE Whatsapps SET qrcode = ?, status = ?, retries = ?, number = ? where id = ?", ["", 'CONNECTED', 0, client.info["wid"]["user"], process.env.WHATSAPP_ID], await new Promise((resolve, reject) => {
dbcc.query("UPDATE Whatsapps SET qrcode = ?, status = ?, retries = ?, number = ? where id = ?", ["", 'CONNECTED', 0, client.info["wid"]["user"], process.env.WHATSAPP_ID],
function (err, result) {
if (err) {
console.log("ERROR: " + err);
reject(err)
}
else {
resolve(result)
}
});
})
function (err, result) {
if (err)
console.log("ERROR: " + err);
// else
// console.log('myslq result: ', result);
});
let url = process.env.CLIENT_URL + '/whatsapp/connection/qrcode' let url = process.env.CLIENT_URL + '/whatsapp/connection/qrcode'
@ -384,7 +399,7 @@ client.on("message_ack", async (msg, ack) => {
}); });
socketIo.on('send_message', async data => { socketIo.on('send_message', async data => {
console.log('#') console.log('#')
console.log('--------------> send_message from number: ', mobileuid); console.log('--------------> send_message from number: ', mobileuid);
console.log('--------------> send_message to number: ', data.msg.number); console.log('--------------> send_message to number: ', data.msg.number);
@ -400,7 +415,7 @@ socketIo.on('send_message', async data => {
socketIo.on('send_media', async data => { socketIo.on('send_media', async data => {
console.log('#') console.log('#')
console.log('--------------> send_message from number: ', mobileuid); console.log('--------------> send_message from number: ', mobileuid);
console.log('--------------> send_message to number: ', data.msg.number); console.log('--------------> send_message to number: ', data.msg.number);
@ -414,7 +429,7 @@ socketIo.on('send_media', async data => {
if (media && !media.filename) if (media && !media.filename)
media.filename = data.msg.media.filename media.filename = data.msg.media.filename
const sentMessage = await client.sendMessage(data.msg.number, media, { sendAudioAsVoice: data.msg.sendAudioAsVoice }); const sentMessage = await client.sendMessage(data.msg.number, media, { sendAudioAsVoice: data.msg.sendAudioAsVoice });
// const fullFilename = process.cwd() + process.env.MEDIA_DOWNLOAD_IN + data.msg.media.filename; // const fullFilename = process.cwd() + process.env.MEDIA_DOWNLOAD_IN + data.msg.media.filename;
@ -683,16 +698,15 @@ const getWbotMessage = async (messageId, number, limit,) => {
} }
async function whatsappMonitor(newState, omnihit_url, data) { async function whatsappMonitor(newState, omnihit_url, data) {
dbcc.query("UPDATE Whatsapps SET status = ? where id = ?", [newState, process.env.WHATSAPP_ID], const whatsapp = await whatsappUpdateStatus(newState)
function (err, result) {
if (err)
console.log("ERROR: " + err);
// else if (whatsapp && whatsapp.affectedRows) {
// console.log('myslq result: ', result); console.log('whatsapp status update affectedRows: ', whatsapp.affectedRows)
}); }
// console.log(' whatsappwhatsappwhatsappwhatsapp: ', whatsapp)
try { try {
@ -702,6 +716,23 @@ async function whatsappMonitor(newState, omnihit_url, data) {
} }
} }
async function whatsappUpdateStatus(newState) {
return await new Promise((resolve, reject) => {
dbcc.query("UPDATE Whatsapps SET status = ? where id = ?", [newState, process.env.WHATSAPP_ID],
function (err, result) {
if (err) {
console.log("ERROR: " + err);
reject(err);
}
else {
resolve(result);
}
});
});
}
async function handleMessage(msg) { async function handleMessage(msg) {
console.log('Entrou no message_create'); console.log('Entrou no message_create');
@ -709,7 +740,7 @@ async function handleMessage(msg) {
let msgContact = null; let msgContact = null;
let media = null; let media = null;
if (msg.fromMe) { if (msg.fromMe) {
msgContact = await client.getContactById(msg.to); msgContact = await client.getContactById(msg.to);
@ -740,7 +771,7 @@ async function handleMessage(msg) {
quotedMsg: quotedMsg ? quotedMsg.id.id : null, quotedMsg: quotedMsg ? quotedMsg.id.id : null,
media: media media: media
}; };
socketIo.emit("message_create", data); socketIo.emit("message_create", data);
@ -868,6 +899,14 @@ async function monitor() {
console.log(`WHATSAPP_ID: ${process.env.WHATSAPP_ID} | CLIENT MOBILEUID: ${mobileuid} | NAME: ${process.env.MOBILENAME} | ENV MOBILEUID: ${process.env.MOBILEUID} | STATUS: ${stat}`) console.log(`WHATSAPP_ID: ${process.env.WHATSAPP_ID} | CLIENT MOBILEUID: ${mobileuid} | NAME: ${process.env.MOBILENAME} | ENV MOBILEUID: ${process.env.MOBILEUID} | STATUS: ${stat}`)
if (stat && stat === 'CONNECTED') {
const result = await whatsappUpdateStatus('CONNECTED')
if (result)
console.log(`Update status to CONNECTED WHATSAPP_ID: ${process.env.WHATSAPP_ID} => result.affectedRows: ${result.affectedRows}`)
}
} catch (error) { } catch (error) {
//new Date(new Date() + 'UTC') //new Date(new Date() + 'UTC')

View File

@ -14,9 +14,7 @@ export const wbotMonitorRemote = async (req: Request, res: Response): Promise<Re
const { action, whatsappId, reason } = req.body const { action, whatsappId, reason } = req.body
console.log('-----------> ACTION: ', req.body['action']) console.log('-----------> ACTION: ', req.body['action'])
// let whatsapp = await ShowWhatsAppService(whatsappId)
const whatsapp: any = await Whatsapp.findByPk(whatsappId, { raw: true }) const whatsapp: any = await Whatsapp.findByPk(whatsappId, { raw: true })