mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 12:19:16 +00:00
fix: handling wwebjs erros without blocking routes
This commit is contained in:
@@ -13,16 +13,19 @@ const ImportContactsService = async (): Promise<void> => {
|
||||
try {
|
||||
phoneContacts = await wbot.getContacts();
|
||||
} catch (err) {
|
||||
throw new AppError(
|
||||
"Could not check whatsapp contact. Check connection page."
|
||||
console.log(
|
||||
"Could not get whatsapp contacts from phone. Check connection page.",
|
||||
err
|
||||
);
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
phoneContacts.map(async ({ number, name }) => {
|
||||
await Contact.create({ number, name });
|
||||
})
|
||||
);
|
||||
if (phoneContacts) {
|
||||
await Promise.all(
|
||||
phoneContacts.map(async ({ number, name }) => {
|
||||
await Contact.create({ number, name });
|
||||
})
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default ImportContactsService;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { MessageMedia, Message as WbotMessage } from "whatsapp-web.js";
|
||||
import AppError from "../../errors/AppError";
|
||||
import GetTicketWbot from "../../helpers/GetTicketWbot";
|
||||
import Ticket from "../../models/Ticket";
|
||||
|
||||
@@ -11,17 +12,24 @@ const SendWhatsAppMedia = async ({
|
||||
media,
|
||||
ticket
|
||||
}: Request): Promise<WbotMessage> => {
|
||||
const wbot = await GetTicketWbot(ticket);
|
||||
try {
|
||||
const wbot = await GetTicketWbot(ticket);
|
||||
|
||||
const newMedia = MessageMedia.fromFilePath(media.path);
|
||||
const newMedia = MessageMedia.fromFilePath(media.path);
|
||||
|
||||
const sentMessage = await wbot.sendMessage(
|
||||
`${ticket.contact.number}@c.us`,
|
||||
newMedia
|
||||
);
|
||||
const sentMessage = await wbot.sendMessage(
|
||||
`${ticket.contact.number}@c.us`,
|
||||
newMedia
|
||||
);
|
||||
|
||||
await ticket.update({ lastMessage: media.filename });
|
||||
return sentMessage;
|
||||
await ticket.update({ lastMessage: media.filename });
|
||||
return sentMessage;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
throw new AppError(
|
||||
"Could not send whatsapp message. Check connections page."
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default SendWhatsAppMedia;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Message as WbotMessage } from "whatsapp-web.js";
|
||||
import AppError from "../../errors/AppError";
|
||||
import GetTicketWbot from "../../helpers/GetTicketWbot";
|
||||
import Ticket from "../../models/Ticket";
|
||||
|
||||
@@ -11,15 +12,22 @@ const SendWhatsAppMessage = async ({
|
||||
body,
|
||||
ticket
|
||||
}: Request): Promise<WbotMessage> => {
|
||||
const wbot = await GetTicketWbot(ticket);
|
||||
try {
|
||||
const wbot = await GetTicketWbot(ticket);
|
||||
|
||||
const sentMessage = await wbot.sendMessage(
|
||||
`${ticket.contact.number}@c.us`,
|
||||
body
|
||||
);
|
||||
const sentMessage = await wbot.sendMessage(
|
||||
`${ticket.contact.number}@c.us`,
|
||||
body
|
||||
);
|
||||
|
||||
await ticket.update({ lastMessage: body });
|
||||
return sentMessage;
|
||||
await ticket.update({ lastMessage: body });
|
||||
return sentMessage;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
throw new AppError(
|
||||
"Could not send whatsapp message. Check connections page."
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
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