mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-20 20:49:15 +00:00
Compare commits
41 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9487c795b4 | ||
|
|
4624cb6c60 | ||
|
|
c7bc021f93 | ||
|
|
ebfd232a6c | ||
|
|
a20f0654eb | ||
|
|
f0a9c41dc5 | ||
|
|
033b3fd411 | ||
|
|
952a87f941 | ||
|
|
17bc227295 | ||
|
|
7cdfcdac64 | ||
|
|
82c5af9605 | ||
|
|
1dbd8a8a67 | ||
|
|
4c67e35d8d | ||
|
|
0c26d96752 | ||
|
|
7ed67e0e2e | ||
|
|
7d1d009fe8 | ||
|
|
ad8ef25d2c | ||
|
|
a71204b384 | ||
|
|
c9a96ca0ac | ||
|
|
e32f5c9210 | ||
|
|
ffaa7b04a2 | ||
|
|
9a2ce98dfd | ||
|
|
f338b77d09 | ||
|
|
1f9af4f7b2 | ||
|
|
67bea61055 | ||
|
|
a63b4cb569 | ||
|
|
9123c58611 | ||
|
|
d3a086fc98 | ||
|
|
effeb3d4e9 | ||
|
|
826c5d9bf6 | ||
|
|
a21f70af1f | ||
|
|
982df6184e | ||
|
|
8a988dab67 | ||
|
|
cef5c618a3 | ||
|
|
4339d56870 | ||
|
|
69720b382a | ||
|
|
691880e628 | ||
|
|
0a5c3055ce | ||
|
|
1533161bbd | ||
|
|
ccca7f5612 | ||
|
|
1d3410ac91 |
11
.env.example
Normal file
11
.env.example
Normal file
@@ -0,0 +1,11 @@
|
||||
######DATABASE: none, mysql, dialogflow
|
||||
|
||||
DEFAULT_MESSAGE=true
|
||||
SAVE_MEDIA=true
|
||||
PORT=3000
|
||||
DATABASE=none
|
||||
LANGUAGE=es
|
||||
SQL_HOST=
|
||||
SQL_USER=
|
||||
SQL_PASS=
|
||||
SQL_DATABASE=
|
||||
9
.gitignore
vendored
9
.gitignore
vendored
@@ -1,3 +1,10 @@
|
||||
/node_modules
|
||||
/node_modules/*
|
||||
session.json
|
||||
session.json
|
||||
chats/*
|
||||
!chats/.gitkeep
|
||||
media/*
|
||||
!media/.gitkeep
|
||||
mediaSend/*
|
||||
!mediaSend/.gitkeep
|
||||
.env
|
||||
1
.prettierrc.json
Normal file
1
.prettierrc.json
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
14
CHANGELOG.md
Normal file
14
CHANGELOG.md
Normal file
@@ -0,0 +1,14 @@
|
||||
#### Actualización 14 Ene 2022
|
||||
- npm update
|
||||
- remove ora and chalk
|
||||
- add env
|
||||
- add mysql
|
||||
- add dialogflow
|
||||
- add scan qr from webpage
|
||||
- update route with middleware
|
||||
- fix send message to story
|
||||
- external download
|
||||
- easy deploy heroku
|
||||
- add support for ubuntu/linux
|
||||
|
||||
https://stackoverflow.com/questions/51855169/dialogflow-403-iam-permission-dialogflow-sessions-detectintent
|
||||
97
README.md
97
README.md
@@ -1,40 +1,101 @@
|
||||
# BOT Whatsapp Gratis
|
||||
## Chatbot Whatsapp (OpenSource)
|
||||
#### Actualizado Enero 2022
|
||||
|
||||
Hola amigos este BOT se realizo en vivo en mi canal de Youtube si quieres ver como se hizo __Subscribete__
|
||||
[https://www.youtube.com/leifermendez](https://www.youtube.com/leifermendez)
|
||||
El siguiente proyecto se realizó con fines educativos para el canal de [Youtube (Leifer Mendez)](https://www.youtube.com/channel/UCgrIGp5QAnC0J8LfNJxDRDw?sub_confirmation=1) donde aprendemos a crear y implementar un chatbot increíble usando [node.js](https://codigoencasa.com/tag/nodejs/) además le agregamos inteligencia artificial gracias al servicio de __dialogflow__.
|
||||
|
||||
🤖 Link video https://www.youtube.com/watch?v=A_Xu0OR_HkE
|
||||
[](https://youtu.be/5lEMCeWEJ8o)
|
||||
|
||||
#### Node
|
||||
> Debes de tener instalado NODE si no sabes como instalarlo te dejo un video en el cual explico como instalar node
|
||||
__https://www.youtube.com/watch?v=6741ceWzsKQ&list=PL_WGMLcL4jzVY1y-SutA3N_PCNCAG7Y46&index=2&t=50s Minuto 0:50__
|
||||
#### Acceso rápido
|
||||
> Si tienes una cuenta en __heroku__ puedes desplegar este proyecto con (1 click)
|
||||
|
||||
[](https://heroku.com/deploy?template=https://github.com/leifermendez/bot-ventas)
|
||||
|
||||
> Comprarme un cafe!
|
||||
|
||||
[](https://www.buymeacoffee.com/leifermendez)
|
||||
|
||||
#### Actualización
|
||||
|
||||
| Feature | Status |
|
||||
| ------------- | ------------- |
|
||||
| Dialogflow | ✅ |
|
||||
| MySQL | ✅ |
|
||||
| JSON File | ✅ |
|
||||
| QR Scan (route) | ✅ |
|
||||
| Easy deploy heroku | ✅ |
|
||||
| Buttons | ✅ |
|
||||
| Add support ubuntu/linux | ✅ |
|
||||
|
||||
### (Nuevo) Botones
|
||||
|
||||
[](https://youtu.be/5lEMCeWEJ8o)
|
||||
|
||||
## Requisitos
|
||||
- node v14 o superior
|
||||
- VSCode (Editor de codigo) [Descargar](https://code.visualstudio.com/download)
|
||||
- MySql (opcional) solo aplica si vas a usar el modo 'mysql' [sql-bot.sql migración](https://github.com/leifermendez/bot-whatsapp/blob/main/sql-bot.sql)
|
||||
- Dialogflow (opcional) solo aplica si vas a usar el modo 'dialogflow'
|
||||
|
||||
## Instruciones
|
||||
__Descargar o Clonar repositorio__
|
||||

|
||||

|
||||
|
||||
__Instalar paquetes (npm install)__
|
||||
__Usas ¿Ubuntu / Linux?__
|
||||
> Asegurate de instalar los siguientes paquetes
|
||||
```
|
||||
sudo apt-get install -y libgbm-dev
|
||||
sudo apt install -y gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
|
||||
```
|
||||
|
||||
__Instalar dependencias (npm install)__
|
||||
> Ubicate en le directorio que descargaste y via consola o terminal ejecuta el siguiente comando
|
||||
|
||||
`npm install`
|
||||
|
||||

|
||||

|
||||
|
||||
__Ejecutar el script app.js__
|
||||
> Ubicate en le directorio que descargaste y via consola o terminal ejecuta el siguiente comando `node app.js` .
|
||||
Escanea el el código QR desde tu aplicación de Whatsapp
|
||||
__Configurar .env__
|
||||
> Con el editor de texto crea un archivo `.env` el cual debes de guiarte del archivo `.env.example`
|
||||
[Ver video explicando](https://youtu.be/5lEMCeWEJ8o?t=381)
|
||||
```
|
||||
######DATABASE: none, mysql, dialogflow
|
||||
|
||||
`node app.js`
|
||||
DEFAULT_MESSAGE=true
|
||||
SAVE_MEDIA=true
|
||||
PORT=3000
|
||||
DATABASE=none
|
||||
LANGUAGE=es
|
||||
SQL_HOST=
|
||||
SQL_USER=
|
||||
SQL_PASS=
|
||||
SQL_DATABASE=
|
||||
```
|
||||
|
||||

|
||||

|
||||
|
||||
__Ejecutar el script__
|
||||
> Ubicate en le directorio que descargaste y via consola o terminal ejecuta el siguiente comando
|
||||
`npm run start`
|
||||
|
||||

|
||||
|
||||
__Whatsapp en tu celular__
|
||||
> Ahora abre la aplicación de Whatsapp en tu dispositivo y escanea el código QR
|
||||
<img src="https://i.imgur.com/RSbPtat.png" width="500" />
|
||||
Visitar la pagina
|
||||
`http://localhost:3000/qr`
|
||||
|
||||

|
||||
|
||||
__Listo 😎__
|
||||
> Cuando sale este mensaje tu BOT está __listo__ para trabajar!
|
||||

|
||||

|
||||
|
||||
## Como usarlo
|
||||
# ¿Quieres ver como se creó? 🤖
|
||||
- [Ver Video 1](https://www.youtube.com/watch?v=A_Xu0OR_HkE)
|
||||
- [¿Como instalarlo? (Actulización)](https://youtu.be/5lEMCeWEJ8o)
|
||||
|
||||
## ¿Como usarlo el chatbot de whatsapp?
|
||||
> Escribe un mensaje al whatsapp que vinculaste con tu BOT
|
||||
|
||||

|
||||
@@ -46,6 +107,6 @@ con el historial de conversación con el número de tu cliente
|
||||

|
||||
|
||||
## Preguntar al BOT
|
||||
> Puedes interactuar con el bot ejemplo escribele __Quieromeme__ y el bot debe responderte con la imagen
|
||||
> Puedes interactuar con el bot ejemplo escribele __hola__ y el bot debe responderte!
|
||||
|
||||

|
||||
|
||||
75
adapter/diaglogflow.js
Normal file
75
adapter/diaglogflow.js
Normal file
@@ -0,0 +1,75 @@
|
||||
const dialogflow = require('@google-cloud/dialogflow');
|
||||
const fs = require('fs')
|
||||
const nanoid = require('nanoid')
|
||||
/**
|
||||
* Debes de tener tu archivo con el nombre "chatbot-account.json" en la raíz del proyecto
|
||||
*/
|
||||
let PROJECID;
|
||||
let CONFIGURATION;
|
||||
let sessionClient;
|
||||
|
||||
const checkFileCredentials = () => {
|
||||
if(!fs.existsSync(`${__dirname}/../chatbot-account.json`)){
|
||||
return false
|
||||
}
|
||||
|
||||
const parseCredentials = JSON.parse(fs.readFileSync(`${__dirname}/../chatbot-account.json`));
|
||||
PROJECID = parseCredentials.project_id;
|
||||
CONFIGURATION = {
|
||||
credentials: {
|
||||
private_key: parseCredentials['private_key'],
|
||||
client_email: parseCredentials['client_email']
|
||||
}
|
||||
}
|
||||
sessionClient = new dialogflow.SessionsClient(CONFIGURATION);
|
||||
}
|
||||
|
||||
// Create a new session
|
||||
|
||||
|
||||
// Detect intent method
|
||||
const detectIntent = async (queryText) => {
|
||||
let media = null;
|
||||
const sessionId = nanoid.nanoid()
|
||||
const sessionPath = sessionClient.projectAgentSessionPath(PROJECID, sessionId);
|
||||
const languageCode = process.env.LANGUAGE
|
||||
const request = {
|
||||
session: sessionPath,
|
||||
queryInput: {
|
||||
text: {
|
||||
text: queryText,
|
||||
languageCode: languageCode,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const responses = await sessionClient.detectIntent(request);
|
||||
const [singleResponse] = responses;
|
||||
const { queryResult } = singleResponse
|
||||
const { intent } = queryResult || { intent: {} }
|
||||
const parseIntent = intent['displayName'] || null
|
||||
const parsePayload = queryResult['fulfillmentMessages'].find((a) => a.message === 'payload');
|
||||
// console.log(singleResponse)
|
||||
if (parsePayload && parsePayload.payload) {
|
||||
const { fields } = parsePayload.payload
|
||||
media = fields.media.stringValue || null
|
||||
}
|
||||
// const customPayload = parsePayload['payload']
|
||||
|
||||
const parseData = {
|
||||
replyMessage: queryResult.fulfillmentText,
|
||||
media,
|
||||
trigger: null
|
||||
}
|
||||
return parseData
|
||||
}
|
||||
|
||||
const getDataIa = (message = '', cb = () => { }) => {
|
||||
detectIntent(message).then((res) => {
|
||||
cb(res)
|
||||
})
|
||||
}
|
||||
|
||||
checkFileCredentials();
|
||||
|
||||
module.exports = { getDataIa }
|
||||
67
adapter/index.js
Normal file
67
adapter/index.js
Normal file
@@ -0,0 +1,67 @@
|
||||
const { getData, getReply } = require('./mysql')
|
||||
const { getDataIa } = require('./diaglogflow')
|
||||
const stepsInitial = require('../flow/initial.json')
|
||||
const stepsReponse = require('../flow/response.json')
|
||||
|
||||
const get = (message) => new Promise((resolve, reject) => {
|
||||
/**
|
||||
* Si no estas usando un gesto de base de datos
|
||||
*/
|
||||
|
||||
if (process.env.DATABASE === 'none') {
|
||||
const { key } = stepsInitial.find(k => k.keywords.includes(message)) || { key: null }
|
||||
const response = key || null
|
||||
resolve(response)
|
||||
}
|
||||
/**
|
||||
* Si usas MYSQL
|
||||
*/
|
||||
if (process.env.DATABASE === 'mysql') {
|
||||
getData(message, (dt) => {
|
||||
resolve(dt)
|
||||
});
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
|
||||
const reply = (step) => new Promise((resolve, reject) => {
|
||||
/**
|
||||
* Si no estas usando un gesto de base de datos
|
||||
*/
|
||||
if (process.env.DATABASE === 'none') {
|
||||
let resData = { replyMessage: '', media: null, trigger: null }
|
||||
const responseFind = stepsReponse[step] || {};
|
||||
resData = {
|
||||
...resData,
|
||||
...responseFind,
|
||||
replyMessage:responseFind.replyMessage.join('')}
|
||||
resolve(resData);
|
||||
return
|
||||
}
|
||||
/**
|
||||
* Si usas MYSQL
|
||||
*/
|
||||
if (process.env.DATABASE === 'mysql') {
|
||||
let resData = { replyMessage: '', media: null, trigger: null }
|
||||
getReply(step, (dt) => {
|
||||
resData = { ...resData, ...dt }
|
||||
resolve(resData)
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
const getIA = (message) => new Promise((resolve, reject) => {
|
||||
/**
|
||||
* Si usas dialogflow
|
||||
*/
|
||||
if (process.env.DATABASE === 'dialogflow') {
|
||||
let resData = { replyMessage: '', media: null, trigger: null }
|
||||
getDataIa(message,(dt) => {
|
||||
resData = { ...resData, ...dt }
|
||||
resolve(resData)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = { get, reply, getIA }
|
||||
28
adapter/mysql.js
Normal file
28
adapter/mysql.js
Normal file
@@ -0,0 +1,28 @@
|
||||
const {connection} = require('../config/mysql')
|
||||
const DATABASE_NAME = process.env.SQL_DATABASE || 'db_test'
|
||||
|
||||
getData = (message = '', callback) => connection.query(
|
||||
`SELECT * FROM ${DATABASE_NAME}.initial WHERE keywords LIKE '%${message}%' LIMIT 1`,
|
||||
(error, results
|
||||
) => {
|
||||
const [response] = results
|
||||
const key = response?.option_key || null
|
||||
callback(key)
|
||||
});
|
||||
|
||||
|
||||
getReply = (option_key = '', callback) => connection.query(
|
||||
`SELECT * FROM ${DATABASE_NAME}.response WHERE option_key = '${option_key}' LIMIT 1`,
|
||||
(error, results
|
||||
) => {
|
||||
const [response] = results;
|
||||
console.log(response)
|
||||
const value = {
|
||||
replyMessage:response?.replyMessage || '',
|
||||
trigger:response?.trigger || '',
|
||||
media:response?.media || ''
|
||||
}
|
||||
callback(value)
|
||||
});
|
||||
|
||||
module.exports = {getData, getReply}
|
||||
412
app.js
412
app.js
@@ -1,99 +1,130 @@
|
||||
/**
|
||||
* ⚡⚡⚡ DECLARAMOS LAS LIBRERIAS y CONSTANTES A USAR! ⚡⚡⚡
|
||||
*/
|
||||
require('dotenv').config()
|
||||
const fs = require('fs');
|
||||
const mimeDb = require('mime-db')
|
||||
const express = require('express');
|
||||
const moment = require('moment');
|
||||
const ora = require('ora');
|
||||
const chalk = require('chalk');
|
||||
const ExcelJS = require('exceljs');
|
||||
const cors = require('cors')
|
||||
const qrcode = require('qrcode-terminal');
|
||||
const { flowConversation } = require('./conversation')
|
||||
const { Client, MessageMedia } = require('whatsapp-web.js');
|
||||
const { Client } = require('whatsapp-web.js');
|
||||
const mysqlConnection = require('./config/mysql')
|
||||
const { middlewareClient } = require('./middleware/client')
|
||||
const { generateImage, cleanNumber } = require('./controllers/handle')
|
||||
const { connectionReady, connectionLost } = require('./controllers/connection')
|
||||
const { saveMedia } = require('./controllers/save')
|
||||
const { getMessages, responseMessages, bothResponse } = require('./controllers/flows')
|
||||
const { sendMedia, sendMessage, lastTrigger, sendMessageButton, readChat } = require('./controllers/send')
|
||||
const app = express();
|
||||
app.use(express.urlencoded({ extended: true }))
|
||||
app.use(cors())
|
||||
app.use(express.json())
|
||||
|
||||
const server = require('http').Server(app)
|
||||
const io = require('socket.io')(server, {
|
||||
cors: {
|
||||
origins: ['http://localhost:4200']
|
||||
}
|
||||
})
|
||||
|
||||
let socketEvents = {sendQR:() => {} ,sendStatus:() => {}};
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
const CHANNEL = 'main-channel';
|
||||
socket.join(CHANNEL);
|
||||
socketEvents = require('./controllers/socket')(socket)
|
||||
console.log('Se conecto')
|
||||
})
|
||||
|
||||
app.use('/', require('./routes/web'))
|
||||
|
||||
const port = process.env.PORT || 3000
|
||||
const SESSION_FILE_PATH = './session.json';
|
||||
let client;
|
||||
let sessionData;
|
||||
|
||||
/**
|
||||
* Guardamos archivos multimedia que nuestro cliente nos envie!
|
||||
* @param {*} media
|
||||
*/
|
||||
const saveMedia = (media) => {
|
||||
|
||||
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 **');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Enviamos archivos multimedia a nuestro cliente
|
||||
* @param {*} number
|
||||
* @param {*} fileName
|
||||
*/
|
||||
const sendMedia = (number, fileName) => {
|
||||
number = number.replace('@c.us', '');
|
||||
number = `${number}@c.us`
|
||||
const media = MessageMedia.fromFilePath(`./mediaSend/${fileName}`);
|
||||
client.sendMessage(number, media);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enviamos un mensaje simple (texto) a nuestro cliente
|
||||
* @param {*} number
|
||||
*/
|
||||
const sendMessage = (number = null, text = null) => {
|
||||
number = number.replace('@c.us', '');
|
||||
number = `${number}@c.us`
|
||||
const message = text || `Hola soy un BOT recuerda https://www.youtube.com/leifermendez`;
|
||||
client.sendMessage(number, message);
|
||||
readChat(number, message)
|
||||
console.log(`${chalk.red('⚡⚡⚡ Enviando mensajes....')}`);
|
||||
}
|
||||
var client;
|
||||
var sessionData;
|
||||
|
||||
/**
|
||||
* Escuchamos cuando entre un mensaje
|
||||
*/
|
||||
const listenMessage = () => {
|
||||
client.on('message', async msg => {
|
||||
const { from, to, body } = msg;
|
||||
//34691015468@c.us
|
||||
console.log(msg.hasMedia);
|
||||
if (msg.hasMedia) {
|
||||
const media = await msg.downloadMedia();
|
||||
saveMedia(media);
|
||||
// do something with the media data here
|
||||
}
|
||||
|
||||
await greetCustomer(from);
|
||||
|
||||
console.log(body);
|
||||
|
||||
await replyAsk(from, body);
|
||||
|
||||
// await readChat(from, body)
|
||||
// console.log(`${chalk.red('⚡⚡⚡ Enviando mensajes....')}`);
|
||||
// console.log('Guardar este número en tu Base de Datos:', from);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Response a pregunta
|
||||
*/
|
||||
|
||||
const replyAsk = (from, answer) => new Promise((resolve, reject) => {
|
||||
console.log(`---------->`, answer);
|
||||
if (answer === 'Quieromeme') {
|
||||
sendMedia(from, 'meme-1.png')
|
||||
resolve(true)
|
||||
const listenMessage = () => client.on('message', async msg => {
|
||||
const { from, body, hasMedia } = msg;
|
||||
// Este bug lo reporto Lucas Aldeco Brescia para evitar que se publiquen estados
|
||||
if (from === 'status@broadcast') {
|
||||
return
|
||||
}
|
||||
message = body.toLowerCase();
|
||||
console.log('BODY',message)
|
||||
const number = cleanNumber(from)
|
||||
await readChat(number, message)
|
||||
/**
|
||||
* Guardamos el archivo multimedia que envia
|
||||
*/
|
||||
if (process.env.SAVE_MEDIA && hasMedia) {
|
||||
const media = await msg.downloadMedia();
|
||||
saveMedia(media);
|
||||
}
|
||||
|
||||
})
|
||||
/**
|
||||
* Si estas usando dialogflow solo manejamos una funcion todo es IA
|
||||
*/
|
||||
|
||||
if (process.env.DATABASE === 'dialogflow') {
|
||||
const response = await bothResponse(message);
|
||||
await sendMessage(client, from, response.replyMessage);
|
||||
if (response.media) {
|
||||
sendMedia(client, from, response.media);
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
/**
|
||||
* Ver si viene de un paso anterior
|
||||
* Aqui podemos ir agregando más pasos
|
||||
* a tu gusto!
|
||||
*/
|
||||
|
||||
const lastStep = await lastTrigger(from) || null;
|
||||
console.log({ lastStep })
|
||||
if (lastStep) {
|
||||
const response = await responseMessages(lastStep)
|
||||
await sendMessage(client, from, response.replyMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Respondemos al primero paso si encuentra palabras clave
|
||||
*/
|
||||
const step = await getMessages(message);
|
||||
console.log({ step })
|
||||
|
||||
if (step) {
|
||||
const response = await responseMessages(step)
|
||||
await sendMessage(client, from, response.replyMessage, response.trigger);
|
||||
|
||||
|
||||
if (!response.delay && response.media) {
|
||||
sendMedia(client, from, response.media);
|
||||
}
|
||||
if (response.delay && response.media) {
|
||||
setTimeout(() => {
|
||||
sendMedia(client, from, response.media);
|
||||
}, response.delay)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//Si quieres tener un mensaje por defecto
|
||||
if (process.env.DEFAULT_MESSAGE === 'true') {
|
||||
const response = await responseMessages('DEFAULT')
|
||||
await sendMessage(client, from, response.replyMessage, response.trigger);
|
||||
|
||||
/**
|
||||
* Si quieres enviar botones
|
||||
*/
|
||||
if(response.hasOwnProperty('actions')){
|
||||
const { actions } = response;
|
||||
await sendMessageButton(client, from, null, actions);
|
||||
}
|
||||
return
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Revisamos si tenemos credenciales guardadas para inciar sessio
|
||||
@@ -101,31 +132,25 @@ const replyAsk = (from, answer) => new Promise((resolve, reject) => {
|
||||
*/
|
||||
const withSession = () => {
|
||||
// Si exsite cargamos el archivo con las credenciales
|
||||
const spinner = ora(`Cargando ${chalk.yellow('Validando session con Whatsapp...')}`);
|
||||
console.log(`Validando session con Whatsapp...`)
|
||||
sessionData = require(SESSION_FILE_PATH);
|
||||
spinner.start();
|
||||
client = new Client({
|
||||
session: sessionData
|
||||
session: sessionData,
|
||||
puppeteer: {
|
||||
args: [
|
||||
'--no-sandbox'
|
||||
],
|
||||
}
|
||||
});
|
||||
|
||||
client.on('ready', () => {
|
||||
console.log('Client is ready!');
|
||||
spinner.stop();
|
||||
|
||||
// sendMessage();
|
||||
// sendMedia();
|
||||
|
||||
connectionReady();
|
||||
|
||||
connectionReady()
|
||||
listenMessage()
|
||||
loadRoutes(client);
|
||||
socketEvents.sendStatus()
|
||||
});
|
||||
|
||||
|
||||
|
||||
client.on('auth_failure', () => {
|
||||
spinner.stop();
|
||||
console.log('** Error de autentificacion vuelve a generar el QRCODE (Borrar el archivo session.json) **');
|
||||
})
|
||||
|
||||
client.on('auth_failure', () => connectionLost())
|
||||
|
||||
client.initialize();
|
||||
}
|
||||
@@ -135,27 +160,35 @@ const withSession = () => {
|
||||
*/
|
||||
const withOutSession = () => {
|
||||
console.log('No tenemos session guardada');
|
||||
client = new Client();
|
||||
client.on('qr', qr => {
|
||||
|
||||
client = new Client({
|
||||
puppeteer: {
|
||||
args: [
|
||||
'--no-sandbox'
|
||||
],
|
||||
}
|
||||
});
|
||||
|
||||
client.on('qr', qr => generateImage(qr, () => {
|
||||
qrcode.generate(qr, { small: true });
|
||||
console.log(`Ver QR http://localhost:${port}/qr`)
|
||||
socketEvents.sendQR(qr)
|
||||
}))
|
||||
|
||||
client.on('ready', (a) => {
|
||||
connectionReady()
|
||||
listenMessage()
|
||||
loadRoutes(client);
|
||||
// socketEvents.sendStatus(client)
|
||||
});
|
||||
|
||||
client.on('ready', () => {
|
||||
console.log('Client is ready!');
|
||||
connectionReady();
|
||||
});
|
||||
|
||||
client.on('auth_failure', () => {
|
||||
console.log('** Error de autentificacion vuelve a generar el QRCODE **');
|
||||
})
|
||||
|
||||
client.on('auth_failure', () => connectionLost());
|
||||
|
||||
client.on('authenticated', (session) => {
|
||||
// Guardamos credenciales de de session para usar luego
|
||||
sessionData = session;
|
||||
fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
console.log(`Ocurrio un error con el archivo: `, err);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -163,145 +196,28 @@ const withOutSession = () => {
|
||||
client.initialize();
|
||||
}
|
||||
|
||||
const connectionReady = () => {
|
||||
listenMessage();
|
||||
readExcel();
|
||||
/**
|
||||
* Cargamos rutas de express
|
||||
*/
|
||||
|
||||
const loadRoutes = (client) => {
|
||||
app.use('/api/', middlewareClient(client), require('./routes/api'))
|
||||
}
|
||||
|
||||
/**
|
||||
* Difundir mensaje a clientes
|
||||
*/
|
||||
const readExcel = async () => {
|
||||
const pathExcel = `./chats/clientes-saludar.xlsx`;
|
||||
const workbook = new ExcelJS.Workbook();
|
||||
await workbook.xlsx.readFile(pathExcel);
|
||||
const worksheet = workbook.getWorksheet(1);
|
||||
const columnNumbers = worksheet.getColumn('A');
|
||||
columnNumbers.eachCell((cell, rowNumber) => {
|
||||
const numberCustomer = cell.value
|
||||
|
||||
const columnDate = worksheet.getRow(rowNumber);
|
||||
let prevDate = columnDate.getCell(2).value;
|
||||
prevDate = moment.unix(prevDate);
|
||||
const diffMinutes = moment().diff(prevDate, 'minutes');
|
||||
|
||||
// Si ha pasado mas de 60 minuitos podemos enviar nuevamente
|
||||
if (diffMinutes > 60) {
|
||||
sendMessage(numberCustomer)
|
||||
columnDate.getCell(2).value = moment().format('X')
|
||||
columnDate.commit();
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
workbook.xlsx.writeFile(pathExcel);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Guardar historial de conversacion
|
||||
* @param {*} number
|
||||
* @param {*} message
|
||||
*/
|
||||
const readChat = async (number, message) => {
|
||||
const pathExcel = `./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.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' }
|
||||
];
|
||||
worksheet.addRow([today, message]);
|
||||
workbook.xlsx.writeFile(pathExcel)
|
||||
.then(() => {
|
||||
|
||||
console.log("saved");
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log("err", err);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saludos a primera respuesta
|
||||
* @param {*} req
|
||||
* @param {*} res
|
||||
*/
|
||||
|
||||
const greetCustomer = (from) => new Promise((resolve, reject) => {
|
||||
from = from.replace('@c.us', '');
|
||||
|
||||
const pathExcel = `./chats/${from}@c.us.xlsx`;
|
||||
if (!fs.existsSync(pathExcel)) {
|
||||
const firstMessage = [
|
||||
'👋 Ey! que pasa bro',
|
||||
'Recuerda subscribirte a mi canal de YT',
|
||||
'https://www.youtube.com/leifermendez',
|
||||
'de regalo te dejo algunos de mis cursos',
|
||||
'🔴 Aprende ANGULAR desde cero 2021 ⮕ https://bit.ly/367tJ32',
|
||||
'✅ Aprende NODE desde cero 2021 ⮕ https://bit.ly/3od1Bl6',
|
||||
'🔵 (Socket.io) NODE (Tutorial) ⮕ https://bit.ly/3pg1Q02',
|
||||
'------',
|
||||
'------',
|
||||
'Veo que es la primera vez que nos escribes ¿Quieres que te envie un MEME?',
|
||||
'Responde Quieromeme'
|
||||
].join(' ')
|
||||
|
||||
sendMessage(from, firstMessage)
|
||||
sendMedia(from, 'curso-1-1.png')
|
||||
sendMedia(from, 'curso-2.png')
|
||||
sendMedia(from, 'curso-3.png')
|
||||
}
|
||||
resolve(true)
|
||||
})
|
||||
|
||||
/**
|
||||
* Controladores
|
||||
*/
|
||||
|
||||
const sendMessagePost = (req, res) => {
|
||||
const { message, number } = req.body
|
||||
console.log(message, number);
|
||||
sendMessage(number, message)
|
||||
res.send({ status: 'Enviado!' })
|
||||
}
|
||||
|
||||
/**
|
||||
* Rutas
|
||||
*/
|
||||
|
||||
app.post('/send', sendMessagePost);
|
||||
|
||||
/**
|
||||
* Revisamos si existe archivo con credenciales!
|
||||
*/
|
||||
(fs.existsSync(SESSION_FILE_PATH)) ? withSession() : withOutSession();
|
||||
|
||||
/**
|
||||
* Verificamos si tienes un gesto de db
|
||||
*/
|
||||
|
||||
if (process.env.DATABASE === 'mysql') {
|
||||
mysqlConnection.connect()
|
||||
}
|
||||
|
||||
server.listen(port, () => {
|
||||
console.log(`El server esta listo por el puerto ${port}`);
|
||||
})
|
||||
|
||||
|
||||
app.listen(9000, () => {
|
||||
console.log('Server ready!');
|
||||
})
|
||||
35
app.json
Normal file
35
app.json
Normal file
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"name": "Chatbot Whatsapp (Leifer Mendez)",
|
||||
"description": "El siguiente proyecto se realizó con fines educativos para el canal de Youtube (Leifer Mendez) donde aprendemos como usando node.js podemos crear un chatbot increíble que además le agregamos inteligencia artificial gracias al servicio de dialogflow",
|
||||
"repository": "https://github.com/leifermendez/bot-whatsapp",
|
||||
"logo": "https://avatars0.githubusercontent.com/u/15802366?s=460&u=77ec7ef359e8ed842aef769693f1675c0ed460fd&v=4",
|
||||
"keywords": [
|
||||
"nodejs",
|
||||
"whatsapp",
|
||||
"bot",
|
||||
"chatbot",
|
||||
"dialogflow"
|
||||
],
|
||||
"addons": [
|
||||
],
|
||||
"buildpacks": [
|
||||
{
|
||||
"url": "heroku/nodejs"
|
||||
},
|
||||
{
|
||||
"url": "https://github.com/jontewks/puppeteer-heroku-buildpack"
|
||||
}
|
||||
],
|
||||
"env": {
|
||||
"SAVE_MEDIA": "false",
|
||||
"DATABASE": {
|
||||
"description": "'none', 'mysql', 'dialogflow' por defecto 'none' Puedes usar alguna de los siguientes opciones. Pero antes debes de saber como funciona y eso lo explico en el video. Puedes obtener más información https://github.com/leifermendez/bot-whatsapp/blob/main/README.md",
|
||||
"value": "none"
|
||||
},
|
||||
"LANGUAGE": "es",
|
||||
"SQL_HOST":"your_host",
|
||||
"SQL_USER":"your_user",
|
||||
"SQL_PASS":"your_password",
|
||||
"SQL_DATABASE":"your_database"
|
||||
}
|
||||
}
|
||||
13
chatbot-account.json
Normal file
13
chatbot-account.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"type": "",
|
||||
"project_id": "",
|
||||
"private_key_id": "",
|
||||
"private_key":"",
|
||||
"client_email": "",
|
||||
"client_id": "",
|
||||
"auth_uri": "",
|
||||
"token_uri": "",
|
||||
"auth_provider_x509_cert_url": "",
|
||||
"client_x509_cert_url":""
|
||||
}
|
||||
|
||||
0
chats/.gitkeep
Normal file
0
chats/.gitkeep
Normal file
Binary file not shown.
18
config/mysql.js
Normal file
18
config/mysql.js
Normal file
@@ -0,0 +1,18 @@
|
||||
const mysql = require('mysql');
|
||||
const connection = mysql.createConnection({
|
||||
host : process.env.SQL_HOST || 'localhost',
|
||||
user : process.env.SQL_USER || 'me',
|
||||
password : process.env.SQL_PASS || 'secret',
|
||||
database : process.env.SQL_DATABASE || 'my_db'
|
||||
});
|
||||
|
||||
const connect = () => connection.connect(function(err) {
|
||||
if (err) {
|
||||
console.error('error connecting: ' + err.stack);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Conexion correcta con tu base de datos MySQL')
|
||||
});
|
||||
|
||||
module.exports = {connect, connection}
|
||||
13
controllers/connection.js
Normal file
13
controllers/connection.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const connectionReady = (cb = () =>{}) => {
|
||||
console.log('Listo para escuchas mensajes')
|
||||
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}
|
||||
28
controllers/flows.js
Normal file
28
controllers/flows.js
Normal file
@@ -0,0 +1,28 @@
|
||||
const {get, reply, getIA} = require('../adapter')
|
||||
const {saveExternalFile} = require('./handle')
|
||||
|
||||
const getMessages = async (message) => {
|
||||
const data = await get(message)
|
||||
return data
|
||||
}
|
||||
|
||||
const responseMessages = async (step) => {
|
||||
const data = await reply(step)
|
||||
if(data && data.media){
|
||||
const file = await saveExternalFile(data.media)
|
||||
return {...data,...{media:file}}
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
const bothResponse = async (message) => {
|
||||
const data = await getIA(message)
|
||||
if(data && data.media){
|
||||
const file = await saveExternalFile(data.media)
|
||||
return {...data,...{media:file}}
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
|
||||
module.exports = { getMessages, responseMessages, bothResponse }
|
||||
42
controllers/handle.js
Normal file
42
controllers/handle.js
Normal file
@@ -0,0 +1,42 @@
|
||||
const http = require('http'); // or 'https' for https:// URLs
|
||||
const https = require('https'); // or 'https' for https:// URLs
|
||||
const fs = require('fs');
|
||||
const qr = require('qr-image')
|
||||
|
||||
|
||||
const cleanNumber = (number) => {
|
||||
number = number.replace('@c.us', '');
|
||||
number = `${number}@c.us`;
|
||||
return number
|
||||
}
|
||||
|
||||
const saveExternalFile = (url) => new Promise((resolve, reject) => {
|
||||
const ext = url.split('.').pop()
|
||||
const checkProtocol = url.split('/').includes('https:');
|
||||
const handleHttp = checkProtocol ? https : http;
|
||||
const name = `${Date.now()}.${ext}`;
|
||||
const file = fs.createWriteStream(`${__dirname}/../mediaSend/${name}`);
|
||||
console.log(url)
|
||||
handleHttp.get(url, function(response) {
|
||||
response.pipe(file);
|
||||
file.on('finish', function() {
|
||||
file.close(); // close() is async, call cb after close completes.
|
||||
resolve(name)
|
||||
});
|
||||
file.on('error', function() {
|
||||
console.log('errro')
|
||||
file.close(); // close() is async, call cb after close completes.
|
||||
resolve(null)
|
||||
});
|
||||
});
|
||||
})
|
||||
|
||||
const generateImage = (base64, cb = () => {}) => {
|
||||
let qr_svg = qr.image(base64, { type: 'svg', margin: 4 });
|
||||
qr_svg.pipe(require('fs').createWriteStream('./mediaSend/qr-code.svg'));
|
||||
console.log(`⚡ Recuerda que el QR se actualiza cada minuto ⚡'`);
|
||||
console.log(`⚡ Actualiza F5 el navegador para mantener el mejor QR⚡`);
|
||||
cb()
|
||||
}
|
||||
|
||||
module.exports = {cleanNumber, saveExternalFile, generateImage}
|
||||
18
controllers/save.js
Normal file
18
controllers/save.js
Normal file
@@ -0,0 +1,18 @@
|
||||
const mimeDb = require('mime-db')
|
||||
const fs = require('fs')
|
||||
|
||||
/**
|
||||
* Guardamos archivos multimedia que nuestro cliente nos envie!
|
||||
* @param {*} media
|
||||
*/
|
||||
|
||||
|
||||
const saveMedia = (media) => {
|
||||
const extensionProcess = mimeDb[media.mimetype]
|
||||
const ext = extensionProcess.extensions[0]
|
||||
fs.writeFile(`./media/${Date.now()}.${ext}`, media.data, { encoding: 'base64' }, function (err) {
|
||||
console.log('** Archivo Media Guardado **');
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {saveMedia}
|
||||
121
controllers/send.js
Normal file
121
controllers/send.js
Normal file
@@ -0,0 +1,121 @@
|
||||
|
||||
const ExcelJS = require('exceljs');
|
||||
const moment = require('moment');
|
||||
const fs = require('fs');
|
||||
const { MessageMedia, Buttons } = require('whatsapp-web.js');
|
||||
const { cleanNumber } = require('./handle')
|
||||
const { saveMedia } = require('../controllers/save')
|
||||
/**
|
||||
* Enviamos archivos multimedia a nuestro cliente
|
||||
* @param {*} number
|
||||
* @param {*} fileName
|
||||
*/
|
||||
|
||||
const sendMedia = (client, number, fileName) => {
|
||||
const dirMedia = `${__dirname}/../mediaSend/${fileName}`;
|
||||
number = cleanNumber(number)
|
||||
if (fs.existsSync(dirMedia)) {
|
||||
const media = MessageMedia.fromFilePath(dirMedia);
|
||||
client.sendMessage(number, media);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enviamos un mensaje simple (texto) a nuestro cliente
|
||||
* @param {*} number
|
||||
*/
|
||||
const sendMessage = async (client, number = null, text = null, trigger = null) => {
|
||||
number = cleanNumber(number)
|
||||
const message = text
|
||||
client.sendMessage(number, message);
|
||||
await readChat(number, message, trigger)
|
||||
console.log(`⚡⚡⚡ Enviando mensajes....`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enviamos un mensaje con buttons a nuestro cliente
|
||||
* @param {*} number
|
||||
*/
|
||||
const sendMessageButton = async (client, number = null, text = null, actionButtons) => {
|
||||
number = cleanNumber(number)
|
||||
const { title = null, message = null, footer = null, buttons = [] } = actionButtons;
|
||||
let button = new Buttons(message,[...buttons], title, footer);
|
||||
client.sendMessage(number, button);
|
||||
|
||||
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) => {
|
||||
setTimeout(() => {
|
||||
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);
|
||||
});
|
||||
}
|
||||
}, 900)
|
||||
}
|
||||
|
||||
module.exports = { sendMessage, sendMedia, lastTrigger, sendMessageButton, readChat }
|
||||
16
controllers/socket.js
Normal file
16
controllers/socket.js
Normal file
@@ -0,0 +1,16 @@
|
||||
module.exports = (socket) => {
|
||||
return {
|
||||
sendQR:(qr) => {
|
||||
socket.emit('connection_qr',{
|
||||
qr
|
||||
})
|
||||
},
|
||||
sendStatus:() => {
|
||||
socket.emit('connection_status',{
|
||||
a:1
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
16
controllers/web.js
Normal file
16
controllers/web.js
Normal file
@@ -0,0 +1,16 @@
|
||||
const fs = require('fs')
|
||||
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!' })
|
||||
}
|
||||
|
||||
const getQr = (req, res) => {
|
||||
res.writeHead(200, { 'content-type': 'image/svg+xml' });
|
||||
fs.createReadStream(`${__dirname}/../mediaSend/qr-code.svg`).pipe(res);
|
||||
}
|
||||
|
||||
module.exports = { sendMessagePost, getQr }
|
||||
@@ -1,3 +0,0 @@
|
||||
const flowConversation = () => { }
|
||||
|
||||
module.exports = { flowConversation }
|
||||
88
flow/initial.json
Normal file
88
flow/initial.json
Normal file
@@ -0,0 +1,88 @@
|
||||
[
|
||||
{
|
||||
"keywords": [
|
||||
"hola",
|
||||
"hola!",
|
||||
"ola",
|
||||
"ole",
|
||||
"inicio",
|
||||
"welcome",
|
||||
"buenos días",
|
||||
"buenas tardes",
|
||||
"buenas noches",
|
||||
"me dieron este número",
|
||||
"venden a crédito",
|
||||
"quisiera saber si venden",
|
||||
"necesito saber"
|
||||
],
|
||||
"key": "STEP_1"
|
||||
},
|
||||
{
|
||||
"keywords": [
|
||||
"cursos",
|
||||
"info",
|
||||
"curso"
|
||||
],
|
||||
"key": "STEP_2"
|
||||
},
|
||||
{
|
||||
"keywords": [
|
||||
"angular"
|
||||
],
|
||||
"key": "STEP_2_1"
|
||||
},
|
||||
{
|
||||
"keywords": [
|
||||
"node"
|
||||
],
|
||||
"key": "STEP_2_2"
|
||||
},
|
||||
{
|
||||
"keywords": [
|
||||
"ngrx"
|
||||
],
|
||||
"key": "STEP_2_3"
|
||||
},
|
||||
{
|
||||
"keywords": [
|
||||
"aws"
|
||||
],
|
||||
"key": "STEP_2_4"
|
||||
},
|
||||
{
|
||||
"keywords": [
|
||||
"asesor",
|
||||
"asesores",
|
||||
"Vendedor",
|
||||
"cobrador"
|
||||
],
|
||||
"key": "STEP_3"
|
||||
},
|
||||
{
|
||||
"keywords": [
|
||||
"muchas gracias",
|
||||
"ok",
|
||||
"gracias",
|
||||
"vale gracias"
|
||||
],
|
||||
"key": "STEP_4"
|
||||
},
|
||||
{
|
||||
"keywords": [
|
||||
"👉 Youtube"
|
||||
],
|
||||
"key": "STEP_5"
|
||||
},
|
||||
{
|
||||
"keywords": [
|
||||
"😎 Cursos"
|
||||
],
|
||||
"key": "STEP_6"
|
||||
},
|
||||
{
|
||||
"keywords": [
|
||||
"😁 Telegram"
|
||||
],
|
||||
"key": "STEP_7"
|
||||
}
|
||||
]
|
||||
135
flow/response.json
Normal file
135
flow/response.json
Normal file
@@ -0,0 +1,135 @@
|
||||
{
|
||||
"DEFAULT":{
|
||||
"replyMessage":[
|
||||
"🆗 Bienvenido a este 🤖 CHATBOT de Whatsapp, lo primero \n",
|
||||
"decirte que mi nombre es *Leifer Mendez*😎 y te dejo opciones rapidas \n"
|
||||
],
|
||||
"media":null,
|
||||
"trigger":null,
|
||||
"actions":{
|
||||
"title":"¿Que te interesa ver?",
|
||||
"message":"Recuerda todo este contenido es gratis y estaria genial que me siguas!",
|
||||
"footer":"Gracias",
|
||||
"buttons":[
|
||||
{"body":"😎 Cursos"},
|
||||
{"body":"👉 Youtube"},
|
||||
{"body":"😁 Telegram"}
|
||||
]
|
||||
}
|
||||
},
|
||||
"STEP_0":{
|
||||
"replyMessage":[
|
||||
"El flujo ha finalizado \n",
|
||||
"pero puedes ver todo el codigo de este \n",
|
||||
"repositorio en https://github.com/leifermendez/bot-whatsapp.git"
|
||||
],
|
||||
"media":null,
|
||||
"trigger":null
|
||||
},
|
||||
"STEP_1":{
|
||||
"replyMessage":[
|
||||
"✌️ Bienvenido a este 🤖 CHATBOT de Whatsapp, lo primero \n",
|
||||
"decirte que mi nombre es *Leifer Mendez*😎 \n",
|
||||
"\n Si necesitas ver más info sobre las capacitacion tecnicas ",
|
||||
"escribe *cursos* o *info*"
|
||||
],
|
||||
"media":null,
|
||||
"trigger":null
|
||||
},
|
||||
"STEP_2":{
|
||||
"replyMessage":[
|
||||
"Perfecto, te voy a pasar la lista ",
|
||||
"de los temas que tengo y un breve video 🙂🤖 \n\n",
|
||||
"*Angular* Basico (Pago) \n",
|
||||
"*Angular* Basico (Gratis) \n",
|
||||
"*Node* Basico (Gratis) \n",
|
||||
"*NGRX* Basico (Gratis) \n",
|
||||
"*AWS* Basico (Pago) \n\n",
|
||||
"Escribe la palabra del tema que te interese \n"
|
||||
],
|
||||
"media":"https://i.giphy.com/media/5J5gN0WUk0VToHaK2p/giphy-downsized.gif",
|
||||
"trigger":null
|
||||
},
|
||||
"STEP_2_1":{
|
||||
"replyMessage":[
|
||||
"Si te interesa Angular tienes disponible \n",
|
||||
"*(Gratis)* https://bit.ly/367tJ32 \n\n",
|
||||
"*(Pago)* https://link.codigoencasa.com/PROMO-INICIAL \n\n",
|
||||
"*(Pago)* https://link.codigoencasa.com/ANGULAR-BASICO-EDTEAM \n\n",
|
||||
"😎😎😎"
|
||||
],
|
||||
"media":"https://i.imgur.com/Q0a5UQI.jpg",
|
||||
"trigger":null
|
||||
},
|
||||
"STEP_2_2":{
|
||||
"replyMessage":[
|
||||
"Si te interesa NODE tienes disponible \n",
|
||||
"*(Gratis)* https://bit.ly/3od1Bl6 \n\n",
|
||||
"Espero pronto tener más material disponible",
|
||||
"🤖"
|
||||
],
|
||||
"media":null,
|
||||
"trigger":null
|
||||
},
|
||||
"STEP_2_3":{
|
||||
"replyMessage":[
|
||||
"NGRX para manejar estados en Angular \n",
|
||||
"*(Gratis)* https://bit.ly/ngrx-desde-cero \n",
|
||||
"A darle! 😮"
|
||||
],
|
||||
"media":null,
|
||||
"trigger":null
|
||||
},
|
||||
"STEP_2_4":{
|
||||
"replyMessage":[
|
||||
"Muy bien AWS esta pronto a salir pre-registrate aquí \n",
|
||||
"*(Pre-registro)* https://link.codigoencasa.com/AWS-BASICO-INVITACION \n",
|
||||
"😮😮"
|
||||
],
|
||||
"media":null,
|
||||
"trigger":null
|
||||
},
|
||||
"STEP_3":{
|
||||
"replyMessage":[
|
||||
"¿Ok cual curso de intereso? \n",
|
||||
"*angular* , *node*, *ngrx*, *aws*"
|
||||
],
|
||||
"media":null,
|
||||
"trigger":null
|
||||
},
|
||||
"STEP_4":{
|
||||
"replyMessage":[
|
||||
"Gracias a ti! \n"
|
||||
],
|
||||
"media":"https://media4.giphy.com/media/hur0SFIU5SH4mxNBWa/giphy.gif",
|
||||
"trigger":null
|
||||
},
|
||||
"STEP_5":{
|
||||
"replyMessage":[
|
||||
"Muy bien te comparto el canal de Youtube \n"
|
||||
],
|
||||
"media":"https://youtube.com/leifermendez",
|
||||
"trigger":null
|
||||
},
|
||||
"STEP_6":{
|
||||
"replyMessage":[
|
||||
"Perfecto, te voy a pasar la lista ",
|
||||
"de los temas que tengo y un breve video 🙂🤖 \n\n",
|
||||
"*Angular* Basico (Pago) \n",
|
||||
"*Angular* Basico (Gratis) \n",
|
||||
"*Node* Basico (Gratis) \n",
|
||||
"*NGRX* Basico (Gratis) \n",
|
||||
"*AWS* Basico (Pago) \n\n",
|
||||
"Escribe la palabra del tema que te interese \n"
|
||||
],
|
||||
"media":"https://i.giphy.com/media/5J5gN0WUk0VToHaK2p/giphy-downsized.gif",
|
||||
"trigger":null
|
||||
},
|
||||
"STEP_7":{
|
||||
"replyMessage":[
|
||||
"Vente al telegram \n"
|
||||
],
|
||||
"media":"https://t.me/leifermendez",
|
||||
"trigger":null
|
||||
}
|
||||
}
|
||||
0
media/.gitkeep
Normal file
0
media/.gitkeep
Normal file
BIN
media/undefined
BIN
media/undefined
Binary file not shown.
|
Before Width: | Height: | Size: 24 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 24 KiB |
0
mediaSend/.gitkeep
Normal file
0
mediaSend/.gitkeep
Normal file
Binary file not shown.
|
Before Width: | Height: | Size: 322 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 307 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 278 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 447 KiB |
21
middleware/client.js
Normal file
21
middleware/client.js
Normal file
@@ -0,0 +1,21 @@
|
||||
const middlewareClient = (client = null) => async (req, res, next) => {
|
||||
try {
|
||||
|
||||
if(!client){
|
||||
res.status(409)
|
||||
console.log(client)
|
||||
res.send({ error: 'Error de client.' })
|
||||
}else{
|
||||
req.clientWs = client;
|
||||
next()
|
||||
}
|
||||
|
||||
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
res.status(409)
|
||||
res.send({ error: 'Error de client' })
|
||||
}
|
||||
|
||||
}
|
||||
module.exports = { middlewareClient }
|
||||
2750
package-lock.json
generated
2750
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
30
package.json
30
package.json
@@ -2,24 +2,36 @@
|
||||
"name": "test-ws-bot",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"main": "app.js",
|
||||
"scripts": {
|
||||
"start": "node ./app.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"chalk": "^4.1.0",
|
||||
"excel4node": "^1.7.2",
|
||||
"exceljs": "^4.2.1",
|
||||
"express": "^4.17.1",
|
||||
"file-type": "^16.3.0",
|
||||
"mime-db": "^1.46.0",
|
||||
"@google-cloud/dialogflow": "^4.6.0",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^11.0.0",
|
||||
"exceljs": "^4.3.0",
|
||||
"express": "^4.17.2",
|
||||
"file-type": "^16.5.3",
|
||||
"mime-db": "^1.51.0",
|
||||
"moment": "^2.29.1",
|
||||
"ora": "^5.4.0",
|
||||
"mysql": "^2.18.1",
|
||||
"nanoid": "^3.1.32",
|
||||
"qr-image": "^3.2.0",
|
||||
"qrcode-terminal": "^0.12.0",
|
||||
"whatsapp-web.js": "^1.12.5",
|
||||
"socket.io": "^4.4.1",
|
||||
"whatsapp-web.js": "^1.15.4",
|
||||
"xlsx": "^0.16.9"
|
||||
},
|
||||
"devDependencies": {
|
||||
"pm2": "^5.1.2",
|
||||
"prettier": "2.5.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": "14.x"
|
||||
}
|
||||
}
|
||||
|
||||
7
routes/api.js
Normal file
7
routes/api.js
Normal file
@@ -0,0 +1,7 @@
|
||||
const express = require('express')
|
||||
const router = express.Router();
|
||||
const { sendMessagePost } = require('../controllers/web')
|
||||
|
||||
router.post('/send', sendMessagePost)
|
||||
|
||||
module.exports = router
|
||||
7
routes/web.js
Normal file
7
routes/web.js
Normal file
@@ -0,0 +1,7 @@
|
||||
const express = require('express');
|
||||
const router = express.Router()
|
||||
const { getQr } = require('../controllers/web')
|
||||
|
||||
router.use('/qr', getQr)
|
||||
|
||||
module.exports = router
|
||||
79
sql-bot.sql
Normal file
79
sql-bot.sql
Normal file
@@ -0,0 +1,79 @@
|
||||
-- MySQL dump 10.13 Distrib 8.0.27, for Win64 (x86_64)
|
||||
--
|
||||
-- Host: 127.0.0.1 Database: db_test
|
||||
-- ------------------------------------------------------
|
||||
-- Server version 5.7.33
|
||||
|
||||
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
|
||||
/*!50503 SET NAMES utf8 */;
|
||||
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
|
||||
/*!40103 SET TIME_ZONE='+00:00' */;
|
||||
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
|
||||
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
|
||||
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
|
||||
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
|
||||
|
||||
--
|
||||
-- Table structure for table `initial`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `initial`;
|
||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||
/*!50503 SET character_set_client = utf8mb4 */;
|
||||
CREATE TABLE `initial` (
|
||||
`option_key` varchar(500) DEFAULT NULL,
|
||||
`keywords` varchar(45) DEFAULT NULL,
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
-- Dumping data for table `initial`
|
||||
--
|
||||
|
||||
LOCK TABLES `initial` WRITE;
|
||||
/*!40000 ALTER TABLE `initial` DISABLE KEYS */;
|
||||
INSERT INTO `initial` VALUES ('STEP_1','hola, hola!,ola,inicio,welcome',2),('STEP_2','cursos,info,curso',3),('STEP_2_1','angular',4),('STEP_2_2','node',5),('STEP_2_3','ngrx',6),('STEP_2_4','aws',7),('STEP_3','asesor',8),('STEP_4','muchas gracias,gracias,vale gracias',9);
|
||||
/*!40000 ALTER TABLE `initial` ENABLE KEYS */;
|
||||
UNLOCK TABLES;
|
||||
|
||||
--
|
||||
-- Table structure for table `response`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `response`;
|
||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||
/*!50503 SET character_set_client = utf8mb4 */;
|
||||
CREATE TABLE `response` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`option_key` varchar(45) DEFAULT NULL,
|
||||
`replyMessage` varchar(45) DEFAULT NULL,
|
||||
`trigger` varchar(45) DEFAULT NULL,
|
||||
`media` varchar(200) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
-- Dumping data for table `response`
|
||||
--
|
||||
|
||||
LOCK TABLES `response` WRITE;
|
||||
/*!40000 ALTER TABLE `response` DISABLE KEYS */;
|
||||
INSERT INTO `response` VALUES (1,'STEP_1','Hola soy el bot escribe curso',NULL,NULL),(2,'STEP_2','Te envio esto',NULL,'https://s2.q4cdn.com/175719177/files/doc_presentations/Placeholder-PDF.pdf');
|
||||
/*!40000 ALTER TABLE `response` ENABLE KEYS */;
|
||||
UNLOCK TABLES;
|
||||
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
|
||||
|
||||
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
|
||||
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
|
||||
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
|
||||
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
|
||||
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
|
||||
|
||||
-- Dump completed on 2022-01-18 20:52:34
|
||||
Reference in New Issue
Block a user