mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 04:09:26 +00:00
feat: add options to handle whatsapp session on frontend
This commit is contained in:
@@ -2,8 +2,6 @@ import { Request, Response } from "express";
|
||||
import { getIO } from "../libs/socket";
|
||||
import { removeWbot } from "../libs/wbot";
|
||||
import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession";
|
||||
import wbotMessageListener from "../services/WbotServices/wbotMessageListener";
|
||||
import wbotMonitor from "../services/WbotServices/wbotMonitor";
|
||||
|
||||
import CreateWhatsAppService from "../services/WhatsappService/CreateWhatsAppService";
|
||||
import DeleteWhatsAppService from "../services/WhatsappService/DeleteWhatsAppService";
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
import { Request, Response } from "express";
|
||||
// import Whatsapp from "../models/Whatsapp";
|
||||
// import { getIO } from "../libs/socket";
|
||||
import { getWbot } from "../libs/wbot";
|
||||
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
|
||||
import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession";
|
||||
// import wbotMonitor from "../services/wbotMonitor";
|
||||
|
||||
const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
const { whatsappId } = req.params;
|
||||
@@ -15,6 +12,17 @@ const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
return res.status(200).json({ message: "Starting session." });
|
||||
};
|
||||
|
||||
const update = async (req: Request, res: Response): Promise<Response> => {
|
||||
const { whatsappId } = req.params;
|
||||
const whatsapp = await ShowWhatsAppService(whatsappId);
|
||||
|
||||
await whatsapp.update({ session: "" });
|
||||
|
||||
StartWhatsAppSession(whatsapp);
|
||||
|
||||
return res.status(200).json({ message: "Starting session." });
|
||||
};
|
||||
|
||||
const remove = async (req: Request, res: Response): Promise<Response> => {
|
||||
const { whatsappId } = req.params;
|
||||
const whatsapp = await ShowWhatsAppService(whatsappId);
|
||||
@@ -26,4 +34,4 @@ const remove = async (req: Request, res: Response): Promise<Response> => {
|
||||
return res.status(200).json({ message: "Session disconnected." });
|
||||
};
|
||||
|
||||
export default { store, remove };
|
||||
export default { store, remove, update };
|
||||
|
||||
@@ -3,7 +3,6 @@ import { Client } from "whatsapp-web.js";
|
||||
import { getIO } from "./socket";
|
||||
import Whatsapp from "../models/Whatsapp";
|
||||
import AppError from "../errors/AppError";
|
||||
import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession";
|
||||
|
||||
interface Session extends Client {
|
||||
id?: number;
|
||||
@@ -22,10 +21,10 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
|
||||
sessionCfg = JSON.parse(whatsapp.session);
|
||||
}
|
||||
|
||||
const sessionIndex = sessions.findIndex(s => s.id === whatsapp.id);
|
||||
if (sessionIndex !== -1) {
|
||||
sessions[sessionIndex].destroy();
|
||||
sessions.splice(sessionIndex, 1);
|
||||
const currentSessionIndex = sessions.findIndex(s => s.id === whatsapp.id);
|
||||
if (currentSessionIndex !== -1) {
|
||||
sessions[currentSessionIndex].destroy();
|
||||
sessions.splice(currentSessionIndex, 1);
|
||||
}
|
||||
|
||||
const wbot: Session = new Client({
|
||||
@@ -37,7 +36,13 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
|
||||
wbot.on("qr", async qr => {
|
||||
console.log("Session:", sessionName);
|
||||
qrCode.generate(qr, { small: true });
|
||||
await whatsapp.update({ qrcode: qr, status: "qrcode" });
|
||||
await whatsapp.update({ qrcode: qr, status: "qrcode", retries: 0 });
|
||||
|
||||
const sessionIndex = sessions.findIndex(s => s.id === whatsapp.id);
|
||||
if (sessionIndex === -1) {
|
||||
wbot.id = whatsapp.id;
|
||||
sessions.push(wbot);
|
||||
}
|
||||
|
||||
io.emit("whatsappSession", {
|
||||
action: "update",
|
||||
@@ -48,20 +53,14 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
|
||||
wbot.on("authenticated", async session => {
|
||||
console.log("Session:", sessionName, "AUTHENTICATED");
|
||||
await whatsapp.update({
|
||||
session: JSON.stringify(session),
|
||||
status: "authenticated"
|
||||
});
|
||||
|
||||
io.emit("whatsappSession", {
|
||||
action: "update",
|
||||
session: whatsapp
|
||||
session: JSON.stringify(session)
|
||||
});
|
||||
});
|
||||
|
||||
wbot.on("auth_failure", async msg => {
|
||||
console.error("Session:", sessionName, "AUTHENTICATION FAILURE", msg);
|
||||
|
||||
if (whatsapp.retries > 2) {
|
||||
if (whatsapp.retries > 1) {
|
||||
await whatsapp.update({ session: "", retries: 0 });
|
||||
}
|
||||
|
||||
@@ -76,7 +75,6 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
|
||||
session: whatsapp
|
||||
});
|
||||
|
||||
StartWhatsAppSession(whatsapp);
|
||||
reject(new Error("Error starting whatsapp session."));
|
||||
});
|
||||
|
||||
@@ -95,8 +93,13 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
|
||||
});
|
||||
|
||||
wbot.sendPresenceAvailable();
|
||||
wbot.id = whatsapp.id;
|
||||
sessions.push(wbot);
|
||||
|
||||
const sessionIndex = sessions.findIndex(s => s.id === whatsapp.id);
|
||||
if (sessionIndex === -1) {
|
||||
wbot.id = whatsapp.id;
|
||||
sessions.push(wbot);
|
||||
}
|
||||
|
||||
resolve(wbot);
|
||||
});
|
||||
} catch (err) {
|
||||
|
||||
@@ -11,6 +11,12 @@ whatsappSessionRoutes.post(
|
||||
WhatsAppSessionController.store
|
||||
);
|
||||
|
||||
whatsappSessionRoutes.put(
|
||||
"/whatsappsession/:whatsappId",
|
||||
isAuth,
|
||||
WhatsAppSessionController.update
|
||||
);
|
||||
|
||||
whatsappSessionRoutes.delete(
|
||||
"/whatsappsession/:whatsappId",
|
||||
isAuth,
|
||||
|
||||
@@ -1,11 +1,20 @@
|
||||
import { initWbot } from "../../libs/wbot";
|
||||
import Whatsapp from "../../models/Whatsapp";
|
||||
import wbotMessageListener from "./wbotMessageListener";
|
||||
import { getIO } from "../../libs/socket";
|
||||
import wbotMonitor from "./wbotMonitor";
|
||||
|
||||
export const StartWhatsAppSession = async (
|
||||
whatsapp: Whatsapp
|
||||
): Promise<void> => {
|
||||
await whatsapp.update({ status: "OPENING" });
|
||||
|
||||
const io = getIO();
|
||||
io.emit("whatsappSession", {
|
||||
action: "update",
|
||||
session: whatsapp
|
||||
});
|
||||
|
||||
try {
|
||||
const wbot = await initWbot(whatsapp);
|
||||
wbotMessageListener(wbot);
|
||||
|
||||
@@ -54,7 +54,7 @@ const wbotMonitor = async (
|
||||
wbot.on("disconnected", async reason => {
|
||||
console.log("Disconnected session:", sessionName, reason);
|
||||
try {
|
||||
await whatsapp.update({ status: "DISCONNECTED", session: "" });
|
||||
await whatsapp.update({ status: "OPENING", session: "" });
|
||||
} catch (err) {
|
||||
Sentry.captureException(err);
|
||||
console.log(err);
|
||||
|
||||
@@ -16,7 +16,7 @@ interface Response {
|
||||
|
||||
const CreateWhatsAppService = async ({
|
||||
name,
|
||||
status = "INITIALIZING",
|
||||
status = "OPENING",
|
||||
isDefault = false
|
||||
}: Request): Promise<Response> => {
|
||||
const schema = Yup.object().shape({
|
||||
|
||||
Reference in New Issue
Block a user