Merge branch 'master' into master

This commit is contained in:
Cassio Santos
2022-01-18 08:36:41 -03:00
committed by GitHub
6 changed files with 59 additions and 14 deletions

View File

@@ -27,6 +27,7 @@
"http-graceful-shutdown": "^2.3.2",
"jsonwebtoken": "^8.5.1",
"multer": "^1.4.2",
"mustache": "^4.2.0",
"mysql2": "^2.2.5",
"pg": "^8.4.1",
"pino": "^6.9.0",
@@ -52,6 +53,7 @@
"@types/jest": "^26.0.15",
"@types/jsonwebtoken": "^8.5.0",
"@types/multer": "^1.4.4",
"@types/mustache": "^4.1.2",
"@types/node": "^14.11.8",
"@types/supertest": "^2.0.10",
"@types/uuid": "^8.3.3",

View File

@@ -8,6 +8,7 @@ import ShowTicketService from "../services/TicketServices/ShowTicketService";
import UpdateTicketService from "../services/TicketServices/UpdateTicketService";
import SendWhatsAppMessage from "../services/WbotServices/SendWhatsAppMessage";
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
import formatBody from "../helpers/Mustache";
type IndexQuery = {
searchParam: string;
@@ -99,14 +100,13 @@ export const update = async (
const { farewellMessage } = whatsapp;
if (farewellMessage) {
await SendWhatsAppMessage({
body: farewellMessage,
await SendWhatsAppMessage({
body: formatBody(farewellMessage, ticket.contact),
ticket
});
}
}
return res.status(200).json(ticket);
};

View File

@@ -0,0 +1,9 @@
import Mustache from "mustache";
import Contact from "../models/Contact";
export default (body: string, contact: Contact): string => {
const view = {
name: contact ? contact.name : ""
};
return Mustache.render(body, view);
};

View File

@@ -4,6 +4,8 @@ import AppError from "../../errors/AppError";
import GetTicketWbot from "../../helpers/GetTicketWbot";
import Ticket from "../../models/Ticket";
import formatBody from "../../helpers/Mustache";
interface Request {
media: Express.Multer.File;
ticket: Ticket;
@@ -23,7 +25,7 @@ const SendWhatsAppMedia = async ({
`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`,
newMedia,
{
caption: body,
caption: formatBody(body as string, ticket.contact),
sendAudioAsVoice: true
}
);

View File

@@ -6,6 +6,8 @@ import SerializeWbotMsgId from "../../helpers/SerializeWbotMsgId";
import Message from "../../models/Message";
import Ticket from "../../models/Ticket";
import formatBody from "../../helpers/Mustache";
interface Request {
body: string;
ticket: Ticket;
@@ -28,7 +30,7 @@ const SendWhatsAppMessage = async ({
try {
const sentMessage = await wbot.sendMessage(
`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`,
body,
formatBody(body, ticket.contact),
{
quotedMessageId: quotedMsgSerializedId,
linkPreview: false

View File

@@ -3,7 +3,7 @@ import { promisify } from "util";
import { writeFile } from "fs";
import * as Sentry from "@sentry/node";
import WAWebJS, {
import {
Contact as WbotContact,
Message as WbotMessage,
MessageAck,
@@ -24,6 +24,7 @@ import { debounce } from "../../helpers/Debounce";
import UpdateTicketService from "../TicketServices/UpdateTicketService";
import CreateContactService from "../ContactServices/CreateContactService";
import GetContactService from "../ContactServices/GetContactService";
import formatBody from "../../helpers/Mustache";
interface Session extends Client {
id?: number;
@@ -172,7 +173,7 @@ const verifyQueue = async (
ticketId: ticket.id
});
const body = `\u200e${choosenQueue.greetingMessage}`;
const body = formatBody(`\u200e${choosenQueue.greetingMessage}`, contact);
const sentMessage = await wbot.sendMessage(`${contact.number}@c.us`, body);
@@ -184,7 +185,7 @@ const verifyQueue = async (
options += `*${index + 1}* - ${queue.name}\n`;
});
const body = `\u200e${greetingMessage}\n${options}`;
const body = formatBody(`\u200e${greetingMessage}\n${options}`, contact);
const debouncedSentMessage = debounce(
async () => {
@@ -251,7 +252,6 @@ const handleMessage = async (
const chat = await msg.getChat();
if (chat.isGroup) {
let msgGroupContact;
@@ -269,7 +269,12 @@ const handleMessage = async (
const contact = await verifyContact(msgContact);
if (unreadMessages === 0 && whatsapp.farewellMessage && whatsapp.farewellMessage === msg.body) return;
if (
unreadMessages === 0 &&
whatsapp.farewellMessage &&
formatBody(whatsapp.farewellMessage, contact) === msg.body
)
return;
const ticket = await FindOrCreateTicketService(
contact,
@@ -294,9 +299,35 @@ const handleMessage = async (
await verifyQueue(wbot, msg, ticket, contact);
}
if (msg.type === "vcard") { try { const array = msg.body.split("\n"); const obj = []; let contact = ""; for (let index = 0; index < array.length; index++) { const v = array[index]; const values = v.split(":"); for (let ind = 0; ind < values.length; ind++) { if (values[ind].indexOf("+") !== -1) { obj.push({ number: values[ind] }); } if (values[ind].indexOf("FN") !== -1) { contact = values[ind + 1]; } } } for await (const ob of obj) { const cont = await CreateContactService({ name: contact, number: ob.number.replace(/\D/g, "") }); } } catch (error) { console.log(error); } }
if (msg.type === "vcard") {
try {
const array = msg.body.split("\n");
const obj = [];
let contact = "";
for (let index = 0; index < array.length; index++) {
const v = array[index];
const values = v.split(":");
for (let ind = 0; ind < values.length; ind++) {
if (values[ind].indexOf("+") !== -1) {
obj.push({ number: values[ind] });
}
if (values[ind].indexOf("FN") !== -1) {
contact = values[ind + 1];
}
}
}
for await (const ob of obj) {
const cont = await CreateContactService({
name: contact,
number: ob.number.replace(/\D/g, "")
});
}
} catch (error) {
console.log(error);
}
}
/*if (msg.type === "multi_vcard") {
/* if (msg.type === "multi_vcard") {
try {
const array = msg.vCards.toString().split("\n");
let name = "";
@@ -355,8 +386,7 @@ const handleMessage = async (
} catch (error) {
console.log(error);
}
}*/
} */
} catch (err) {
Sentry.captureException(err);
logger.error(`Error handling whatsapp message: Err: ${err}`);