mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-17 19:26:23 +00:00
fixed dialogflow session id
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
const dialogflow = require('@google-cloud/dialogflow');
|
||||
const fs = require('fs')
|
||||
const crypto = require('crypto');
|
||||
|
||||
/**
|
||||
* Debes de tener tu archivo con el nombre "chatbot-account.json" en la raíz del proyecto
|
||||
*/
|
||||
@@ -30,9 +30,9 @@ const checkFileCredentials = () => {
|
||||
|
||||
|
||||
// Detect intent method
|
||||
const detectIntent = async (queryText) => {
|
||||
const detectIntent = async (queryText, waPhoneNumber) => {
|
||||
let media = null;
|
||||
const sessionId = KEEP_DIALOG_FLOW ? 1 : crypto.randomUUID();
|
||||
const sessionId = KEEP_DIALOG_FLOW ? 1 : waPhoneNumber;
|
||||
const sessionPath = sessionClient.projectAgentSessionPath(PROJECID, sessionId);
|
||||
const languageCode = process.env.LANGUAGE
|
||||
const request = {
|
||||
@@ -66,12 +66,12 @@ 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)
|
||||
})
|
||||
}
|
||||
|
||||
checkFileCredentials();
|
||||
|
||||
module.exports = { getDataIa }
|
||||
module.exports = { getDataIa }
|
||||
@@ -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)
|
||||
})
|
||||
|
||||
47
app.js
47
app.js
@@ -6,7 +6,7 @@ const fs = require('fs');
|
||||
const express = require('express');
|
||||
const cors = require('cors')
|
||||
const qrcode = require('qrcode-terminal');
|
||||
const { Client,LocalAuth } = 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, createClient, isValidNumber } = require('./controllers/handle')
|
||||
@@ -30,7 +30,7 @@ app.use('/', require('./routes/web'))
|
||||
const listenMessage = () => client.on('message', async msg => {
|
||||
const { from, body, hasMedia } = msg;
|
||||
|
||||
if(!isValidNumber(from)){
|
||||
if (!isValidNumber(from)) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ const listenMessage = () => client.on('message', async msg => {
|
||||
return
|
||||
}
|
||||
message = body.toLowerCase();
|
||||
console.log('BODY',message)
|
||||
console.log('BODY', message)
|
||||
const number = cleanNumber(from)
|
||||
await readChat(number, message)
|
||||
|
||||
@@ -56,8 +56,8 @@ const listenMessage = () => client.on('message', async msg => {
|
||||
*/
|
||||
|
||||
if (process.env.DATABASE === 'dialogflow') {
|
||||
if(!message.length) return;
|
||||
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);
|
||||
@@ -91,7 +91,7 @@ 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
|
||||
@@ -116,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);
|
||||
}
|
||||
@@ -127,33 +127,33 @@ const listenMessage = () => client.on('message', async msg => {
|
||||
|
||||
|
||||
client = new Client({
|
||||
authStrategy: new LocalAuth(),
|
||||
puppeteer: { headless: true }
|
||||
});
|
||||
|
||||
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)
|
||||
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)
|
||||
connectionReady()
|
||||
listenMessage()
|
||||
// socketEvents.sendStatus(client)
|
||||
});
|
||||
|
||||
client.on('auth_failure', (e) => {
|
||||
// console.log(e)
|
||||
// connectionLost()
|
||||
// console.log(e)
|
||||
// connectionLost()
|
||||
});
|
||||
|
||||
client.on('authenticated', () => {
|
||||
console.log('AUTHENTICATED');
|
||||
console.log('AUTHENTICATED');
|
||||
});
|
||||
|
||||
client.initialize();
|
||||
client.initialize();
|
||||
|
||||
|
||||
|
||||
@@ -168,5 +168,4 @@ if (process.env.DATABASE === 'mysql') {
|
||||
server.listen(port, () => {
|
||||
console.log(`El server esta listo por el puerto ${port}`);
|
||||
})
|
||||
checkEnvFile();
|
||||
|
||||
checkEnvFile();
|
||||
@@ -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}}
|
||||
|
||||
42
package-lock.json
generated
42
package-lock.json
generated
@@ -22,13 +22,12 @@
|
||||
"qrcode-terminal": "^0.12.0",
|
||||
"socket.io": "^4.5.1",
|
||||
"stormdb": "^0.6.0",
|
||||
"whatsapp-web.js": "^1.18.0",
|
||||
"whatsapp-web.js": "^1.18.4",
|
||||
"xlsx": "^0.18.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"pm2": "^5.2.0",
|
||||
"prettier": "2.7.1",
|
||||
"rollup": "^3.2.3"
|
||||
"prettier": "2.7.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": "16.x"
|
||||
@@ -4242,22 +4241,6 @@
|
||||
"url": "https://github.com/sponsors/isaacs"
|
||||
}
|
||||
},
|
||||
"node_modules/rollup": {
|
||||
"version": "3.2.3",
|
||||
"resolved": "https://registry.npmjs.org/rollup/-/rollup-3.2.3.tgz",
|
||||
"integrity": "sha512-qfadtkY5kl0F5e4dXVdj2D+GtOdifasXHFMiL1SMf9ADQDv5Eti6xReef9FKj+iQPR2pvtqWna57s/PjARY4fg==",
|
||||
"dev": true,
|
||||
"bin": {
|
||||
"rollup": "dist/bin/rollup"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.18.0",
|
||||
"npm": ">=8.0.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"fsevents": "~2.3.2"
|
||||
}
|
||||
},
|
||||
"node_modules/run-series": {
|
||||
"version": "1.1.9",
|
||||
"resolved": "https://registry.npmjs.org/run-series/-/run-series-1.1.9.tgz",
|
||||
@@ -5039,9 +5022,9 @@
|
||||
"integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ=="
|
||||
},
|
||||
"node_modules/whatsapp-web.js": {
|
||||
"version": "1.18.0",
|
||||
"resolved": "https://registry.npmjs.org/whatsapp-web.js/-/whatsapp-web.js-1.18.0.tgz",
|
||||
"integrity": "sha512-3cCW28/w7llZmTgiO9osumaopOuBBduvyuW02yfy3q9Rz4Wq9Oe1dRedE/kkq764zB8AZcuTs0tE8OKTq2Zobw==",
|
||||
"version": "1.18.4",
|
||||
"resolved": "https://registry.npmjs.org/whatsapp-web.js/-/whatsapp-web.js-1.18.4.tgz",
|
||||
"integrity": "sha512-Dqu6Q37tDDAcVJ44aMdRE76sI/9rBCUG+NTz1Kxh2w4obX2WtpoRetilxqgx1r4+pFUl58Lf21wGOEwPZ1pT/A==",
|
||||
"dependencies": {
|
||||
"@pedroslopez/moduleraid": "^5.0.2",
|
||||
"fluent-ffmpeg": "^2.1.2",
|
||||
@@ -8598,15 +8581,6 @@
|
||||
"glob": "^7.1.3"
|
||||
}
|
||||
},
|
||||
"rollup": {
|
||||
"version": "3.2.3",
|
||||
"resolved": "https://registry.npmjs.org/rollup/-/rollup-3.2.3.tgz",
|
||||
"integrity": "sha512-qfadtkY5kl0F5e4dXVdj2D+GtOdifasXHFMiL1SMf9ADQDv5Eti6xReef9FKj+iQPR2pvtqWna57s/PjARY4fg==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"fsevents": "~2.3.2"
|
||||
}
|
||||
},
|
||||
"run-series": {
|
||||
"version": "1.1.9",
|
||||
"resolved": "https://registry.npmjs.org/run-series/-/run-series-1.1.9.tgz",
|
||||
@@ -9193,9 +9167,9 @@
|
||||
"integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ=="
|
||||
},
|
||||
"whatsapp-web.js": {
|
||||
"version": "1.18.0",
|
||||
"resolved": "https://registry.npmjs.org/whatsapp-web.js/-/whatsapp-web.js-1.18.0.tgz",
|
||||
"integrity": "sha512-3cCW28/w7llZmTgiO9osumaopOuBBduvyuW02yfy3q9Rz4Wq9Oe1dRedE/kkq764zB8AZcuTs0tE8OKTq2Zobw==",
|
||||
"version": "1.18.4",
|
||||
"resolved": "https://registry.npmjs.org/whatsapp-web.js/-/whatsapp-web.js-1.18.4.tgz",
|
||||
"integrity": "sha512-Dqu6Q37tDDAcVJ44aMdRE76sI/9rBCUG+NTz1Kxh2w4obX2WtpoRetilxqgx1r4+pFUl58Lf21wGOEwPZ1pT/A==",
|
||||
"requires": {
|
||||
"@pedroslopez/moduleraid": "^5.0.2",
|
||||
"archiver": "^5.3.1",
|
||||
|
||||
@@ -43,7 +43,7 @@
|
||||
"qrcode-terminal": "^0.12.0",
|
||||
"socket.io": "^4.5.1",
|
||||
"stormdb": "^0.6.0",
|
||||
"whatsapp-web.js": "latest",
|
||||
"whatsapp-web.js": "^1.18.4",
|
||||
"xlsx": "^0.18.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
Reference in New Issue
Block a user