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

@ -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)

View File

@ -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 []
}
}
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 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)

View File

@ -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",

View File

@ -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')
}
}
};

View File

@ -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 = () => {
<SignalCellularConnectedNoInternet2Bar color="secondary" />
</CustomToolTip>
)}
{/* {whatsApp.status === "RESTORING" && (
<CustomToolTip
title={i18n.t("connections.toolTips.disconnected.title")}
content={i18n.t("connections.toolTips.disconnected.content")}
>
<Restore color="secondary" />
</CustomToolTip>
)} */}
</div>
);
};
@ -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);
}