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