Adição do recurso para transferir ticket quando entrar em loop infinito de ura

pull/21/head
adriano 2023-08-10 09:32:38 -03:00
parent 7b1d2f2d2b
commit ee11b977cc
2 changed files with 100 additions and 30 deletions

View File

@ -0,0 +1,56 @@
import { subSeconds } from "date-fns";
import Message from "../models/Message";
import { Op, Sequelize } from "sequelize";
const mostRepeatedPhrase = async (
ticketId: number | string,
fromMe: boolean = false
) => {
let res: any = { body: "", occurrences: 0 };
try {
const mostRepeatedPhrase: any = await Message.findOne({
where: {
ticketId: ticketId,
fromMe: fromMe ? fromMe : 0,
body: {
[Op.notRegexp]: "^[0-9]+$"
},
updatedAt: {
[Op.between]: [+subSeconds(new Date(), 150), +new Date()]
}
},
attributes: [
"body",
[Sequelize.fn("COUNT", Sequelize.col("body")), "occurrences"]
],
group: ["body"],
order: [[Sequelize.literal("occurrences"), "DESC"]],
limit: 1
});
if (mostRepeatedPhrase) {
const { body, occurrences } = mostRepeatedPhrase.get();
console.log(
`The most repeated phrase is "${body}" with ${occurrences} occurrences.`
);
const isNumber = /^\d+$/.test(body.trim());
if (!isNumber) {
return { body, occurrences };
}
} else {
console.log("No phrases found.");
}
} catch (error) {
console.log("error on MostRepeatedPhrase: ", error);
}
return { body: "", occurrences: 0 };
};
export default mostRepeatedPhrase;

View File

@ -82,6 +82,7 @@ import {
import AppError from "../../errors/AppError"; import AppError from "../../errors/AppError";
import { setMessageAsRead } from "../../helpers/SetMessageAsRead"; import { setMessageAsRead } from "../../helpers/SetMessageAsRead";
import SettingTicket from "../../models/SettingTicket"; import SettingTicket from "../../models/SettingTicket";
import mostRepeatedPhrase from "../../helpers/MostRepeatedPhrase";
var lst: any[] = getWhatsappIds(); var lst: any[] = getWhatsappIds();
@ -299,12 +300,20 @@ const verifyQueue = async (
sendWhatsAppMessageSocket(ticket, body); sendWhatsAppMessageSocket(ticket, body);
} else { } else {
//test del transfere o atendimento se entrar na ura infinita //test del transfere o atendimento se entrar na ura infinita
let ticket_message = await ShowTicketMessage(ticket.id, false); const repet: any = await mostRepeatedPhrase(ticket.id);
if (ticket_message.length > 10) {
if (repet.occurrences > 4) {
await UpdateTicketService({ await UpdateTicketService({
ticketData: { status: "pending", queueId: queues[0].id }, ticketData: { status: "pending", queueId: queues[0].id },
ticketId: ticket.id ticketId: ticket.id
}); });
await SendWhatsAppMessage({
body: `Seu atendimento foi transferido para um agente!
`,
ticket,
number: `${contact.number}@c.us`
});
} else { } else {
let options = ""; let options = "";
@ -613,52 +622,57 @@ const handleMessage = async (msg: any, wbot: any): Promise<void> => {
await setMessageAsRead(ticket); await setMessageAsRead(ticket);
} }
// MESSAGE TO HOLIDAY let ticketHasQueue = false;
const holiday: any = await isHoliday();
if (holiday.set) { if (ticket?.queueId) {
if (msg.fromMe && holiday.msg == msg.body) { ticketHasQueue = true;
console.log("HOLIDAY MESSAGE IGNORED"); }
if (ticketHasQueue) {
// MESSAGE TO HOLIDAY
const holiday: any = await isHoliday();
if (holiday.set) {
if (msg.fromMe && holiday.msg == msg.body) {
console.log("HOLIDAY MESSAGE IGNORED");
return;
}
botSendMessage(ticket, holiday.msg);
return; return;
} }
botSendMessage(ticket, holiday.msg); // MESSAGES TO SATURDAY OR SUNDAY
return; const weekend: any = await isWeekend();
}
// MESSAGES TO SATURDAY OR SUNDAY if (weekend.set) {
const weekend: any = await isWeekend(); if (msg.fromMe && weekend.msg == msg.body) {
console.log("WEEKEND MESSAGE IGNORED");
return;
}
if (weekend.set) { botSendMessage(ticket, weekend.msg);
if (msg.fromMe && weekend.msg == msg.body) {
console.log("WEEKEND MESSAGE IGNORED");
return; return;
} }
botSendMessage(ticket, weekend.msg); // MESSAGE TO BUSINESS TIME
return; const businessTime = await isOutBusinessTime();
}
// MESSAGE TO BUSINESS TIME if (businessTime.set) {
const businessTime = await isOutBusinessTime(); if (msg.fromMe && businessTime.msg == msg.body) {
console.log("BUSINESS TIME MESSAGE IGNORED");
return;
}
if (businessTime.set) { botSendMessage(ticket, businessTime.msg);
if (msg.fromMe && businessTime.msg == msg.body) {
console.log("BUSINESS TIME MESSAGE IGNORED");
return; return;
} }
botSendMessage(ticket, businessTime.msg);
return;
} }
} catch (err) { } catch (err) {
Sentry.captureException(err); Sentry.captureException(err);
console.log("Error handling whatsapp message: Err: ", err); console.log("Error handling whatsapp message: Err: ", err);
logger.error(`Error handling whatsapp message: Err: ${err}`); logger.error(`Error handling whatsapp message: Err: ${err}`);
} }
}; };
const handleMsgAck = async (msg_id: any, ack: any) => { const handleMsgAck = async (msg_id: any, ack: any) => {