mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-20 20:49:15 +00:00
Compare commits
44 Commits
1.0.1
...
add-code-o
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ec32dc429f | ||
|
|
5303dd7d25 | ||
|
|
0246fc0b91 | ||
|
|
2858e910dc | ||
|
|
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 | ||
|
|
ba4f05ebb2 |
@@ -8,4 +8,6 @@ LANGUAGE=es
|
||||
SQL_HOST=
|
||||
SQL_USER=
|
||||
SQL_PASS=
|
||||
SQL_DATABASE=
|
||||
SQL_DATABASE=
|
||||
KEEP_DIALOG_FLOW=false
|
||||
MULTI_DEVICE=true
|
||||
2
.github/FUNDING.yml
vendored
Normal file
2
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
patreon: leifermendez
|
||||
custom: "https://www.buymeacoffee.com/leifermendez"
|
||||
128
CODE_OF_CONDUCT.md
Normal file
128
CODE_OF_CONDUCT.md
Normal file
@@ -0,0 +1,128 @@
|
||||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
We as members, contributors, and leaders pledge to make participation in our
|
||||
community a harassment-free experience for everyone, regardless of age, body
|
||||
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
||||
identity and expression, level of experience, education, socio-economic status,
|
||||
nationality, personal appearance, race, religion, or sexual identity
|
||||
and orientation.
|
||||
|
||||
We pledge to act and interact in ways that contribute to an open, welcoming,
|
||||
diverse, inclusive, and healthy community.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to a positive environment for our
|
||||
community include:
|
||||
|
||||
* Demonstrating empathy and kindness toward other people
|
||||
* Being respectful of differing opinions, viewpoints, and experiences
|
||||
* Giving and gracefully accepting constructive feedback
|
||||
* Accepting responsibility and apologizing to those affected by our mistakes,
|
||||
and learning from the experience
|
||||
* Focusing on what is best not just for us as individuals, but for the
|
||||
overall community
|
||||
|
||||
Examples of unacceptable behavior include:
|
||||
|
||||
* The use of sexualized language or imagery, and sexual attention or
|
||||
advances of any kind
|
||||
* Trolling, insulting or derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or email
|
||||
address, without their explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a
|
||||
professional setting
|
||||
|
||||
## Enforcement Responsibilities
|
||||
|
||||
Community leaders are responsible for clarifying and enforcing our standards of
|
||||
acceptable behavior and will take appropriate and fair corrective action in
|
||||
response to any behavior that they deem inappropriate, threatening, offensive,
|
||||
or harmful.
|
||||
|
||||
Community leaders have the right and responsibility to remove, edit, or reject
|
||||
comments, commits, code, wiki edits, issues, and other contributions that are
|
||||
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
||||
decisions when appropriate.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies within all community spaces, and also applies when
|
||||
an individual is officially representing the community in public spaces.
|
||||
Examples of representing our community include using an official e-mail address,
|
||||
posting via an official social media account, or acting as an appointed
|
||||
representative at an online or offline event.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||
reported to the community leaders responsible for enforcement at
|
||||
leifer.contacto@gmail.com.
|
||||
All complaints will be reviewed and investigated promptly and fairly.
|
||||
|
||||
All community leaders are obligated to respect the privacy and security of the
|
||||
reporter of any incident.
|
||||
|
||||
## Enforcement Guidelines
|
||||
|
||||
Community leaders will follow these Community Impact Guidelines in determining
|
||||
the consequences for any action they deem in violation of this Code of Conduct:
|
||||
|
||||
### 1. Correction
|
||||
|
||||
**Community Impact**: Use of inappropriate language or other behavior deemed
|
||||
unprofessional or unwelcome in the community.
|
||||
|
||||
**Consequence**: A private, written warning from community leaders, providing
|
||||
clarity around the nature of the violation and an explanation of why the
|
||||
behavior was inappropriate. A public apology may be requested.
|
||||
|
||||
### 2. Warning
|
||||
|
||||
**Community Impact**: A violation through a single incident or series
|
||||
of actions.
|
||||
|
||||
**Consequence**: A warning with consequences for continued behavior. No
|
||||
interaction with the people involved, including unsolicited interaction with
|
||||
those enforcing the Code of Conduct, for a specified period of time. This
|
||||
includes avoiding interactions in community spaces as well as external channels
|
||||
like social media. Violating these terms may lead to a temporary or
|
||||
permanent ban.
|
||||
|
||||
### 3. Temporary Ban
|
||||
|
||||
**Community Impact**: A serious violation of community standards, including
|
||||
sustained inappropriate behavior.
|
||||
|
||||
**Consequence**: A temporary ban from any sort of interaction or public
|
||||
communication with the community for a specified period of time. No public or
|
||||
private interaction with the people involved, including unsolicited interaction
|
||||
with those enforcing the Code of Conduct, is allowed during this period.
|
||||
Violating these terms may lead to a permanent ban.
|
||||
|
||||
### 4. Permanent Ban
|
||||
|
||||
**Community Impact**: Demonstrating a pattern of violation of community
|
||||
standards, including sustained inappropriate behavior, harassment of an
|
||||
individual, or aggression toward or disparagement of classes of individuals.
|
||||
|
||||
**Consequence**: A permanent ban from any sort of public interaction within
|
||||
the community.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
||||
version 2.0, available at
|
||||
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
||||
|
||||
Community Impact Guidelines were inspired by [Mozilla's code of conduct
|
||||
enforcement ladder](https://github.com/mozilla/diversity).
|
||||
|
||||
[homepage]: https://www.contributor-covenant.org
|
||||
|
||||
For answers to common questions about this code of conduct, see the FAQ at
|
||||
https://www.contributor-covenant.org/faq. Translations are available at
|
||||
https://www.contributor-covenant.org/translations.
|
||||
21
LICENSE.md
Normal file
21
LICENSE.md
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 Leifer Mendez
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
48
README.md
48
README.md
@@ -1,26 +1,4 @@
|
||||
## Chatbot Whatsapp (OpenSource)
|
||||
#### Actualizado Enero 2022
|
||||
|
||||
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__.
|
||||
|
||||
[](https://youtu.be/5lEMCeWEJ8o)
|
||||
|
||||
### ATENCION 1 🔴
|
||||
> 💥💥 Si te aparece el Error Multi-device es porque tienes la cuenta de whatsapp afiliada al modo "BETA de Multi dispositivo" por el momento no se tiene soporte para esas personas si tu quieres hacer uso de este __BOT__ debes de salir del modo BETA y intentarlo de la manera tradicional
|
||||
|
||||
### ATENCION 2 🔴
|
||||
> El core de whatsapp esta en constante actualizaciones por lo cual siempre revisa la ultima fecha de la actualizacion
|
||||
> [VER](https://github.com/leifermendez/bot-whatsapp/commits/main)
|
||||
|
||||
|
||||
#### 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-whatsapp)
|
||||
|
||||
> Comprarme un cafe!
|
||||
|
||||
[](https://www.buymeacoffee.com/leifermendez)
|
||||
|
||||
#### Actualización
|
||||
|
||||
@@ -31,7 +9,7 @@ El siguiente proyecto se realizó con fines educativos para el canal de [Youtube
|
||||
| JSON File | ✅ |
|
||||
| QR Scan (route) | ✅ |
|
||||
| Easy deploy heroku | ✅ |
|
||||
| Buttons | ✅ |
|
||||
| Buttons | ✅ℹ️ (No funciona en multi-device)|
|
||||
| Send Voice Note | ✅ |
|
||||
| Add support ubuntu/linux | ✅ |
|
||||
|
||||
@@ -80,7 +58,6 @@ await sendMediaVoiceNote(client, from, 'PTT-20220223-WA0000.opus')
|
||||
|
||||
## Instruciones
|
||||
__Descargar o Clonar repositorio__
|
||||

|
||||
|
||||
__Usas ¿Ubuntu / Linux?__
|
||||
> Asegurate de instalar los siguientes paquetes
|
||||
@@ -92,9 +69,9 @@ sudo apt install -y gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups
|
||||
__Instalar dependencias (npm install)__
|
||||
> 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`
|
||||
@@ -113,21 +90,14 @@ 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`
|
||||
|
||||

|
||||
Tambien puedes visitar la pagina http://127.0.0.1:3000/qr
|
||||
|
||||
__Listo 😎__
|
||||
> Cuando sale este mensaje tu BOT está __listo__ para trabajar!
|
||||
@@ -144,11 +114,3 @@ __Listo 😎__
|
||||
|
||||
> 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
|
||||
|
||||

|
||||

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

|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
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
|
||||
*/
|
||||
|
||||
const KEEP_DIALOG_FLOW = (process.env.KEEP_DIALOG_FLOW === 'true')
|
||||
let PROJECID;
|
||||
let CONFIGURATION;
|
||||
let sessionClient;
|
||||
@@ -28,9 +30,9 @@ const checkFileCredentials = () => {
|
||||
|
||||
|
||||
// Detect intent method
|
||||
const detectIntent = async (queryText) => {
|
||||
const detectIntent = async (queryText, waPhoneNumber) => {
|
||||
let media = null;
|
||||
const sessionId = nanoid.nanoid()
|
||||
const sessionId = KEEP_DIALOG_FLOW ? 1 : waPhoneNumber;
|
||||
const sessionPath = sessionClient.projectAgentSessionPath(PROJECID, sessionId);
|
||||
const languageCode = process.env.LANGUAGE
|
||||
const request = {
|
||||
@@ -54,7 +56,7 @@ const detectIntent = async (queryText) => {
|
||||
const { fields } = parsePayload.payload
|
||||
media = fields.media.stringValue || null
|
||||
}
|
||||
// const customPayload = parsePayload['payload']
|
||||
const customPayload = parsePayload ? parsePayload['payload'] : null
|
||||
|
||||
const parseData = {
|
||||
replyMessage: queryResult.fulfillmentText,
|
||||
@@ -64,8 +66,8 @@ const detectIntent = async (queryText) => {
|
||||
return parseData
|
||||
}
|
||||
|
||||
const getDataIa = (message = '', cb = () => { }) => {
|
||||
detectIntent(message).then((res) => {
|
||||
const getDataIa = (message = '', sessionId = '', cb = () => { }) => {
|
||||
detectIntent(message, sessionId).then((res) => {
|
||||
cb(res)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -52,13 +52,13 @@ const reply = (step) => new Promise((resolve, reject) => {
|
||||
}
|
||||
})
|
||||
|
||||
const getIA = (message) => new Promise((resolve, reject) => {
|
||||
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,(dt) => {
|
||||
getDataIa(message, sessionId, (dt) => {
|
||||
resData = { ...resData, ...dt }
|
||||
resolve(resData)
|
||||
})
|
||||
@@ -82,6 +82,7 @@ const saveMessage = ( message, trigger, number ) => new Promise( async (resolve
|
||||
resolve( await saveMessageJson( message, trigger, number ) )
|
||||
break;
|
||||
default:
|
||||
resolve(true)
|
||||
break;
|
||||
}
|
||||
})
|
||||
|
||||
@@ -21,6 +21,7 @@ getReply = (option_key = '', callback) => connection.query(
|
||||
replyMessage:response?.replyMessage || '',
|
||||
trigger:response?.trigger || '',
|
||||
media:response?.media || ''
|
||||
|
||||
}
|
||||
callback(value)
|
||||
});
|
||||
@@ -51,13 +52,15 @@ saveMessageMysql = ( message, date, trigger, number ) => new Promise((resolve,re
|
||||
connection.query(
|
||||
`INSERT INTO ${DATABASE_NAME}.messages `+"( `message`, `date`, `trigger`, `number`)"+` VALUES ('${message}','${date}','${trigger}', '${number}')` , (error, results) => {
|
||||
if(error) {
|
||||
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)
|
||||
})
|
||||
}
|
||||
//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 )
|
||||
|
||||
154
app.js
154
app.js
@@ -6,10 +6,10 @@ const fs = require('fs');
|
||||
const express = require('express');
|
||||
const cors = require('cors')
|
||||
const qrcode = require('qrcode-terminal');
|
||||
const { Client, LegacySessionAuth } = require('whatsapp-web.js');
|
||||
const { Client, LocalAuth } = require('whatsapp-web.js');
|
||||
const mysqlConnection = require('./config/mysql')
|
||||
const { middlewareClient } = require('./middleware/client')
|
||||
const { generateImage, cleanNumber, checkEnvFile } = require('./controllers/handle')
|
||||
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')
|
||||
@@ -17,41 +17,29 @@ const { sendMedia, sendMessage, lastTrigger, sendMessageButton, readChat } = req
|
||||
const app = express();
|
||||
app.use(cors())
|
||||
app.use(express.json())
|
||||
|
||||
const MULTI_DEVICE = process.env.MULTI_DEVICE || 'true';
|
||||
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';
|
||||
var client;
|
||||
var sessionData;
|
||||
app.use('/', require('./routes/web'))
|
||||
|
||||
/**
|
||||
* Escuchamos cuando entre un mensaje
|
||||
*/
|
||||
const listenMessage = () => client.on('message', async msg => {
|
||||
const { from, body, hasMedia } = msg;
|
||||
|
||||
if (!isValidNumber(from)) {
|
||||
return
|
||||
}
|
||||
|
||||
// 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)
|
||||
console.log('BODY', message)
|
||||
const number = cleanNumber(from)
|
||||
await readChat(number, message)
|
||||
|
||||
@@ -68,7 +56,8 @@ const listenMessage = () => client.on('message', async msg => {
|
||||
*/
|
||||
|
||||
if (process.env.DATABASE === 'dialogflow') {
|
||||
const response = await bothResponse(message);
|
||||
if (!message.length) return;
|
||||
const response = await bothResponse(message, number);
|
||||
await sendMessage(client, from, response.replyMessage);
|
||||
if (response.media) {
|
||||
sendMedia(client, from, response.media);
|
||||
@@ -101,7 +90,8 @@ const listenMessage = () => client.on('message', async msg => {
|
||||
*/
|
||||
|
||||
await sendMessage(client, from, response.replyMessage, response.trigger);
|
||||
if(response.hasOwnProperty('actions')){
|
||||
|
||||
if (response.hasOwnProperty('actions')) {
|
||||
const { actions } = response;
|
||||
await sendMessageButton(client, from, null, actions);
|
||||
return
|
||||
@@ -126,7 +116,7 @@ const listenMessage = () => client.on('message', async msg => {
|
||||
/**
|
||||
* Si quieres enviar botones
|
||||
*/
|
||||
if(response.hasOwnProperty('actions')){
|
||||
if (response.hasOwnProperty('actions')) {
|
||||
const { actions } = response;
|
||||
await sendMessageButton(client, from, null, actions);
|
||||
}
|
||||
@@ -134,105 +124,38 @@ const listenMessage = () => client.on('message', async msg => {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* 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
|
||||
console.log(`Validando session con Whatsapp...`)
|
||||
sessionData = require(SESSION_FILE_PATH);
|
||||
client = new Client({
|
||||
authStrategy: new LegacySessionAuth({
|
||||
session: sessionData // saved session object
|
||||
}),
|
||||
restartOnAuthFail: true,
|
||||
puppeteer: {
|
||||
args: [
|
||||
'--no-sandbox'
|
||||
],
|
||||
}
|
||||
});
|
||||
|
||||
client.on('ready', () => {
|
||||
connectionReady()
|
||||
listenMessage()
|
||||
loadRoutes(client);
|
||||
socketEvents.sendStatus()
|
||||
});
|
||||
|
||||
client.on('auth_failure', () => connectionLost())
|
||||
client = new Client({
|
||||
authStrategy: new LocalAuth(),
|
||||
puppeteer: { headless: true }
|
||||
});
|
||||
|
||||
client.initialize();
|
||||
}
|
||||
client.on('qr', qr => generateImage(qr, () => {
|
||||
qrcode.generate(qr, { small: true });
|
||||
|
||||
/**
|
||||
* Generamos un QRCODE para iniciar sesion
|
||||
*/
|
||||
const withOutSession = () => {
|
||||
console.log('No tenemos session guardada');
|
||||
console.log([
|
||||
'🙌 El core de whatsapp se esta actualizando',
|
||||
'🙌 para proximamente dar paso al multi-device',
|
||||
'🙌 falta poco si quieres estar al pendiente unete',
|
||||
'🙌 http://t.me/leifermendez',
|
||||
'________________________',
|
||||
].join('\n'));
|
||||
console.log(`Ver QR http://localhost:${port}/qr`)
|
||||
socketEvents.sendQR(qr)
|
||||
}))
|
||||
|
||||
client = new Client({
|
||||
session: { },
|
||||
// authStrategy: new LegacySessionAuth({
|
||||
// session: { }
|
||||
// }),
|
||||
restartOnAuthFail: true,
|
||||
puppeteer: {
|
||||
args: [
|
||||
'--no-sandbox'
|
||||
],
|
||||
}
|
||||
});
|
||||
client.on('ready', (a) => {
|
||||
connectionReady()
|
||||
listenMessage()
|
||||
// socketEvents.sendStatus(client)
|
||||
});
|
||||
|
||||
client.on('qr', qr => generateImage(qr, () => {
|
||||
qrcode.generate(qr, { small: true });
|
||||
console.log(`Ver QR http://localhost:${port}/qr`)
|
||||
socketEvents.sendQR(qr)
|
||||
}))
|
||||
client.on('auth_failure', (e) => {
|
||||
// console.log(e)
|
||||
// connectionLost()
|
||||
});
|
||||
|
||||
client.on('ready', (a) => {
|
||||
connectionReady()
|
||||
listenMessage()
|
||||
loadRoutes(client);
|
||||
// socketEvents.sendStatus(client)
|
||||
});
|
||||
client.on('authenticated', () => {
|
||||
console.log('AUTHENTICATED');
|
||||
});
|
||||
|
||||
client.on('auth_failure', (e) => {
|
||||
// console.log(e)
|
||||
// connectionLost()
|
||||
});
|
||||
client.initialize();
|
||||
|
||||
client.on('authenticated', (session) => {
|
||||
sessionData = session;
|
||||
fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function (err) {
|
||||
if (err) {
|
||||
console.log(`Ocurrio un error con el archivo: `, err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
client.initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Cargamos rutas de express
|
||||
*/
|
||||
|
||||
const loadRoutes = (client) => {
|
||||
app.use('/api/', middlewareClient(client), require('./routes/api'))
|
||||
}
|
||||
/**
|
||||
* Revisamos si existe archivo con credenciales!
|
||||
*/
|
||||
(fs.existsSync(SESSION_FILE_PATH)) ? withSession() : withOutSession();
|
||||
|
||||
/**
|
||||
* Verificamos si tienes un gesto de db
|
||||
@@ -245,5 +168,4 @@ if (process.env.DATABASE === 'mysql') {
|
||||
server.listen(port, () => {
|
||||
console.log(`El server esta listo por el puerto ${port}`);
|
||||
})
|
||||
checkEnvFile();
|
||||
|
||||
checkEnvFile();
|
||||
@@ -1,9 +1,9 @@
|
||||
const mysql = require('mysql');
|
||||
const connection = mysql.createConnection({
|
||||
host : process.env.SQL_HOST || 'localhost',
|
||||
user : process.env.SQL_USER || 'me',
|
||||
user : process.env.SQL_USER || 'root',
|
||||
password : process.env.SQL_PASS || '',
|
||||
database : process.env.SQL_DATABASE || 'my_db'
|
||||
database : process.env.SQL_DATABASE || 'pruebas'
|
||||
});
|
||||
|
||||
const connect = () => connection.connect(function(err) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const connectionReady = (cb = () =>{}) => {
|
||||
console.log('Listo para escuchas mensajes')
|
||||
console.log('Client is ready!');
|
||||
console.log('🔴 escribe: hola');
|
||||
cb()
|
||||
}
|
||||
|
||||
|
||||
@@ -15,8 +15,8 @@ const responseMessages = async (step) => {
|
||||
return data
|
||||
}
|
||||
|
||||
const bothResponse = async (message) => {
|
||||
const data = await getIA(message)
|
||||
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}}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
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', '');
|
||||
@@ -57,4 +59,25 @@ const checkEnvFile = () => {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {cleanNumber, saveExternalFile, generateImage, checkIsUrl, checkEnvFile}
|
||||
/**
|
||||
*
|
||||
* @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}
|
||||
@@ -15,12 +15,17 @@ const { saveMessage } = require('../adapter')
|
||||
* @param {*} fileName
|
||||
*/
|
||||
|
||||
const sendMedia = (client, number, fileName) => {
|
||||
number = cleanNumber(number)
|
||||
const file = `${DIR_MEDIA}/${fileName}`;
|
||||
if (fs.existsSync(file)) {
|
||||
const media = MessageMedia.fromFilePath(file);
|
||||
client.sendMessage(number, media, { sendAudioAsVoice: true });
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,15 +35,21 @@ const sendMedia = (client, number, fileName) => {
|
||||
* @param {*} fileName
|
||||
*/
|
||||
|
||||
const sendMediaVoiceNote = (client, number, fileName) => {
|
||||
number = cleanNumber(number)
|
||||
const file = `${DIR_MEDIA}/${fileName}`;
|
||||
if (fs.existsSync(file)) {
|
||||
const media = MessageMedia.fromFilePath(file);
|
||||
client.sendMessage(number, media ,{ sendAudioAsVoice: true });
|
||||
}
|
||||
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
|
||||
@@ -99,4 +110,4 @@ const readChat = async (number, message, trigger = null) => {
|
||||
console.log('Saved')
|
||||
}
|
||||
|
||||
module.exports = { sendMessage, sendMedia, lastTrigger, sendMessageButton, readChat, sendMediaVoiceNote }
|
||||
module.exports = { sendMessage, sendMedia, lastTrigger, sendMessageButton, readChat, sendMediaVoiceNote }
|
||||
|
||||
@@ -2,6 +2,7 @@ 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)
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
"\n Si necesitas ver más info sobre las capacitacion tecnicas ",
|
||||
"escribe *cursos* o *info* o escribe *audio*"
|
||||
],
|
||||
"media":null,
|
||||
"media":"https://media2.giphy.com/media/VQJu0IeULuAmCwf5SL/giphy.gif",
|
||||
"trigger":null,
|
||||
"actions":{
|
||||
"title":"¿Que te interesa ver?",
|
||||
|
||||
7265
package-lock.json
generated
7265
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
54
package.json
54
package.json
@@ -1,38 +1,56 @@
|
||||
{
|
||||
"name": "test-ws-bot",
|
||||
"name": "bot-whatsapp",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"description": "Bot de wahtsapp open source para MVP o pequeños negocios",
|
||||
"main": "app.js",
|
||||
"scripts": {
|
||||
"start": "node ./app.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"keywords": [
|
||||
"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",
|
||||
"dependencies": {
|
||||
"@google-cloud/dialogflow": "^4.6.0",
|
||||
"@google-cloud/dialogflow": "^5.2.0",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^11.0.0",
|
||||
"dotenv": "^16.0.1",
|
||||
"exceljs": "^4.3.0",
|
||||
"express": "^4.17.2",
|
||||
"file-type": "^16.5.3",
|
||||
"mime-db": "^1.51.0",
|
||||
"moment": "^2.29.1",
|
||||
"express": "^4.18.1",
|
||||
"file-type": "^17.1.6",
|
||||
"mime-db": "^1.52.0",
|
||||
"moment": "^2.29.4",
|
||||
"mysql": "^2.18.1",
|
||||
"nanoid": "^3.1.32",
|
||||
"qr-image": "^3.2.0",
|
||||
"qrcode-terminal": "^0.12.0",
|
||||
"socket.io": "^4.4.1",
|
||||
"stormdb": "^0.5.2",
|
||||
"whatsapp-web.js": "^1.16.4",
|
||||
"xlsx": "^0.16.9"
|
||||
"socket.io": "^4.5.1",
|
||||
"stormdb": "^0.6.0",
|
||||
"whatsapp-web.js": "^1.18.4",
|
||||
"xlsx": "^0.18.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"pm2": "^5.1.2",
|
||||
"prettier": "2.5.1"
|
||||
"pm2": "^5.2.0",
|
||||
"prettier": "2.7.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": "14.x"
|
||||
"node": "16.x"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const express = require('express')
|
||||
const router = express.Router();
|
||||
const { sendMessagePost } = require('../controllers/web')
|
||||
const { sendMessagePost } = require('../controllers/web')|
|
||||
|
||||
router.post('/send', sendMessagePost)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user