mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 03:39:29 +00:00
feat: started multiple whatsapps support
This commit is contained in:
@@ -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());
|
||||
|
||||
|
||||
@@ -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");
|
||||
},
|
||||
};
|
||||
@@ -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];
|
||||
},
|
||||
};
|
||||
|
||||
@@ -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 },
|
||||
|
||||
@@ -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" ||
|
||||
|
||||
@@ -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);
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user