mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-19 20:19:15 +00:00
improved many thing
This commit is contained in:
12
controllers/connection.js
Normal file
12
controllers/connection.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const connectionReady = (cb = () =>{}) => {
|
||||
console.log('Client is ready!');
|
||||
cb()
|
||||
}
|
||||
|
||||
const connectionLost = (cb = () =>{}) => {
|
||||
console.log('** Error de autentificacion vuelve a generar el QRCODE (Borrar el archivo session.json) **');
|
||||
cb()
|
||||
}
|
||||
|
||||
|
||||
module.exports = {connectionReady, connectionLost}
|
||||
26
controllers/flows.js
Normal file
26
controllers/flows.js
Normal file
@@ -0,0 +1,26 @@
|
||||
const getMessages = (step) => {
|
||||
switch (step) {
|
||||
case 'STEP_1':
|
||||
return ['hola', 'hi']
|
||||
break;
|
||||
case 'STEP_2':
|
||||
return ['hola', 'hi']
|
||||
break;
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
|
||||
const responseMessages = (step) => {
|
||||
switch (step) {
|
||||
case 'STEP_1':
|
||||
return ['Si como estas', '🤔'].join('')
|
||||
break;
|
||||
case 'STEP_2':
|
||||
return ['pa como estas', '🤔'].join('')
|
||||
break;
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
module.exports = { getMessages, responseMessages }
|
||||
7
controllers/handle.js
Normal file
7
controllers/handle.js
Normal file
@@ -0,0 +1,7 @@
|
||||
const cleanNumber = (number) => {
|
||||
number = number.replace('@c.us', '');
|
||||
number = `${number}@c.us`;
|
||||
return number
|
||||
}
|
||||
|
||||
module.exports = {cleanNumber}
|
||||
17
controllers/save.js
Normal file
17
controllers/save.js
Normal file
@@ -0,0 +1,17 @@
|
||||
const mimeDb = require('mime-db')
|
||||
|
||||
/**
|
||||
* Guardamos archivos multimedia que nuestro cliente nos envie!
|
||||
* @param {*} media
|
||||
*/
|
||||
|
||||
|
||||
const saveMedia = () => {
|
||||
const extensionProcess = mimeDb[media.mimetype]
|
||||
const ext = extensionProcess.extensions[0]
|
||||
fs.writeFile(`../media/${media.filename}.${ext}`, media.data, { encoding: 'base64' }, function (err) {
|
||||
console.log('** Archivo Media Guardado **');
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {saveMedia}
|
||||
102
controllers/send.js
Normal file
102
controllers/send.js
Normal file
@@ -0,0 +1,102 @@
|
||||
|
||||
const ExcelJS = require('exceljs');
|
||||
const moment = require('moment');
|
||||
const fs = require('fs');
|
||||
const { MessageMedia } = require('whatsapp-web.js');
|
||||
const { cleanNumber } = require('./handle')
|
||||
/**
|
||||
* Enviamos archivos multimedia a nuestro cliente
|
||||
* @param {*} number
|
||||
* @param {*} fileName
|
||||
*/
|
||||
|
||||
const sendMedia = (client, number, fileName) => {
|
||||
number = cleanNumber(number)
|
||||
const media = MessageMedia.fromFilePath(`${__dirname}/../mediaSend/${fileName}`);
|
||||
client.sendMessage(number, media);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enviamos un mensaje simple (texto) a nuestro cliente
|
||||
* @param {*} number
|
||||
*/
|
||||
const sendMessage = (client, number = null, text = null, trigger = null) => {
|
||||
number = cleanNumber(number)
|
||||
const message = text
|
||||
client.sendMessage(number, message);
|
||||
readChat(number, message, trigger)
|
||||
console.log(`⚡⚡⚡ Enviando mensajes....`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Opte
|
||||
*/
|
||||
const lastTrigger = (number) => new Promise((resolve, reject) => {
|
||||
number = cleanNumber(number)
|
||||
const pathExcel = `${__dirname}/../chats/${number}.xlsx`;
|
||||
const workbook = new ExcelJS.Workbook();
|
||||
if(fs.existsSync(pathExcel))
|
||||
{
|
||||
workbook.xlsx.readFile(pathExcel)
|
||||
.then(() => {
|
||||
const worksheet = workbook.getWorksheet(1);
|
||||
const lastRow = worksheet.lastRow;
|
||||
const getRowPrevStep = worksheet.getRow(lastRow.number);
|
||||
const lastStep = getRowPrevStep.getCell('C').value;
|
||||
resolve(lastStep)
|
||||
});
|
||||
}else{
|
||||
resolve(null)
|
||||
}
|
||||
})
|
||||
|
||||
/**
|
||||
* Guardar historial de conversacion
|
||||
* @param {*} number
|
||||
* @param {*} message
|
||||
*/
|
||||
const readChat = async (number, message, trigger = null) => {
|
||||
const pathExcel = `${__dirname}/../chats/${number}.xlsx`;
|
||||
const workbook = new ExcelJS.Workbook();
|
||||
const today = moment().format('DD-MM-YYYY hh:mm')
|
||||
|
||||
if (fs.existsSync(pathExcel)) {
|
||||
/**
|
||||
* Si existe el archivo de conversacion lo actualizamos
|
||||
*/
|
||||
const workbook = new ExcelJS.Workbook();
|
||||
workbook.xlsx.readFile(pathExcel)
|
||||
.then(() => {
|
||||
const worksheet = workbook.getWorksheet(1);
|
||||
const lastRow = worksheet.lastRow;
|
||||
var getRowInsert = worksheet.getRow(++(lastRow.number));
|
||||
getRowInsert.getCell('A').value = today;
|
||||
getRowInsert.getCell('B').value = message;
|
||||
getRowInsert.getCell('C').value = trigger;
|
||||
getRowInsert.commit();
|
||||
workbook.xlsx.writeFile(pathExcel);
|
||||
});
|
||||
|
||||
} else {
|
||||
/**
|
||||
* NO existe el archivo de conversacion lo creamos
|
||||
*/
|
||||
const worksheet = workbook.addWorksheet('Chats');
|
||||
worksheet.columns = [
|
||||
{ header: 'Fecha', key: 'number_customer' },
|
||||
{ header: 'Mensajes', key: 'message' },
|
||||
{ header: 'Disparador', key: 'trigger' }
|
||||
];
|
||||
worksheet.addRow([today, message, trigger]);
|
||||
workbook.xlsx.writeFile(pathExcel)
|
||||
.then(() => {
|
||||
|
||||
console.log("saved");
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log("err", err);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { sendMessage, sendMedia, lastTrigger }
|
||||
10
controllers/web.js
Normal file
10
controllers/web.js
Normal file
@@ -0,0 +1,10 @@
|
||||
const { sendMessage } = require('../controllers/send')
|
||||
|
||||
const sendMessagePost = (req, res) => {
|
||||
const { message, number } = req.body
|
||||
const client = req.clientWs || null;
|
||||
sendMessage(client, number, message)
|
||||
res.send({ status: 'Enviado!' })
|
||||
}
|
||||
|
||||
module.exports = { sendMessagePost }
|
||||
Reference in New Issue
Block a user