2022-01-06 01:26:15 +00:00
|
|
|
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";
|
|
|
|
|
2022-04-23 15:18:02 +00:00
|
|
|
import ShowWhatsAppService from "../WhatsappService/ShowWhatsAppService";
|
|
|
|
import wbotByUserQueue from '../../helpers/GetWbotByUserQueue'
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
interface Request {
|
|
|
|
body: string;
|
|
|
|
ticket: Ticket;
|
|
|
|
quotedMsg?: Message;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SendWhatsAppMessage = async ({
|
|
|
|
body,
|
|
|
|
ticket,
|
|
|
|
quotedMsg
|
|
|
|
}: Request): Promise<WbotMessage> => {
|
|
|
|
let quotedMsgSerializedId: string | undefined;
|
|
|
|
if (quotedMsg) {
|
|
|
|
await GetWbotMessage(ticket, quotedMsg.id);
|
|
|
|
quotedMsgSerializedId = SerializeWbotMsgId(ticket, quotedMsg);
|
|
|
|
}
|
|
|
|
|
2022-04-23 15:18:02 +00:00
|
|
|
// test del
|
|
|
|
const whatsapp = await ShowWhatsAppService(ticket.whatsappId);
|
|
|
|
|
|
|
|
if(whatsapp.status!='CONNECTED'){
|
|
|
|
|
|
|
|
let whatsapps = await wbotByUserQueue(ticket.userId)
|
|
|
|
|
|
|
|
if(whatsapps.length > 0){
|
|
|
|
|
|
|
|
await ticket.update({ whatsappId: whatsapps[0].id });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
//
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const wbot = await GetTicketWbot(ticket);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const sentMessage = await wbot.sendMessage(
|
|
|
|
`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`,
|
|
|
|
body,
|
|
|
|
{
|
|
|
|
quotedMessageId: quotedMsgSerializedId,
|
|
|
|
linkPreview: false
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
await ticket.update({ lastMessage: body });
|
|
|
|
return sentMessage;
|
|
|
|
} catch (err) {
|
|
|
|
throw new AppError("ERR_SENDING_WAPP_MSG");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SendWhatsAppMessage;
|