diff --git a/backend/package.json b/backend/package.json index a2beb90..d21ce59 100644 --- a/backend/package.json +++ b/backend/package.json @@ -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", diff --git a/backend/src/controllers/TicketController.ts b/backend/src/controllers/TicketController.ts index a6af5ef..bacd4fb 100644 --- a/backend/src/controllers/TicketController.ts +++ b/backend/src/controllers/TicketController.ts @@ -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); }; diff --git a/backend/src/helpers/Mustache.ts b/backend/src/helpers/Mustache.ts new file mode 100644 index 0000000..92bd3de --- /dev/null +++ b/backend/src/helpers/Mustache.ts @@ -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); +}; diff --git a/backend/src/services/WbotServices/SendWhatsAppMedia.ts b/backend/src/services/WbotServices/SendWhatsAppMedia.ts index 80778c2..9c07421 100644 --- a/backend/src/services/WbotServices/SendWhatsAppMedia.ts +++ b/backend/src/services/WbotServices/SendWhatsAppMedia.ts @@ -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 } ); diff --git a/backend/src/services/WbotServices/SendWhatsAppMessage.ts b/backend/src/services/WbotServices/SendWhatsAppMessage.ts index f2ef571..58328b8 100644 --- a/backend/src/services/WbotServices/SendWhatsAppMessage.ts +++ b/backend/src/services/WbotServices/SendWhatsAppMessage.ts @@ -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 diff --git a/backend/src/services/WbotServices/wbotMessageListener.ts b/backend/src/services/WbotServices/wbotMessageListener.ts index ea5eb0d..d8daaab 100644 --- a/backend/src/services/WbotServices/wbotMessageListener.ts +++ b/backend/src/services/WbotServices/wbotMessageListener.ts @@ -2,7 +2,6 @@ import { join } from "path"; import { promisify } from "util"; import { writeFile } from "fs"; import * as Sentry from "@sentry/node"; - import { Contact as WbotContact, Message as WbotMessage, @@ -24,6 +23,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; @@ -158,7 +158,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); @@ -170,7 +170,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 () => { @@ -198,7 +198,7 @@ const isValidMsg = (msg: WbotMessage): boolean => { msg.type === "image" || msg.type === "document" || msg.type === "vcard" || - //msg.type === "multi_vcard" || + // msg.type === "multi_vcard" || msg.type === "sticker" ) return true; @@ -225,9 +225,13 @@ const handleMessage = async ( // media messages sent from me from cell phone, first comes with "hasMedia = false" and type = "image/ptt/etc" // 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" - //&& msg.type !== "multi_vcard" - ) return; + if ( + !msg.hasMedia && + msg.type !== "chat" && + msg.type !== "vcard" + // && msg.type !== "multi_vcard" + ) + return; msgContact = await wbot.getContactById(msg.to); } else { @@ -236,7 +240,6 @@ const handleMessage = async ( const chat = await msg.getChat(); - if (chat.isGroup) { let msgGroupContact; @@ -254,7 +257,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, @@ -279,9 +287,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 = ""; @@ -340,8 +374,7 @@ const handleMessage = async ( } catch (error) { console.log(error); } - }*/ - + } */ } catch (err) { Sentry.captureException(err); logger.error(`Error handling whatsapp message: Err: ${err}`);