mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 20:29:17 +00:00
feat: added routes to handle whatsapp session
This commit is contained in:
@@ -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