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