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";
|
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
import { updateTicketCacheByTicketId } from "../../helpers/TicketCache";
|
2022-10-25 14:16:36 +00:00
|
|
|
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";
|
2023-09-08 19:50:51 +00:00
|
|
|
import sendWhatsMediaOfficialAPI from "../../helpers/sendWhatsMediaOfficialAPI";
|
|
|
|
import { mediaTypeWhatsappOfficial } from "./wbotMessageListener";
|
|
|
|
import { bytesToMB } from "../../helpers/BytesToMB";
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
interface Request {
|
|
|
|
media: Express.Multer.File;
|
2023-09-08 19:50:51 +00:00
|
|
|
ticket: Ticket;
|
|
|
|
mic_audio?: any
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const SendWhatsAppMedia = async ({
|
|
|
|
media,
|
2023-09-08 19:50:51 +00:00
|
|
|
ticket,
|
|
|
|
mic_audio
|
2023-02-07 15:47:40 +00:00
|
|
|
}: Request): Promise<WbotMessage | any> => {
|
2023-09-08 19:50:51 +00:00
|
|
|
const { phoneNumberId } = ticket;
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
if (phoneNumberId) {
|
|
|
|
const { type, mbMaxSize }: any = mediaTypeWhatsappOfficial(media.mimetype);
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
const filesize: any = bytesToMB(media.size);
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
if (filesize > mbMaxSize) {
|
|
|
|
throw new AppError("FILE TOO LARGE!");
|
|
|
|
}
|
|
|
|
if (!type) {
|
|
|
|
throw new AppError("FILE TYPE NOT SUPPORTED!");
|
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
sendWhatsMediaOfficialAPI(ticket, media, type, mic_audio);
|
|
|
|
return;
|
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
try {
|
|
|
|
const newMedia = MessageMedia.fromFilePath(media.path);
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
sendWhatsAppMediaSocket(ticket, newMedia);
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
await ticket.update({ lastMessage: media.filename });
|
|
|
|
|
|
|
|
await updateTicketCacheByTicketId(ticket.id, {
|
|
|
|
lastMessage: media.filename,
|
|
|
|
updatedAt: new Date(ticket.updatedAt).toISOString()
|
|
|
|
});
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
console.log("media.path: ", media.path);
|
|
|
|
fs.unlinkSync(media.path);
|
2022-01-06 01:26:15 +00:00
|
|
|
} catch (err) {
|
|
|
|
throw new AppError("ERR_SENDING_WAPP_MSG");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SendWhatsAppMedia;
|