mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 20:59:16 +00:00
fix: handling wwebjs erros without blocking routes
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import { getIO } from "../libs/socket";
|
import { getIO } from "../libs/socket";
|
||||||
import { initWbot } from "../libs/wbot";
|
import { initWbot, removeWbot } from "../libs/wbot";
|
||||||
import wbotMessageListener from "../services/WbotServices/wbotMessageListener";
|
import wbotMessageListener from "../services/WbotServices/wbotMessageListener";
|
||||||
import wbotMonitor from "../services/WbotServices/wbotMonitor";
|
import wbotMonitor from "../services/WbotServices/wbotMonitor";
|
||||||
|
|
||||||
@@ -33,10 +33,6 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
|||||||
|
|
||||||
const whatsapp = await CreateWhatsAppService({ name, status, isDefault });
|
const whatsapp = await CreateWhatsAppService({ name, status, isDefault });
|
||||||
|
|
||||||
// if (!whatsapp) {
|
|
||||||
// return res.status(400).json({ error: "Cannot create whatsapp session." });
|
|
||||||
// }
|
|
||||||
|
|
||||||
initWbot(whatsapp)
|
initWbot(whatsapp)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
wbotMessageListener(whatsapp);
|
wbotMessageListener(whatsapp);
|
||||||
@@ -70,14 +66,6 @@ export const update = async (
|
|||||||
|
|
||||||
const whatsapp = await UpdateWhatsAppService({ whatsappData, whatsappId });
|
const whatsapp = await UpdateWhatsAppService({ whatsappData, whatsappId });
|
||||||
|
|
||||||
// const whatsapp = await Whatsapp.findByPk(whatsappId);
|
|
||||||
|
|
||||||
// if (!whatsapp) {
|
|
||||||
// return res.status(404).json({ message: "Whatsapp not found" });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// await whatsapp.update(req.body);
|
|
||||||
|
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
io.emit("whatsapp", {
|
io.emit("whatsapp", {
|
||||||
action: "update",
|
action: "update",
|
||||||
@@ -94,7 +82,7 @@ export const remove = async (
|
|||||||
const { whatsappId } = req.params;
|
const { whatsappId } = req.params;
|
||||||
|
|
||||||
await DeleteWhatsAppService(whatsappId);
|
await DeleteWhatsAppService(whatsappId);
|
||||||
// removeWbot(whatsapp.id);
|
removeWbot(+whatsappId);
|
||||||
|
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
io.emit("whatsapp", {
|
io.emit("whatsapp", {
|
||||||
|
|||||||
@@ -6,6 +6,22 @@ let io: SocketIO;
|
|||||||
|
|
||||||
export const initIO = (httpServer: Server): SocketIO => {
|
export const initIO = (httpServer: Server): SocketIO => {
|
||||||
io = socketIo(httpServer);
|
io = socketIo(httpServer);
|
||||||
|
io.on("connection", socket => {
|
||||||
|
console.log("Client Connected");
|
||||||
|
socket.on("joinChatBox", ticketId => {
|
||||||
|
console.log("A client joined a ticket channel");
|
||||||
|
socket.join(ticketId);
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("joinNotification", () => {
|
||||||
|
console.log("A client joined notification channel");
|
||||||
|
socket.join("notification");
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("disconnect", () => {
|
||||||
|
console.log("Client disconnected");
|
||||||
|
});
|
||||||
|
});
|
||||||
return io;
|
return io;
|
||||||
};
|
};
|
||||||
export const getIO = (): SocketIO => {
|
export const getIO = (): SocketIO => {
|
||||||
|
|||||||
@@ -11,12 +11,7 @@ import uploadConfig from "./config/upload";
|
|||||||
import AppError from "./errors/AppError";
|
import AppError from "./errors/AppError";
|
||||||
import routes from "./routes";
|
import routes from "./routes";
|
||||||
import { initIO } from "./libs/socket";
|
import { initIO } from "./libs/socket";
|
||||||
import { initWbot } from "./libs/wbot";
|
import { StartWhatsAppSessions } from "./services/WbotServices/StartWhatsAppSessions";
|
||||||
|
|
||||||
// const wbotMonitor = require("./services/wbotMonitor");
|
|
||||||
import Whatsapp from "./models/Whatsapp";
|
|
||||||
import wbotMessageListener from "./services/WbotServices/wbotMessageListener";
|
|
||||||
import wbotMonitor from "./services/WbotServices/wbotMonitor";
|
|
||||||
|
|
||||||
Sentry.init({ dsn: process.env.SENTRY_DSN });
|
Sentry.init({ dsn: process.env.SENTRY_DSN });
|
||||||
|
|
||||||
@@ -34,38 +29,8 @@ 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}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
const io = initIO(server);
|
initIO(server);
|
||||||
io.on("connection", socket => {
|
StartWhatsAppSessions();
|
||||||
console.log("Client Connected");
|
|
||||||
socket.on("joinChatBox", ticketId => {
|
|
||||||
console.log("A client joined a ticket channel");
|
|
||||||
socket.join(ticketId);
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on("joinNotification", () => {
|
|
||||||
console.log("A client joined notification channel");
|
|
||||||
socket.join("notification");
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on("disconnect", () => {
|
|
||||||
console.log("Client disconnected");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
const startWhatsAppSessions = async () => {
|
|
||||||
const whatsapps = await Whatsapp.findAll();
|
|
||||||
if (whatsapps.length > 0) {
|
|
||||||
whatsapps.forEach(whatsapp => {
|
|
||||||
initWbot(whatsapp)
|
|
||||||
.then(() => {
|
|
||||||
wbotMessageListener(whatsapp);
|
|
||||||
wbotMonitor(whatsapp);
|
|
||||||
})
|
|
||||||
.catch(err => console.log(err));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
startWhatsAppSessions();
|
|
||||||
|
|
||||||
app.use(Sentry.Handlers.errorHandler());
|
app.use(Sentry.Handlers.errorHandler());
|
||||||
|
|
||||||
|
|||||||
@@ -13,16 +13,19 @@ const ImportContactsService = async (): Promise<void> => {
|
|||||||
try {
|
try {
|
||||||
phoneContacts = await wbot.getContacts();
|
phoneContacts = await wbot.getContacts();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new AppError(
|
console.log(
|
||||||
"Could not check whatsapp contact. Check connection page."
|
"Could not get whatsapp contacts from phone. Check connection page.",
|
||||||
|
err
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (phoneContacts) {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
phoneContacts.map(async ({ number, name }) => {
|
phoneContacts.map(async ({ number, name }) => {
|
||||||
await Contact.create({ number, name });
|
await Contact.create({ number, name });
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ImportContactsService;
|
export default ImportContactsService;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { MessageMedia, Message as WbotMessage } from "whatsapp-web.js";
|
import { MessageMedia, Message as WbotMessage } from "whatsapp-web.js";
|
||||||
|
import AppError from "../../errors/AppError";
|
||||||
import GetTicketWbot from "../../helpers/GetTicketWbot";
|
import GetTicketWbot from "../../helpers/GetTicketWbot";
|
||||||
import Ticket from "../../models/Ticket";
|
import Ticket from "../../models/Ticket";
|
||||||
|
|
||||||
@@ -11,6 +12,7 @@ const SendWhatsAppMedia = async ({
|
|||||||
media,
|
media,
|
||||||
ticket
|
ticket
|
||||||
}: Request): Promise<WbotMessage> => {
|
}: Request): Promise<WbotMessage> => {
|
||||||
|
try {
|
||||||
const wbot = await GetTicketWbot(ticket);
|
const wbot = await GetTicketWbot(ticket);
|
||||||
|
|
||||||
const newMedia = MessageMedia.fromFilePath(media.path);
|
const newMedia = MessageMedia.fromFilePath(media.path);
|
||||||
@@ -22,6 +24,12 @@ const SendWhatsAppMedia = async ({
|
|||||||
|
|
||||||
await ticket.update({ lastMessage: media.filename });
|
await ticket.update({ lastMessage: media.filename });
|
||||||
return sentMessage;
|
return sentMessage;
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
throw new AppError(
|
||||||
|
"Could not send whatsapp message. Check connections page."
|
||||||
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default SendWhatsAppMedia;
|
export default SendWhatsAppMedia;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Message as WbotMessage } from "whatsapp-web.js";
|
import { Message as WbotMessage } from "whatsapp-web.js";
|
||||||
|
import AppError from "../../errors/AppError";
|
||||||
import GetTicketWbot from "../../helpers/GetTicketWbot";
|
import GetTicketWbot from "../../helpers/GetTicketWbot";
|
||||||
import Ticket from "../../models/Ticket";
|
import Ticket from "../../models/Ticket";
|
||||||
|
|
||||||
@@ -11,6 +12,7 @@ const SendWhatsAppMessage = async ({
|
|||||||
body,
|
body,
|
||||||
ticket
|
ticket
|
||||||
}: Request): Promise<WbotMessage> => {
|
}: Request): Promise<WbotMessage> => {
|
||||||
|
try {
|
||||||
const wbot = await GetTicketWbot(ticket);
|
const wbot = await GetTicketWbot(ticket);
|
||||||
|
|
||||||
const sentMessage = await wbot.sendMessage(
|
const sentMessage = await wbot.sendMessage(
|
||||||
@@ -20,6 +22,12 @@ const SendWhatsAppMessage = async ({
|
|||||||
|
|
||||||
await ticket.update({ lastMessage: body });
|
await ticket.update({ lastMessage: body });
|
||||||
return sentMessage;
|
return sentMessage;
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
throw new AppError(
|
||||||
|
"Could not send whatsapp message. Check connections page."
|
||||||
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default SendWhatsAppMessage;
|
export default SendWhatsAppMessage;
|
||||||
|
|||||||
18
backend/src/services/WbotServices/StartWhatsAppSessions.ts
Normal file
18
backend/src/services/WbotServices/StartWhatsAppSessions.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
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(whatsapp => {
|
||||||
|
initWbot(whatsapp)
|
||||||
|
.then(() => {
|
||||||
|
wbotMessageListener(whatsapp);
|
||||||
|
wbotMonitor(whatsapp);
|
||||||
|
})
|
||||||
|
.catch(err => console.log(err));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user