mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 11:49:19 +00:00
improvement: better names on wbot methods
This commit is contained in:
@@ -6,90 +6,83 @@ const { getIO } = require("../libs/socket");
|
||||
let sessions = [];
|
||||
|
||||
module.exports = {
|
||||
init: async dbSession => {
|
||||
const io = getIO();
|
||||
const sessionName = dbSession.name;
|
||||
let sessionCfg;
|
||||
initWbot: async dbSession => {
|
||||
try {
|
||||
const io = getIO();
|
||||
const sessionName = dbSession.name;
|
||||
let sessionCfg;
|
||||
|
||||
if (dbSession && dbSession.session) {
|
||||
sessionCfg = JSON.parse(dbSession.session);
|
||||
if (dbSession && dbSession.session) {
|
||||
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: "qrcode" });
|
||||
|
||||
io.emit("session", {
|
||||
action: "update",
|
||||
session: dbSession,
|
||||
});
|
||||
});
|
||||
|
||||
wbot.on("authenticated", async session => {
|
||||
console.log("Session:", sessionName, "AUTHENTICATED");
|
||||
|
||||
await dbSession.update({
|
||||
session: JSON.stringify(session),
|
||||
status: "authenticated",
|
||||
});
|
||||
|
||||
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,
|
||||
});
|
||||
|
||||
wbot.sendPresenceAvailable();
|
||||
});
|
||||
|
||||
wbot.id = dbSession.id;
|
||||
sessions.push(wbot);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
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: "qrcode" });
|
||||
|
||||
io.emit("session", {
|
||||
action: "update",
|
||||
session: dbSession,
|
||||
});
|
||||
});
|
||||
|
||||
wbot.on("authenticated", async session => {
|
||||
console.log("Session:", sessionName, "AUTHENTICATED");
|
||||
|
||||
await dbSession.update({
|
||||
session: JSON.stringify(session),
|
||||
status: "authenticated",
|
||||
});
|
||||
|
||||
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) {
|
||||
// if (chat.unreadCount > 0) {
|
||||
// unreadMessages = await chat.fetchMessages({
|
||||
// limit: chat.unreadCount,
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
|
||||
// console.log(unreadMessages);
|
||||
wbot.sendPresenceAvailable();
|
||||
});
|
||||
|
||||
wbot.id = dbSession.id;
|
||||
sessions.push(wbot);
|
||||
|
||||
return null;
|
||||
},
|
||||
|
||||
@@ -103,4 +96,16 @@ module.exports = {
|
||||
}
|
||||
return sessions[sessionIndex];
|
||||
},
|
||||
|
||||
removeWbot: sessionId => {
|
||||
try {
|
||||
const sessionIndex = sessions.findIndex(s => s.id === sessionId);
|
||||
if (sessionIndex !== -1) {
|
||||
sessions[sessionIndex].destroy();
|
||||
sessions.splice(sessionIndex, 1);
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user