From ccca7f5612648fcdb3301e8b89dd6d6cb58acd2a Mon Sep 17 00:00:00 2001 From: Leifer Mendez Date: Sat, 15 Jan 2022 13:23:56 +0100 Subject: [PATCH] start with adapter --- .env.example | 9 ++++++++- adapter/index.js | 12 ++++++++++++ adapter/mysql.js | 9 +++++++++ app.js | 20 ++++++++++++++------ config/mysql.js | 18 ++++++++++++++++++ controllers/flows.js | 15 +++++---------- package-lock.json | 37 +++++++++++++++++++++++++++++++++++++ package.json | 1 + 8 files changed, 104 insertions(+), 17 deletions(-) create mode 100644 adapter/index.js create mode 100644 adapter/mysql.js create mode 100644 config/mysql.js diff --git a/.env.example b/.env.example index c0cdf79..a1e2e89 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,9 @@ SAVE_MEDIA=true -PORT=3000 \ No newline at end of file +PORT=3000 +DATABASE=none + +#DATABASE: none, mysql, mongo +SQL_HOST= +SQL_USER= +SQL_PASS= +SQL_DATABASE= \ No newline at end of file diff --git a/adapter/index.js b/adapter/index.js new file mode 100644 index 0000000..b90f607 --- /dev/null +++ b/adapter/index.js @@ -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} \ No newline at end of file diff --git a/adapter/mysql.js b/adapter/mysql.js new file mode 100644 index 0000000..9644ff4 --- /dev/null +++ b/adapter/mysql.js @@ -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} \ No newline at end of file diff --git a/app.js b/app.js index af97bba..a596764 100644 --- a/app.js +++ b/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}`); diff --git a/config/mysql.js b/config/mysql.js new file mode 100644 index 0000000..27b3639 --- /dev/null +++ b/config/mysql.js @@ -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} \ No newline at end of file diff --git a/controllers/flows.js b/controllers/flows.js index 371c593..2a28331 100644 --- a/controllers/flows.js +++ b/controllers/flows.js @@ -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) } diff --git a/package-lock.json b/package-lock.json index 068221c..ad323a2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index a8acafd..1ff38c5 100644 --- a/package.json +++ b/package.json @@ -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",