2022-01-06 01:26:15 +00:00
|
|
|
import qrCode from "qrcode-terminal";
|
|
|
|
import { Client } from "whatsapp-web.js";
|
|
|
|
import { getIO } from "./socket";
|
|
|
|
import Whatsapp from "../models/Whatsapp";
|
|
|
|
import AppError from "../errors/AppError";
|
|
|
|
import { logger } from "../utils/logger";
|
|
|
|
import { handleMessage } from "../services/WbotServices/wbotMessageListener";
|
2022-02-07 13:58:27 +00:00
|
|
|
const fs = require('fs')
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
interface Session extends Client {
|
|
|
|
id?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sessions: Session[] = [];
|
|
|
|
|
|
|
|
const syncUnreadMessages = async (wbot: Session) => {
|
|
|
|
const chats = await wbot.getChats();
|
|
|
|
|
|
|
|
/* eslint-disable no-restricted-syntax */
|
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
for (const chat of chats) {
|
|
|
|
if (chat.unreadCount > 0) {
|
|
|
|
const unreadMessages = await chat.fetchMessages({
|
|
|
|
limit: chat.unreadCount
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const msg of unreadMessages) {
|
2022-01-10 20:10:20 +00:00
|
|
|
console.log('--BACKEND MSG: ', msg)
|
2022-01-06 01:26:15 +00:00
|
|
|
await handleMessage(msg, wbot);
|
|
|
|
}
|
|
|
|
|
|
|
|
await chat.sendSeen();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
try {
|
2022-02-07 13:58:27 +00:00
|
|
|
// const io = getIO();
|
|
|
|
// const sessionName = whatsapp.name;
|
|
|
|
// let sessionCfg;
|
|
|
|
|
|
|
|
// if (whatsapp && whatsapp.session) {
|
|
|
|
// sessionCfg = JSON.parse(whatsapp.session);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// const wbot: Session = new Client({
|
|
|
|
// session: sessionCfg,
|
|
|
|
// puppeteer: {
|
|
|
|
// executablePath: process.env.CHROME_BIN || undefined
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const io = getIO();
|
|
|
|
const sessionName = whatsapp.name;
|
2022-02-07 13:58:27 +00:00
|
|
|
const SESSION_FILE_PATH = './session.json'
|
|
|
|
let sessionCfg
|
|
|
|
if(fs.existsSync(SESSION_FILE_PATH)){
|
|
|
|
sessionCfg = require(SESSION_FILE_PATH)
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
2022-02-07 13:58:27 +00:00
|
|
|
const wbot: Session = new Client({ puppeteer: { headless: true }, clientId: 'bd_'+whatsapp.id})
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
wbot.initialize();
|
|
|
|
|
|
|
|
wbot.on("qr", async qr => {
|
|
|
|
logger.info("Session:", sessionName);
|
|
|
|
qrCode.generate(qr, { small: true });
|
|
|
|
await whatsapp.update({ qrcode: qr, status: "qrcode", retries: 0 });
|
|
|
|
|
|
|
|
const sessionIndex = sessions.findIndex(s => s.id === whatsapp.id);
|
|
|
|
if (sessionIndex === -1) {
|
|
|
|
wbot.id = whatsapp.id;
|
|
|
|
sessions.push(wbot);
|
|
|
|
}
|
|
|
|
|
|
|
|
io.emit("whatsappSession", {
|
|
|
|
action: "update",
|
|
|
|
session: whatsapp
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
wbot.on("authenticated", async session => {
|
|
|
|
logger.info(`Session: ${sessionName} AUTHENTICATED`);
|
|
|
|
await whatsapp.update({
|
|
|
|
session: JSON.stringify(session)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
wbot.on("auth_failure", async msg => {
|
|
|
|
console.error(
|
|
|
|
`Session: ${sessionName} AUTHENTICATION FAILURE! Reason: ${msg}`
|
|
|
|
);
|
|
|
|
|
|
|
|
if (whatsapp.retries > 1) {
|
|
|
|
await whatsapp.update({ session: "", retries: 0 });
|
|
|
|
}
|
|
|
|
|
|
|
|
const retry = whatsapp.retries;
|
|
|
|
await whatsapp.update({
|
|
|
|
status: "DISCONNECTED",
|
|
|
|
retries: retry + 1
|
|
|
|
});
|
|
|
|
|
|
|
|
io.emit("whatsappSession", {
|
|
|
|
action: "update",
|
|
|
|
session: whatsapp
|
|
|
|
});
|
|
|
|
|
|
|
|
reject(new Error("Error starting whatsapp session."));
|
|
|
|
});
|
|
|
|
|
|
|
|
wbot.on("ready", async () => {
|
|
|
|
logger.info(`Session: ${sessionName} READY`);
|
|
|
|
|
|
|
|
await whatsapp.update({
|
|
|
|
status: "CONNECTED",
|
|
|
|
qrcode: "",
|
|
|
|
retries: 0
|
|
|
|
});
|
|
|
|
|
|
|
|
io.emit("whatsappSession", {
|
|
|
|
action: "update",
|
|
|
|
session: whatsapp
|
|
|
|
});
|
|
|
|
|
|
|
|
const sessionIndex = sessions.findIndex(s => s.id === whatsapp.id);
|
|
|
|
if (sessionIndex === -1) {
|
|
|
|
wbot.id = whatsapp.id;
|
|
|
|
sessions.push(wbot);
|
|
|
|
}
|
|
|
|
|
|
|
|
wbot.sendPresenceAvailable();
|
|
|
|
await syncUnreadMessages(wbot);
|
|
|
|
|
|
|
|
resolve(wbot);
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getWbot = (whatsappId: number): Session => {
|
|
|
|
const sessionIndex = sessions.findIndex(s => s.id === whatsappId);
|
|
|
|
|
|
|
|
if (sessionIndex === -1) {
|
|
|
|
throw new AppError("ERR_WAPP_NOT_INITIALIZED");
|
|
|
|
}
|
|
|
|
return sessions[sessionIndex];
|
|
|
|
};
|
|
|
|
|
|
|
|
export const removeWbot = (whatsappId: number): void => {
|
|
|
|
try {
|
|
|
|
const sessionIndex = sessions.findIndex(s => s.id === whatsappId);
|
|
|
|
if (sessionIndex !== -1) {
|
|
|
|
sessions[sessionIndex].destroy();
|
|
|
|
sessions.splice(sessionIndex, 1);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(err);
|
|
|
|
}
|
|
|
|
};
|