projeto-hit/backend/src/services/WbotServices/SendWhatsAppMessage.ts

139 lines
3.8 KiB
TypeScript
Raw Normal View History

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";
2022-11-17 14:40:37 +00:00
import { insertOrUpeateWhatsCache, searchWhatsappCache } from "../../helpers/WhatsCache";
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
2022-12-14 12:29:42 +00:00
import autoRestore from "../../helpers/AutoRestore";
2022-12-22 21:49:44 +00:00
import { _restore } from "../../helpers/RestoreControll";
interface Request {
body: string;
ticket: Ticket;
quotedMsg?: Message;
}
const SendWhatsAppMessage = async ({
body,
ticket,
quotedMsg
}: Request): Promise<WbotMessage> => {
let timestamp = Math.floor(Date.now() / 1000)
var timetaken = `########################################${timestamp}| TicketId: ${ticket.id} => Time taken to send the message`;
2022-11-17 14:40:37 +00:00
console.time(timetaken)
2022-11-17 14:40:37 +00:00
let quotedMsgSerializedId: string | undefined;
if (quotedMsg) {
await GetWbotMessage(ticket, quotedMsg.id);
quotedMsgSerializedId = SerializeWbotMsgId(ticket, quotedMsg);
}
let whatsapps: any
2022-12-14 12:29:42 +00:00
let listWhatsapp = null
2022-12-14 12:29:42 +00:00
listWhatsapp = await searchWhatsappCache(`${ticket.whatsappId}`, 'CONNECTED')
2022-12-14 12:29:42 +00:00
if (!listWhatsapp) {
listWhatsapp = await ListWhatsAppsNumber(ticket.whatsappId, 'CONNECTED')
}
if (listWhatsapp.length > 1) {
console.log('entrou --------------------->')
2022-12-14 12:29:42 +00:00
const _whatsapp = listWhatsapp[Math.floor(Math.random() * listWhatsapp.length)];
2022-12-30 01:12:36 +00:00
await ticket.update({ whatsappId: +_whatsapp.id });
2022-12-14 12:29:42 +00:00
}
2022-12-14 12:29:42 +00:00
console.log('1 --------> ticket.whatsappId: ', ticket.whatsappId)
2022-12-14 12:29:42 +00:00
if (listWhatsapp.length == 0) {
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 {
2022-11-17 14:40:37 +00:00
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() })
2022-11-17 14:40:37 +00:00
console.timeEnd(timetaken)
return sentMessage;
} catch (err) {
2022-12-22 21:49:44 +00:00
const whatsapp = await ShowWhatsAppService(ticket.whatsappId);
2022-12-22 21:49:44 +00:00
await _restore(whatsapp, 'auto_send_message')
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;