mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 19:59:20 +00:00
feat: added new method to fetch old whatsapp messages
This commit is contained in:
@@ -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