mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-17 19:37:02 +00:00
Merge branch 'master' into master
This commit is contained in:
@@ -27,6 +27,7 @@
|
|||||||
"http-graceful-shutdown": "^2.3.2",
|
"http-graceful-shutdown": "^2.3.2",
|
||||||
"jsonwebtoken": "^8.5.1",
|
"jsonwebtoken": "^8.5.1",
|
||||||
"multer": "^1.4.2",
|
"multer": "^1.4.2",
|
||||||
|
"mustache": "^4.2.0",
|
||||||
"mysql2": "^2.2.5",
|
"mysql2": "^2.2.5",
|
||||||
"pg": "^8.4.1",
|
"pg": "^8.4.1",
|
||||||
"pino": "^6.9.0",
|
"pino": "^6.9.0",
|
||||||
@@ -52,6 +53,7 @@
|
|||||||
"@types/jest": "^26.0.15",
|
"@types/jest": "^26.0.15",
|
||||||
"@types/jsonwebtoken": "^8.5.0",
|
"@types/jsonwebtoken": "^8.5.0",
|
||||||
"@types/multer": "^1.4.4",
|
"@types/multer": "^1.4.4",
|
||||||
|
"@types/mustache": "^4.1.2",
|
||||||
"@types/node": "^14.11.8",
|
"@types/node": "^14.11.8",
|
||||||
"@types/supertest": "^2.0.10",
|
"@types/supertest": "^2.0.10",
|
||||||
"@types/uuid": "^8.3.3",
|
"@types/uuid": "^8.3.3",
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import ShowTicketService from "../services/TicketServices/ShowTicketService";
|
|||||||
import UpdateTicketService from "../services/TicketServices/UpdateTicketService";
|
import UpdateTicketService from "../services/TicketServices/UpdateTicketService";
|
||||||
import SendWhatsAppMessage from "../services/WbotServices/SendWhatsAppMessage";
|
import SendWhatsAppMessage from "../services/WbotServices/SendWhatsAppMessage";
|
||||||
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
|
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
|
||||||
|
import formatBody from "../helpers/Mustache";
|
||||||
|
|
||||||
type IndexQuery = {
|
type IndexQuery = {
|
||||||
searchParam: string;
|
searchParam: string;
|
||||||
@@ -99,14 +100,13 @@ export const update = async (
|
|||||||
const { farewellMessage } = whatsapp;
|
const { farewellMessage } = whatsapp;
|
||||||
|
|
||||||
if (farewellMessage) {
|
if (farewellMessage) {
|
||||||
await SendWhatsAppMessage({
|
await SendWhatsAppMessage({
|
||||||
body: farewellMessage,
|
body: formatBody(farewellMessage, ticket.contact),
|
||||||
ticket
|
ticket
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return res.status(200).json(ticket);
|
return res.status(200).json(ticket);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
9
backend/src/helpers/Mustache.ts
Normal file
9
backend/src/helpers/Mustache.ts
Normal 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);
|
||||||
|
};
|
||||||
@@ -4,6 +4,8 @@ import AppError from "../../errors/AppError";
|
|||||||
import GetTicketWbot from "../../helpers/GetTicketWbot";
|
import GetTicketWbot from "../../helpers/GetTicketWbot";
|
||||||
import Ticket from "../../models/Ticket";
|
import Ticket from "../../models/Ticket";
|
||||||
|
|
||||||
|
import formatBody from "../../helpers/Mustache";
|
||||||
|
|
||||||
interface Request {
|
interface Request {
|
||||||
media: Express.Multer.File;
|
media: Express.Multer.File;
|
||||||
ticket: Ticket;
|
ticket: Ticket;
|
||||||
@@ -23,7 +25,7 @@ const SendWhatsAppMedia = async ({
|
|||||||
`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`,
|
`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`,
|
||||||
newMedia,
|
newMedia,
|
||||||
{
|
{
|
||||||
caption: body,
|
caption: formatBody(body as string, ticket.contact),
|
||||||
sendAudioAsVoice: true
|
sendAudioAsVoice: true
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import SerializeWbotMsgId from "../../helpers/SerializeWbotMsgId";
|
|||||||
import Message from "../../models/Message";
|
import Message from "../../models/Message";
|
||||||
import Ticket from "../../models/Ticket";
|
import Ticket from "../../models/Ticket";
|
||||||
|
|
||||||
|
import formatBody from "../../helpers/Mustache";
|
||||||
|
|
||||||
interface Request {
|
interface Request {
|
||||||
body: string;
|
body: string;
|
||||||
ticket: Ticket;
|
ticket: Ticket;
|
||||||
@@ -28,7 +30,7 @@ const SendWhatsAppMessage = async ({
|
|||||||
try {
|
try {
|
||||||
const sentMessage = await wbot.sendMessage(
|
const sentMessage = await wbot.sendMessage(
|
||||||
`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`,
|
`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`,
|
||||||
body,
|
formatBody(body, ticket.contact),
|
||||||
{
|
{
|
||||||
quotedMessageId: quotedMsgSerializedId,
|
quotedMessageId: quotedMsgSerializedId,
|
||||||
linkPreview: false
|
linkPreview: false
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { promisify } from "util";
|
|||||||
import { writeFile } from "fs";
|
import { writeFile } from "fs";
|
||||||
import * as Sentry from "@sentry/node";
|
import * as Sentry from "@sentry/node";
|
||||||
|
|
||||||
import WAWebJS, {
|
import {
|
||||||
Contact as WbotContact,
|
Contact as WbotContact,
|
||||||
Message as WbotMessage,
|
Message as WbotMessage,
|
||||||
MessageAck,
|
MessageAck,
|
||||||
@@ -24,6 +24,7 @@ import { debounce } from "../../helpers/Debounce";
|
|||||||
import UpdateTicketService from "../TicketServices/UpdateTicketService";
|
import UpdateTicketService from "../TicketServices/UpdateTicketService";
|
||||||
import CreateContactService from "../ContactServices/CreateContactService";
|
import CreateContactService from "../ContactServices/CreateContactService";
|
||||||
import GetContactService from "../ContactServices/GetContactService";
|
import GetContactService from "../ContactServices/GetContactService";
|
||||||
|
import formatBody from "../../helpers/Mustache";
|
||||||
|
|
||||||
interface Session extends Client {
|
interface Session extends Client {
|
||||||
id?: number;
|
id?: number;
|
||||||
@@ -172,7 +173,7 @@ const verifyQueue = async (
|
|||||||
ticketId: ticket.id
|
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);
|
const sentMessage = await wbot.sendMessage(`${contact.number}@c.us`, body);
|
||||||
|
|
||||||
@@ -184,7 +185,7 @@ const verifyQueue = async (
|
|||||||
options += `*${index + 1}* - ${queue.name}\n`;
|
options += `*${index + 1}* - ${queue.name}\n`;
|
||||||
});
|
});
|
||||||
|
|
||||||
const body = `\u200e${greetingMessage}\n${options}`;
|
const body = formatBody(`\u200e${greetingMessage}\n${options}`, contact);
|
||||||
|
|
||||||
const debouncedSentMessage = debounce(
|
const debouncedSentMessage = debounce(
|
||||||
async () => {
|
async () => {
|
||||||
@@ -251,7 +252,6 @@ const handleMessage = async (
|
|||||||
|
|
||||||
const chat = await msg.getChat();
|
const chat = await msg.getChat();
|
||||||
|
|
||||||
|
|
||||||
if (chat.isGroup) {
|
if (chat.isGroup) {
|
||||||
let msgGroupContact;
|
let msgGroupContact;
|
||||||
|
|
||||||
@@ -269,7 +269,12 @@ const handleMessage = async (
|
|||||||
|
|
||||||
const contact = await verifyContact(msgContact);
|
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(
|
const ticket = await FindOrCreateTicketService(
|
||||||
contact,
|
contact,
|
||||||
@@ -294,9 +299,35 @@ const handleMessage = async (
|
|||||||
await verifyQueue(wbot, msg, ticket, contact);
|
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 {
|
try {
|
||||||
const array = msg.vCards.toString().split("\n");
|
const array = msg.vCards.toString().split("\n");
|
||||||
let name = "";
|
let name = "";
|
||||||
@@ -355,8 +386,7 @@ const handleMessage = async (
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
}
|
}
|
||||||
}*/
|
} */
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Sentry.captureException(err);
|
Sentry.captureException(err);
|
||||||
logger.error(`Error handling whatsapp message: Err: ${err}`);
|
logger.error(`Error handling whatsapp message: Err: ${err}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user