2022-01-06 01:26:15 +00:00
|
|
|
import fs from "fs";
|
|
|
|
import { MessageMedia, Message as WbotMessage } from "whatsapp-web.js";
|
|
|
|
import AppError from "../../errors/AppError";
|
|
|
|
import GetTicketWbot from "../../helpers/GetTicketWbot";
|
|
|
|
import Ticket from "../../models/Ticket";
|
|
|
|
|
2022-10-25 14:16:36 +00:00
|
|
|
import { updateTicketCacheByTicketId } from '../../helpers/TicketCache'
|
|
|
|
import { date } from "faker";
|
2023-02-07 15:47:40 +00:00
|
|
|
import { getIO } from "../../libs/socket";
|
|
|
|
import sendWhatsAppMessageSocket from "../../helpers/SendWhatsappMessageSocket";
|
|
|
|
import sendWhatsAppMediaSocket from "../../helpers/SendWhatsappMessageMediaSocket";
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
interface Request {
|
|
|
|
media: Express.Multer.File;
|
|
|
|
ticket: Ticket;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SendWhatsAppMedia = async ({
|
|
|
|
media,
|
|
|
|
ticket
|
2023-02-07 15:47:40 +00:00
|
|
|
}: Request): Promise<WbotMessage | any> => {
|
2022-01-06 01:26:15 +00:00
|
|
|
try {
|
2023-02-07 15:47:40 +00:00
|
|
|
// const wbot = await GetTicketWbot(ticket);
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const newMedia = MessageMedia.fromFilePath(media.path);
|
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
//const sentMessage = await wbot.sendMessage(`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`, newMedia, { sendAudioAsVoice: true });
|
|
|
|
|
|
|
|
sendWhatsAppMediaSocket(ticket, newMedia);
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
await ticket.update({ lastMessage: media.filename });
|
|
|
|
|
2022-10-25 14:16:36 +00:00
|
|
|
// TEST DEL
|
|
|
|
await updateTicketCacheByTicketId(ticket.id, { lastMessage: media.filename, updatedAt: new Date(ticket.updatedAt).toISOString() })
|
|
|
|
//
|
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
console.log('media.path: ', media.path)
|
2022-01-06 01:26:15 +00:00
|
|
|
fs.unlinkSync(media.path);
|
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
// return sentMessage;
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
} catch (err) {
|
|
|
|
throw new AppError("ERR_SENDING_WAPP_MSG");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SendWhatsAppMedia;
|
2023-02-07 15:47:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|