2022-01-06 01:26:15 +00:00
|
|
|
import { Request, Response } from "express";
|
2023-08-28 17:05:53 +00:00
|
|
|
import whatsappOfficialAPI from "../helpers/WhatsappOfficialAPI";
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
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";
|
|
|
|
import DeleteWhatsAppMessage from "../services/WbotServices/DeleteWhatsAppMessage";
|
|
|
|
import SendWhatsAppMedia from "../services/WbotServices/SendWhatsAppMedia";
|
|
|
|
import SendWhatsAppMessage from "../services/WbotServices/SendWhatsAppMessage";
|
2023-08-28 17:05:53 +00:00
|
|
|
import axios from "axios";
|
|
|
|
import Contact from "../models/Contact";
|
|
|
|
import {
|
|
|
|
isValidMsg,
|
|
|
|
verifyMessage
|
|
|
|
} from "../services/WbotServices/wbotMessageListener";
|
|
|
|
import CreateOrUpdateContactService from "../services/ContactServices/CreateOrUpdateContactService";
|
2023-09-08 19:50:51 +00:00
|
|
|
import sendWhatsAppMessageOfficialAPI from "../helpers/sendWhatsAppMessageOfficialAPI";
|
2023-09-19 12:41:15 +00:00
|
|
|
import Whatsapp from "../models/Whatsapp";
|
|
|
|
import checkLastClientMsg24hs from "../helpers/CheckLastClientMsg24hs";
|
|
|
|
import AppError from "../errors/AppError";
|
2024-03-13 14:19:40 +00:00
|
|
|
import { get } from "../helpers/RedisClient";
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
type IndexQuery = {
|
|
|
|
pageNumber: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type MessageData = {
|
|
|
|
body: string;
|
|
|
|
fromMe: boolean;
|
|
|
|
read: boolean;
|
|
|
|
quotedMsg?: Message;
|
2023-09-19 12:41:15 +00:00
|
|
|
mic_audio?: boolean;
|
|
|
|
params: any;
|
2022-01-06 01:26:15 +00:00
|
|
|
};
|
|
|
|
|
2024-03-13 14:19:40 +00:00
|
|
|
export const index = async (req: Request, res: Response): Promise<Response> => {
|
2022-01-06 01:26:15 +00:00
|
|
|
const { ticketId } = req.params;
|
|
|
|
const { pageNumber } = req.query as IndexQuery;
|
|
|
|
|
|
|
|
const { count, messages, ticket, hasMore } = await ListMessagesService({
|
|
|
|
pageNumber,
|
|
|
|
ticketId
|
|
|
|
});
|
2023-09-19 12:41:15 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
return res.json({ count, messages, ticket, hasMore });
|
|
|
|
};
|
|
|
|
|
2023-08-28 17:05:53 +00:00
|
|
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
2022-01-06 01:26:15 +00:00
|
|
|
const { ticketId } = req.params;
|
2023-09-19 12:41:15 +00:00
|
|
|
const { body, quotedMsg, mic_audio, params }: MessageData = req.body;
|
2022-01-06 01:26:15 +00:00
|
|
|
const medias = req.files as Express.Multer.File[];
|
|
|
|
const ticket = await ShowTicketService(ticketId);
|
|
|
|
|
2023-08-28 17:05:53 +00:00
|
|
|
const { queueId } = ticket;
|
2023-09-19 12:41:15 +00:00
|
|
|
console.log(
|
|
|
|
"-----------> queueId: ",
|
|
|
|
queueId,
|
|
|
|
" | quotedMsg: ",
|
|
|
|
quotedMsg,
|
|
|
|
" | params: ",
|
|
|
|
params
|
|
|
|
);
|
|
|
|
|
|
|
|
const { phoneNumberId, whatsappId } = ticket;
|
|
|
|
|
|
|
|
if (phoneNumberId) {
|
|
|
|
const into24hs = await checkLastClientMsg24hs(ticket);
|
|
|
|
|
|
|
|
if (into24hs && into24hs.length == 0) {
|
|
|
|
if (params) {
|
|
|
|
console.log("SEND TEMPLATE PARAMS: ", params);
|
|
|
|
|
|
|
|
// return res.send()
|
|
|
|
|
|
|
|
let payloadComponents = [];
|
|
|
|
|
2024-03-01 20:47:18 +00:00
|
|
|
try {
|
2023-09-19 12:41:15 +00:00
|
|
|
for (let i in params) {
|
|
|
|
const { parameters, language, type } = params[i];
|
|
|
|
if (type == "BODY") {
|
|
|
|
if (parameters && parameters.length > 0) {
|
|
|
|
let components: any = [{ type: "body", parameters: [] }];
|
|
|
|
for (let x in parameters) {
|
|
|
|
const { type, text, index } = parameters[x];
|
|
|
|
console.log(text);
|
|
|
|
components[0].parameters.splice(index - 1, 0, {
|
|
|
|
type,
|
|
|
|
text
|
|
|
|
});
|
|
|
|
}
|
|
|
|
payloadComponents.push(components[0]);
|
|
|
|
}
|
|
|
|
} else if (type == "BUTTONS") {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const name = params.find((p: any) => p?.template_name);
|
2024-03-13 14:19:40 +00:00
|
|
|
const { language }: any =
|
|
|
|
params?.find((p: any) => p?.language) || "pt_BR";
|
2023-09-19 12:41:15 +00:00
|
|
|
|
|
|
|
const { template_name } = name;
|
|
|
|
|
|
|
|
if (template_name && language) {
|
|
|
|
const template: any = {
|
|
|
|
template: {
|
|
|
|
name: template_name,
|
|
|
|
language: { code: language },
|
|
|
|
components: payloadComponents
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-01 20:47:18 +00:00
|
|
|
console.log("TEMPLATE: ", template);
|
|
|
|
|
2023-09-19 12:41:15 +00:00
|
|
|
sendWhatsAppMessageOfficialAPI(ticket, body, null, template);
|
|
|
|
|
|
|
|
return res.send();
|
|
|
|
}
|
|
|
|
} catch (error: any) {
|
|
|
|
throw new AppError(error.message);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
const { wabaId }: any = await Whatsapp.findByPk(whatsappId);
|
|
|
|
|
|
|
|
const { data } = await whatsappOfficialAPI.get(
|
|
|
|
`/${process.env.VERSION}/${wabaId}/message_templates?language=pt_BR`
|
|
|
|
);
|
|
|
|
|
|
|
|
return res.status(200).json(data);
|
|
|
|
} catch (error) {
|
|
|
|
return res
|
|
|
|
.status(500)
|
|
|
|
.json({ message: "Não foi possível baixar os templates!" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-28 17:05:53 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
if (medias) {
|
|
|
|
await Promise.all(
|
2023-08-28 17:05:53 +00:00
|
|
|
medias.map(async (media: Express.Multer.File) => {
|
|
|
|
console.log(
|
|
|
|
`\n >>>>>>>>>> SENDING MESSAGE MEDIA
|
2022-07-20 15:34:12 +00:00
|
|
|
Parcial ticket info and media:
|
|
|
|
ticket.id: ${ticket.id}
|
|
|
|
ticket.status: ${ticket.status}
|
|
|
|
ticket.whatsapp.id: ${ticket.whatsappId}
|
|
|
|
ticket.contact.number: ${ticket.contact.number}
|
|
|
|
ticket.contact.name: ${ticket.contact.name}
|
|
|
|
ticket.contact.profilePicUrl: ${ticket.contact.profilePicUrl}
|
|
|
|
ticket.user.id: ${ticket.user.id}
|
|
|
|
ticket.user.name: ${ticket.user.name}
|
2023-08-28 17:05:53 +00:00
|
|
|
media:`,
|
|
|
|
media,
|
|
|
|
"\n"
|
2023-09-19 12:41:15 +00:00
|
|
|
);
|
2023-09-08 19:50:51 +00:00
|
|
|
await SendWhatsAppMedia({ media, ticket, mic_audio });
|
2022-01-06 01:26:15 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
} else {
|
2022-07-20 15:34:12 +00:00
|
|
|
console.log(`\n >>>>>>>>>> SENDING MESSAGE
|
|
|
|
Parcial ticket info:
|
|
|
|
ticket.id: ${ticket.id}
|
|
|
|
ticket.status: ${ticket.status}
|
2022-11-16 18:29:20 +00:00
|
|
|
ticket.whatsapp.id: ${ticket.whatsappId}
|
2022-07-20 15:34:12 +00:00
|
|
|
ticket.contact.number: ${ticket.contact.number}
|
2022-11-16 18:29:20 +00:00
|
|
|
message: ${body}
|
2022-07-20 15:34:12 +00:00
|
|
|
ticket.contact.name: ${ticket.contact.name}
|
|
|
|
ticket.contact.profilePicUrl: ${ticket.contact.profilePicUrl}
|
|
|
|
ticket.user.id: ${ticket.user.id}
|
2023-08-28 17:05:53 +00:00
|
|
|
ticket.user.name: ${ticket.user.name}\n`);
|
2022-03-01 03:27:17 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
await SendWhatsAppMessage({ body, ticket, quotedMsg });
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.send();
|
|
|
|
};
|
|
|
|
|
|
|
|
export const remove = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
|
|
|
const { messageId } = req.params;
|
|
|
|
|
|
|
|
const message = await DeleteWhatsAppMessage(messageId);
|
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
io.to(message.ticketId.toString()).emit("appMessage", {
|
|
|
|
action: "update",
|
|
|
|
message
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.send();
|
|
|
|
};
|