mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 04:09:26 +00:00
feat: new connections page to handle multiple whats
This commit is contained in:
@@ -7,6 +7,7 @@ let sessions = [];
|
||||
|
||||
module.exports = {
|
||||
init: async dbSession => {
|
||||
const io = getIO();
|
||||
const sessionName = dbSession.name;
|
||||
let sessionCfg;
|
||||
|
||||
@@ -14,43 +15,64 @@ module.exports = {
|
||||
sessionCfg = JSON.parse(dbSession.session);
|
||||
}
|
||||
|
||||
const sessionIndex = sessions.findIndex(s => s.id === dbSession.id);
|
||||
if (sessionIndex !== -1) {
|
||||
sessions[sessionIndex].destroy();
|
||||
sessions.splice(sessionIndex, 1);
|
||||
}
|
||||
|
||||
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", {
|
||||
|
||||
await dbSession.update({ id: 1, qrcode: qr, status: "qrcode" });
|
||||
|
||||
io.emit("session", {
|
||||
action: "update",
|
||||
qr: qr,
|
||||
session: dbSession,
|
||||
});
|
||||
});
|
||||
|
||||
wbot.on("authenticated", async session => {
|
||||
console.log("Session:", sessionName, "AUTHENTICATED");
|
||||
|
||||
await dbSession.update({
|
||||
session: JSON.stringify(session),
|
||||
status: "authenticated",
|
||||
});
|
||||
getIO().emit("session", {
|
||||
action: "authentication",
|
||||
|
||||
io.emit("session", {
|
||||
action: "update",
|
||||
session: dbSession,
|
||||
});
|
||||
});
|
||||
|
||||
wbot.on("auth_failure", async msg => {
|
||||
console.error("Session:", sessionName, "AUTHENTICATION FAILURE", msg);
|
||||
|
||||
await dbSession.update({ session: "" });
|
||||
});
|
||||
|
||||
wbot.on("ready", async () => {
|
||||
console.log("Session:", sessionName, "READY");
|
||||
|
||||
await dbSession.update({
|
||||
status: "CONNECTED",
|
||||
qrcode: "",
|
||||
});
|
||||
|
||||
io.emit("session", {
|
||||
action: "update",
|
||||
session: dbSession,
|
||||
});
|
||||
|
||||
// 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) {
|
||||
@@ -62,16 +84,19 @@ module.exports = {
|
||||
// }
|
||||
|
||||
// console.log(unreadMessages);
|
||||
// wbot.sendPresenceAvailable();
|
||||
wbot.sendPresenceAvailable();
|
||||
});
|
||||
|
||||
wbot.name = sessionName;
|
||||
wbot.id = dbSession.id;
|
||||
sessions.push(wbot);
|
||||
|
||||
return null;
|
||||
},
|
||||
|
||||
getWbot: sessionName => {
|
||||
const sessionIndex = sessions.findIndex(s => s.name === sessionName);
|
||||
getWbot: sessionId => {
|
||||
console.log(sessionId);
|
||||
console.log(sessions.map(session => session.id));
|
||||
const sessionIndex = sessions.findIndex(s => s.id === sessionId);
|
||||
|
||||
if (sessionIndex === -1) {
|
||||
throw new Error("This Wbot session is not initialized");
|
||||
|
||||
Reference in New Issue
Block a user