Bot
Bot
await chat.sendStateTyping();
await delay(DELAY_TIME);
await client.sendMessage(chat.id._serialized, message);
function getFirstName(contact) {
return contact.pushname ? contact.pushname.split(" ")[0] : "Cliente";
}
function getWelcomeMessage() {
return "*Seja Bem Vindo(a) ao Canal de Atendimento da Desktop*";
}
function getCPFRequestMessage() {
return "Por favor, Digite o seu CPF:";
}
function getFaturaOptionsMessage() {
return "Como deseja receber sua fatura?\n\n1 - Pix (Aprovação imediata)\n2 -
Código do Boleto\n3 - Boleto em PDF";
}
function isValidCPFFormat(cpf) {
const numericCPF = cpf.replace(/\D/g, '');
const formatRegex = /^\d{11}$|^\d{3}\.\d{3}\.\d{3}-\d{2}$/;
return formatRegex.test(cpf);
}
client.on('qr', qr => {
qrcode.generate(qr, { small: true });
});
client.on('ready', () => {
console.log('Tudo certo! WhatsApp conectado.');
});
client.initialize();
switch (currentState.state) {
case CHAT_STATES.AWAITING_CPF:
if (isValidCPFFormat(msg.body)) {
currentState = {
state: CHAT_STATES.AWAITING_OPTION,
cpf: msg.body
};
userStates.set(chatId, currentState);
await sendMessageWithTyping(chat, getFaturaOptionsMessage());
} else {
await sendMessageWithTyping(chat, "Por favor, digite seu CPF em
um dos seguintes formatos:\n00000000000 ou 000.000.000-00");
}
break;
case CHAT_STATES.AWAITING_OPTION:
if (msg.body === "1" || msg.body === "2" || msg.body === "3") {
currentState = { state: CHAT_STATES.CPF, cpf:
currentState.cpf };
userStates.set(chatId, currentState);
await sendMessageWithTyping(chat, "Ok! Aguarde enquanto puxo
sua fatura no banco de dados..");
} else {
await sendMessageWithTyping(chat, "Por favor, escolha uma opção
válida:\n\n1 - Pix (Aprovação imediata)\n2 - Código do Boleto\n3 - Boleto em PDF");
}
break;
}
} catch (error) {
console.error('Erro no processamento da mensagem:', error);
// Make sure to clear the message queue if there's an error
messageQueue.delete(chat.id._serialized);
}
});