mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-20 20:49:15 +00:00
Compare commits
100 Commits
1.0
...
0.0.1-alph
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ee87e9e875 | ||
|
|
1884832192 | ||
|
|
f21a58b6ff | ||
|
|
8a4f134327 | ||
|
|
9b6ce92612 | ||
|
|
663fcafc9c | ||
|
|
f36ddd3014 | ||
|
|
3fadaaaf13 | ||
|
|
dfa569f29d | ||
|
|
601508f379 | ||
|
|
e7ad205268 | ||
|
|
f62ba0a076 | ||
|
|
a9efa0aa58 | ||
|
|
3276c21bc3 | ||
|
|
1114f25a71 | ||
|
|
f13a34ff75 | ||
|
|
d45ea85e7d | ||
|
|
10e2b138d3 | ||
|
|
a1bf5ba5c2 | ||
|
|
19102b7b3a | ||
|
|
5efcc2a9a6 | ||
|
|
8279c07a88 | ||
|
|
02d7b3bd98 | ||
|
|
f8f6a3000d | ||
|
|
9a92b152a4 | ||
|
|
f86700deaf | ||
|
|
4ba259b46c | ||
|
|
cf459e94d2 | ||
|
|
4f8ed1361c | ||
|
|
bad8802241 | ||
|
|
f09ac862d5 | ||
|
|
fe7567e1a9 | ||
|
|
9b0b7f4d54 | ||
|
|
3ddbf462a8 | ||
|
|
e6043c99a7 | ||
|
|
b1daa0020e | ||
|
|
190d35c9a5 | ||
|
|
e4378fe848 | ||
|
|
981a6bd928 | ||
|
|
676e48021f | ||
|
|
1d4daf10db | ||
|
|
3c9341d87d | ||
|
|
04982941a7 | ||
|
|
ba4f05ebb2 | ||
|
|
5aaf761fce | ||
|
|
12539d00fa | ||
|
|
ec8ad955ee | ||
|
|
d10504c40b | ||
|
|
d200100caa | ||
|
|
902431c533 | ||
|
|
e23540593a | ||
|
|
9b548d9418 | ||
|
|
c25de59a93 | ||
|
|
cfe2c17165 | ||
|
|
1309b7f806 | ||
|
|
1071469e53 | ||
|
|
1795e8de20 | ||
|
|
7414d958ab | ||
|
|
9487c795b4 | ||
|
|
a3ebebb19c | ||
|
|
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 |
13
.env.example
Normal file
13
.env.example
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
######DATABASE: none, mysql, dialogflow
|
||||||
|
|
||||||
|
DEFAULT_MESSAGE=true
|
||||||
|
SAVE_MEDIA=true
|
||||||
|
PORT=3000
|
||||||
|
DATABASE=none
|
||||||
|
LANGUAGE=es
|
||||||
|
SQL_HOST=
|
||||||
|
SQL_USER=
|
||||||
|
SQL_PASS=
|
||||||
|
SQL_DATABASE=
|
||||||
|
KEEP_DIALOG_FLOW=false
|
||||||
|
MULTI_DEVICE=true
|
||||||
9
.gitignore
vendored
9
.gitignore
vendored
@@ -1,3 +1,12 @@
|
|||||||
/node_modules
|
/node_modules
|
||||||
/node_modules/*
|
/node_modules/*
|
||||||
session.json
|
session.json
|
||||||
|
chats/*
|
||||||
|
!chats/.gitkeep
|
||||||
|
media/*
|
||||||
|
!media/.gitkeep
|
||||||
|
mediaSend/*
|
||||||
|
!mediaSend/.gitkeep
|
||||||
|
!mediaSend/nota-de-voz.mp3
|
||||||
|
.env
|
||||||
|
.wwebjs_auth
|
||||||
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
|
||||||
119
README.md
119
README.md
@@ -1,51 +1,116 @@
|
|||||||
# BOT Whatsapp Gratis
|
## Chatbot Whatsapp (OpenSource)
|
||||||
|
|
||||||
Hola amigos este BOT se realizo en vivo en mi canal de Youtube si quieres ver como se hizo __Subscribete__
|
#### Actualización
|
||||||
[https://www.youtube.com/leifermendez](https://www.youtube.com/leifermendez)
|
|
||||||
|
|
||||||
🤖 Link video https://www.youtube.com/watch?v=A_Xu0OR_HkE
|
| Feature | Status |
|
||||||
|
| ------------- | ------------- |
|
||||||
|
| Dialogflow | ✅ |
|
||||||
|
| MySQL | ✅ |
|
||||||
|
| JSON File | ✅ |
|
||||||
|
| QR Scan (route) | ✅ |
|
||||||
|
| Easy deploy heroku | ✅ |
|
||||||
|
| Buttons | ✅ℹ️ (No funciona en multi-device)|
|
||||||
|
| Send Voice Note | ✅ |
|
||||||
|
| Add support ubuntu/linux | ✅ |
|
||||||
|
|
||||||
#### Node
|
## Requisitos
|
||||||
> Debes de tener instalado NODE si no sabes como instalarlo te dejo un video en el cual explico como instalar node
|
- node v14 o superior
|
||||||
__https://www.youtube.com/watch?v=6741ceWzsKQ&list=PL_WGMLcL4jzVY1y-SutA3N_PCNCAG7Y46&index=2&t=50s Minuto 0:50__
|
- 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'
|
||||||
|
|
||||||
|
### (Nuevo) Botones
|
||||||
|
|
||||||
|
[](https://youtu.be/5lEMCeWEJ8o)
|
||||||
|
|
||||||
|
> Implementar los botones solo necesitas hacer uso del metodo __sendMessageButton__ que se encuentra dentro `./controllers/send` dejo un ejemplo de como usarlo.
|
||||||
|
[Ver implementación](https://github.com/leifermendez/bot-whatsapp/blob/main/app.js#L123)
|
||||||
|
|
||||||
|
``` javascript
|
||||||
|
const { sendMessageButton } = require('./controllers/send')
|
||||||
|
|
||||||
|
await sendMessageButton(
|
||||||
|
{
|
||||||
|
"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"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## Notas de Voz
|
||||||
|
[](https://i.imgur.com/zq6xYDp.png)
|
||||||
|
|
||||||
|
> Se pueden enviar notas de voz con formato nativo para que no se vea como reenviado. En este ejemplo enviare el archivo __PTT-20220223-WA0000.opus__ que se encuentra dentro de la carpeta de __/mediaSend__
|
||||||
|
|
||||||
|
``` javascript
|
||||||
|
const { sendMediaVoiceNote } = require('./controllers/send')
|
||||||
|
|
||||||
|
await sendMediaVoiceNote(client, from, 'PTT-20220223-WA0000.opus')
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
## Instruciones
|
## Instruciones
|
||||||
__Descargar o Clonar repositorio__
|
__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
|
> Ubicate en le directorio que descargaste y via consola o terminal ejecuta el siguiente comando
|
||||||
|
|
||||||
`npm install`
|
```
|
||||||
|
npm i
|
||||||
|
```
|
||||||
|
|
||||||

|
__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
|
||||||
|
|
||||||
__Ejecutar el script app.js__
|
DEFAULT_MESSAGE=true
|
||||||
> Ubicate en le directorio que descargaste y via consola o terminal ejecuta el siguiente comando `node app.js` .
|
SAVE_MEDIA=true
|
||||||
Escanea el el código QR desde tu aplicación de Whatsapp
|
PORT=3000
|
||||||
|
DATABASE=none
|
||||||
|
LANGUAGE=es
|
||||||
|
SQL_HOST=
|
||||||
|
SQL_USER=
|
||||||
|
SQL_PASS=
|
||||||
|
SQL_DATABASE=
|
||||||
|
```
|
||||||
|
|
||||||
`node app.js`
|
__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
|
> 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" />
|
<img src="https://i.imgur.com/RSbPtat.png" width="500" />
|
||||||
|
Tambien puedes visitar la pagina http://127.0.0.1:3000/qr
|
||||||
|
|
||||||
|
__Listo 😎__
|
||||||
> Cuando sale este mensaje tu BOT está __listo__ para trabajar!
|
> 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
|
> Escribe un mensaje al whatsapp que vinculaste con tu BOT
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
> Ahora deberías obtener un arespuesta por parte del BOT como la siguiente, ademas de esto tambien se crea un archivo excel
|
> Ahora deberías obtener un arespuesta por parte del BOT como la siguiente, ademas de esto tambien se crea un archivo excel
|
||||||
con el historial de conversación con el número de tu cliente
|
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
|
|
||||||
|
|
||||||

|
|
||||||
|
|||||||
77
adapter/diaglogflow.js
Normal file
77
adapter/diaglogflow.js
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
const dialogflow = require('@google-cloud/dialogflow');
|
||||||
|
const fs = require('fs')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Debes de tener tu archivo con el nombre "chatbot-account.json" en la raíz del proyecto
|
||||||
|
*/
|
||||||
|
|
||||||
|
const KEEP_DIALOG_FLOW = (process.env.KEEP_DIALOG_FLOW === 'true')
|
||||||
|
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, waPhoneNumber) => {
|
||||||
|
let media = null;
|
||||||
|
const sessionId = KEEP_DIALOG_FLOW ? 1 : waPhoneNumber;
|
||||||
|
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 ? parsePayload['payload'] : null
|
||||||
|
|
||||||
|
const parseData = {
|
||||||
|
replyMessage: queryResult.fulfillmentText,
|
||||||
|
media,
|
||||||
|
trigger: null
|
||||||
|
}
|
||||||
|
return parseData
|
||||||
|
}
|
||||||
|
|
||||||
|
const getDataIa = (message = '', sessionId = '', cb = () => { }) => {
|
||||||
|
detectIntent(message, sessionId).then((res) => {
|
||||||
|
cb(res)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
checkFileCredentials();
|
||||||
|
|
||||||
|
module.exports = { getDataIa }
|
||||||
90
adapter/index.js
Normal file
90
adapter/index.js
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
const { getData, getReply, saveMessageMysql } = require('./mysql')
|
||||||
|
const { saveMessageJson } = require('./jsonDb')
|
||||||
|
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, sessionId) => new Promise((resolve, reject) => {
|
||||||
|
/**
|
||||||
|
* Si usas dialogflow
|
||||||
|
*/
|
||||||
|
if (process.env.DATABASE === 'dialogflow') {
|
||||||
|
let resData = { replyMessage: '', media: null, trigger: null }
|
||||||
|
getDataIa(message, sessionId, (dt) => {
|
||||||
|
resData = { ...resData, ...dt }
|
||||||
|
resolve(resData)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {*} message
|
||||||
|
* @param {*} date
|
||||||
|
* @param {*} trigger
|
||||||
|
* @param {*} number
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
const saveMessage = ( message, trigger, number ) => new Promise( async (resolve, reject) => {
|
||||||
|
switch ( process.env.DATABASE ) {
|
||||||
|
case 'mysql':
|
||||||
|
resolve( await saveMessageMysql( message, trigger, number ) )
|
||||||
|
break;
|
||||||
|
case 'none':
|
||||||
|
resolve( await saveMessageJson( message, trigger, number ) )
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
resolve(true)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
module.exports = { get, reply, getIA, saveMessage }
|
||||||
20
adapter/jsonDb.js
Normal file
20
adapter/jsonDb.js
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
const Path = require('path')
|
||||||
|
const StormDB = require("stormdb");
|
||||||
|
const date = new Date().toISOString();
|
||||||
|
const saveMessageJson = (message, trigger, number) => new Promise( async(resolve,reject) =>{
|
||||||
|
try {
|
||||||
|
const engine = new StormDB.localFileEngine( Path.join(__dirname, `/../chats/${number}.json`) );
|
||||||
|
const db = new StormDB(engine);
|
||||||
|
// set default db value if db is empty
|
||||||
|
db.default({ messages: [] });
|
||||||
|
// add new users entry
|
||||||
|
db.get("messages").push({ message, date, trigger });
|
||||||
|
db.save();
|
||||||
|
resolve('Saved')
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
reject(error)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
module.exports = { saveMessageJson }
|
||||||
74
adapter/mysql.js
Normal file
74
adapter/mysql.js
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
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)
|
||||||
|
});
|
||||||
|
|
||||||
|
getMessages = ( number ) => new Promise((resolve,reejct) => {
|
||||||
|
try {
|
||||||
|
connection.query(
|
||||||
|
`SELECT * FROM ${DATABASE_NAME}.response WHERE number = '${number}'`, (error, results) => {
|
||||||
|
if(error) {
|
||||||
|
console.log(error)
|
||||||
|
}
|
||||||
|
const [response] = results;
|
||||||
|
console.log(response)
|
||||||
|
const value = {
|
||||||
|
replyMessage:response?.replyMessage || '',
|
||||||
|
trigger:response?.trigger || '',
|
||||||
|
media:response?.media || ''
|
||||||
|
}
|
||||||
|
resolve(value)
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
saveMessageMysql = ( message, date, trigger, number ) => new Promise((resolve,reejct) => {
|
||||||
|
try {
|
||||||
|
connection.query(
|
||||||
|
`INSERT INTO ${DATABASE_NAME}.messages `+"( `message`, `date`, `trigger`, `number`)"+` VALUES ('${message}','${date}','${trigger}', '${number}')` , (error, results) => {
|
||||||
|
if(error) {
|
||||||
|
//TODO esta parte es mejor incluirla directamente en el archivo .sql template
|
||||||
|
console.log('DEBES DE CREAR LA TABLA DE MESSAGE')
|
||||||
|
// if( error.code === 'ER_NO_SUCH_TABLE' ){
|
||||||
|
// connection.query( `CREATE TABLE ${DATABASE_NAME}.messages `+"( `date` DATE NOT NULL , `message` VARCHAR(450) NOT NULL , `trigger` VARCHAR(450) NOT NULL , `number` VARCHAR(50) NOT NULL ) ENGINE = InnoDB", async (error, results) => {
|
||||||
|
// setTimeout( async () => {
|
||||||
|
// return resolve( await this.saveMessageMysql( message, date, trigger, number ) )
|
||||||
|
// }, 150)
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
console.log('Saved')
|
||||||
|
console.log( results )
|
||||||
|
resolve(results)
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
module.exports = {getData, getReply, saveMessageMysql}
|
||||||
426
app.js
426
app.js
@@ -1,307 +1,171 @@
|
|||||||
/**
|
/**
|
||||||
* ⚡⚡⚡ DECLARAMOS LAS LIBRERIAS y CONSTANTES A USAR! ⚡⚡⚡
|
* ⚡⚡⚡ DECLARAMOS LAS LIBRERIAS y CONSTANTES A USAR! ⚡⚡⚡
|
||||||
*/
|
*/
|
||||||
|
require('dotenv').config()
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const mimeDb = require('mime-db')
|
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const moment = require('moment');
|
const cors = require('cors')
|
||||||
const ora = require('ora');
|
|
||||||
const chalk = require('chalk');
|
|
||||||
const ExcelJS = require('exceljs');
|
|
||||||
const qrcode = require('qrcode-terminal');
|
const qrcode = require('qrcode-terminal');
|
||||||
const { flowConversation } = require('./conversation')
|
const { Client, LocalAuth } = require('whatsapp-web.js');
|
||||||
const { Client, MessageMedia } = require('whatsapp-web.js');
|
const mysqlConnection = require('./config/mysql')
|
||||||
|
const { middlewareClient } = require('./middleware/client')
|
||||||
|
const { generateImage, cleanNumber, checkEnvFile, createClient, isValidNumber } = 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();
|
const app = express();
|
||||||
app.use(express.urlencoded({ extended: true }))
|
app.use(cors())
|
||||||
const SESSION_FILE_PATH = './session.json';
|
app.use(express.json())
|
||||||
let client;
|
const MULTI_DEVICE = process.env.MULTI_DEVICE || 'true';
|
||||||
let sessionData;
|
const server = require('http').Server(app)
|
||||||
|
|
||||||
/**
|
const port = process.env.PORT || 3000
|
||||||
* Guardamos archivos multimedia que nuestro cliente nos envie!
|
var client;
|
||||||
* @param {*} media
|
app.use('/', require('./routes/web'))
|
||||||
*/
|
|
||||||
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....')}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Escuchamos cuando entre un mensaje
|
* Escuchamos cuando entre un mensaje
|
||||||
*/
|
*/
|
||||||
const listenMessage = () => {
|
const listenMessage = () => client.on('message', async msg => {
|
||||||
client.on('message', async msg => {
|
const { from, body, hasMedia } = msg;
|
||||||
const { from, to, body } = msg;
|
|
||||||
//34691015468@c.us
|
if (!isValidNumber(from)) {
|
||||||
console.log(msg.hasMedia);
|
return
|
||||||
if (msg.hasMedia) {
|
}
|
||||||
const media = await msg.downloadMedia();
|
|
||||||
saveMedia(media);
|
// Este bug lo reporto Lucas Aldeco Brescia para evitar que se publiquen estados
|
||||||
// do something with the media data here
|
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') {
|
||||||
|
if (!message.length) return;
|
||||||
|
const response = await bothResponse(message, number);
|
||||||
|
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;
|
||||||
|
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);
|
||||||
|
|
||||||
|
if (step) {
|
||||||
|
const response = await responseMessages(step);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Si quieres enviar botones
|
||||||
|
*/
|
||||||
|
|
||||||
|
await sendMessage(client, from, response.replyMessage, response.trigger);
|
||||||
|
|
||||||
|
if (response.hasOwnProperty('actions')) {
|
||||||
|
const { actions } = response;
|
||||||
|
await sendMessageButton(client, from, null, actions);
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
await greetCustomer(from);
|
if (!response.delay && response.media) {
|
||||||
|
sendMedia(client, from, response.media);
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Revisamos si tenemos credenciales guardadas para inciar sessio
|
|
||||||
* este paso evita volver a escanear el QRCODE
|
|
||||||
*/
|
|
||||||
const withSession = () => {
|
|
||||||
// Si exsite cargamos el archivo con las credenciales
|
|
||||||
const spinner = ora(`Cargando ${chalk.yellow('Validando session con Whatsapp...')}`);
|
|
||||||
sessionData = require(SESSION_FILE_PATH);
|
|
||||||
spinner.start();
|
|
||||||
client = new Client({
|
|
||||||
session: sessionData
|
|
||||||
});
|
|
||||||
|
|
||||||
client.on('ready', () => {
|
|
||||||
console.log('Client is ready!');
|
|
||||||
spinner.stop();
|
|
||||||
|
|
||||||
// sendMessage();
|
|
||||||
// sendMedia();
|
|
||||||
|
|
||||||
connectionReady();
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
client.on('auth_failure', () => {
|
|
||||||
spinner.stop();
|
|
||||||
console.log('** Error de autentificacion vuelve a generar el QRCODE (Borrar el archivo session.json) **');
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
client.initialize();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generamos un QRCODE para iniciar sesion
|
|
||||||
*/
|
|
||||||
const withOutSession = () => {
|
|
||||||
console.log('No tenemos session guardada');
|
|
||||||
client = new Client();
|
|
||||||
client.on('qr', qr => {
|
|
||||||
qrcode.generate(qr, { small: true });
|
|
||||||
});
|
|
||||||
|
|
||||||
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('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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
client.initialize();
|
|
||||||
}
|
|
||||||
|
|
||||||
const connectionReady = () => {
|
|
||||||
listenMessage();
|
|
||||||
readExcel();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
if (response.delay && response.media) {
|
||||||
|
setTimeout(() => {
|
||||||
workbook.xlsx.writeFile(pathExcel);
|
sendMedia(client, from, response.media);
|
||||||
|
}, response.delay)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
//Si quieres tener un mensaje por defecto
|
||||||
* Saludos a primera respuesta
|
if (process.env.DEFAULT_MESSAGE === 'true') {
|
||||||
* @param {*} req
|
const response = await responseMessages('DEFAULT')
|
||||||
* @param {*} res
|
await sendMessage(client, from, response.replyMessage, response.trigger);
|
||||||
*/
|
|
||||||
|
|
||||||
const greetCustomer = (from) => new Promise((resolve, reject) => {
|
/**
|
||||||
from = from.replace('@c.us', '');
|
* Si quieres enviar botones
|
||||||
|
*/
|
||||||
const pathExcel = `./chats/${from}@c.us.xlsx`;
|
if (response.hasOwnProperty('actions')) {
|
||||||
if (!fs.existsSync(pathExcel)) {
|
const { actions } = response;
|
||||||
const firstMessage = [
|
await sendMessageButton(client, from, null, actions);
|
||||||
'👋 Ey! que pasa bro',
|
}
|
||||||
'Recuerda subscribirte a mi canal de YT',
|
return
|
||||||
'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)
|
});
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
|
client = new Client({
|
||||||
|
authStrategy: new LocalAuth(),
|
||||||
|
puppeteer: { headless: true }
|
||||||
|
});
|
||||||
|
|
||||||
|
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()
|
||||||
|
// socketEvents.sendStatus(client)
|
||||||
|
});
|
||||||
|
|
||||||
|
client.on('auth_failure', (e) => {
|
||||||
|
// console.log(e)
|
||||||
|
// connectionLost()
|
||||||
|
});
|
||||||
|
|
||||||
|
client.on('authenticated', () => {
|
||||||
|
console.log('AUTHENTICATED');
|
||||||
|
});
|
||||||
|
|
||||||
|
client.initialize();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Controladores
|
* Verificamos si tienes un gesto de db
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const sendMessagePost = (req, res) => {
|
if (process.env.DATABASE === 'mysql') {
|
||||||
const { message, number } = req.body
|
mysqlConnection.connect()
|
||||||
console.log(message, number);
|
|
||||||
sendMessage(number, message)
|
|
||||||
res.send({ status: 'Enviado!' })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
server.listen(port, () => {
|
||||||
* Rutas
|
console.log(`El server esta listo por el puerto ${port}`);
|
||||||
*/
|
|
||||||
|
|
||||||
app.post('/send', sendMessagePost);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Revisamos si existe archivo con credenciales!
|
|
||||||
*/
|
|
||||||
(fs.existsSync(SESSION_FILE_PATH)) ? withSession() : withOutSession();
|
|
||||||
|
|
||||||
|
|
||||||
app.listen(9000, () => {
|
|
||||||
console.log('Server ready!');
|
|
||||||
})
|
})
|
||||||
|
checkEnvFile();
|
||||||
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 || 'root',
|
||||||
|
password : process.env.SQL_PASS || '',
|
||||||
|
database : process.env.SQL_DATABASE || 'pruebas'
|
||||||
|
});
|
||||||
|
|
||||||
|
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}
|
||||||
14
controllers/connection.js
Normal file
14
controllers/connection.js
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
const connectionReady = (cb = () =>{}) => {
|
||||||
|
console.log('Listo para escuchas mensajes')
|
||||||
|
console.log('Client is ready!');
|
||||||
|
console.log('🔴 escribe: hola');
|
||||||
|
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, checkIsUrl} = 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 = checkIsUrl(data.media) ? await saveExternalFile(data.media) : data.media;
|
||||||
|
return {...data,...{media:file}}
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
const bothResponse = async (message, sessionId) => {
|
||||||
|
const data = await getIA(message, sessionId)
|
||||||
|
if(data && data.media){
|
||||||
|
const file = await saveExternalFile(data.media)
|
||||||
|
return {...data,...{media:file}}
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = { getMessages, responseMessages, bothResponse }
|
||||||
83
controllers/handle.js
Normal file
83
controllers/handle.js
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
const { Client, LegacySessionAuth, LocalAuth } = require('whatsapp-web.js');
|
||||||
|
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 MULTI_DEVICE = process.env.MULTI_DEVICE || 'true';
|
||||||
|
|
||||||
|
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 checkIsUrl = (path) => {
|
||||||
|
try{
|
||||||
|
regex = /^(http(s)?:\/\/)[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/i;
|
||||||
|
match = path.match(regex);
|
||||||
|
return match[0]
|
||||||
|
}catch(e){
|
||||||
|
return 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()
|
||||||
|
}
|
||||||
|
|
||||||
|
const checkEnvFile = () => {
|
||||||
|
const pathEnv = `${__dirname}/../.env`;
|
||||||
|
const isExist = fs.existsSync(pathEnv);
|
||||||
|
if(!isExist){
|
||||||
|
console.log(`🆗 ATENCION! 🆗 te falta crear tu archivo .env de lo contrario no funcionara`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {*} session
|
||||||
|
* @param {*} cb
|
||||||
|
*/
|
||||||
|
const createClient = () => {
|
||||||
|
client = new Client({
|
||||||
|
authStrategy: new LocalAuth(
|
||||||
|
{dataPath: './sessions/',
|
||||||
|
clientId: 'bot'}),
|
||||||
|
puppeteer: { headless: false }
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const isValidNumber = (rawNumber) => {
|
||||||
|
const regexGroup = /\@g.us\b/gm;
|
||||||
|
const exist = rawNumber.match(regexGroup);
|
||||||
|
return !exist
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {cleanNumber, saveExternalFile, generateImage, checkIsUrl, checkEnvFile, createClient, isValidNumber}
|
||||||
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}
|
||||||
113
controllers/send.js
Normal file
113
controllers/send.js
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
|
||||||
|
const ExcelJS = require('exceljs');
|
||||||
|
const moment = require('moment');
|
||||||
|
const fs = require('fs');
|
||||||
|
const { MessageMedia, Buttons } = require('whatsapp-web.js');
|
||||||
|
const { cleanNumber } = require('./handle')
|
||||||
|
const DELAY_TIME = 170; //ms
|
||||||
|
const DIR_MEDIA = `${__dirname}/../mediaSend`;
|
||||||
|
// import { Low, JSONFile } from 'lowdb'
|
||||||
|
// import { join } from 'path'
|
||||||
|
const { saveMessage } = require('../adapter')
|
||||||
|
/**
|
||||||
|
* Enviamos archivos multimedia a nuestro cliente
|
||||||
|
* @param {*} number
|
||||||
|
* @param {*} fileName
|
||||||
|
*/
|
||||||
|
|
||||||
|
const sendMedia = (client, number = null, fileName = null) => {
|
||||||
|
if(!client) return cosnole.error("El objeto cliente no está definido.");
|
||||||
|
try {
|
||||||
|
number = cleanNumber(number || 0)
|
||||||
|
const file = `${DIR_MEDIA}/${fileName}`;
|
||||||
|
if (fs.existsSync(file)) {
|
||||||
|
const media = MessageMedia.fromFilePath(file);
|
||||||
|
client.sendMessage(number, media, { sendAudioAsVoice: true });
|
||||||
|
}
|
||||||
|
} catch(e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enviamos archivos como notas de voz
|
||||||
|
* @param {*} number
|
||||||
|
* @param {*} fileName
|
||||||
|
*/
|
||||||
|
|
||||||
|
const sendMediaVoiceNote = (client, number = null, fileName = null) => {
|
||||||
|
if(!client) return cosnole.error("El objeto cliente no está definido.");
|
||||||
|
try {
|
||||||
|
number = cleanNumber(number || 0)
|
||||||
|
const file = `${DIR_MEDIA}/${fileName}`;
|
||||||
|
if (fs.existsSync(file)) {
|
||||||
|
const media = MessageMedia.fromFilePath(file);
|
||||||
|
client.sendMessage(number, media ,{ sendAudioAsVoice: true });
|
||||||
|
|
||||||
|
}
|
||||||
|
}catch(e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Enviamos un mensaje simple (texto) a nuestro cliente
|
||||||
|
* @param {*} number
|
||||||
|
*/
|
||||||
|
const sendMessage = async (client, number = null, text = null, trigger = null) => {
|
||||||
|
setTimeout(async () => {
|
||||||
|
number = cleanNumber(number)
|
||||||
|
const message = text
|
||||||
|
client.sendMessage(number, message);
|
||||||
|
await readChat(number, message, trigger)
|
||||||
|
console.log(`⚡⚡⚡ Enviando mensajes....`);
|
||||||
|
},DELAY_TIME)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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) => {
|
||||||
|
number = cleanNumber(number)
|
||||||
|
await saveMessage( message, trigger, number )
|
||||||
|
console.log('Saved')
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { sendMessage, sendMedia, lastTrigger, sendMessageButton, readChat, sendMediaVoiceNote }
|
||||||
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
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
17
controllers/web.js
Normal file
17
controllers/web.js
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
const fs = require('fs')
|
||||||
|
const { sendMessage } = require('../controllers/send')
|
||||||
|
|
||||||
|
const sendMessagePost = (req, res) => {
|
||||||
|
console.log('asdasdasdasdasd')
|
||||||
|
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 }
|
|
||||||
93
flow/initial.json
Normal file
93
flow/initial.json
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"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": [
|
||||||
|
"VER_CURSOS"
|
||||||
|
],
|
||||||
|
"key": "STEP_6"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"keywords": [
|
||||||
|
"telegram"
|
||||||
|
],
|
||||||
|
"key": "STEP_7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"keywords": [
|
||||||
|
"audio"
|
||||||
|
],
|
||||||
|
"key": "STEP_8"
|
||||||
|
}
|
||||||
|
]
|
||||||
148
flow/response.json
Normal file
148
flow/response.json
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
{
|
||||||
|
"DEFAULT":{
|
||||||
|
"replyMessage":[
|
||||||
|
"*Esta respuesta es un respuesta default* cuando no se consigue una palabra clave \n",
|
||||||
|
"la puedes desactivar en tu archivo .env DEFAULT_MESSAGE=false \n",
|
||||||
|
"tambien te quiero recordar que si presentas algun error pasarte por el repositorio \n",
|
||||||
|
"https://github.com/leifermendez/bot-whatsapp#chatbot-whatsapp-opensource \n",
|
||||||
|
"y recuerda tener la ultima versión del proyecto \n\n",
|
||||||
|
"Prueba escribiendo *hola* \n"
|
||||||
|
],
|
||||||
|
"media":null,
|
||||||
|
"trigger":null
|
||||||
|
},
|
||||||
|
"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":[
|
||||||
|
"Hola! y✌️ 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* o escribe *audio*"
|
||||||
|
],
|
||||||
|
"media":"https://media2.giphy.com/media/VQJu0IeULuAmCwf5SL/giphy.gif",
|
||||||
|
"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_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",
|
||||||
|
"https://youtube.com/leifermendez \n"
|
||||||
|
],
|
||||||
|
"media":null,
|
||||||
|
"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",
|
||||||
|
"https://t.me/leifermendez \n"
|
||||||
|
],
|
||||||
|
"media":null,
|
||||||
|
"trigger":null
|
||||||
|
},
|
||||||
|
"STEP_8":{
|
||||||
|
"replyMessage":[
|
||||||
|
"Esto es una nota de voz \n"
|
||||||
|
],
|
||||||
|
"media":"nota-de-voz.mp3",
|
||||||
|
"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
BIN
mediaSend/PTT-20220223-WA0000.opus
Normal file
BIN
mediaSend/PTT-20220223-WA0000.opus
Normal file
Binary file not shown.
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 |
BIN
mediaSend/nota-de-voz.mp3
Normal file
BIN
mediaSend/nota-de-voz.mp3
Normal file
Binary file not shown.
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 }
|
||||||
0
middleware/db.js
Normal file
0
middleware/db.js
Normal file
9127
package-lock.json
generated
9127
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
61
package.json
61
package.json
@@ -1,25 +1,56 @@
|
|||||||
{
|
{
|
||||||
"name": "test-ws-bot",
|
"name": "bot-whatsapp",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "Bot de wahtsapp open source para MVP o pequeños negocios",
|
||||||
"main": "index.js",
|
"main": "app.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"start": "node ./app.js",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [
|
||||||
"author": "",
|
"whatsapp",
|
||||||
|
"bot-whatsapp",
|
||||||
|
"node-bot-whatsapp"
|
||||||
|
],
|
||||||
|
"contributors": [
|
||||||
|
{
|
||||||
|
"email": "leifer33@gmail.com",
|
||||||
|
"name": "Leifer Mendez",
|
||||||
|
"url": "https://leifermendez.github.io"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "aurik3",
|
||||||
|
"email": "aurik3@aurik3.com",
|
||||||
|
"url": "https://github.com/aurik3"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/leifermendez/bot-whatsapp"
|
||||||
|
},
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"chalk": "^4.1.0",
|
"@google-cloud/dialogflow": "^5.2.0",
|
||||||
"excel4node": "^1.7.2",
|
"cors": "^2.8.5",
|
||||||
"exceljs": "^4.2.1",
|
"dotenv": "^16.0.1",
|
||||||
"express": "^4.17.1",
|
"exceljs": "^4.3.0",
|
||||||
"file-type": "^16.3.0",
|
"express": "^4.18.1",
|
||||||
"mime-db": "^1.46.0",
|
"file-type": "^17.1.6",
|
||||||
"moment": "^2.29.1",
|
"mime-db": "^1.52.0",
|
||||||
"ora": "^5.4.0",
|
"moment": "^2.29.4",
|
||||||
|
"mysql": "^2.18.1",
|
||||||
|
"qr-image": "^3.2.0",
|
||||||
"qrcode-terminal": "^0.12.0",
|
"qrcode-terminal": "^0.12.0",
|
||||||
"whatsapp-web.js": "^1.12.5",
|
"socket.io": "^4.5.1",
|
||||||
"xlsx": "^0.16.9"
|
"stormdb": "^0.6.0",
|
||||||
|
"whatsapp-web.js": "^1.18.4",
|
||||||
|
"xlsx": "^0.18.5"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"pm2": "^5.2.0",
|
||||||
|
"prettier": "2.7.1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": "16.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