mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-17 19:26:23 +00:00
start with adapter
This commit is contained in:
@@ -1,2 +1,9 @@
|
||||
SAVE_MEDIA=true
|
||||
PORT=3000
|
||||
PORT=3000
|
||||
DATABASE=none
|
||||
|
||||
#DATABASE: none, mysql, mongo
|
||||
SQL_HOST=
|
||||
SQL_USER=
|
||||
SQL_PASS=
|
||||
SQL_DATABASE=
|
||||
12
adapter/index.js
Normal file
12
adapter/index.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const {getData} = require('./mysql')
|
||||
|
||||
const get = (step) => new Promise((resolve, reject) => {
|
||||
if(process.env.DATABASE === 'mysql'){
|
||||
getData(step,(dt) => {
|
||||
console.log('--->datos--',dt)
|
||||
resolve(dt)
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = {get}
|
||||
9
adapter/mysql.js
Normal file
9
adapter/mysql.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const {connection} = require('../config/mysql')
|
||||
|
||||
const getData = (option_key = '', callback) => connection.query(`SELECT * FROM db_test.keywords WHERE option_key = '${option_key}' LIMIT 1`,(error, results, fields) => {
|
||||
const [response] = results
|
||||
let parseResponse = response?.value || '';
|
||||
parseResponse = parseResponse.split(',') || []
|
||||
callback(parseResponse)
|
||||
});
|
||||
module.exports = {getData}
|
||||
20
app.js
20
app.js
@@ -6,6 +6,7 @@ const fs = require('fs');
|
||||
const express = require('express');
|
||||
const qrcode = require('qrcode-terminal');
|
||||
const { Client } = require('whatsapp-web.js');
|
||||
const mysqlConnection = require('./config/mysql')
|
||||
const { middlewareClient } = require('./middleware/client')
|
||||
const { connectionReady, connectionLost } = require('./controllers/connection')
|
||||
const { saveMedia } = require('./controllers/save')
|
||||
@@ -61,17 +62,17 @@ const listenMessage = () => client.on('message', async msg => {
|
||||
/**
|
||||
* Respondemos al primero paso si encuentra palabras clave
|
||||
*/
|
||||
if (getMessages('STEP_1').includes(message)) {
|
||||
if (await getMessages('STEP_1', message)) {
|
||||
const response = responseMessages('STEP_1')
|
||||
sendMessage(client, from, response, 'STEP_2');
|
||||
return
|
||||
}
|
||||
|
||||
if (getMessages('STEP_2').includes(message)) {
|
||||
const response = responseMessages('STEP_2')
|
||||
sendMessage(client, from, response);
|
||||
return
|
||||
}
|
||||
// if (getMessages('STEP_2').includes(message)) {
|
||||
// const response = responseMessages('STEP_2')
|
||||
// sendMessage(client, from, response);
|
||||
// return
|
||||
// }
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -139,6 +140,13 @@ const withOutSession = () => {
|
||||
*/
|
||||
(fs.existsSync(SESSION_FILE_PATH)) ? withSession() : withOutSession();
|
||||
|
||||
/**
|
||||
* Verificamos si tienes un gesto de db
|
||||
*/
|
||||
|
||||
if(process.env.DATABASE === 'mysql'){
|
||||
mysqlConnection.connect()
|
||||
}
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`El server esta listo por el puerto ${port}`);
|
||||
|
||||
18
config/mysql.js
Normal file
18
config/mysql.js
Normal file
@@ -0,0 +1,18 @@
|
||||
const mysql = require('mysql');
|
||||
const connection = mysql.createConnection({
|
||||
host : process.env.SQL_HOST || 'localhost',
|
||||
user : process.env.SQL_USER || 'me',
|
||||
password : process.env.SQL_PASS || 'secret',
|
||||
database : process.env.SQL_DATABASE || 'my_db'
|
||||
});
|
||||
|
||||
const connect = () => connection.connect(function(err) {
|
||||
if (err) {
|
||||
console.error('error connecting: ' + err.stack);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Conexion correcta con tu base de datos MySQL')
|
||||
});
|
||||
|
||||
module.exports = {connect, connection}
|
||||
@@ -1,13 +1,8 @@
|
||||
const getMessages = (step) => {
|
||||
switch (step) {
|
||||
case 'STEP_1':
|
||||
return ['hola', 'hi']
|
||||
break;
|
||||
case 'STEP_2':
|
||||
return ['hola', 'hi']
|
||||
break;
|
||||
}
|
||||
return null
|
||||
const {get} = require('../adapter')
|
||||
|
||||
const getMessages = async (step, message) => {
|
||||
const data = await get(step)
|
||||
return data.includes(message)
|
||||
}
|
||||
|
||||
|
||||
|
||||
37
package-lock.json
generated
37
package-lock.json
generated
@@ -223,6 +223,11 @@
|
||||
"resolved": "https://registry.npmjs.org/big-integer/-/big-integer-1.6.51.tgz",
|
||||
"integrity": "sha512-GPEid2Y9QU1Exl1rpO9B2IPJGHPSupF5GnVIP0blYvNOMer2bTvSWs1jGOUg04hTmu67nmLsQ9TBo1puaotBHg=="
|
||||
},
|
||||
"bignumber.js": {
|
||||
"version": "9.0.0",
|
||||
"resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz",
|
||||
"integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A=="
|
||||
},
|
||||
"binary": {
|
||||
"version": "0.3.0",
|
||||
"resolved": "https://registry.npmjs.org/binary/-/binary-0.3.0.tgz",
|
||||
@@ -1234,6 +1239,33 @@
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
|
||||
},
|
||||
"mysql": {
|
||||
"version": "2.18.1",
|
||||
"resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz",
|
||||
"integrity": "sha512-Bca+gk2YWmqp2Uf6k5NFEurwY/0td0cpebAucFpY/3jhrwrVGuxU2uQFCHjU19SJfje0yQvi+rVWdq78hR5lig==",
|
||||
"requires": {
|
||||
"bignumber.js": "9.0.0",
|
||||
"readable-stream": "2.3.7",
|
||||
"safe-buffer": "5.1.2",
|
||||
"sqlstring": "2.3.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"readable-stream": {
|
||||
"version": "2.3.7",
|
||||
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz",
|
||||
"integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==",
|
||||
"requires": {
|
||||
"core-util-is": "~1.0.0",
|
||||
"inherits": "~2.0.3",
|
||||
"isarray": "~1.0.0",
|
||||
"process-nextick-args": "~2.0.0",
|
||||
"safe-buffer": "~5.1.1",
|
||||
"string_decoder": "~1.1.1",
|
||||
"util-deprecate": "~1.0.1"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"napi-build-utils": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/napi-build-utils/-/napi-build-utils-1.0.2.tgz",
|
||||
@@ -1747,6 +1779,11 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"sqlstring": {
|
||||
"version": "2.3.1",
|
||||
"resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.1.tgz",
|
||||
"integrity": "sha1-R1OT/56RR5rqYtyvDKPRSYOn+0A="
|
||||
},
|
||||
"ssf": {
|
||||
"version": "0.11.2",
|
||||
"resolved": "https://registry.npmjs.org/ssf/-/ssf-0.11.2.tgz",
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
"file-type": "^16.5.3",
|
||||
"mime-db": "^1.51.0",
|
||||
"moment": "^2.29.1",
|
||||
"mysql": "^2.18.1",
|
||||
"qrcode-terminal": "^0.12.0",
|
||||
"socket.io": "^4.4.1",
|
||||
"whatsapp-web.js": "^1.15.3",
|
||||
|
||||
Reference in New Issue
Block a user