Skip to content

Commit

Permalink
Implementação da tag {{ticket}} e correções
Browse files Browse the repository at this point in the history
  • Loading branch information
rtenorioh authored Jul 1, 2022
1 parent 38ce3a6 commit 9c92276
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 45 deletions.
6 changes: 3 additions & 3 deletions backend/src/controllers/TicketController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const update = async (
if (ticketData.transf) {
const { greetingMessage } = await ShowQueueService(ticketData.queueId);
if (greetingMessage) {
const msgtxt = "*Mensagem Automática:* \n" + greetingMessage;
const msgtxt = formatBody(`\u200e${greetingMessage}`);
await SendWhatsAppMessage({ body: msgtxt, ticket });
}
}
Expand All @@ -111,7 +111,7 @@ export const update = async (

if (farewellMessage) {
await SendWhatsAppMessage({
body: formatBody(farewellMessage, ticket.contact),
body: formatBody(`\u200e${farewellMessage}`, ticket),
ticket
});
}
Expand All @@ -138,4 +138,4 @@ export const remove = async (
});

return res.status(200).json({ message: "ticket deleted" });
};
};
45 changes: 30 additions & 15 deletions backend/src/helpers/Mustache.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,50 @@
import Mustache from "mustache";
import Contact from "../models/Contact";
import Ticket from "../models/Ticket";

