2022-01-06 01:26:15 +00:00
|
|
|
import { initWbot } from "../../libs/wbot";
|
|
|
|
import Whatsapp from "../../models/Whatsapp";
|
|
|
|
import { wbotMessageListener } from "./wbotMessageListener";
|
|
|
|
import { getIO } from "../../libs/socket";
|
|
|
|
import wbotMonitor from "./wbotMonitor";
|
|
|
|
import { logger } from "../../utils/logger";
|
2022-11-17 14:40:37 +00:00
|
|
|
import { insertOrUpeateWhatsCache } from "../../helpers/WhatsCache";
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2022-12-14 12:29:42 +00:00
|
|
|
import { getRestoreControll, setRestoreControll, shifRestoreControll } from "../../helpers/RestoreControll";
|
|
|
|
import ShowWhatsAppService from "../WhatsappService/ShowWhatsAppService";
|
|
|
|
import { restartWhatsSession } from "../../helpers/RestartWhatsSession";
|
|
|
|
|
|
|
|
import autoRestore from "../../helpers/AutoRestore";
|
|
|
|
|
|
|
|
let lstAutoRestore: any = []
|
|
|
|
|
2022-07-12 12:37:34 +00:00
|
|
|
export const StartWhatsAppSession = async (whatsapp: Whatsapp, backupSession: boolean = false): Promise<void> => {
|
2022-12-14 12:29:42 +00:00
|
|
|
await whatsapp.update({ status: "OPENING" });
|
2022-12-12 16:02:18 +00:00
|
|
|
|
2022-11-17 14:40:37 +00:00
|
|
|
await insertOrUpeateWhatsCache(`whatsapp:${whatsapp.id}`, {
|
|
|
|
status: "OPENING",
|
|
|
|
})
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2022-12-14 12:29:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
let lstRestore: any = getRestoreControll()
|
|
|
|
|
|
|
|
if (Object.keys(lstRestore.filter((e: any) => +e.id == +whatsapp.id)).length) {
|
|
|
|
|
2022-12-15 17:44:02 +00:00
|
|
|
console.log('Restore executed by human whatasappId: ', whatsapp.id)
|
2022-12-14 12:29:42 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
lstAutoRestore.push({ 'whatsappId': +whatsapp.id })
|
|
|
|
|
|
|
|
setTimeout(async () => {
|
|
|
|
|
|
|
|
lstRestore = getRestoreControll()
|
|
|
|
|
|
|
|
let count = lstAutoRestore.length
|
|
|
|
|
|
|
|
for (let i = 0; i < count; i++) {
|
|
|
|
|
|
|
|
let autoR = lstAutoRestore.shift()
|
|
|
|
|
|
|
|
console.log('----------------> autoR: ', autoR)
|
|
|
|
|
|
|
|
if (autoR && autoR.whatsappId) {
|
|
|
|
|
|
|
|
if (Object.keys(lstRestore.filter((e: any) => +e.id == +autoR.whatsappId)).length) {
|
|
|
|
|
|
|
|
console.log(' ACONTECENDO RESTORING autoR: ', autoR)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
const _whatsapp = await Whatsapp.findOne({ where: { id: autoR.whatsappId } });
|
|
|
|
|
|
|
|
let whatsappStatus = ["CONFLICT",
|
|
|
|
"DEPRECATED_VERSION ",
|
|
|
|
"OPENING ",
|
|
|
|
"PROXYBLOCK ",
|
|
|
|
"SMB_TOS_BLOCK ",
|
|
|
|
"TIMEOUT ",
|
|
|
|
"TOS_BLOCK ",
|
|
|
|
"UNLAUNCHED ",
|
|
|
|
"UNPAIRED ",
|
|
|
|
"UNPAIRED_IDLE"]
|
|
|
|
|
|
|
|
if (_whatsapp?.status) {
|
|
|
|
|
|
|
|
if (whatsappStatus.includes(_whatsapp.status)) {
|
|
|
|
|
|
|
|
await autoRestore(autoR.whatsappId, 'auto_monit')
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}, 20000);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
console.log('There was an error on try execute AUTO-RESTORE: ', error)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const io = getIO();
|
|
|
|
io.emit("whatsappSession", {
|
|
|
|
action: "update",
|
|
|
|
session: whatsapp
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2022-07-12 12:37:34 +00:00
|
|
|
const wbot = await initWbot(whatsapp, backupSession);
|
2022-01-06 01:26:15 +00:00
|
|
|
wbotMessageListener(wbot);
|
|
|
|
wbotMonitor(wbot, whatsapp);
|
2022-06-27 06:21:04 +00:00
|
|
|
} catch (err) {
|
|
|
|
|
2022-02-22 20:36:11 +00:00
|
|
|
logger.error(`${err}`);
|
2022-06-27 06:21:04 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
|
|
|
};
|