mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-21 21:19:17 +00:00
feat(contexts): ⚡ add new dialogflowcx
This commit is contained in:
@@ -8,7 +8,8 @@
|
|||||||
],
|
],
|
||||||
"exports": {
|
"exports": {
|
||||||
"./mock": "./lib/mock/index.cjs",
|
"./mock": "./lib/mock/index.cjs",
|
||||||
"./dialogflow": "./lib/dialogflow/index.cjs"
|
"./dialogflow": "./lib/dialogflow/index.cjs",
|
||||||
|
"./dialogflowcx": "./lib/dialogflow-cx/index.cjs"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@bot-whatsapp/bot": "*"
|
"@bot-whatsapp/bot": "*"
|
||||||
|
|||||||
@@ -21,4 +21,13 @@ module.exports = [
|
|||||||
},
|
},
|
||||||
plugins: [commonjs()],
|
plugins: [commonjs()],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
input: join(__dirname, 'src', 'dialogflow-cx', 'index.js'),
|
||||||
|
output: {
|
||||||
|
banner: banner['banner.output'].join(''),
|
||||||
|
file: join(__dirname, 'lib', 'dialogflow-cx', 'index.cjs'),
|
||||||
|
format: 'cjs',
|
||||||
|
},
|
||||||
|
plugins: [commonjs()],
|
||||||
|
},
|
||||||
]
|
]
|
||||||
|
|||||||
121
packages/contexts/src/dialogflow-cx/dialogflow-cx.class.js
Normal file
121
packages/contexts/src/dialogflow-cx/dialogflow-cx.class.js
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
const { CoreClass } = require('@bot-whatsapp/bot')
|
||||||
|
const { SessionsClient } = require('@google-cloud/dialogflow-cx').v3beta1
|
||||||
|
const { existsSync, readFileSync } = require('fs')
|
||||||
|
const { join } = require('path')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Necesita extender de core.class
|
||||||
|
* handleMsg(messageInComming) // const { body, from } = messageInComming
|
||||||
|
*/
|
||||||
|
|
||||||
|
const GOOGLE_ACCOUNT_PATH = join(process.cwd(), 'google-key.json')
|
||||||
|
|
||||||
|
class DialogFlowCXContext extends CoreClass {
|
||||||
|
// Opciones del usuario
|
||||||
|
optionsDX = {
|
||||||
|
language: 'es',
|
||||||
|
location: '',
|
||||||
|
agentId: '',
|
||||||
|
}
|
||||||
|
projectId = null
|
||||||
|
configuration = null
|
||||||
|
sessionClient = null
|
||||||
|
|
||||||
|
constructor(_database, _provider, _optionsDX = {}) {
|
||||||
|
super(null, _database, _provider)
|
||||||
|
this.optionsDX = { ...this.optionsDX, ..._optionsDX }
|
||||||
|
this.init()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verificar conexión con servicio de DialogFlow
|
||||||
|
*/
|
||||||
|
init = () => {
|
||||||
|
if (!existsSync(GOOGLE_ACCOUNT_PATH)) {
|
||||||
|
console.log(`[ERROR]: No se encontro ${GOOGLE_ACCOUNT_PATH}`)
|
||||||
|
/**
|
||||||
|
* Emitir evento de error para que se mueste por consola dicinedo que no tiene el json
|
||||||
|
* */
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.optionsDX.location.length)
|
||||||
|
throw new Error('LOCATION_NO_ENCONTRADO')
|
||||||
|
if (!this.optionsDX.agentId.length)
|
||||||
|
throw new Error('AGENTID_NO_ENCONTRADO')
|
||||||
|
|
||||||
|
const rawJson = readFileSync(GOOGLE_ACCOUNT_PATH, 'utf-8')
|
||||||
|
const { project_id, private_key, client_email } = JSON.parse(rawJson)
|
||||||
|
|
||||||
|
this.projectId = project_id
|
||||||
|
|
||||||
|
this.sessionClient = new SessionsClient({
|
||||||
|
credentials: { private_key, client_email },
|
||||||
|
apiEndpoint: `${this.optionsDX.location}-dialogflow.googleapis.com`,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GLOSSARY.md
|
||||||
|
* @param {*} messageCtxInComming
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
handleMsg = async (messageCtxInComming) => {
|
||||||
|
const languageCode = this.optionsDX.language
|
||||||
|
const { from, body } = messageCtxInComming
|
||||||
|
|
||||||
|
let customPayload = {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📄 Creamos session de contexto basado en el numero de la persona
|
||||||
|
* para evitar este problema.
|
||||||
|
* https://github.com/codigoencasa/bot-whatsapp/pull/140
|
||||||
|
*/
|
||||||
|
|
||||||
|
const session = this.sessionClient.projectLocationAgentSessionPath(
|
||||||
|
this.projectId,
|
||||||
|
this.optionsDX.location,
|
||||||
|
this.optionsDX.agentId,
|
||||||
|
from
|
||||||
|
)
|
||||||
|
|
||||||
|
const reqDialog = {
|
||||||
|
session,
|
||||||
|
queryInput: {
|
||||||
|
text: {
|
||||||
|
text: body,
|
||||||
|
},
|
||||||
|
languageCode,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
const [single] = (await this.sessionClient.detectIntent(reqDialog)) || [
|
||||||
|
null,
|
||||||
|
]
|
||||||
|
|
||||||
|
const { queryResult } = single
|
||||||
|
|
||||||
|
const msgPayload = queryResult?.fulfillmentMessages?.find(
|
||||||
|
(a) => a.message === 'payload'
|
||||||
|
)
|
||||||
|
|
||||||
|
if (msgPayload && msgPayload?.payload) {
|
||||||
|
const { fields } = msgPayload.payload
|
||||||
|
const mapButtons = fields?.buttons?.listValue?.values.map((m) => {
|
||||||
|
return m?.structValue?.fields?.body?.stringValue
|
||||||
|
})
|
||||||
|
customPayload = {
|
||||||
|
media: fields?.media?.stringValue,
|
||||||
|
buttons: mapButtons,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const ctxFromDX = {
|
||||||
|
...customPayload,
|
||||||
|
answer: queryResult?.fulfillmentText,
|
||||||
|
}
|
||||||
|
|
||||||
|
this.sendFlow([ctxFromDX], from)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = DialogFlowCXContext
|
||||||
14
packages/contexts/src/dialogflow-cx/index.js
Normal file
14
packages/contexts/src/dialogflow-cx/index.js
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
const DialogCXFlowClass = require('./dialogflow-cx.class')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Crear instancia de clase Bot
|
||||||
|
* @param {*} args
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
const createBotDialog = async ({ database, provider }, _options) =>
|
||||||
|
new DialogCXFlowClass(database, provider, _options)
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
createBotDialog,
|
||||||
|
DialogCXFlowClass,
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user