mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 20:29:17 +00:00
feat: option to handle whatsapp session on app
This commit is contained in:
@@ -66,9 +66,9 @@ io.on("connection", socket => {
|
||||
|
||||
wBot
|
||||
.init()
|
||||
.then(() => {
|
||||
.then(({ dbSession }) => {
|
||||
wbotMessageListener();
|
||||
wbotMonitor();
|
||||
wbotMonitor(dbSession);
|
||||
})
|
||||
.catch(err => console.log(err));
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
const Whatsapp = require("../models/Whatsapp");
|
||||
// const { getIO } = require("../libs/socket");
|
||||
// const { getWbot, init } = require("../libs/wbot");
|
||||
const { getIO } = require("../libs/socket");
|
||||
const { getWbot } = require("../libs/wbot");
|
||||
|
||||
exports.show = async (req, res, next) => {
|
||||
exports.show = async (req, res) => {
|
||||
const { sessionId } = req.params;
|
||||
const dbSession = await Whatsapp.findByPk(sessionId);
|
||||
|
||||
@@ -13,6 +13,28 @@ exports.show = async (req, res, next) => {
|
||||
return res.status(200).json(dbSession);
|
||||
};
|
||||
|
||||
exports.delete = async (req, res) => {
|
||||
const wbot = getWbot();
|
||||
const io = getIO();
|
||||
|
||||
const { sessionId } = req.params;
|
||||
const dbSession = await Whatsapp.findByPk(sessionId);
|
||||
|
||||
if (!dbSession) {
|
||||
return res.status(200).json({ message: "Session not found" });
|
||||
}
|
||||
|
||||
await dbSession.update({ session: "", status: "pending" });
|
||||
wbot.logout();
|
||||
|
||||
io.emit("session", {
|
||||
action: "logout",
|
||||
session: dbSession,
|
||||
});
|
||||
|
||||
return res.status(200).json({ message: "session disconnected" });
|
||||
};
|
||||
|
||||
// exports.getContacts = async (req, res, next) => {
|
||||
// const io = getIO();
|
||||
// const wbot = getWbot();
|
||||
|
||||
@@ -9,7 +9,12 @@ module.exports = {
|
||||
init: async () => {
|
||||
let sessionCfg;
|
||||
|
||||
const dbSession = await Whatsapp.findOne({ where: { id: 1 } });
|
||||
const [dbSession] = await Whatsapp.findOrCreate({
|
||||
where: { id: 1 },
|
||||
defaults: {
|
||||
id: 1,
|
||||
},
|
||||
});
|
||||
if (dbSession && dbSession.session) {
|
||||
sessionCfg = JSON.parse(dbSession.session);
|
||||
}
|
||||
@@ -21,20 +26,21 @@ module.exports = {
|
||||
wbot.initialize();
|
||||
wbot.on("qr", async qr => {
|
||||
qrCode.generate(qr, { small: true });
|
||||
await Whatsapp.upsert({ id: 1, qrcode: qr, status: "pending" });
|
||||
getIO().emit("qrcode", {
|
||||
await dbSession.update({ id: 1, qrcode: qr, status: "disconnected" });
|
||||
getIO().emit("session", {
|
||||
action: "update",
|
||||
qr: qr,
|
||||
session: dbSession,
|
||||
});
|
||||
});
|
||||
wbot.on("authenticated", async session => {
|
||||
console.log("AUTHENTICATED");
|
||||
await Whatsapp.upsert({
|
||||
await dbSession.update({
|
||||
id: 1,
|
||||
session: JSON.stringify(session),
|
||||
status: "authenticated",
|
||||
});
|
||||
getIO().emit("whats_auth", {
|
||||
getIO().emit("session", {
|
||||
action: "authentication",
|
||||
session: dbSession,
|
||||
});
|
||||
@@ -45,8 +51,8 @@ module.exports = {
|
||||
});
|
||||
wbot.on("ready", async () => {
|
||||
console.log("READY");
|
||||
await Whatsapp.update(
|
||||
{ status: "online", qrcode: "" },
|
||||
await dbSession.update(
|
||||
{ status: "CONNECTED", qrcode: "" },
|
||||
{ where: { id: 1 } }
|
||||
);
|
||||
// const chats = await wbot.getChats(); // pega as mensagens nao lidas (recebidas quando o bot estava offline)
|
||||
@@ -62,7 +68,7 @@ module.exports = {
|
||||
// console.log(unreadMessages);
|
||||
wbot.sendPresenceAvailable();
|
||||
});
|
||||
return wbot;
|
||||
return { wbot, dbSession };
|
||||
},
|
||||
|
||||
getWbot: () => {
|
||||
|
||||
@@ -11,6 +11,12 @@ routes.get(
|
||||
WhatsAppSessionController.show
|
||||
);
|
||||
|
||||
routes.delete(
|
||||
"/whatsapp/session/:sessionId",
|
||||
isAuth,
|
||||
WhatsAppSessionController.delete
|
||||
);
|
||||
|
||||
// fetch contacts in user cellphone, not in use
|
||||
// routes.get("/whatsapp/contacts", isAuth, WhatsappController.getContacts);
|
||||
|
||||
|
||||
@@ -1,41 +1,67 @@
|
||||
const Sentry = require("@sentry/node");
|
||||
|
||||
const Whatsapp = require("../models/Whatsapp");
|
||||
const wbotMessageListener = require("./wbotMessageListener");
|
||||
|
||||
const { getIO } = require("../libs/socket");
|
||||
const { getWbot, init } = require("../libs/wbot");
|
||||
|
||||
const wbotMonitor = () => {
|
||||
const wbotMonitor = dbSession => {
|
||||
const io = getIO();
|
||||
const wbot = getWbot();
|
||||
|
||||
try {
|
||||
wbot.on("change_state", newState => {
|
||||
wbot.on("change_state", async newState => {
|
||||
console.log("monitor", newState);
|
||||
});
|
||||
|
||||
wbot.on("change_battery", async batteryInfo => {
|
||||
// Battery percentage for attached device has changed
|
||||
const { battery, plugged } = batteryInfo;
|
||||
try {
|
||||
await Whatsapp.update({ battery, plugged }, { where: { id: 1 } });
|
||||
await dbSession.update({ status: newState });
|
||||
} catch (err) {
|
||||
Sentry.captureException(err);
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
console.log(`Battery: ${battery}% - Charging? ${plugged}`); //todo> save batery state to db
|
||||
io.emit("session", {
|
||||
action: "update",
|
||||
session: dbSession,
|
||||
});
|
||||
});
|
||||
|
||||
wbot.on("disconnected", reason => {
|
||||
console.log("disconnected", reason); //todo> save connection status to DB
|
||||
wbot.on("change_battery", async batteryInfo => {
|
||||
const { battery, plugged } = batteryInfo;
|
||||
console.log(`Battery: ${battery}% - Charging? ${plugged}`);
|
||||
|
||||
try {
|
||||
await dbSession.update({ battery, plugged });
|
||||
} catch (err) {
|
||||
Sentry.captureException(err);
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
io.emit("session", {
|
||||
action: "update",
|
||||
session: dbSession,
|
||||
});
|
||||
});
|
||||
|
||||
wbot.on("disconnected", async reason => {
|
||||
console.log("disconnected", reason);
|
||||
try {
|
||||
await dbSession.update({ status: "disconnected" });
|
||||
} catch (err) {
|
||||
Sentry.captureException(err);
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
io.emit("session", {
|
||||
action: "logout",
|
||||
session: dbSession,
|
||||
});
|
||||
|
||||
setTimeout(
|
||||
() =>
|
||||
init()
|
||||
.then(res => {
|
||||
.then(({ dbSession }) => {
|
||||
wbotMessageListener();
|
||||
wbotMonitor();
|
||||
wbotMonitor(dbSession);
|
||||
})
|
||||
.catch(err => {
|
||||
Sentry.captureException(err);
|
||||
|
||||
Reference in New Issue
Block a user