ATENÇÃO! Essa alteração é uma adaptação para que possa ser enviado a ura padrão sem a alternativa do usuário escolher opções. Ex 1-.. 2-.. etc. Caso queira voltar para o comportamento padrão que é utilizar as opções definidas nas filas para que o usuário possa escolher, você precisa alterar o arquivo wbotMessageListener.ts e habilitar o seguintes trechos de código: const selectedOption = msg.body; const choosenQueue = queues[+selectedOption - 1];

pull/1/head
adriano 2022-01-18 10:47:19 -03:00
parent 9e02795e0f
commit 1d75ec1514
2 changed files with 33 additions and 30 deletions

View File

@ -1,9 +1,11 @@
import { subHours } from "date-fns"; import { subHours, subMinutes } from "date-fns";
import { Op } from "sequelize"; import { Op } from "sequelize";
import Contact from "../../models/Contact"; import Contact from "../../models/Contact";
import Ticket from "../../models/Ticket"; import Ticket from "../../models/Ticket";
import ShowTicketService from "./ShowTicketService"; import ShowTicketService from "./ShowTicketService";
const FindOrCreateTicketService = async ( const FindOrCreateTicketService = async (
contact: Contact, contact: Contact,
whatsappId: number, whatsappId: number,
@ -40,11 +42,15 @@ const FindOrCreateTicketService = async (
} }
} }
if (!ticket && !groupContact) { if (!ticket && !groupContact) {
ticket = await Ticket.findOne({ ticket = await Ticket.findOne({
where: { where: {
updatedAt: { updatedAt: {
[Op.between]: [+subHours(new Date(), 2), +new Date()] //[Op.between]: [+subHours(new Date(), 2), +new Date()]
// Tempo osioso para a ura responder thuanny
[Op.between]: [+subMinutes(new Date(), 4), +new Date()]
}, },
contactId: contact.id contactId: contact.id
}, },
@ -61,6 +67,7 @@ const FindOrCreateTicketService = async (
} }
if (!ticket) { if (!ticket) {
ticket = await Ticket.create({ ticket = await Ticket.create({
contactId: groupContact ? groupContact.id : contact.id, contactId: groupContact ? groupContact.id : contact.id,
status: "pending", status: "pending",

View File

@ -22,6 +22,8 @@ import FindOrCreateTicketService from "../TicketServices/FindOrCreateTicketServi
import ShowWhatsAppService from "../WhatsappService/ShowWhatsAppService"; import ShowWhatsAppService from "../WhatsappService/ShowWhatsAppService";
import { debounce } from "../../helpers/Debounce"; import { debounce } from "../../helpers/Debounce";
import UpdateTicketService from "../TicketServices/UpdateTicketService"; import UpdateTicketService from "../TicketServices/UpdateTicketService";
import { date } from "faker";
interface Session extends Client { interface Session extends Client {
id?: number; id?: number;
@ -99,7 +101,7 @@ const verifyMediaMessage = async (
mediaUrl: media.filename, mediaUrl: media.filename,
mediaType: media.mimetype.split("/")[0], mediaType: media.mimetype.split("/")[0],
quotedMsgId: quotedMsg?.id quotedMsgId: quotedMsg?.id
}; };
await ticket.update({ lastMessage: msg.body || media.filename }); await ticket.update({ lastMessage: msg.body || media.filename });
const newMessage = await CreateMessageService({ messageData }); const newMessage = await CreateMessageService({ messageData });
@ -112,6 +114,8 @@ const verifyMessage = async (
ticket: Ticket, ticket: Ticket,
contact: Contact contact: Contact
) => { ) => {
const quotedMsg = await verifyQuotedMessage(msg); const quotedMsg = await verifyQuotedMessage(msg);
const messageData = { const messageData = {
id: msg.id.id, id: msg.id.id,
@ -123,6 +127,7 @@ const verifyMessage = async (
read: msg.fromMe, read: msg.fromMe,
quotedMsgId: quotedMsg?.id quotedMsgId: quotedMsg?.id
}; };
await ticket.update({ lastMessage: msg.body }); await ticket.update({ lastMessage: msg.body });
@ -136,22 +141,23 @@ const verifyQueue = async (
contact: Contact contact: Contact
) => { ) => {
const { queues, greetingMessage } = await ShowWhatsAppService(wbot.id!); const { queues, greetingMessage } = await ShowWhatsAppService(wbot.id!);
console.log('---greetingMessage: ',greetingMessage)
if (queues.length === 1) { if (queues.length === 1) {
await UpdateTicketService({ await UpdateTicketService({
ticketData: { queueId: queues[0].id }, ticketData: { queueId: queues[0].id },
ticketId: ticket.id ticketId: ticket.id
}); });
return; return;
} }
//const selectedOption = msg.body;
//const choosenQueue = queues[+selectedOption - 1];
const selectedOption = msg.body; //apagar essa gambiarra e ativar as 2 linhas acima
const selectedOption = 1;
const choosenQueue = queues[+selectedOption - 1];
const choosenQueue = queues[+selectedOption - 1];
if (choosenQueue) { if (choosenQueue) {
await UpdateTicketService({ await UpdateTicketService({
@ -173,8 +179,6 @@ const verifyQueue = async (
const body = `\u200e${greetingMessage}\n${options}`; const body = `\u200e${greetingMessage}\n${options}`;
console.log('BACKEND BODY TEST: ', body)
const debouncedSentMessage = debounce( const debouncedSentMessage = debounce(
async () => { async () => {
const sentMessage = await wbot.sendMessage( const sentMessage = await wbot.sendMessage(
@ -211,7 +215,7 @@ const handleMessage = async (
msg: WbotMessage, msg: WbotMessage,
wbot: Session wbot: Session
): Promise<void> => { ): Promise<void> => {
if (!isValidMsg(msg)) { if (!isValidMsg(msg)) {
return; return;
} }
@ -228,14 +232,13 @@ const handleMessage = async (
// in this case, return and let this message be handled by "media_uploaded" event, when it will have "hasMedia = true" // in this case, return and let this message be handled by "media_uploaded" event, when it will have "hasMedia = true"
if (!msg.hasMedia && msg.type !== "chat" && msg.type !== "vcard") return; if (!msg.hasMedia && msg.type !== "chat" && msg.type !== "vcard") return;
msgContact = await wbot.getContactById(msg.to);
msgContact = await wbot.getContactById(msg.to);
} else { } else {
msgContact = await msg.getContact(); msgContact = await msg.getContact();
console.log('---msgContact: ', msgContact, ' | msg.body: ', msg.body) console.log('-----msgContact TESTE MSG: ', msgContact, ' | msg.body: ', msg.body)
} }
const chat = await msg.getChat(); const chat = await msg.getChat();
@ -268,31 +271,24 @@ const handleMessage = async (
groupContact groupContact
); );
console.log('---passou 1')
if (msg.hasMedia) { if (msg.hasMedia) {
await verifyMediaMessage(msg, ticket, contact); await verifyMediaMessage(msg, ticket, contact);
} else { } else {
await verifyMessage(msg, ticket, contact); await verifyMessage(msg, ticket, contact);
} }
console.log('---passou 2 ') //setTimeout(()=>verifyQueue(wbot, msg, ticket, contact), 3000);
//await verifyQueue(wbot, msg, ticket, contact);
if ( if (
!ticket.queue && !ticket.queue &&
!chat.isGroup && !chat.isGroup &&
!msg.fromMe && !msg.fromMe &&
!ticket.userId && !ticket.userId &&
whatsapp.queues.length >= 1 whatsapp.queues.length >= 1
) { ) {
await verifyQueue(wbot, msg, ticket, contact); await verifyQueue(wbot, msg, ticket, contact);
} }
} catch (err) { } catch (err) {