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