mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 20:29:17 +00:00
feat: finished multiple whatsapps handle in frontend
This commit is contained in:
@@ -60,11 +60,11 @@ const startWhatsAppSessions = async () => {
|
||||
const whatsapps = await Whatsapp.findAll();
|
||||
|
||||
if (whatsapps.length > 0) {
|
||||
whatsapps.forEach(dbSession => {
|
||||
initWbot(dbSession)
|
||||
whatsapps.forEach(whatsapp => {
|
||||
initWbot(whatsapp)
|
||||
.then(() => {
|
||||
wbotMessageListener(dbSession);
|
||||
wbotMonitor(dbSession);
|
||||
wbotMessageListener(whatsapp);
|
||||
wbotMonitor(whatsapp);
|
||||
})
|
||||
.catch(err => console.log(err));
|
||||
});
|
||||
@@ -72,14 +72,6 @@ const startWhatsAppSessions = async () => {
|
||||
};
|
||||
startWhatsAppSessions();
|
||||
|
||||
// wBot
|
||||
// .init()
|
||||
// .then(({ dbSession }) => {
|
||||
// wbotMessageListener();
|
||||
// wbotMonitor(dbSession);
|
||||
// })
|
||||
// .catch(err => console.log(err));
|
||||
|
||||
app.use(Sentry.Handlers.errorHandler());
|
||||
|
||||
app.use(async (err, req, res, next) => {
|
||||
@@ -88,5 +80,6 @@ app.use(async (err, req, res, next) => {
|
||||
console.log(err);
|
||||
return res.status(500).json(errors);
|
||||
}
|
||||
|
||||
return res.status(500).json({ error: "Internal server error" });
|
||||
});
|
||||
|
||||
86
backend/src/controllers/WhatsAppController.js
Normal file
86
backend/src/controllers/WhatsAppController.js
Normal file
@@ -0,0 +1,86 @@
|
||||
const Whatsapp = require("../models/Whatsapp");
|
||||
const { getIO } = require("../libs/socket");
|
||||
const { getWbot, initWbot, removeWbot } = require("../libs/wbot");
|
||||
const wbotMessageListener = require("../services/wbotMessageListener");
|
||||
const wbotMonitor = require("../services/wbotMonitor");
|
||||
|
||||
exports.index = async (req, res) => {
|
||||
const whatsapp = await Whatsapp.findAll();
|
||||
|
||||
return res.status(200).json(whatsapp);
|
||||
};
|
||||
|
||||
exports.store = async (req, res) => {
|
||||
const io = getIO();
|
||||
const whatsapp = await Whatsapp.create(req.body);
|
||||
|
||||
if (!whatsapp) {
|
||||
return res.status(400).json({ error: "Cannot create whatsapp session." });
|
||||
}
|
||||
|
||||
initWbot(whatsapp)
|
||||
.then(() => {
|
||||
wbotMessageListener(whatsapp);
|
||||
wbotMonitor(whatsapp);
|
||||
})
|
||||
.catch(err => console.log(err));
|
||||
|
||||
io.emit("whatsapp", {
|
||||
action: "update",
|
||||
whatsapp: whatsapp,
|
||||
});
|
||||
|
||||
return res.status(200).json(whatsapp);
|
||||
};
|
||||
|
||||
exports.show = async (req, res) => {
|
||||
const { whatsappId } = req.params;
|
||||
const whatsapp = await Whatsapp.findByPk(whatsappId);
|
||||
|
||||
if (!whatsapp) {
|
||||
return res.status(200).json({ message: "Session not found" });
|
||||
}
|
||||
|
||||
return res.status(200).json(whatsapp);
|
||||
};
|
||||
|
||||
exports.update = async (req, res) => {
|
||||
const io = getIO();
|
||||
const { whatsappId } = req.params;
|
||||
|
||||
const whatsapp = await Whatsapp.findByPk(whatsappId);
|
||||
|
||||
if (!whatsapp) {
|
||||
return res.status(404).json({ message: "Whatsapp not found" });
|
||||
}
|
||||
|
||||
await whatsapp.update(req.body);
|
||||
|
||||
io.emit("whatsapp", {
|
||||
action: "update",
|
||||
whatsapp: whatsapp,
|
||||
});
|
||||
|
||||
return res.status(200).json({ message: "Whatsapp updated" });
|
||||
};
|
||||
|
||||
exports.delete = async (req, res) => {
|
||||
const io = getIO();
|
||||
const { whatsappId } = req.params;
|
||||
|
||||
const whatsapp = await Whatsapp.findByPk(whatsappId);
|
||||
|
||||
if (!whatsapp) {
|
||||
return res.status(404).json({ message: "Whatsapp not found" });
|
||||
}
|
||||
|
||||
await whatsapp.destroy();
|
||||
removeWbot(whatsapp.id);
|
||||
|
||||
io.emit("whatsapp", {
|
||||
action: "delete",
|
||||
whatsappId: whatsapp.id,
|
||||
});
|
||||
|
||||
return res.status(200).json({ message: "Whatsapp deleted." });
|
||||
};
|
||||
@@ -1,91 +1,32 @@
|
||||
const Whatsapp = require("../models/Whatsapp");
|
||||
const { getIO } = require("../libs/socket");
|
||||
const { getWbot, initWbot, removeWbot } = require("../libs/wbot");
|
||||
const wbotMessageListener = require("../services/wbotMessageListener");
|
||||
const wbotMonitor = require("../services/wbotMonitor");
|
||||
// const Whatsapp = require("../models/Whatsapp");
|
||||
// const { getIO } = require("../libs/socket");
|
||||
// const { getWbot, initWbot, removeWbot } = require("../libs/wbot");
|
||||
// const wbotMessageListener = require("../services/wbotMessageListener");
|
||||
// const wbotMonitor = require("../services/wbotMonitor");
|
||||
|
||||
exports.index = async (req, res) => {
|
||||
const dbSession = await Whatsapp.findAll();
|
||||
// exports.show = async (req, res) => {
|
||||
// const { whatsappId } = req.params;
|
||||
// const dbSession = await Whatsapp.findByPk(whatsappId);
|
||||
|
||||
return res.status(200).json(dbSession);
|
||||
};
|
||||
// if (!dbSession) {
|
||||
// return res.status(200).json({ message: "Session not found" });
|
||||
// }
|
||||
|
||||
exports.store = async (req, res) => {
|
||||
const io = getIO();
|
||||
const dbSession = await Whatsapp.create(req.body);
|
||||
// return res.status(200).json(dbSession);
|
||||
// };
|
||||
|
||||
if (!dbSession) {
|
||||
return res.status(400).json({ error: "Cannot create whatsapp session." });
|
||||
}
|
||||
// exports.delete = async (req, res) => {
|
||||
// const { whatsappId } = req.params;
|
||||
|
||||
initWbot(dbSession)
|
||||
.then(() => {
|
||||
wbotMessageListener(dbSession);
|
||||
wbotMonitor(dbSession);
|
||||
})
|
||||
.catch(err => console.log(err));
|
||||
// const dbSession = await Whatsapp.findByPk(whatsappId);
|
||||
|
||||
io.emit("session", {
|
||||
action: "update",
|
||||
session: dbSession,
|
||||
});
|
||||
// if (!dbSession) {
|
||||
// return res.status(404).json({ message: "Session not found" });
|
||||
// }
|
||||
|
||||
return res.status(200).json(dbSession);
|
||||
};
|
||||
// const wbot = getWbot(dbSession.id);
|
||||
|
||||
exports.show = async (req, res) => {
|
||||
const { sessionId } = req.params;
|
||||
const dbSession = await Whatsapp.findByPk(sessionId);
|
||||
// wbot.logout();
|
||||
|
||||
if (!dbSession) {
|
||||
return res.status(200).json({ message: "Session not found" });
|
||||
}
|
||||
|
||||
return res.status(200).json(dbSession);
|
||||
};
|
||||
|
||||
exports.update = async (req, res) => {
|
||||
const io = getIO();
|
||||
const { sessionId } = req.params;
|
||||
|
||||
const dbSession = await Whatsapp.findByPk(sessionId);
|
||||
|
||||
if (!dbSession) {
|
||||
return res.status(404).json({ message: "Session not found" });
|
||||
}
|
||||
|
||||
const wbot = getWbot(dbSession.id);
|
||||
wbot.logout();
|
||||
|
||||
await dbSession.update(req.body);
|
||||
|
||||
io.emit("session", {
|
||||
action: "update",
|
||||
session: dbSession,
|
||||
});
|
||||
|
||||
return res.status(200).json({ message: "Session updated" });
|
||||
};
|
||||
|
||||
exports.delete = async (req, res) => {
|
||||
const io = getIO();
|
||||
const { sessionId } = req.params;
|
||||
|
||||
const dbSession = await Whatsapp.findByPk(sessionId);
|
||||
|
||||
if (!dbSession) {
|
||||
return res.status(404).json({ message: "Session not found" });
|
||||
}
|
||||
|
||||
const wbot = getWbot(dbSession.id);
|
||||
await dbSession.destroy();
|
||||
|
||||
removeWbot(dbSession.id);
|
||||
|
||||
io.emit("session", {
|
||||
action: "delete",
|
||||
sessionId: dbSession.id,
|
||||
});
|
||||
|
||||
return res.status(200).json({ message: "Session deleted." });
|
||||
};
|
||||
// return res.status(200).json({ message: "Session disconnected." });
|
||||
// };
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = {
|
||||
up: (queryInterface, Sequelize) => {
|
||||
return queryInterface.addColumn("Whatsapps", "default", {
|
||||
type: Sequelize.BOOLEAN,
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
});
|
||||
},
|
||||
|
||||
down: queryInterface => {
|
||||
return queryInterface.removeColumn("Whatsapps", "default");
|
||||
},
|
||||
};
|
||||
@@ -6,17 +6,17 @@ const { getIO } = require("../libs/socket");
|
||||
let sessions = [];
|
||||
|
||||
module.exports = {
|
||||
initWbot: async dbSession => {
|
||||
initWbot: async whatsapp => {
|
||||
try {
|
||||
const io = getIO();
|
||||
const sessionName = dbSession.name;
|
||||
const sessionName = whatsapp.name;
|
||||
let sessionCfg;
|
||||
|
||||
if (dbSession && dbSession.session) {
|
||||
sessionCfg = JSON.parse(dbSession.session);
|
||||
if (whatsapp && whatsapp.session) {
|
||||
sessionCfg = JSON.parse(whatsapp.session);
|
||||
}
|
||||
|
||||
const sessionIndex = sessions.findIndex(s => s.id === dbSession.id);
|
||||
const sessionIndex = sessions.findIndex(s => s.id === whatsapp.id);
|
||||
if (sessionIndex !== -1) {
|
||||
sessions[sessionIndex].destroy();
|
||||
sessions.splice(sessionIndex, 1);
|
||||
@@ -33,51 +33,51 @@ module.exports = {
|
||||
|
||||
qrCode.generate(qr, { small: true });
|
||||
|
||||
await dbSession.update({ id: 1, qrcode: qr, status: "qrcode" });
|
||||
await whatsapp.update({ qrcode: qr, status: "qrcode" });
|
||||
|
||||
io.emit("session", {
|
||||
io.emit("whatsappSession", {
|
||||
action: "update",
|
||||
session: dbSession,
|
||||
session: whatsapp,
|
||||
});
|
||||
});
|
||||
|
||||
wbot.on("authenticated", async session => {
|
||||
console.log("Session:", sessionName, "AUTHENTICATED");
|
||||
|
||||
await dbSession.update({
|
||||
await whatsapp.update({
|
||||
session: JSON.stringify(session),
|
||||
status: "authenticated",
|
||||
});
|
||||
|
||||
io.emit("session", {
|
||||
io.emit("whatsappSession", {
|
||||
action: "update",
|
||||
session: dbSession,
|
||||
session: whatsapp,
|
||||
});
|
||||
});
|
||||
|
||||
wbot.on("auth_failure", async msg => {
|
||||
console.error("Session:", sessionName, "AUTHENTICATION FAILURE", msg);
|
||||
|
||||
await dbSession.update({ session: "" });
|
||||
await whatsapp.update({ session: "" });
|
||||
});
|
||||
|
||||
wbot.on("ready", async () => {
|
||||
console.log("Session:", sessionName, "READY");
|
||||
|
||||
await dbSession.update({
|
||||
await whatsapp.update({
|
||||
status: "CONNECTED",
|
||||
qrcode: "",
|
||||
});
|
||||
|
||||
io.emit("session", {
|
||||
io.emit("whatsappSession", {
|
||||
action: "update",
|
||||
session: dbSession,
|
||||
session: whatsapp,
|
||||
});
|
||||
|
||||
wbot.sendPresenceAvailable();
|
||||
});
|
||||
|
||||
wbot.id = dbSession.id;
|
||||
wbot.id = whatsapp.id;
|
||||
sessions.push(wbot);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
|
||||
@@ -10,6 +10,11 @@ class Whatsapp extends Sequelize.Model {
|
||||
status: { type: Sequelize.STRING },
|
||||
battery: { type: Sequelize.STRING },
|
||||
plugged: { type: Sequelize.BOOLEAN },
|
||||
default: {
|
||||
type: Sequelize.BOOLEAN,
|
||||
defaultValue: false,
|
||||
allowNull: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
|
||||
@@ -1,30 +1,18 @@
|
||||
const express = require("express");
|
||||
const isAuth = require("../../middleware/is-auth");
|
||||
|
||||
const WhatsAppSessionController = require("../../controllers/WhatsAppSessionController");
|
||||
const WhatsAppController = require("../../controllers/WhatsAppController");
|
||||
|
||||
const routes = express.Router();
|
||||
|
||||
routes.get("/whatsapp/session/", isAuth, WhatsAppSessionController.index);
|
||||
routes.get("/whatsapp/", isAuth, WhatsAppController.index);
|
||||
|
||||
routes.post("/whatsapp/session", isAuth, WhatsAppSessionController.store);
|
||||
routes.post("/whatsapp/", isAuth, WhatsAppController.store);
|
||||
|
||||
routes.get(
|
||||
"/whatsapp/session/:sessionId",
|
||||
isAuth,
|
||||
WhatsAppSessionController.show
|
||||
);
|
||||
routes.get("/whatsapp/:whatsappId", isAuth, WhatsAppController.show);
|
||||
|
||||
routes.put(
|
||||
"/whatsapp/session/:sessionId",
|
||||
isAuth,
|
||||
WhatsAppSessionController.update
|
||||
);
|
||||
routes.put("/whatsapp/:whatsappId", isAuth, WhatsAppController.update);
|
||||
|
||||
routes.delete(
|
||||
"/whatsapp/session/:sessionId",
|
||||
isAuth,
|
||||
WhatsAppSessionController.delete
|
||||
);
|
||||
routes.delete("/whatsapp/:whatsappId", isAuth, WhatsAppController.delete);
|
||||
|
||||
module.exports = routes;
|
||||
|
||||
20
backend/src/router/routes/whatsappsessions.js
Normal file
20
backend/src/router/routes/whatsappsessions.js
Normal file
@@ -0,0 +1,20 @@
|
||||
const express = require("express");
|
||||
const isAuth = require("../../middleware/is-auth");
|
||||
|
||||
const WhatsAppSessionController = require("../../controllers/WhatsAppSessionController");
|
||||
|
||||
const routes = express.Router();
|
||||
|
||||
routes.get(
|
||||
"/whatsappsession/:whatsappId",
|
||||
isAuth,
|
||||
WhatsAppSessionController.show
|
||||
);
|
||||
|
||||
routes.delete(
|
||||
"/whatsappsession/:whatsappId",
|
||||
isAuth,
|
||||
WhatsAppSessionController.delete
|
||||
);
|
||||
|
||||
module.exports = routes;
|
||||
@@ -132,8 +132,8 @@ const handleMessage = async (msg, ticket, contact) => {
|
||||
});
|
||||
};
|
||||
|
||||
const wbotMessageListener = dbSession => {
|
||||
const wbot = getWbot(dbSession.id);
|
||||
const wbotMessageListener = whatsapp => {
|
||||
const wbot = getWbot(whatsapp.id);
|
||||
const io = getIO();
|
||||
|
||||
wbot.on("message_create", async msg => {
|
||||
|
||||
@@ -5,16 +5,16 @@ const wbotMessageListener = require("./wbotMessageListener");
|
||||
const { getIO } = require("../libs/socket");
|
||||
const { getWbot, initWbot } = require("../libs/wbot");
|
||||
|
||||
const wbotMonitor = dbSession => {
|
||||
const wbotMonitor = whatsapp => {
|
||||
const io = getIO();
|
||||
const sessionName = dbSession.name;
|
||||
const wbot = getWbot(dbSession.id);
|
||||
const sessionName = whatsapp.name;
|
||||
const wbot = getWbot(whatsapp.id);
|
||||
|
||||
try {
|
||||
wbot.on("change_state", async newState => {
|
||||
console.log("Monitor session:", sessionName, newState);
|
||||
try {
|
||||
await dbSession.update({ status: newState });
|
||||
await whatsapp.update({ status: newState });
|
||||
} catch (err) {
|
||||
Sentry.captureException(err);
|
||||
console.log(err);
|
||||
@@ -22,7 +22,7 @@ const wbotMonitor = dbSession => {
|
||||
|
||||
io.emit("session", {
|
||||
action: "update",
|
||||
session: dbSession,
|
||||
session: whatsapp,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -33,7 +33,7 @@ const wbotMonitor = dbSession => {
|
||||
);
|
||||
|
||||
try {
|
||||
await dbSession.update({ battery, plugged });
|
||||
await whatsapp.update({ battery, plugged });
|
||||
} catch (err) {
|
||||
Sentry.captureException(err);
|
||||
console.log(err);
|
||||
@@ -41,14 +41,14 @@ const wbotMonitor = dbSession => {
|
||||
|
||||
io.emit("session", {
|
||||
action: "update",
|
||||
session: dbSession,
|
||||
session: whatsapp,
|
||||
});
|
||||
});
|
||||
|
||||
wbot.on("disconnected", async reason => {
|
||||
console.log("Disconnected session:", sessionName, reason);
|
||||
try {
|
||||
await dbSession.update({ status: "disconnected" });
|
||||
await whatsapp.update({ status: "disconnected" });
|
||||
} catch (err) {
|
||||
Sentry.captureException(err);
|
||||
console.log(err);
|
||||
@@ -56,15 +56,15 @@ const wbotMonitor = dbSession => {
|
||||
|
||||
io.emit("session", {
|
||||
action: "update",
|
||||
session: dbSession,
|
||||
session: whatsapp,
|
||||
});
|
||||
|
||||
setTimeout(
|
||||
() =>
|
||||
initWbot(dbSession)
|
||||
initWbot(whatsapp)
|
||||
.then(() => {
|
||||
wbotMessageListener(dbSession);
|
||||
wbotMonitor(dbSession);
|
||||
wbotMessageListener(whatsapp);
|
||||
wbotMonitor(whatsapp);
|
||||
})
|
||||
.catch(err => {
|
||||
Sentry.captureException(err);
|
||||
|
||||
Reference in New Issue
Block a user