mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 19:59:20 +00:00
feat: start delete whatsapp messages backend
This commit is contained in:
37
backend/src/helpers/GetWbotMessage.ts
Normal file
37
backend/src/helpers/GetWbotMessage.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { Message as WbotMessage } from "whatsapp-web.js";
|
||||
import Ticket from "../models/Ticket";
|
||||
import GetTicketWbot from "./GetTicketWbot";
|
||||
import AppError from "../errors/AppError";
|
||||
|
||||
export const GetWbotMessage = async (
|
||||
ticket: Ticket,
|
||||
messageId: string
|
||||
): Promise<WbotMessage> => {
|
||||
const wbot = await GetTicketWbot(ticket);
|
||||
|
||||
const wbotChat = await wbot.getChatById(`${ticket.contact.number}@c.us`);
|
||||
|
||||
try {
|
||||
const chatMessages = await wbotChat.fetchMessages({ limit: 20 });
|
||||
|
||||
const msgToDelete = chatMessages.find(msg => msg.id.id === messageId);
|
||||
|
||||
if (!msgToDelete) {
|
||||
throw new AppError("msgNotFound");
|
||||
}
|
||||
|
||||
return msgToDelete;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
if (err.message === "msgNotFound") {
|
||||
throw new AppError(
|
||||
"Could not find a message witht this ID in WhatsApp chat."
|
||||
);
|
||||
}
|
||||
throw new AppError(
|
||||
"Could not valid WhatsApp contact. Check connections page"
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default GetWbotMessage;
|
||||
Reference in New Issue
Block a user