Skip to content

Commit

Permalink
emitOwnEvents: false and remove hidden character from messages #fix 190
Browse files Browse the repository at this point in the history
  • Loading branch information
allgood committed Oct 21, 2024
1 parent ab1be9b commit 9718eea
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 62 deletions.
2 changes: 1 addition & 1 deletion backend/src/helpers/SendMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const SendMessage = async (

let message;

const body = `\u200e${messageData.body}`;
const body = `${messageData.body}`;

if (messageData.mediaPath) {
const options = await getMessageOptions(body, messageData.mediaPath);
Expand Down
1 change: 1 addition & 0 deletions backend/src/libs/wbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export const initWASocket = async (whatsapp: Whatsapp): Promise<Session> => {
wsocket = makeWASocket({
logger: loggerBaileys,
printQRInTerminal: false,
emitOwnEvents: false,
browser: [clientName, "Desktop", appVersion],
auth: {
creds: state.creds,
Expand Down
4 changes: 2 additions & 2 deletions backend/src/queues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ async function handlePrepareContact(job: { data: PrepareContactData }) {
variables,
contact
);
campaignShipping.message = `\u200c${message}`;
campaignShipping.message = `${message}`;
}

if (campaign.confirmation) {
Expand All @@ -482,7 +482,7 @@ async function handlePrepareContact(job: { data: PrepareContactData }) {
variables,
contact
);
campaignShipping.confirmationMessage = `\u200c${message}`;
campaignShipping.confirmationMessage = `${message}`;
}
}

Expand Down
10 changes: 5 additions & 5 deletions backend/src/services/TicketServices/UpdateTicketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const UpdateTicketService = async ({
if (ticketTraking.ratingAt == null && !justClose) {
const ratingTxt =
ratingMessage?.trim() || "Por favor avalie nosso atendimento";
const bodyRatingMessage = `\u200e${ratingTxt}\n\n*Digite uma nota de 1 a 5*\n`;
const bodyRatingMessage = `${ratingTxt}\n\n*Digite uma nota de 1 a 5*\n`;

if (ticket.channel === "whatsapp") {
await SendWhatsAppMessage({ body: bodyRatingMessage, ticket });
Expand Down Expand Up @@ -160,7 +160,7 @@ const UpdateTicketService = async ({
!isNil(complationMessage) &&
complationMessage !== ""
) {
const body = `\u200e${complationMessage}`;
const body = `${complationMessage}`;

if (ticket.channel === "whatsapp" && !ticket.isGroup) {
const sentMessage = await SendWhatsAppMessage({ body, ticket });
Expand Down Expand Up @@ -211,7 +211,7 @@ const UpdateTicketService = async ({
ticket.isGroup ? "g.us" : "s.whatsapp.net"
}`,
{
text: `\u200e${transferMessage}`
text: `${transferMessage}`
}
);
await verifyMessage(queueChangedMessage, ticket, ticket.contact);
Expand All @@ -221,7 +221,7 @@ const UpdateTicketService = async ({
ticket.isGroup ? "g.us" : "s.whatsapp.net"
}`,
{
text: "\u200eVocê foi transferido, em breve iremos iniciar seu atendimento."
text: "Você foi transferido, em breve iremos iniciar seu atendimento."
}
);
await verifyMessage(queueChangedMessage, ticket, ticket.contact);
Expand All @@ -233,7 +233,7 @@ const UpdateTicketService = async ({
`Checking if ${ticket.contact.number} is a valid ${ticket.channel} contact`
);
await sendFaceMessage({
body: "\u200eVocê foi transferido, em breve iremos iniciar seu atendimento.",
body: "Você foi transferido, em breve iremos iniciar seu atendimento.",
ticket
});
}
Expand Down
76 changes: 23 additions & 53 deletions backend/src/services/WbotServices/wbotMessageListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ const downloadMedia = async (msg: proto.IWebMessageInfo, wbot: Session, ticket:
const fileLimit = parseInt(await CheckSettings("downloadLimit", "15"), 10);
if (wbot && message?.fileLength && +message.fileLength > fileLimit*1024*1024) {
const fileLimitMessage = {
text: `\u200e*Mensagem Automática*:\nNosso sistema aceita apenas arquivos com no máximo ${fileLimit} MiB`
text: `*Mensagem Automática*:\nNosso sistema aceita apenas arquivos com no máximo ${fileLimit} MiB`
};

if (!ticket.isGroup) {
Expand All @@ -377,7 +377,7 @@ const downloadMedia = async (msg: proto.IWebMessageInfo, wbot: Session, ticket:
fileLimitMessage
);

sendMsg.message.extendedTextMessage.text = "\u200e*Mensagem do sistema*:\nArquivo recebido além do limite de tamanho do sistema, se for necessário ele pode ser obtido no aplicativo do whatsapp.";
sendMsg.message.extendedTextMessage.text = "*Mensagem do sistema*:\nArquivo recebido além do limite de tamanho do sistema, se for necessário ele pode ser obtido no aplicativo do whatsapp.";

// eslint-disable-next-line no-use-before-define
await verifyMessage(sendMsg, ticket, ticket.contact);
Expand Down Expand Up @@ -861,7 +861,7 @@ const sendMenu = async (
];

const listMessage = {
text: formatBody(`\u200e${message}`, ticket.contact),
text: formatBody(`${message}`, ticket.contact),
buttonText: "Escolha uma opção",
sections
};
Expand Down Expand Up @@ -890,7 +890,7 @@ const sendMenu = async (
});

const buttonMessage = {
text: formatBody(`\u200e${message}`, ticket.contact),
text: formatBody(`${message}`, ticket.contact),
buttons,
headerType: 4
};
Expand All @@ -912,7 +912,7 @@ const sendMenu = async (
options += "\n*[ # ]* - Voltar Menu Inicial";

const textMessage = {
text: formatBody(`\u200e${message}\n\n${options}`, ticket.contact),
text: formatBody(`${message}\n\n${options}`, ticket.contact),
};

const sendMsg = await wbot.sendMessage(
Expand Down Expand Up @@ -999,7 +999,7 @@ const startQueue = async (wbot: Session, ticket: Ticket, queue: Queue) => {

if (queue.options.length === 0) {
if (queue.greetingMessage?.trim()) {
const body = formatBody(`\u200e${queue.greetingMessage.trim()}`, ticket.contact);
const body = formatBody(`${queue.greetingMessage.trim()}`, ticket.contact);

if (filePath) {
optionsMsg.caption = body;
Expand Down Expand Up @@ -1073,7 +1073,7 @@ const verifyQueue = async (
];

const listMessage = {
text: formatBody(`\u200e${greetingMessage}`, contact),
text: formatBody(`${greetingMessage}`, contact),
buttonText: "Escolha uma opção",
sections
};
Expand All @@ -1097,7 +1097,7 @@ const verifyQueue = async (
});

const buttonMessage = {
text: formatBody(`\u200e${greetingMessage}`, contact),
text: formatBody(`${greetingMessage}`, contact),
buttons,
headerType: 4
};
Expand All @@ -1119,7 +1119,7 @@ const verifyQueue = async (


const textMessage = {
text: formatBody(`\u200e${greetingMessage}\n\n${options}`, contact),
text: formatBody(`${greetingMessage}\n\n${options}`, contact),
};

const sendMsg = await wbot.sendMessage(
Expand Down Expand Up @@ -1201,7 +1201,7 @@ export const handleRating = async (
});

const complationMessage = whatsapp.complationMessage.trim() || "Atendimento finalizado";
const body = formatBody(`\u200e${complationMessage}`, ticket.contact);
const body = formatBody(`${complationMessage}`, ticket.contact);
await SendWhatsAppMessage({ body, ticket });

await ticketTraking.update({
Expand Down Expand Up @@ -1317,7 +1317,7 @@ const handleChartbot = async (ticket: Ticket, msg: WAMessage, wbot: Session, don
const whatsapp = await Whatsapp.findByPk(ticket.whatsappId);
const contact = await Contact.findByPk(ticket.contactId);
if (whatsapp.transferMessage) {
const body = formatBody(`\u200e${whatsapp.transferMessage}`, contact);
const body = formatBody(`${whatsapp.transferMessage}`, contact);
await SendWhatsAppMessage({ body, ticket });
}
}
Expand Down Expand Up @@ -1361,7 +1361,7 @@ const handleChartbot = async (ticket: Ticket, msg: WAMessage, wbot: Session, don
}

if (currentOption.exitChatbot || currentOption.forwardQueueId) {
const text = formatBody(`\u200e${currentOption.message}`, ticket.contact);
const text = formatBody(`${currentOption.message}`, ticket.contact);

if (filePath) {
optionsMsg.caption = text;
Expand Down Expand Up @@ -1439,7 +1439,7 @@ const handleMessage = async (
const unpackedMessage = getUnpackedMessage(msg);
const messageMedia = getMessageMedia(unpackedMessage);
if (msg.key.fromMe) {
if (bodyMessage?.startsWith("\u200e")) return;
if (bodyMessage?.startsWith("")) return;

if (
!messageMedia &&
Expand Down Expand Up @@ -1546,7 +1546,7 @@ const handleMessage = async (
`${ticketTracking.ticket.contact.number}@${ticketTracking.ticket.isGroup ? "g.us" : "s.whatsapp.net"
}`,
{
text: "\u200e\n*Por favor avalie nosso atendimento com uma nota de 1 a 5*"
text: "\n*Por favor avalie nosso atendimento com uma nota de 1 a 5*"
}
);
},
Expand Down Expand Up @@ -1636,7 +1636,7 @@ const handleMessage = async (
`${ticket.contact.number}@${ticket.isGroup ? "g.us" : "s.whatsapp.net"
}`,
{
text: `\u200e${body}`
text: `${body}`
}
);
},
Expand Down Expand Up @@ -1668,7 +1668,7 @@ const handleMessage = async (
`${ticket.contact.number}@${ticket.isGroup ? "g.us" : "s.whatsapp.net"
}`,
{
text: `\u200e${body}`
text: `${body}`
}
);
},
Expand Down Expand Up @@ -1722,7 +1722,7 @@ const handleMessage = async (
`${ticket.contact.number}@${ticket.isGroup ? "g.us" : "s.whatsapp.net"
}`,
{
text: `\u200e${body}`
text: `${body}`
}
);
},
Expand Down Expand Up @@ -1762,7 +1762,7 @@ const handleMessage = async (
`${ticket.contact.number}@${ticket.isGroup ? "g.us" : "s.whatsapp.net"
}`,
{
text: formatBody(`\u200e${whatsapp.greetingMessage}`, contact, ticket)
text: formatBody(`${whatsapp.greetingMessage}`, contact, ticket)
}
);
},
Expand Down Expand Up @@ -1859,42 +1859,11 @@ const verifyRecentCampaign = async (
delay: parseToMilliseconds(randomValue(0, 10)),
}
);
return true;
}
}
}
};

const verifyCampaignMessageAndCloseTicket = async (
message: proto.IWebMessageInfo,
companyId: number
) => {
const io = getIO();
const body = getBodyMessage(message);
const isCampaign = /\u200c/.test(body);
if (message.key.fromMe && isCampaign) {
const messageRecord = await Message.findOne({
where: { id: message.key.id!, companyId },
});
const ticket = await Ticket.findByPk(messageRecord.ticketId);
await ticket.update({ status: "closed" });

io.to(`company-${ticket.companyId}-open`)
.to(`queue-${ticket.queueId}-open`)
.emit(`company-${ticket.companyId}-ticket`, {
action: "delete",
ticket,
ticketId: ticket.id,
});

io.to(`company-${ticket.companyId}-${ticket.status}`)
.to(`queue-${ticket.queueId}-${ticket.status}`)
.to(ticket.id.toString())
.emit(`company-${ticket.companyId}-ticket`, {
action: "update",
ticket,
ticketId: ticket.id,
});
}
return false;
};

const filterMessages = (msg: WAMessage): boolean => {
Expand Down Expand Up @@ -1993,9 +1962,10 @@ const wbotMessageListener = async (wbot: Session, companyId: number): Promise<vo

if (!messageExists) {

if (await verifyRecentCampaign(message, companyId)) {
return;
}
await handleMessage(message, wbot, companyId);
await verifyRecentCampaign(message, companyId);
await verifyCampaignMessageAndCloseTicket(message, companyId);
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion backend/src/services/WbotServices/wbotMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const wbotMonitor = async (

if (sendMsgCall.value === "disabled") {
await wbot.sendMessage(node.attrs.from, {
text: "\u200e*Mensagem Automática:*\n\nAs chamadas de voz e vídeo estão desabilitas para esse WhatsApp, favor enviar uma mensagem de texto. Obrigado"
text: "*Mensagem Automática:*\n\nAs chamadas de voz e vídeo estão desabilitas para esse WhatsApp, favor enviar uma mensagem de texto. Obrigado"
});

const number = node.attrs.from.replace(/\D/g, "");
Expand Down

0 comments on commit 9718eea

Please sign in to comment.