diff --git a/backend/src/app.js b/backend/src/app.js index 98f1607..ba664b9 100644 --- a/backend/src/app.js +++ b/backend/src/app.js @@ -10,6 +10,7 @@ const Sentry = require("@sentry/node"); const wBot = require("./libs/wbot"); const wbotMessageListener = require("./services/wbotMessageListener"); const wbotMonitor = require("./services/wbotMonitor"); +const Whatsapp = require("./models/Whatsapp"); const Router = require("./router"); @@ -55,13 +56,30 @@ io.on("connection", socket => { }); }); -wBot - .init() - .then(({ dbSession }) => { - wbotMessageListener(); - wbotMonitor(dbSession); - }) - .catch(err => console.log(err)); +const startWhatsAppSessions = async () => { + const whatsapps = await Whatsapp.findAll(); + + if (whatsapps.length > 0) { + whatsapps.forEach(dbSession => { + wBot + .init(dbSession) + .then(() => { + wbotMessageListener(dbSession); + wbotMonitor(dbSession); + }) + .catch(err => console.log(err)); + }); + } +}; +startWhatsAppSessions(); + +// wBot +// .init() +// .then(({ dbSession }) => { +// wbotMessageListener(); +// wbotMonitor(dbSession); +// }) +// .catch(err => console.log(err)); app.use(Sentry.Handlers.errorHandler()); diff --git a/backend/src/database/migrations/20200904220257-add-name-to-whatsapp.js b/backend/src/database/migrations/20200904220257-add-name-to-whatsapp.js new file mode 100644 index 0000000..b2fefc2 --- /dev/null +++ b/backend/src/database/migrations/20200904220257-add-name-to-whatsapp.js @@ -0,0 +1,15 @@ +"use strict"; + +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.addColumn("Whatsapps", "name", { + type: Sequelize.STRING, + allowNull: false, + unique: true, + }); + }, + + down: queryInterface => { + return queryInterface.removeColumn("Whatsapps", "name"); + }, +}; diff --git a/backend/src/libs/wbot.js b/backend/src/libs/wbot.js index ff62ade..ee98d2f 100644 --- a/backend/src/libs/wbot.js +++ b/backend/src/libs/wbot.js @@ -3,28 +3,24 @@ const { Client } = require("whatsapp-web.js"); const Whatsapp = require("../models/Whatsapp"); const { getIO } = require("../libs/socket"); -let wbot; +let sessions = []; module.exports = { - init: async () => { + init: async dbSession => { + const sessionName = dbSession.name; let sessionCfg; - const [dbSession] = await Whatsapp.findOrCreate({ - where: { id: 1 }, - defaults: { - id: 1, - }, - }); if (dbSession && dbSession.session) { sessionCfg = JSON.parse(dbSession.session); } - wbot = new Client({ + const wbot = new Client({ session: sessionCfg, restartOnAuthFail: true, }); wbot.initialize(); wbot.on("qr", async qr => { + console.log("Session:", sessionName); qrCode.generate(qr, { small: true }); await dbSession.update({ id: 1, qrcode: qr, status: "disconnected" }); getIO().emit("session", { @@ -34,9 +30,8 @@ module.exports = { }); }); wbot.on("authenticated", async session => { - console.log("AUTHENTICATED"); + console.log("Session:", sessionName, "AUTHENTICATED"); await dbSession.update({ - id: 1, session: JSON.stringify(session), status: "authenticated", }); @@ -46,15 +41,16 @@ module.exports = { }); }); wbot.on("auth_failure", async msg => { - console.error("AUTHENTICATION FAILURE", msg); - await Whatsapp.update({ session: "" }, { where: { id: 1 } }); + console.error("Session:", sessionName, "AUTHENTICATION FAILURE", msg); + await dbSession.update({ session: "" }); }); wbot.on("ready", async () => { - console.log("READY"); - await dbSession.update( - { status: "CONNECTED", qrcode: "" }, - { where: { id: 1 } } - ); + console.log("Session:", sessionName, "READY"); + await dbSession.update({ + status: "CONNECTED", + qrcode: "", + }); + // const chats = await wbot.getChats(); // pega as mensagens nao lidas (recebidas quando o bot estava offline) // let unreadMessages; // todo > salvar isso no DB pra mostrar no frontend // for (let chat of chats) { @@ -66,15 +62,20 @@ module.exports = { // } // console.log(unreadMessages); - wbot.sendPresenceAvailable(); + // wbot.sendPresenceAvailable(); }); - return { wbot, dbSession }; + + wbot.name = sessionName; + sessions.push(wbot); + return null; }, - getWbot: () => { - if (!wbot) { - throw new Error("Wbot not initialized"); + getWbot: sessionName => { + const sessionIndex = sessions.findIndex(s => s.name === sessionName); + + if (sessionIndex === -1) { + throw new Error("This Wbot session is not initialized"); } - return wbot; + return sessions[sessionIndex]; }, }; diff --git a/backend/src/models/Whatsapp.js b/backend/src/models/Whatsapp.js index d60604a..05b57a3 100644 --- a/backend/src/models/Whatsapp.js +++ b/backend/src/models/Whatsapp.js @@ -6,6 +6,7 @@ class Whatsapp extends Sequelize.Model { { session: { type: Sequelize.TEXT }, qrcode: { type: Sequelize.TEXT }, + name: { type: Sequelize.STRING, unique: true, allowNull: false }, status: { type: Sequelize.STRING }, battery: { type: Sequelize.STRING }, plugged: { type: Sequelize.BOOLEAN }, diff --git a/backend/src/services/wbotMessageListener.js b/backend/src/services/wbotMessageListener.js index 329640b..b6dcb32 100644 --- a/backend/src/services/wbotMessageListener.js +++ b/backend/src/services/wbotMessageListener.js @@ -132,12 +132,13 @@ const handleMessage = async (msg, ticket, contact) => { }); }; -const wbotMessageListener = () => { - const wbot = getWbot(); +const wbotMessageListener = dbSession => { + const wbot = getWbot(dbSession.name); const io = getIO(); wbot.on("message_create", async msg => { console.log(msg); + if ( msg.from === "status@broadcast" || msg.type === "location" || diff --git a/backend/src/services/wbotMonitor.js b/backend/src/services/wbotMonitor.js index b8e4145..b49b8a1 100644 --- a/backend/src/services/wbotMonitor.js +++ b/backend/src/services/wbotMonitor.js @@ -7,7 +7,7 @@ const { getWbot, init } = require("../libs/wbot"); const wbotMonitor = dbSession => { const io = getIO(); - const wbot = getWbot(); + const wbot = getWbot(dbSession.name); try { wbot.on("change_state", async newState => { @@ -58,8 +58,8 @@ const wbotMonitor = dbSession => { setTimeout( () => - init() - .then(({ dbSession }) => { + init(dbSession) + .then(() => { wbotMessageListener(); wbotMonitor(dbSession); })