mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 19:59:20 +00:00
improvement: stop deleting wbot session if phone is offline (timeout)
This commit is contained in:
@@ -6,13 +6,14 @@ import wbotMonitor from "./wbotMonitor";
|
||||
export const StartWhatsAppSessions = async (): Promise<void> => {
|
||||
const whatsapps = await Whatsapp.findAll();
|
||||
if (whatsapps.length > 0) {
|
||||
whatsapps.forEach(whatsapp => {
|
||||
initWbot(whatsapp)
|
||||
.then(() => {
|
||||
wbotMessageListener(whatsapp);
|
||||
wbotMonitor(whatsapp);
|
||||
})
|
||||
.catch(err => console.log(err));
|
||||
whatsapps.forEach(async whatsapp => {
|
||||
try {
|
||||
const wbot = await initWbot(whatsapp);
|
||||
wbotMessageListener(wbot);
|
||||
wbotMonitor(wbot, whatsapp);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -7,20 +7,23 @@ import * as Sentry from "@sentry/node";
|
||||
|
||||
import {
|
||||
Contact as WbotContact,
|
||||
Message as WbotMessage
|
||||
Message as WbotMessage,
|
||||
Client
|
||||
} from "whatsapp-web.js";
|
||||
|
||||
import Contact from "../../models/Contact";
|
||||
import Ticket from "../../models/Ticket";
|
||||
import Message from "../../models/Message";
|
||||
import Whatsapp from "../../models/Whatsapp";
|
||||
|
||||
import { getIO } from "../../libs/socket";
|
||||
import { getWbot } from "../../libs/wbot";
|
||||
import AppError from "../../errors/AppError";
|
||||
import ShowTicketService from "../TicketServices/ShowTicketService";
|
||||
import CreateMessageService from "../MessageServices/CreateMessageService";
|
||||
|
||||
interface Session extends Client {
|
||||
id?: number;
|
||||
}
|
||||
|
||||
const writeFileAsync = promisify(writeFile);
|
||||
|
||||
const verifyContact = async (
|
||||
@@ -236,9 +239,8 @@ const isValidMsg = (msg: WbotMessage): boolean => {
|
||||
return false;
|
||||
};
|
||||
|
||||
const wbotMessageListener = (whatsapp: Whatsapp): void => {
|
||||
const whatsappId = whatsapp.id;
|
||||
const wbot = getWbot(whatsappId);
|
||||
const wbotMessageListener = (wbot: Session): void => {
|
||||
const whatsappId = wbot.id;
|
||||
const io = getIO();
|
||||
|
||||
wbot.on("message_create", async msg => {
|
||||
@@ -278,7 +280,7 @@ const wbotMessageListener = (whatsapp: Whatsapp): void => {
|
||||
|
||||
const profilePicUrl = await msgContact.getProfilePicUrl();
|
||||
const contact = await verifyContact(msgContact, profilePicUrl);
|
||||
const ticket = await verifyTicket(contact, whatsappId, groupContact);
|
||||
const ticket = await verifyTicket(contact, whatsappId!, groupContact);
|
||||
|
||||
await handleMessage(msg, ticket, contact);
|
||||
} catch (err) {
|
||||
@@ -298,7 +300,7 @@ const wbotMessageListener = (whatsapp: Whatsapp): void => {
|
||||
|
||||
const profilePicUrl = await msgContact.getProfilePicUrl();
|
||||
const contact = await verifyContact(msgContact, profilePicUrl);
|
||||
const ticket = await verifyTicket(contact, whatsappId, groupContact);
|
||||
const ticket = await verifyTicket(contact, whatsappId!, groupContact);
|
||||
|
||||
await handleMessage(msg, ticket, contact);
|
||||
} catch (err) {
|
||||
|
||||
@@ -1,15 +1,22 @@
|
||||
import * as Sentry from "@sentry/node";
|
||||
import { Client } from "whatsapp-web.js";
|
||||
|
||||
import wbotMessageListener from "./wbotMessageListener";
|
||||
|
||||
import { getIO } from "../../libs/socket";
|
||||
import { getWbot, initWbot } from "../../libs/wbot";
|
||||
import { initWbot } from "../../libs/wbot";
|
||||
import Whatsapp from "../../models/Whatsapp";
|
||||
|
||||
const wbotMonitor = (whatsapp: Whatsapp): void => {
|
||||
interface Session extends Client {
|
||||
id?: number;
|
||||
}
|
||||
|
||||
const wbotMonitor = async (
|
||||
wbot: Session,
|
||||
whatsapp: Whatsapp
|
||||
): Promise<void> => {
|
||||
const io = getIO();
|
||||
const sessionName = whatsapp.name;
|
||||
const wbot = getWbot(whatsapp.id);
|
||||
|
||||
try {
|
||||
wbot.on("change_state", async newState => {
|
||||
@@ -60,12 +67,14 @@ const wbotMonitor = (whatsapp: Whatsapp): void => {
|
||||
session: whatsapp
|
||||
});
|
||||
|
||||
// to be removed after adding buttons to rebuild session on frontend
|
||||
|
||||
setTimeout(
|
||||
() =>
|
||||
initWbot(whatsapp)
|
||||
.then(() => {
|
||||
wbotMessageListener(whatsapp);
|
||||
wbotMonitor(whatsapp);
|
||||
wbotMessageListener(wbot);
|
||||
wbotMonitor(wbot, whatsapp);
|
||||
})
|
||||
.catch(err => {
|
||||
Sentry.captureException(err);
|
||||
|
||||
Reference in New Issue
Block a user