mirror of
https://github.com/cheveguerra/Whaticket.git
synced 2026-04-19 03:59:15 +00:00
Initial commit
This commit is contained in:
44
backend/src/helpers/GetWbotMessage.ts
Normal file
44
backend/src/helpers/GetWbotMessage.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
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}@${ticket.isGroup ? "g" : "c"}.us`
|
||||
);
|
||||
|
||||
let limit = 20;
|
||||
|
||||
const fetchWbotMessagesGradually = async (): Promise<void | WbotMessage> => {
|
||||
const chatMessages = await wbotChat.fetchMessages({ limit });
|
||||
|
||||
const msgFound = chatMessages.find(msg => msg.id.id === messageId);
|
||||
|
||||
if (!msgFound && limit < 100) {
|
||||
limit += 20;
|
||||
return fetchWbotMessagesGradually();
|
||||
}
|
||||
|
||||
return msgFound;
|
||||
};
|
||||
|
||||
try {
|
||||
const msgFound = await fetchWbotMessagesGradually();
|
||||
|
||||
if (!msgFound) {
|
||||
throw new Error("Cannot found message within 100 last messages");
|
||||
}
|
||||
|
||||
return msgFound;
|
||||
} catch (err) {
|
||||
throw new AppError("ERR_FETCH_WAPP_MSG");
|
||||
}
|
||||
};
|
||||
|
||||
export default GetWbotMessage;
|
||||
Reference in New Issue
Block a user