diff --git a/backend/src/helpers/AutoRestore.ts b/backend/src/helpers/AutoRestore.ts index e56e3c2..595c3ea 100644 --- a/backend/src/helpers/AutoRestore.ts +++ b/backend/src/helpers/AutoRestore.ts @@ -15,6 +15,8 @@ import { number } from "yargs"; const autoRestore = async (whatsappId: string | number, started_action_by: string = '') => { const whatsapp = await ShowWhatsAppService(whatsappId); + + // console.log('>>>>>>>>>>> Whatsapp.id: ', whatsappId) restartWhatsSession(whatsapp, true) diff --git a/backend/src/helpers/RestoreControll.ts b/backend/src/helpers/RestoreControll.ts index 22ae676..fb6e456 100644 --- a/backend/src/helpers/RestoreControll.ts +++ b/backend/src/helpers/RestoreControll.ts @@ -2,6 +2,11 @@ import os from 'os'; import dir from 'path'; import fs from 'fs'; +import ShowWhatsAppService from '../services/WhatsappService/ShowWhatsAppService'; +import { insertOrUpeateWhatsCache } from './WhatsCache'; +import autoRestore from './AutoRestore'; +import Whatsapp from '../models/Whatsapp'; +import { getIO } from '../libs/socket'; export const setRestoreControll = (obj: object) => { @@ -132,4 +137,28 @@ export const getRestoreControll = () => { return [] -} \ No newline at end of file +} + + +export const _restore = async (whatsapp: Whatsapp, msg_file_title: string) => { + + if (whatsapp.status != 'RESTORING') { + + console.log('THE WHATSAAP ID: ', whatsapp.id, ' WILL BE RESTORED SOON!') + + await whatsapp.update({ status: "RESTORING", }); + + const io = getIO(); + + io.emit("whatsappSession", { + action: "update", + session: whatsapp + }); + + await insertOrUpeateWhatsCache(`whatsapp:${whatsapp.id}`, { status: "RESTORING", }) + + setTimeout(async () => await autoRestore(whatsapp.id, msg_file_title), 95000); + + } + +} \ No newline at end of file diff --git a/backend/src/services/WbotServices/SendWhatsAppMessage.ts b/backend/src/services/WbotServices/SendWhatsAppMessage.ts index 66b5bac..9b42891 100644 --- a/backend/src/services/WbotServices/SendWhatsAppMessage.ts +++ b/backend/src/services/WbotServices/SendWhatsAppMessage.ts @@ -23,6 +23,7 @@ import { restartWhatsSession } from "../../helpers/RestartWhatsSession"; import { insertOrUpeateWhatsCache, searchWhatsappCache } from "../../helpers/WhatsCache"; import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp"; import autoRestore from "../../helpers/AutoRestore"; +import { _restore } from "../../helpers/RestoreControll"; @@ -54,11 +55,6 @@ const SendWhatsAppMessage = async ({ let whatsapps: any - //TEST DEL - - // const defaultWhatsapp = await GetDefaultWhatsApp(); - // console.log('DEFAULT WHATSAPP: ', JSON.parse(JSON.stringify(defaultWhatsapp))) - let listWhatsapp = null listWhatsapp = await searchWhatsappCache(`${ticket.whatsappId}`, 'CONNECTED') @@ -67,9 +63,6 @@ const SendWhatsAppMessage = async ({ listWhatsapp = await ListWhatsAppsNumber(ticket.whatsappId, 'CONNECTED') } - // console.log('---') - // console.log('listWhatsapp search: ', listWhatsapp) - // console.log('---') if (listWhatsapp.length > 1) { @@ -104,64 +97,12 @@ const SendWhatsAppMessage = async ({ } - - // - - - - - - // const listWhatsapp = await ListWhatsAppsNumber(ticket.whatsappId, 'CONNECTED') - - // if (listWhatsapp.length > 1) { - - // const _whatsapp = listWhatsapp[Math.floor(Math.random() * listWhatsapp.length)]; - - // await ticket.update({ whatsappId: _whatsapp.id }); - - // } - // else { - - // whatsapps = await Whatsapp.findOne({ - // where: { id: ticket.whatsappId }, - // attributes: ['status'] - // }) - - // } - - - // console.log('1 --------> ticket.whatsappId: ', ticket.whatsappId) - - - - // if (listWhatsapp.length == 0 || (whatsapps && whatsapps.status != 'CONNECTED')) { - - // whatsapps = await wbotByUserQueue(ticket.userId) - - // if (whatsapps.length > 0) { - - // if (whatsapps.length > 1) { - - // await ticket.update({ whatsappId: whatsapps[+WhatsIndex(whatsapps)].id }); - - // } - // else { - - // await ticket.update({ whatsappId: whatsapps[0].id }); - - // } - - // } - - // } - const wbot = await GetTicketWbot(ticket); console.log('2 --------> send from whatsapp ticket.whatsappId: ', ticket.whatsappId) try { - console.time const sentMessage = await wbot.sendMessage(`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`, body, { quotedMessageId: quotedMsgSerializedId, linkPreview: false }); @@ -174,23 +115,9 @@ const SendWhatsAppMessage = async ({ return sentMessage; } catch (err) { - const whatsapp = await ShowWhatsAppService(ticket.whatsappId); + const whatsapp = await ShowWhatsAppService(ticket.whatsappId); - if (whatsapp.status != 'RESTORING') { - - console.log('THE WHATSAAP ID: ', whatsapp.id, ' WILL BE RESTORED SOON!') - - await whatsapp.update({ - status: "RESTORING", - }); - - await insertOrUpeateWhatsCache(`whatsapp:${whatsapp.id}`, { - status: "RESTORING", - }) - - // setTimeout(() => restartWhatsSession(whatsapp, true), 90000); - setTimeout(async () => await autoRestore(whatsapp.id, 'auto_send_message'), 95000); - } + await _restore(whatsapp, 'auto_send_message') const sentMessage = await sendMessageMultiSession(ticket, body, quotedMsgSerializedId) diff --git a/backend/src/services/WbotServices/StartWhatsAppSession.ts b/backend/src/services/WbotServices/StartWhatsAppSession.ts index 5fe013d..68bdb3b 100644 --- a/backend/src/services/WbotServices/StartWhatsAppSession.ts +++ b/backend/src/services/WbotServices/StartWhatsAppSession.ts @@ -61,7 +61,7 @@ export const StartWhatsAppSession = async (whatsapp: Whatsapp, backupSession: bo console.log('----------------> autoR exec after 120 seconds: ', autoR) - let whatsappStatus = ["CONFLICT", + let whatsappStatus = ["CONFLICT", "DEPRECATED_VERSION", "OPENING", "PROXYBLOCK", diff --git a/backend/src/services/WbotServices/wbotMessageListener.ts b/backend/src/services/WbotServices/wbotMessageListener.ts index c2132f2..4cd981d 100644 --- a/backend/src/services/WbotServices/wbotMessageListener.ts +++ b/backend/src/services/WbotServices/wbotMessageListener.ts @@ -55,6 +55,8 @@ import { splitDateTime } from "../../helpers/SplitDateTime"; import { updateTicketCacheByTicketId } from '../../helpers/TicketCache' import { insertMessageContactCache, getLastId } from '../../helpers/LastMessageIdByContactCache' +import autoRestore from "../../helpers/AutoRestore"; +import { _restore } from "../../helpers/RestoreControll"; @@ -371,12 +373,12 @@ const botSendMessage = (ticket: Ticket, contact: Contact, wbot: Session, msg: st const _clear_lst = () => { - if (lst.length <= 200 ) return + if (lst.length <= 200) return const chunk: any = Math.floor((lst.length / 2)) - lst = lst.slice(chunk, chunk + lst.length); - + lst = lst.slice(chunk, chunk + lst.length); + } @@ -386,7 +388,7 @@ const clearMultiSessionWhatsappMessageId = () => { try { clearInterval(clear_lst); - _clear_lst() + _clear_lst() } catch (error) { console.log('error on clear lst whatsapp id message: ', error) @@ -424,7 +426,7 @@ const handleMessage = async ( // console.log('LIST OF ID MESSAGE lst: ', lst) - console.log('PASSOU.................................FROM: ', msg.from.split("@")[0], ' | ID: ', msg.id.id) + console.log('PASSOU.................................FROM: ', msg.from.split("@")[0], ' | ID: ', msg.id.id) if (!isValidMsg(msg)) { @@ -843,6 +845,8 @@ const handleMessage = async ( Sentry.captureException(err); logger.error(`Error handling whatsapp message: Err: ${err}`); + const whatsapp = await ShowWhatsAppService(wbot.id!); + //Solução para contornar erro de sessão if ((`${err}`).includes("Evaluation failed: r")) { @@ -851,9 +855,7 @@ const handleMessage = async ( let log = new Date(new Date() + 'UTC'); const dateToday = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR }))) - const whatsapp = await ShowWhatsAppService(wbot.id!); - - if (whatsapp.status == 'CONNECTED') { + if (whatsapp.status == 'CONNECTED') { let timestamp = Math.floor(Date.now() / 1000) @@ -864,6 +866,11 @@ const handleMessage = async ( } } + else if (`${err}`.includes('[object Object]')) { + + await _restore(whatsapp, 'auto_object_error') + + } } }; diff --git a/frontend/src/pages/Connections/index.js b/frontend/src/pages/Connections/index.js index 9e968cb..7c21a96 100644 --- a/frontend/src/pages/Connections/index.js +++ b/frontend/src/pages/Connections/index.js @@ -27,6 +27,7 @@ import { SignalCellular4Bar, CropFree, DeleteOutline, + // Restore } from "@material-ui/icons"; import MainContainer from "../../components/MainContainer"; @@ -114,8 +115,8 @@ const Connections = () => { const [confirmModalOpen, setConfirmModalOpen] = useState(false); const [diskSpaceInfo, setDiskSpaceInfo] = useState({}); - - const [disabled, setDisabled] = useState(true); + + const [disabled, setDisabled] = useState(true); const confirmationModalInitialState = { @@ -326,6 +327,15 @@ const Connections = () => { )} + + {/* {whatsApp.status === "RESTORING" && ( + + + + )} */} ); }; @@ -340,10 +350,10 @@ const Connections = () => { const fetchQueries = async () => { try { - await api.post(`/restartwhatsappsession/0`, { params: { status: 'status' }, }); + await api.post(`/restartwhatsappsession/0`, { params: { status: 'status' }, }); + + setDisabled(false) - setDisabled(false) - } catch (err) { console.log(err); }