Skip to content

Commit

Permalink
Merge pull request canove#337 from w3nder/master
Browse files Browse the repository at this point in the history
Feat add. variables {{name}} the message sent
  • Loading branch information
canove authored Jan 18, 2022
2 parents 783c05d + f4653aa commit 72a3e8f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 18 deletions.
2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions backend/src/controllers/TicketController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
};

Expand Down
9 changes: 9 additions & 0 deletions backend/src/helpers/Mustache.ts
Original file line number Diff line number Diff line change
@@ -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);
};
4 changes: 3 additions & 1 deletion backend/src/services/WbotServices/SendWhatsAppMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
}
);
Expand Down
4 changes: 3 additions & 1 deletion backend/src/services/WbotServices/SendWhatsAppMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
59 changes: 46 additions & 13 deletions backend/src/services/WbotServices/wbotMessageListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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 () => {
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -236,7 +240,6 @@ const handleMessage = async (

const chat = await msg.getChat();


if (chat.isGroup) {
let msgGroupContact;

Expand All @@ -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,
Expand All @@ -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 = "";
Expand Down Expand Up @@ -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}`);
Expand Down

0 comments on commit 72a3e8f

Please sign in to comment.