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,6 +622,13 @@ const handleMessage = async (msg: any, wbot: any): Promise<void> => {
await setMessageAsRead(ticket);
}
let ticketHasQueue = false;
if (ticket?.queueId) {
ticketHasQueue = true;
}
if (ticketHasQueue) {
// MESSAGE TO HOLIDAY
const holiday: any = await isHoliday();
@ -651,14 +667,12 @@ const handleMessage = async (msg: any, wbot: any): Promise<void> => {
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) => {