export default (body: string, contact: Contact): string => {
export const msgsd = (): string => {

let ms = "";

const hh = new Date().getHours();

if (hh >= 6){ms = "Bom dia";}
if (hh > 11){ms = "Boa tarde";}
if (hh > 17){ms = "Boa noite";}
if (hh > 23 || hh < 6){ms = "Boa madrugada";}

return ms;
};

export const control = (): string => {
const Hr = new Date();

const dd: string = ("0" + Hr.getDate()).slice(-2);
const mm: string = ("0" + (Hr.getMonth() + 1)).slice(-2);
const yy: string = Hr.getFullYear().toString();

const ctrl = yy + mm + dd + "T";
return ctrl;
};

export const hour = (): string => {
const Hr = new Date();

const hh: number = Hr.getHours();
const min: string = ("0" + Hr.getMinutes()).slice(-2);
const ss: string = ("0" + Hr.getSeconds()).slice(-2);

if (hh >= 6){ms = "Bom dia";}
if (hh > 11){ms = "Boa tarde";}
if (hh > 17){ms = "Boa noite";}
if (hh > 23 || hh < 6){ms = "Boa madrugada";}

let protocol = yy+mm+dd+String(hh)+min+ss;

let hora = hh+":"+min+":"+ss;
const hours = hh + ":" + min + ":" + ss;
return hours;
};

export default (body: string, ticket?: Ticket): string => {
const view = {
name: contact ? contact.name : "",
ms: ms,
protocol: protocol,
hora: hora,
name: ticket ? ticket.contact.name : "",
ticket_id: ticket ? ticket.id : "",
ms: msgsd(),
protocol: control(),
hour: hour(),
};

return Mustache.render(body, view);

};
23 changes: 12 additions & 11 deletions backend/src/libs/wbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,22 @@ const syncUnreadMessages = async (wbot: Session) => {
export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
return new Promise((resolve, reject) => {
try {
logger.level = "trace";
const io = getIO();
const sessionName = whatsapp.name;
let sessionCfg;

if (whatsapp && whatsapp.session) {
sessionCfg = JSON.parse(whatsapp.session);
}
const wbot: Session = new Client({

const wbot: Session = new Client({
session: sessionCfg,
authStrategy: new LocalAuth({clientId: 'bd_'+whatsapp.id}),
puppeteer: {
args: ['--no-sandbox', '--disable-setuid-sandbox'],
authStrategy: new LocalAuth({ clientId: 'bd_' + whatsapp.id }),
puppeteer: {
args: ['--no-sandbox', '--disable-setuid-sandbox'],
executablePath: process.env.CHROME_BIN || undefined
},
},
});

wbot.initialize();
Expand All @@ -73,9 +74,9 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {

wbot.on("authenticated", async session => {
logger.info(`Session: ${sessionName} AUTHENTICATED`);
// await whatsapp.update({
// session: JSON.stringify(session)
// });
// await whatsapp.update({
// session: JSON.stringify(session)
// });
});

wbot.on("auth_failure", async msg => {
Expand Down Expand Up @@ -126,7 +127,7 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {

resolve(wbot);
});
} catch (err) {
} catch (err: any) {
logger.error(err);
}
});
Expand All @@ -148,7 +149,7 @@ export const removeWbot = (whatsappId: number): void => {
sessions[sessionIndex].destroy();
sessions.splice(sessionIndex, 1);
}
} catch (err) {
} catch (err: any) {
logger.error(err);
}
};
2 changes: 1 addition & 1 deletion backend/src/services/WbotServices/SendWhatsAppMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const SendWhatsAppMedia = async ({
try {
const wbot = await GetTicketWbot(ticket);
const hasBody = body
? formatBody(body as string, ticket.contact)
? formatBody(body as string, ticket)
: undefined;

const newMedia = MessageMedia.fromFilePath(media.path);
Expand Down
4 changes: 2 additions & 2 deletions backend/src/services/WbotServices/SendWhatsAppMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const SendWhatsAppMessage = async ({
try {
const sentMessage = await wbot.sendMessage(
`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`,
formatBody(body, ticket.contact),
formatBody(body, ticket),
{
quotedMessageId: quotedMsgSerializedId,
linkPreview: false
Expand All @@ -44,4 +44,4 @@ const SendWhatsAppMessage = async ({
}
};

export default SendWhatsAppMessage;
export default SendWhatsAppMessage;
25 changes: 15 additions & 10 deletions backend/src/services/WbotServices/wbotMessageListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ import ShowWhatsAppService from "../WhatsappService/ShowWhatsAppService";
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 @@ -90,7 +88,7 @@ const verifyMediaMessage = async (
media.data,
"base64"
);
} catch (err) {
} catch (err: any) {
Sentry.captureException(err);
logger.error(err);
}
Expand Down Expand Up @@ -157,7 +155,7 @@ const verifyQueue = async (
const choosenQueue = queues[+selectedOption - 1];

if (choosenQueue) {

const Hr = new Date();

const hh: number = Hr.getHours() * 60 * 60;
Expand All @@ -176,7 +174,7 @@ const verifyQueue = async (

if ((hora < horainicio) || (hora > horatermino)) {

const body = formatBody(`\u200e${choosenQueue.absenceMessage}`, contact);
const body = formatBody(`\u200e${choosenQueue.absenceMessage}`, ticket);
const debouncedSentMessage = debounce(
async () => {
const sentMessage = await wbot.sendMessage(`${contact.number}@c.us`, body);
Expand All @@ -199,7 +197,7 @@ const verifyQueue = async (

const body = formatBody(
`\u200e${choosenQueue.greetingMessage}`,
contact
ticket
);

const sentMessage = await wbot.sendMessage(`${contact.number}@c.us`, body);
Expand All @@ -217,8 +215,8 @@ const verifyQueue = async (
});

const body = formatBody(
`${greetingMessage}\n\n${options}`,
contact
`\u200e${greetingMessage}\n\n${options}`,
ticket
);

const debouncedSentMessage = debounce(
Expand Down Expand Up @@ -329,14 +327,21 @@ const handleMessage = async (

const contact = await verifyContact(msgContact);

let ticket = await FindOrCreateTicketService(
contact,
wbot.id!,
unreadMessages,
groupContact
);

if (
unreadMessages === 0 &&
whatsapp.farewellMessage &&
whatsapp.farewellMessage === msg.body
formatBody(whatsapp.farewellMessage, ticket) === msg.body
)
return;

const ticket = await FindOrCreateTicketService(
ticket = await FindOrCreateTicketService(
contact,
wbot.id!,
unreadMessages,
Expand Down
5 changes: 3 additions & 2 deletions backend/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import pino from "pino";
const logger = pino({
prettyPrint: {
ignore: "pid,hostname"
}
},
level: "trace"
});

export { logger };
export { logger };
2 changes: 1 addition & 1 deletion frontend/src/components/TicketListItem/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ const TicketListItem = ({ ticket }) => {
if (isMounted.current) {
setLoading(false);
}
// history.push(`/tickets/${id}`);
history.push(`/tickets/${id}`);
};


Expand Down

0 comments on commit 9c92276

Please sign in to comment.