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"; import Whatsapp from "../../models/Whatsapp"; import ShowWhatsAppService from "../WhatsappService/ShowWhatsAppService"; import wbotByUserQueue from '../../helpers/GetWbotByUserQueue' import { WhatsIndex } from "../../helpers/LoadBalanceWhatsSameQueue"; import { deleteTicketsByContactsCache, updateTicketCacheByTicketId } from '../../helpers/TicketCache' import ListWhatsAppsNumber from "../WhatsappService/ListWhatsAppsNumber"; import { getWbot } from "../../libs/wbot"; import { json } from "sequelize/types"; import sendMessageMultiSession from "../../helpers/TrySendMessageMultiSession"; import { restartWhatsSession } from "../../helpers/RestartWhatsSession"; interface Request { body: string; ticket: Ticket; quotedMsg?: Message; } const SendWhatsAppMessage = async ({ body, ticket, quotedMsg }: Request): Promise => { let quotedMsgSerializedId: string | undefined; if (quotedMsg) { await GetWbotMessage(ticket, quotedMsg.id); quotedMsgSerializedId = SerializeWbotMsgId(ticket, quotedMsg); } //TEST DEL // const whatsapp = await ShowWhatsAppService(33); // const wbot2 = getWbot(whatsapp.id); // await wbot2.logout(); // let whatsapps: any const listWhatsapp = await ListWhatsAppsNumber(ticket.whatsappId, 'CONNECTED') if (listWhatsapp.length > 1) { const _whatsapp = listWhatsapp[Math.floor(Math.random() * listWhatsapp.length)]; await ticket.update({ whatsappId: _whatsapp.id }); } else { whatsapps = await Whatsapp.findOne({ where: { id: ticket.whatsappId }, attributes: ['status'] }) } console.log('1 --------> ticket.whatsappId: ', ticket.whatsappId) if (listWhatsapp.length == 0 || (whatsapps && whatsapps.status != 'CONNECTED')) { whatsapps = await wbotByUserQueue(ticket.userId) if (whatsapps.length > 0) { if (whatsapps.length > 1) { await ticket.update({ whatsappId: whatsapps[+WhatsIndex(whatsapps)].id }); } else { await ticket.update({ whatsappId: whatsapps[0].id }); } } } const wbot = await GetTicketWbot(ticket); console.log('2 --------> send from whatsapp ticket.whatsappId: ', ticket.whatsappId) try { const sentMessage = await wbot.sendMessage(`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`, body, { quotedMessageId: quotedMsgSerializedId, linkPreview: false }); await ticket.update({ lastMessage: body }); await updateTicketCacheByTicketId(ticket.id, { lastMessage: body, updatedAt: new Date(ticket.updatedAt).toISOString() }) return sentMessage; } catch (err) { const whatsapp = await ShowWhatsAppService(ticket.whatsappId); if (whatsapp.status != 'RESTORING') { console.log('THE WHATSAAP ID: ', whatsapp.id, ' WILL BE RESTORED SOON!') await whatsapp.update({ status: "RESTORING", }); setTimeout(() => restartWhatsSession(whatsapp, true), 90000); } const sentMessage = await sendMessageMultiSession(ticket, body, quotedMsgSerializedId) if (sentMessage.length > 0) { await ticket.update({ lastMessage: body }); await updateTicketCacheByTicketId(ticket.id, { lastMessage: body, updatedAt: new Date(ticket.updatedAt).toISOString() }) return sentMessage; } throw new AppError("ERR_SENDING_WAPP_MSG"); } }; export default SendWhatsAppMessage;