mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 20:59:16 +00:00
feat: added routes to handle whatsapp session
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import { getIO } from "../libs/socket";
|
import { getIO } from "../libs/socket";
|
||||||
import { initWbot, removeWbot } from "../libs/wbot";
|
import { removeWbot } from "../libs/wbot";
|
||||||
|
import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession";
|
||||||
import wbotMessageListener from "../services/WbotServices/wbotMessageListener";
|
import wbotMessageListener from "../services/WbotServices/wbotMessageListener";
|
||||||
import wbotMonitor from "../services/WbotServices/wbotMonitor";
|
import wbotMonitor from "../services/WbotServices/wbotMonitor";
|
||||||
|
|
||||||
@@ -31,13 +32,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
|||||||
isDefault
|
isDefault
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
StartWhatsAppSession(whatsapp);
|
||||||
const wbot = await initWbot(whatsapp);
|
|
||||||
wbotMessageListener(wbot);
|
|
||||||
wbotMonitor(wbot, whatsapp);
|
|
||||||
} catch (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
io.emit("whatsapp", {
|
io.emit("whatsapp", {
|
||||||
|
|||||||
@@ -1,32 +1,29 @@
|
|||||||
// const Whatsapp = require("../models/Whatsapp");
|
import { Request, Response } from "express";
|
||||||
// const { getIO } = require("../libs/socket");
|
// import Whatsapp from "../models/Whatsapp";
|
||||||
// const { getWbot, initWbot, removeWbot } = require("../libs/wbot");
|
// import { getIO } from "../libs/socket";
|
||||||
// const wbotMessageListener = require("../services/wbotMessageListener");
|
import { getWbot } from "../libs/wbot";
|
||||||
// const wbotMonitor = require("../services/wbotMonitor");
|
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
|
||||||
|
import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession";
|
||||||
|
// import wbotMonitor from "../services/wbotMonitor";
|
||||||
|
|
||||||
// exports.show = async (req, res) => {
|
const store = async (req: Request, res: Response): Promise<Response> => {
|
||||||
// const { whatsappId } = req.params;
|
const { whatsappId } = req.params;
|
||||||
// const dbSession = await Whatsapp.findByPk(whatsappId);
|
const whatsapp = await ShowWhatsAppService(whatsappId);
|
||||||
|
|
||||||
// if (!dbSession) {
|
StartWhatsAppSession(whatsapp);
|
||||||
// return res.status(200).json({ message: "Session not found" });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return res.status(200).json(dbSession);
|
return res.status(200).json({ message: "Starting session." });
|
||||||
// };
|
};
|
||||||
|
|
||||||
// exports.delete = async (req, res) => {
|
const remove = async (req: Request, res: Response): Promise<Response> => {
|
||||||
// const { whatsappId } = req.params;
|
const { whatsappId } = req.params;
|
||||||
|
const whatsapp = await ShowWhatsAppService(whatsappId);
|
||||||
|
|
||||||
// const dbSession = await Whatsapp.findByPk(whatsappId);
|
const wbot = getWbot(whatsapp.id);
|
||||||
|
|
||||||
// if (!dbSession) {
|
wbot.logout();
|
||||||
// return res.status(404).json({ message: "Session not found" });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// const wbot = getWbot(dbSession.id);
|
return res.status(200).json({ message: "Session disconnected." });
|
||||||
|
};
|
||||||
|
|
||||||
// wbot.logout();
|
export default { store, remove };
|
||||||
|
|
||||||
// return res.status(200).json({ message: "Session disconnected." });
|
|
||||||
// };
|
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import { QueryInterface, DataTypes } from "sequelize";
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
up: (queryInterface: QueryInterface) => {
|
||||||
|
return queryInterface.addColumn("Whatsapps", "retries", {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
defaultValue: 0,
|
||||||
|
allowNull: false
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
down: (queryInterface: QueryInterface) => {
|
||||||
|
return queryInterface.removeColumn("Whatsapps", "retries");
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -3,6 +3,7 @@ import { Client } from "whatsapp-web.js";
|
|||||||
import { getIO } from "./socket";
|
import { getIO } from "./socket";
|
||||||
import Whatsapp from "../models/Whatsapp";
|
import Whatsapp from "../models/Whatsapp";
|
||||||
import AppError from "../errors/AppError";
|
import AppError from "../errors/AppError";
|
||||||
|
import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession";
|
||||||
|
|
||||||
interface Session extends Client {
|
interface Session extends Client {
|
||||||
id?: number;
|
id?: number;
|
||||||
@@ -37,6 +38,7 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
|
|||||||
console.log("Session:", sessionName);
|
console.log("Session:", sessionName);
|
||||||
qrCode.generate(qr, { small: true });
|
qrCode.generate(qr, { small: true });
|
||||||
await whatsapp.update({ qrcode: qr, status: "qrcode" });
|
await whatsapp.update({ qrcode: qr, status: "qrcode" });
|
||||||
|
|
||||||
io.emit("whatsappSession", {
|
io.emit("whatsappSession", {
|
||||||
action: "update",
|
action: "update",
|
||||||
session: whatsapp
|
session: whatsapp
|
||||||
@@ -49,6 +51,7 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
|
|||||||
session: JSON.stringify(session),
|
session: JSON.stringify(session),
|
||||||
status: "authenticated"
|
status: "authenticated"
|
||||||
});
|
});
|
||||||
|
|
||||||
io.emit("whatsappSession", {
|
io.emit("whatsappSession", {
|
||||||
action: "update",
|
action: "update",
|
||||||
session: whatsapp
|
session: whatsapp
|
||||||
@@ -57,20 +60,40 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
|
|||||||
|
|
||||||
wbot.on("auth_failure", async msg => {
|
wbot.on("auth_failure", async msg => {
|
||||||
console.error("Session:", sessionName, "AUTHENTICATION FAILURE", msg);
|
console.error("Session:", sessionName, "AUTHENTICATION FAILURE", msg);
|
||||||
await whatsapp.update({ status: "OFFLINE" });
|
|
||||||
|
if (whatsapp.retries > 2) {
|
||||||
|
await whatsapp.update({ session: "", retries: 0 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const retry = whatsapp.retries;
|
||||||
|
await whatsapp.update({
|
||||||
|
status: "DISCONNECTED",
|
||||||
|
retries: retry + 1
|
||||||
|
});
|
||||||
|
|
||||||
|
io.emit("whatsappSession", {
|
||||||
|
action: "update",
|
||||||
|
session: whatsapp
|
||||||
|
});
|
||||||
|
|
||||||
|
StartWhatsAppSession(whatsapp);
|
||||||
reject(new Error("Error starting whatsapp session."));
|
reject(new Error("Error starting whatsapp session."));
|
||||||
});
|
});
|
||||||
|
|
||||||
wbot.on("ready", async () => {
|
wbot.on("ready", async () => {
|
||||||
console.log("Session:", sessionName, "READY");
|
console.log("Session:", sessionName, "READY");
|
||||||
|
|
||||||
await whatsapp.update({
|
await whatsapp.update({
|
||||||
status: "CONNECTED",
|
status: "CONNECTED",
|
||||||
qrcode: ""
|
qrcode: "",
|
||||||
|
retries: 0
|
||||||
});
|
});
|
||||||
|
|
||||||
io.emit("whatsappSession", {
|
io.emit("whatsappSession", {
|
||||||
action: "update",
|
action: "update",
|
||||||
session: whatsapp
|
session: whatsapp
|
||||||
});
|
});
|
||||||
|
|
||||||
wbot.sendPresenceAvailable();
|
wbot.sendPresenceAvailable();
|
||||||
wbot.id = whatsapp.id;
|
wbot.id = whatsapp.id;
|
||||||
sessions.push(wbot);
|
sessions.push(wbot);
|
||||||
|
|||||||
@@ -41,6 +41,9 @@ class Whatsapp extends Model<Whatsapp> {
|
|||||||
@Column
|
@Column
|
||||||
plugged: boolean;
|
plugged: boolean;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
retries: number;
|
||||||
|
|
||||||
@Default(false)
|
@Default(false)
|
||||||
@AllowNull
|
@AllowNull
|
||||||
@Column
|
@Column
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import contactRoutes from "./contactRoutes";
|
|||||||
import ticketRoutes from "./ticketRoutes";
|
import ticketRoutes from "./ticketRoutes";
|
||||||
import whatsappRoutes from "./whatsappRoutes";
|
import whatsappRoutes from "./whatsappRoutes";
|
||||||
import messageRoutes from "./messageRoutes";
|
import messageRoutes from "./messageRoutes";
|
||||||
|
import whatsappSessionRoutes from "./whatsappSessionRoutes";
|
||||||
|
|
||||||
const routes = Router();
|
const routes = Router();
|
||||||
|
|
||||||
@@ -17,5 +18,7 @@ routes.use(contactRoutes);
|
|||||||
routes.use(ticketRoutes);
|
routes.use(ticketRoutes);
|
||||||
routes.use(whatsappRoutes);
|
routes.use(whatsappRoutes);
|
||||||
routes.use(messageRoutes);
|
routes.use(messageRoutes);
|
||||||
|
routes.use(messageRoutes);
|
||||||
|
routes.use(whatsappSessionRoutes);
|
||||||
|
|
||||||
export default routes;
|
export default routes;
|
||||||
|
|||||||
@@ -1,20 +1,20 @@
|
|||||||
// const express = require("express");
|
import { Router } from "express";
|
||||||
// const isAuth = require("../../middleware/is-auth");
|
import isAuth from "../middleware/isAuth";
|
||||||
|
|
||||||
// const WhatsAppSessionController = require("../../controllers/WhatsAppSessionController");
|
import WhatsAppSessionController from "../controllers/WhatsAppSessionController";
|
||||||
|
|
||||||
// const routes = express.Router();
|
const whatsappSessionRoutes = Router();
|
||||||
|
|
||||||
// routes.get(
|
whatsappSessionRoutes.post(
|
||||||
// "/whatsappsession/:whatsappId",
|
"/whatsappsession/:whatsappId",
|
||||||
// isAuth,
|
isAuth,
|
||||||
// WhatsAppSessionController.show
|
WhatsAppSessionController.store
|
||||||
// );
|
);
|
||||||
|
|
||||||
// routes.delete(
|
whatsappSessionRoutes.delete(
|
||||||
// "/whatsappsession/:whatsappId",
|
"/whatsappsession/:whatsappId",
|
||||||
// isAuth,
|
isAuth,
|
||||||
// WhatsAppSessionController.delete
|
WhatsAppSessionController.remove
|
||||||
// );
|
);
|
||||||
|
|
||||||
// module.exports = routes;
|
export default whatsappSessionRoutes;
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import app from "./app";
|
import app from "./app";
|
||||||
import { initIO } from "./libs/socket";
|
import { initIO } from "./libs/socket";
|
||||||
import { StartWhatsAppSessions } from "./services/WbotServices/StartWhatsAppSessions";
|
import { StartAllWhatsAppsSessions } from "./services/WbotServices/StartAllWhatsAppsSessions";
|
||||||
|
|
||||||
const server = app.listen(process.env.PORT, () => {
|
const server = app.listen(process.env.PORT, () => {
|
||||||
console.log(`Server started on port: ${process.env.PORT}`);
|
console.log(`Server started on port: ${process.env.PORT}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
initIO(server);
|
initIO(server);
|
||||||
StartWhatsAppSessions();
|
StartAllWhatsAppsSessions();
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import Whatsapp from "../../models/Whatsapp";
|
||||||
|
import { StartWhatsAppSession } from "./StartWhatsAppSession";
|
||||||
|
|
||||||
|
export const StartAllWhatsAppsSessions = async (): Promise<void> => {
|
||||||
|
const whatsapps = await Whatsapp.findAll();
|
||||||
|
if (whatsapps.length > 0) {
|
||||||
|
whatsapps.forEach(whatsapp => {
|
||||||
|
StartWhatsAppSession(whatsapp);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
16
backend/src/services/WbotServices/StartWhatsAppSession.ts
Normal file
16
backend/src/services/WbotServices/StartWhatsAppSession.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { initWbot } from "../../libs/wbot";
|
||||||
|
import Whatsapp from "../../models/Whatsapp";
|
||||||
|
import wbotMessageListener from "./wbotMessageListener";
|
||||||
|
import wbotMonitor from "./wbotMonitor";
|
||||||
|
|
||||||
|
export const StartWhatsAppSession = async (
|
||||||
|
whatsapp: Whatsapp
|
||||||
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const wbot = await initWbot(whatsapp);
|
||||||
|
wbotMessageListener(wbot);
|
||||||
|
wbotMonitor(wbot, whatsapp);
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
import { initWbot } from "../../libs/wbot";
|
|
||||||
import Whatsapp from "../../models/Whatsapp";
|
|
||||||
import wbotMessageListener from "./wbotMessageListener";
|
|
||||||
import wbotMonitor from "./wbotMonitor";
|
|
||||||
|
|
||||||
export const StartWhatsAppSessions = async (): Promise<void> => {
|
|
||||||
const whatsapps = await Whatsapp.findAll();
|
|
||||||
if (whatsapps.length > 0) {
|
|
||||||
whatsapps.forEach(async whatsapp => {
|
|
||||||
try {
|
|
||||||
const wbot = await initWbot(whatsapp);
|
|
||||||
wbotMessageListener(wbot);
|
|
||||||
wbotMonitor(wbot, whatsapp);
|
|
||||||
} catch (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -1,11 +1,9 @@
|
|||||||
import * as Sentry from "@sentry/node";
|
import * as Sentry from "@sentry/node";
|
||||||
import { Client } from "whatsapp-web.js";
|
import { Client } from "whatsapp-web.js";
|
||||||
|
|
||||||
import wbotMessageListener from "./wbotMessageListener";
|
|
||||||
|
|
||||||
import { getIO } from "../../libs/socket";
|
import { getIO } from "../../libs/socket";
|
||||||
import { initWbot } from "../../libs/wbot";
|
|
||||||
import Whatsapp from "../../models/Whatsapp";
|
import Whatsapp from "../../models/Whatsapp";
|
||||||
|
import { StartWhatsAppSession } from "./StartWhatsAppSession";
|
||||||
|
|
||||||
interface Session extends Client {
|
interface Session extends Client {
|
||||||
id?: number;
|
id?: number;
|
||||||
@@ -56,7 +54,7 @@ const wbotMonitor = async (
|
|||||||
wbot.on("disconnected", async reason => {
|
wbot.on("disconnected", async reason => {
|
||||||
console.log("Disconnected session:", sessionName, reason);
|
console.log("Disconnected session:", sessionName, reason);
|
||||||
try {
|
try {
|
||||||
await whatsapp.update({ status: "disconnected" });
|
await whatsapp.update({ status: "DISCONNECTED", session: "" });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Sentry.captureException(err);
|
Sentry.captureException(err);
|
||||||
console.log(err);
|
console.log(err);
|
||||||
@@ -67,26 +65,8 @@ const wbotMonitor = async (
|
|||||||
session: whatsapp
|
session: whatsapp
|
||||||
});
|
});
|
||||||
|
|
||||||
// to be removed after adding buttons to rebuild session on frontend
|
setTimeout(() => StartWhatsAppSession(whatsapp), 2000);
|
||||||
|
|
||||||
setTimeout(
|
|
||||||
() =>
|
|
||||||
initWbot(whatsapp)
|
|
||||||
.then(() => {
|
|
||||||
wbotMessageListener(wbot);
|
|
||||||
wbotMonitor(wbot, whatsapp);
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
Sentry.captureException(err);
|
|
||||||
console.log(err);
|
|
||||||
}),
|
|
||||||
2000
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// setInterval(() => {
|
|
||||||
// wbot.resetState();
|
|
||||||
// }, 20000);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Sentry.captureException(err);
|
Sentry.captureException(err);
|
||||||
console.log(err);
|
console.log(err);
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
import Whatsapp from "../../models/Whatsapp";
|
import Whatsapp from "../../models/Whatsapp";
|
||||||
import AppError from "../../errors/AppError";
|
import AppError from "../../errors/AppError";
|
||||||
|
|
||||||
const ShowWhatsAppService = async (
|
const ShowWhatsAppService = async (id: string | number): Promise<Whatsapp> => {
|
||||||
id: string | number
|
|
||||||
): Promise<Whatsapp | undefined> => {
|
|
||||||
const whatsapp = await Whatsapp.findByPk(id);
|
const whatsapp = await Whatsapp.findByPk(id);
|
||||||
|
|
||||||
if (!whatsapp) {
|
if (!whatsapp) {
|
||||||
|
|||||||
Reference in New Issue
Block a user