mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 11:49:19 +00:00
feat: added new method to fetch old whatsapp messages
This commit is contained in:
@@ -2,6 +2,7 @@ import { Request, Response } from "express";
|
||||
|
||||
import SetTicketMessagesAsRead from "../helpers/SetTicketMessagesAsRead";
|
||||
import { getIO } from "../libs/socket";
|
||||
import Message from "../models/Message";
|
||||
|
||||
import ListMessagesService from "../services/MessageServices/ListMessagesService";
|
||||
import ShowTicketService from "../services/TicketServices/ShowTicketService";
|
||||
@@ -17,6 +18,7 @@ type MessageData = {
|
||||
body: string;
|
||||
fromMe: boolean;
|
||||
read: boolean;
|
||||
quotedMsg?: Message;
|
||||
};
|
||||
|
||||
export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||
@@ -35,7 +37,7 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||
|
||||
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
const { ticketId } = req.params;
|
||||
const { body }: MessageData = req.body;
|
||||
const { body, quotedMsg }: MessageData = req.body;
|
||||
const medias = req.files as Express.Multer.File[];
|
||||
|
||||
const ticket = await ShowTicketService(ticketId);
|
||||
@@ -47,7 +49,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
})
|
||||
);
|
||||
} else {
|
||||
await SendWhatsAppMessage({ body, ticket });
|
||||
await SendWhatsAppMessage({ body, ticket, quotedMsg });
|
||||
}
|
||||
|
||||
await SetTicketMessagesAsRead(ticket);
|
||||
|
||||
@@ -13,19 +13,32 @@ export const GetWbotMessage = async (
|
||||
`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`
|
||||
);
|
||||
|
||||
try {
|
||||
const chatMessages = await wbotChat.fetchMessages({ limit: 20 });
|
||||
let limit = 20;
|
||||
|
||||
const msgToDelete = chatMessages.find(msg => msg.id.id === messageId);
|
||||
const fetchWbotMessagesGradually = async (): Promise<void | WbotMessage> => {
|
||||
const chatMessages = await wbotChat.fetchMessages({ limit });
|
||||
|
||||
if (!msgToDelete) {
|
||||
throw new Error();
|
||||
const msgFound = chatMessages.find(msg => msg.id.id === messageId);
|
||||
|
||||
if (!msgFound && limit < 100) {
|
||||
limit += 20;
|
||||
return fetchWbotMessagesGradually();
|
||||
}
|
||||
|
||||
return msgToDelete;
|
||||
return msgFound;
|
||||
};
|
||||
|
||||
try {
|
||||
const msgFound = await fetchWbotMessagesGradually();
|
||||
|
||||
if (!msgFound) {
|
||||
throw new Error("Cannot found message within 100 last messages");
|
||||
}
|
||||
|
||||
return msgFound;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
throw new AppError("ERR_DELETE_WAPP_MSG");
|
||||
throw new AppError("ERR_FETCH_WAPP_MSG");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
12
backend/src/helpers/SerializeWbotMsgId.ts
Normal file
12
backend/src/helpers/SerializeWbotMsgId.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import Message from "../models/Message";
|
||||
import Ticket from "../models/Ticket";
|
||||
|
||||
const SerializeWbotMsgId = (ticket: Ticket, message: Message): string => {
|
||||
const serializedMsgId = `${message.fromMe}_${ticket.contact.number}@${
|
||||
ticket.isGroup ? "g" : "c"
|
||||
}.us_${message.id}`;
|
||||
|
||||
return serializedMsgId;
|
||||
};
|
||||
|
||||
export default SerializeWbotMsgId;
|
||||
@@ -28,7 +28,14 @@ const CreateMessageService = async ({
|
||||
await Message.upsert(messageData);
|
||||
|
||||
const message = await Message.findByPk(messageData.id, {
|
||||
include: ["contact", "quotedMsg"]
|
||||
include: [
|
||||
"contact",
|
||||
{
|
||||
model: Message,
|
||||
as: "quotedMsg",
|
||||
include: ["contact"]
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
if (!message) {
|
||||
|
||||
@@ -32,7 +32,14 @@ const ListMessagesService = async ({
|
||||
const { count, rows: messages } = await Message.findAndCountAll({
|
||||
where: { ticketId },
|
||||
limit,
|
||||
include: ["contact", "quotedMsg"],
|
||||
include: [
|
||||
"contact",
|
||||
{
|
||||
model: Message,
|
||||
as: "quotedMsg",
|
||||
include: ["contact"]
|
||||
}
|
||||
],
|
||||
offset,
|
||||
order: [["createdAt", "DESC"]]
|
||||
});
|
||||
|
||||
@@ -1,23 +1,37 @@
|
||||
import { Message as WbotMessage } from "whatsapp-web.js";
|
||||
import AppError from "../../errors/AppError";
|
||||
import GetTicketWbot from "../../helpers/GetTicketWbot";
|
||||
import GetWbotMessage from "../../helpers/GetWbotMessage";
|
||||
import SerializeWbotMsgId from "../../helpers/SerializeWbotMsgId";
|
||||
import Message from "../../models/Message";
|
||||
import Ticket from "../../models/Ticket";
|
||||
|
||||
interface Request {
|
||||
body: string;
|
||||
ticket: Ticket;
|
||||
quotedMsg?: Message;
|
||||
}
|
||||
|
||||
const SendWhatsAppMessage = async ({
|
||||
body,
|
||||
ticket
|
||||
ticket,
|
||||
quotedMsg
|
||||
}: Request): Promise<WbotMessage> => {
|
||||
try {
|
||||
const wbot = await GetTicketWbot(ticket);
|
||||
let quotedMsgSerializedId: string | undefined;
|
||||
if (quotedMsg) {
|
||||
await GetWbotMessage(ticket, quotedMsg.id);
|
||||
quotedMsgSerializedId = SerializeWbotMsgId(ticket, quotedMsg);
|
||||
}
|
||||
|
||||
const wbot = await GetTicketWbot(ticket);
|
||||
|
||||
try {
|
||||
const sentMessage = await wbot.sendMessage(
|
||||
`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`,
|
||||
body
|
||||
body,
|
||||
{
|
||||
quotedMessageId: quotedMsgSerializedId
|
||||
}
|
||||
);
|
||||
|
||||
await ticket.update({ lastMessage: body });
|
||||
|
||||
@@ -344,7 +344,14 @@ const wbotMessageListener = (wbot: Session): void => {
|
||||
|
||||
try {
|
||||
const messageToUpdate = await Message.findByPk(msg.id.id, {
|
||||
include: ["contact"]
|
||||
include: [
|
||||
"contact",
|
||||
{
|
||||
model: Message,
|
||||
as: "quotedMsg",
|
||||
include: ["contact"]
|
||||
}
|
||||
]
|
||||
});
|
||||
if (!messageToUpdate) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user