Alteração no auto restore

pull/21/head
adriano 2022-12-22 18:49:44 -03:00
parent a2d30f329e
commit 7c7627873c
6 changed files with 66 additions and 91 deletions

View File

@ -16,6 +16,8 @@ const autoRestore = async (whatsappId: string | number, started_action_by: strin
const whatsapp = await ShowWhatsAppService(whatsappId); const whatsapp = await ShowWhatsAppService(whatsappId);
// console.log('>>>>>>>>>>> Whatsapp.id: ', whatsappId)
restartWhatsSession(whatsapp, true) restartWhatsSession(whatsapp, true)

View File

@ -2,6 +2,11 @@
import os from 'os'; import os from 'os';
import dir from 'path'; import dir from 'path';
import fs from 'fs'; 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) => { export const setRestoreControll = (obj: object) => {
@ -133,3 +138,27 @@ export const getRestoreControll = () => {
return [] return []
} }
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);
}
}

View File

@ -23,6 +23,7 @@ import { restartWhatsSession } from "../../helpers/RestartWhatsSession";
import { insertOrUpeateWhatsCache, searchWhatsappCache } from "../../helpers/WhatsCache"; import { insertOrUpeateWhatsCache, searchWhatsappCache } from "../../helpers/WhatsCache";
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp"; import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
import autoRestore from "../../helpers/AutoRestore"; import autoRestore from "../../helpers/AutoRestore";
import { _restore } from "../../helpers/RestoreControll";
@ -54,11 +55,6 @@ const SendWhatsAppMessage = async ({
let whatsapps: any let whatsapps: any
//TEST DEL
// const defaultWhatsapp = await GetDefaultWhatsApp();
// console.log('DEFAULT WHATSAPP: ', JSON.parse(JSON.stringify(defaultWhatsapp)))
let listWhatsapp = null let listWhatsapp = null
listWhatsapp = await searchWhatsappCache(`${ticket.whatsappId}`, 'CONNECTED') listWhatsapp = await searchWhatsappCache(`${ticket.whatsappId}`, 'CONNECTED')
@ -67,9 +63,6 @@ const SendWhatsAppMessage = async ({
listWhatsapp = await ListWhatsAppsNumber(ticket.whatsappId, 'CONNECTED') listWhatsapp = await ListWhatsAppsNumber(ticket.whatsappId, 'CONNECTED')
} }
// console.log('---')
// console.log('listWhatsapp search: ', listWhatsapp)
// console.log('---')
if (listWhatsapp.length > 1) { 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); const wbot = await GetTicketWbot(ticket);
console.log('2 --------> send from whatsapp ticket.whatsappId: ', ticket.whatsappId) console.log('2 --------> send from whatsapp ticket.whatsappId: ', ticket.whatsappId)
try { try {
console.time
const sentMessage = await wbot.sendMessage(`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`, body, { quotedMessageId: quotedMsgSerializedId, linkPreview: false }); const sentMessage = await wbot.sendMessage(`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`, body, { quotedMessageId: quotedMsgSerializedId, linkPreview: false });
@ -176,21 +117,7 @@ const SendWhatsAppMessage = async ({
const whatsapp = await ShowWhatsAppService(ticket.whatsappId); const whatsapp = await ShowWhatsAppService(ticket.whatsappId);
if (whatsapp.status != 'RESTORING') { await _restore(whatsapp, 'auto_send_message')
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);
}
const sentMessage = await sendMessageMultiSession(ticket, body, quotedMsgSerializedId) const sentMessage = await sendMessageMultiSession(ticket, body, quotedMsgSerializedId)

View File

@ -55,6 +55,8 @@ import { splitDateTime } from "../../helpers/SplitDateTime";
import { updateTicketCacheByTicketId } from '../../helpers/TicketCache' import { updateTicketCacheByTicketId } from '../../helpers/TicketCache'
import { insertMessageContactCache, getLastId } from '../../helpers/LastMessageIdByContactCache' import { insertMessageContactCache, getLastId } from '../../helpers/LastMessageIdByContactCache'
import autoRestore from "../../helpers/AutoRestore";
import { _restore } from "../../helpers/RestoreControll";
@ -371,7 +373,7 @@ const botSendMessage = (ticket: Ticket, contact: Contact, wbot: Session, msg: st
const _clear_lst = () => { const _clear_lst = () => {
if (lst.length <= 200 ) return if (lst.length <= 200) return
const chunk: any = Math.floor((lst.length / 2)) const chunk: any = Math.floor((lst.length / 2))
@ -843,6 +845,8 @@ const handleMessage = async (
Sentry.captureException(err); Sentry.captureException(err);
logger.error(`Error handling whatsapp message: Err: ${err}`); logger.error(`Error handling whatsapp message: Err: ${err}`);
const whatsapp = await ShowWhatsAppService(wbot.id!);
//Solução para contornar erro de sessão //Solução para contornar erro de sessão
if ((`${err}`).includes("Evaluation failed: r")) { if ((`${err}`).includes("Evaluation failed: r")) {
@ -851,8 +855,6 @@ const handleMessage = async (
let log = new Date(new Date() + 'UTC'); let log = new Date(new Date() + 'UTC');
const dateToday = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR }))) 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) 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')
}
} }
}; };

View File

@ -27,6 +27,7 @@ import {
SignalCellular4Bar, SignalCellular4Bar,
CropFree, CropFree,
DeleteOutline, DeleteOutline,
// Restore
} from "@material-ui/icons"; } from "@material-ui/icons";
import MainContainer from "../../components/MainContainer"; import MainContainer from "../../components/MainContainer";
@ -326,6 +327,15 @@ const Connections = () => {
<SignalCellularConnectedNoInternet2Bar color="secondary" /> <SignalCellularConnectedNoInternet2Bar color="secondary" />
</CustomToolTip> </CustomToolTip>
)} )}
{/* {whatsApp.status === "RESTORING" && (
<CustomToolTip
title={i18n.t("connections.toolTips.disconnected.title")}
content={i18n.t("connections.toolTips.disconnected.content")}
>
<Restore color="secondary" />
</CustomToolTip>
)} */}
</div> </div>
); );
}; };
@ -340,9 +350,9 @@ const Connections = () => {
const fetchQueries = async () => { const fetchQueries = async () => {
try { try {
await api.post(`/restartwhatsappsession/0`, { params: { status: 'status' }, }); await api.post(`/restartwhatsappsession/0`, { params: { status: 'status' }, });
setDisabled(false) setDisabled(false)
} catch (err) { } catch (err) {
console.log(err); console.log(err);