improvement: stop deleting wbot session if phone is offline (timeout)

This commit is contained in:
canove
2020-10-24 22:54:24 -03:00
parent 63fdecd038
commit 443528d8ed
5 changed files with 107 additions and 100 deletions

View File

@@ -31,12 +31,13 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
isDefault isDefault
}); });
initWbot(whatsapp) try {
.then(() => { const wbot = await initWbot(whatsapp);
wbotMessageListener(whatsapp); wbotMessageListener(wbot);
wbotMonitor(whatsapp); wbotMonitor(wbot, whatsapp);
}) } catch (err) {
.catch(err => console.log(err)); console.log(err);
}
const io = getIO(); const io = getIO();
io.emit("whatsapp", { io.emit("whatsapp", {

View File

@@ -10,82 +10,76 @@ interface Session extends Client {
const sessions: Session[] = []; const sessions: Session[] = [];
export const initWbot = async (whatsapp: Whatsapp): Promise<void> => { export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
try { return new Promise((resolve, reject) => {
const io = getIO(); try {
const sessionName = whatsapp.name; const io = getIO();
let sessionCfg; const sessionName = whatsapp.name;
let sessionCfg;
if (whatsapp && whatsapp.session) { if (whatsapp && whatsapp.session) {
sessionCfg = JSON.parse(whatsapp.session); sessionCfg = JSON.parse(whatsapp.session);
}
const sessionIndex = sessions.findIndex(s => s.id === whatsapp.id);
if (sessionIndex !== -1) {
sessions[sessionIndex].destroy();
sessions.splice(sessionIndex, 1);
}
const wbot: Session = new Client({
session: sessionCfg
});
wbot.initialize();
wbot.on("qr", async qr => {
console.log("Session:", sessionName);
qrCode.generate(qr, { small: true });
await whatsapp.update({ qrcode: qr, status: "qrcode" });
io.emit("whatsappSession", {
action: "update",
session: whatsapp
});
});
wbot.on("authenticated", async session => {
console.log("Session:", sessionName, "AUTHENTICATED");
await whatsapp.update({
session: JSON.stringify(session),
status: "authenticated"
});
io.emit("whatsappSession", {
action: "update",
session: whatsapp
});
});
wbot.on("auth_failure", async msg => {
console.error("Session:", sessionName, "AUTHENTICATION FAILURE", msg);
await whatsapp.update({ status: "OFFLINE" });
reject(new Error("Error starting whatsapp session."));
});
wbot.on("ready", async () => {
console.log("Session:", sessionName, "READY");
await whatsapp.update({
status: "CONNECTED",
qrcode: ""
});
io.emit("whatsappSession", {
action: "update",
session: whatsapp
});
wbot.sendPresenceAvailable();
wbot.id = whatsapp.id;
sessions.push(wbot);
resolve(wbot);
});
} catch (err) {
console.log(err);
} }
});
const sessionIndex = sessions.findIndex(s => s.id === whatsapp.id);
if (sessionIndex !== -1) {
sessions[sessionIndex].destroy();
sessions.splice(sessionIndex, 1);
}
const wbot: Session = new Client({
session: sessionCfg,
restartOnAuthFail: true
});
wbot.initialize();
wbot.on("qr", async qr => {
console.log("Session:", sessionName);
qrCode.generate(qr, { small: true });
await whatsapp.update({ qrcode: qr, status: "qrcode" });
io.emit("whatsappSession", {
action: "update",
session: whatsapp
});
});
wbot.on("authenticated", async session => {
console.log("Session:", sessionName, "AUTHENTICATED");
await whatsapp.update({
session: JSON.stringify(session),
status: "authenticated"
});
io.emit("whatsappSession", {
action: "update",
session: whatsapp
});
});
wbot.on("auth_failure", async msg => {
console.error("Session:", sessionName, "AUTHENTICATION FAILURE", msg);
await whatsapp.update({ session: "" });
});
wbot.on("ready", async () => {
console.log("Session:", sessionName, "READY");
await whatsapp.update({
status: "CONNECTED",
qrcode: ""
});
io.emit("whatsappSession", {
action: "update",
session: whatsapp
});
wbot.sendPresenceAvailable();
});
wbot.id = whatsapp.id;
sessions.push(wbot);
} catch (err) {
console.log(err);
}
}; };
export const getWbot = (whatsappId: number): Session => { export const getWbot = (whatsappId: number): Session => {

View File

@@ -6,13 +6,14 @@ import wbotMonitor from "./wbotMonitor";
export const StartWhatsAppSessions = async (): Promise<void> => { export const StartWhatsAppSessions = async (): Promise<void> => {
const whatsapps = await Whatsapp.findAll(); const whatsapps = await Whatsapp.findAll();
if (whatsapps.length > 0) { if (whatsapps.length > 0) {
whatsapps.forEach(whatsapp => { whatsapps.forEach(async whatsapp => {
initWbot(whatsapp) try {
.then(() => { const wbot = await initWbot(whatsapp);
wbotMessageListener(whatsapp); wbotMessageListener(wbot);
wbotMonitor(whatsapp); wbotMonitor(wbot, whatsapp);
}) } catch (err) {
.catch(err => console.log(err)); console.log(err);
}
}); });
} }
}; };

View File

@@ -7,20 +7,23 @@ import * as Sentry from "@sentry/node";
import { import {
Contact as WbotContact, Contact as WbotContact,
Message as WbotMessage Message as WbotMessage,
Client
} from "whatsapp-web.js"; } from "whatsapp-web.js";
import Contact from "../../models/Contact"; import Contact from "../../models/Contact";
import Ticket from "../../models/Ticket"; import Ticket from "../../models/Ticket";
import Message from "../../models/Message"; import Message from "../../models/Message";
import Whatsapp from "../../models/Whatsapp";
import { getIO } from "../../libs/socket"; import { getIO } from "../../libs/socket";
import { getWbot } from "../../libs/wbot";
import AppError from "../../errors/AppError"; import AppError from "../../errors/AppError";
import ShowTicketService from "../TicketServices/ShowTicketService"; import ShowTicketService from "../TicketServices/ShowTicketService";
import CreateMessageService from "../MessageServices/CreateMessageService"; import CreateMessageService from "../MessageServices/CreateMessageService";
interface Session extends Client {
id?: number;
}
const writeFileAsync = promisify(writeFile); const writeFileAsync = promisify(writeFile);
const verifyContact = async ( const verifyContact = async (
@@ -236,9 +239,8 @@ const isValidMsg = (msg: WbotMessage): boolean => {
return false; return false;
}; };
const wbotMessageListener = (whatsapp: Whatsapp): void => { const wbotMessageListener = (wbot: Session): void => {
const whatsappId = whatsapp.id; const whatsappId = wbot.id;
const wbot = getWbot(whatsappId);
const io = getIO(); const io = getIO();
wbot.on("message_create", async msg => { wbot.on("message_create", async msg => {
@@ -278,7 +280,7 @@ const wbotMessageListener = (whatsapp: Whatsapp): void => {
const profilePicUrl = await msgContact.getProfilePicUrl(); const profilePicUrl = await msgContact.getProfilePicUrl();
const contact = await verifyContact(msgContact, profilePicUrl); 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); await handleMessage(msg, ticket, contact);
} catch (err) { } catch (err) {
@@ -298,7 +300,7 @@ const wbotMessageListener = (whatsapp: Whatsapp): void => {
const profilePicUrl = await msgContact.getProfilePicUrl(); const profilePicUrl = await msgContact.getProfilePicUrl();
const contact = await verifyContact(msgContact, profilePicUrl); 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); await handleMessage(msg, ticket, contact);
} catch (err) { } catch (err) {

View File

@@ -1,15 +1,22 @@
import * as Sentry from "@sentry/node"; import * as Sentry from "@sentry/node";
import { Client } from "whatsapp-web.js";
import wbotMessageListener from "./wbotMessageListener"; import wbotMessageListener from "./wbotMessageListener";
import { getIO } from "../../libs/socket"; import { getIO } from "../../libs/socket";
import { getWbot, initWbot } from "../../libs/wbot"; import { initWbot } from "../../libs/wbot";
import Whatsapp from "../../models/Whatsapp"; 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 io = getIO();
const sessionName = whatsapp.name; const sessionName = whatsapp.name;
const wbot = getWbot(whatsapp.id);
try { try {
wbot.on("change_state", async newState => { wbot.on("change_state", async newState => {
@@ -60,12 +67,14 @@ const wbotMonitor = (whatsapp: Whatsapp): void => {
session: whatsapp session: whatsapp
}); });
// to be removed after adding buttons to rebuild session on frontend
setTimeout( setTimeout(
() => () =>
initWbot(whatsapp) initWbot(whatsapp)
.then(() => { .then(() => {
wbotMessageListener(whatsapp); wbotMessageListener(wbot);
wbotMonitor(whatsapp); wbotMonitor(wbot, whatsapp);
}) })
.catch(err => { .catch(err => {
Sentry.captureException(err); Sentry.captureException(err);