mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 04:39:20 +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 wBot = require("./libs/wbot");
|
||||||
const wbotMessageListener = require("./services/wbotMessageListener");
|
const wbotMessageListener = require("./services/wbotMessageListener");
|
||||||
const wbotMonitor = require("./services/wbotMonitor");
|
const wbotMonitor = require("./services/wbotMonitor");
|
||||||
|
const Whatsapp = require("./models/Whatsapp");
|
||||||
|
|
||||||
const Router = require("./router");
|
const Router = require("./router");
|
||||||
|
|
||||||
@@ -55,13 +56,30 @@ io.on("connection", socket => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
wBot
|
const startWhatsAppSessions = async () => {
|
||||||
.init()
|
const whatsapps = await Whatsapp.findAll();
|
||||||
.then(({ dbSession }) => {
|
|
||||||
wbotMessageListener();
|
if (whatsapps.length > 0) {
|
||||||
wbotMonitor(dbSession);
|
whatsapps.forEach(dbSession => {
|
||||||
})
|
wBot
|
||||||
.catch(err => console.log(err));
|
.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());
|
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 Whatsapp = require("../models/Whatsapp");
|
||||||
const { getIO } = require("../libs/socket");
|
const { getIO } = require("../libs/socket");
|
||||||
|
|
||||||
let wbot;
|
let sessions = [];
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
init: async () => {
|
init: async dbSession => {
|
||||||
|
const sessionName = dbSession.name;
|
||||||
let sessionCfg;
|
let sessionCfg;
|
||||||
|
|
||||||
const [dbSession] = await Whatsapp.findOrCreate({
|
|
||||||
where: { id: 1 },
|
|
||||||
defaults: {
|
|
||||||
id: 1,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
if (dbSession && dbSession.session) {
|
if (dbSession && dbSession.session) {
|
||||||
sessionCfg = JSON.parse(dbSession.session);
|
sessionCfg = JSON.parse(dbSession.session);
|
||||||
}
|
}
|
||||||
|
|
||||||
wbot = new Client({
|
const wbot = new Client({
|
||||||
session: sessionCfg,
|
session: sessionCfg,
|
||||||
restartOnAuthFail: true,
|
restartOnAuthFail: true,
|
||||||
});
|
});
|
||||||
wbot.initialize();
|
wbot.initialize();
|
||||||
wbot.on("qr", async qr => {
|
wbot.on("qr", async qr => {
|
||||||
|
console.log("Session:", sessionName);
|
||||||
qrCode.generate(qr, { small: true });
|
qrCode.generate(qr, { small: true });
|
||||||
await dbSession.update({ id: 1, qrcode: qr, status: "disconnected" });
|
await dbSession.update({ id: 1, qrcode: qr, status: "disconnected" });
|
||||||
getIO().emit("session", {
|
getIO().emit("session", {
|
||||||
@@ -34,9 +30,8 @@ module.exports = {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
wbot.on("authenticated", async session => {
|
wbot.on("authenticated", async session => {
|
||||||
console.log("AUTHENTICATED");
|
console.log("Session:", sessionName, "AUTHENTICATED");
|
||||||
await dbSession.update({
|
await dbSession.update({
|
||||||
id: 1,
|
|
||||||
session: JSON.stringify(session),
|
session: JSON.stringify(session),
|
||||||
status: "authenticated",
|
status: "authenticated",
|
||||||
});
|
});
|
||||||
@@ -46,15 +41,16 @@ module.exports = {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
wbot.on("auth_failure", async msg => {
|
wbot.on("auth_failure", async msg => {
|
||||||
console.error("AUTHENTICATION FAILURE", msg);
|
console.error("Session:", sessionName, "AUTHENTICATION FAILURE", msg);
|
||||||
await Whatsapp.update({ session: "" }, { where: { id: 1 } });
|
await dbSession.update({ session: "" });
|
||||||
});
|
});
|
||||||
wbot.on("ready", async () => {
|
wbot.on("ready", async () => {
|
||||||
console.log("READY");
|
console.log("Session:", sessionName, "READY");
|
||||||
await dbSession.update(
|
await dbSession.update({
|
||||||
{ status: "CONNECTED", qrcode: "" },
|
status: "CONNECTED",
|
||||||
{ where: { id: 1 } }
|
qrcode: "",
|
||||||
);
|
});
|
||||||
|
|
||||||
// const chats = await wbot.getChats(); // pega as mensagens nao lidas (recebidas quando o bot estava offline)
|
// 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
|
// let unreadMessages; // todo > salvar isso no DB pra mostrar no frontend
|
||||||
// for (let chat of chats) {
|
// for (let chat of chats) {
|
||||||
@@ -66,15 +62,20 @@ module.exports = {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
// console.log(unreadMessages);
|
// console.log(unreadMessages);
|
||||||
wbot.sendPresenceAvailable();
|
// wbot.sendPresenceAvailable();
|
||||||
});
|
});
|
||||||
return { wbot, dbSession };
|
|
||||||
|
wbot.name = sessionName;
|
||||||
|
sessions.push(wbot);
|
||||||
|
return null;
|
||||||
},
|
},
|
||||||
|
|
||||||
getWbot: () => {
|
getWbot: sessionName => {
|
||||||
if (!wbot) {
|
const sessionIndex = sessions.findIndex(s => s.name === sessionName);
|
||||||
throw new Error("Wbot not initialized");
|
|
||||||
|
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 },
|
session: { type: Sequelize.TEXT },
|
||||||
qrcode: { type: Sequelize.TEXT },
|
qrcode: { type: Sequelize.TEXT },
|
||||||
|
name: { type: Sequelize.STRING, unique: true, allowNull: false },
|
||||||
status: { type: Sequelize.STRING },
|
status: { type: Sequelize.STRING },
|
||||||
battery: { type: Sequelize.STRING },
|
battery: { type: Sequelize.STRING },
|
||||||
plugged: { type: Sequelize.BOOLEAN },
|
plugged: { type: Sequelize.BOOLEAN },
|
||||||
|
|||||||
@@ -132,12 +132,13 @@ const handleMessage = async (msg, ticket, contact) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const wbotMessageListener = () => {
|
const wbotMessageListener = dbSession => {
|
||||||
const wbot = getWbot();
|
const wbot = getWbot(dbSession.name);
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
|
|
||||||
wbot.on("message_create", async msg => {
|
wbot.on("message_create", async msg => {
|
||||||
console.log(msg);
|
console.log(msg);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
msg.from === "status@broadcast" ||
|
msg.from === "status@broadcast" ||
|
||||||
msg.type === "location" ||
|
msg.type === "location" ||
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ const { getWbot, init } = require("../libs/wbot");
|
|||||||
|
|
||||||
const wbotMonitor = dbSession => {
|
const wbotMonitor = dbSession => {
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
const wbot = getWbot();
|
const wbot = getWbot(dbSession.name);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
wbot.on("change_state", async newState => {
|
wbot.on("change_state", async newState => {
|
||||||
@@ -58,8 +58,8 @@ const wbotMonitor = dbSession => {
|
|||||||
|
|
||||||
setTimeout(
|
setTimeout(
|
||||||
() =>
|
() =>
|
||||||
init()
|
init(dbSession)
|
||||||
.then(({ dbSession }) => {
|
.then(() => {
|
||||||
wbotMessageListener();
|
wbotMessageListener();
|
||||||
wbotMonitor(dbSession);
|
wbotMonitor(dbSession);
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user