mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 12:19:16 +00:00
feat: finished multiple whatsapps handle in frontend
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user