forked from codigoencasa/builderbot
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a52aaa1
commit 4ec6f1e
Showing
11 changed files
with
228 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
CTX: Es el objeto que representa un mensaje, con opciones, id, ref | ||
messageInComming: Objeto entrante del provider {body, from,...} | ||
messageInComming: Objeto entrante del provider {body, from,to,...} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "@bot-whatsapp/contexts", | ||
"version": "0.0.1", | ||
"description": "", | ||
"main": "./lib/bundle.contexts.cjs", | ||
"files": [ | ||
"./lib/" | ||
], | ||
"exports": { | ||
"./mock": "./lib/mock/index.cjs", | ||
"./dialogflow": "./lib/dialogflow/index.cjs" | ||
}, | ||
"dependencies": { | ||
"@bot-whatsapp/bot": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const banner = require('../../config/banner.rollup.json') | ||
const commonjs = require('@rollup/plugin-commonjs') | ||
const { join } = require('path') | ||
|
||
module.exports = [ | ||
{ | ||
input: join(__dirname, 'src', 'mock', 'index.js'), | ||
output: { | ||
banner: banner['banner.output'].join(''), | ||
file: join(__dirname, 'lib', 'mock', 'index.cjs'), | ||
format: 'cjs', | ||
}, | ||
plugins: [commonjs()], | ||
}, | ||
{ | ||
input: join(__dirname, 'src', 'dialogflow', 'index.js'), | ||
output: { | ||
banner: banner['banner.output'].join(''), | ||
file: join(__dirname, 'lib', 'dialogflow', 'index.cjs'), | ||
format: 'cjs', | ||
}, | ||
plugins: [commonjs()], | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
const { CoreClass } = require('@bot-whatsapp/bot') | ||
const dialogflow = require('@google-cloud/dialogflow') | ||
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 DialogFlowContext extends CoreClass { | ||
projectId = null | ||
configuration = null | ||
sessionClient = null | ||
|
||
constructor(_database, _provider) { | ||
super(null, _database, _provider) | ||
} | ||
|
||
/** | ||
* Verificar conexión con servicio de DialogFlow | ||
*/ | ||
init = () => { | ||
if (!existsSync(GOOGLE_ACCOUNT_PATH)) { | ||
/** | ||
* Emitir evento de error para que se mueste por consola dicinedo que no tiene el json | ||
* */ | ||
} | ||
|
||
const rawJson = readFileSync(GOOGLE_ACCOUNT_PATH, 'utf-8') | ||
const { project_id, private_key, client_email } = JSON.parse(rawJson) | ||
|
||
this.projectId = project_id | ||
this.configuration = { | ||
credentials: { | ||
private_key, | ||
client_email, | ||
}, | ||
} | ||
|
||
this.sessionClient = new dialogflow.SessionsClient(this.configuration) | ||
} | ||
|
||
/** | ||
* GLOSSARY.md | ||
* @param {*} messageCtxInComming | ||
* @returns | ||
*/ | ||
handleMsg = async (messageCtxInComming) => { | ||
const languageCode = process.env.LANGUAGE || 'es' //????? language | ||
|
||
console.log('DEBUG:', messageCtxInComming) | ||
const { from, body } = messageCtxInComming // body: hola | ||
let media = null | ||
|
||
/** | ||
* 📄 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.projectAgentSessionPath( | ||
this.projectId, | ||
from | ||
) | ||
const requestDialog = { | ||
session, | ||
queryInput: { | ||
text: { | ||
text: body, | ||
languageCode, | ||
}, | ||
}, | ||
} | ||
|
||
const [singleResponse] = (await sessionClient.detectIntent( | ||
requestDialog | ||
)) || [null] | ||
|
||
const { queryResult } = singleResponse | ||
const { intent } = queryResult || { intent: {} } | ||
|
||
// const parseIntent = intent['displayName'] || null | ||
//****** HE LLEGADO A ESTE PUNTO */ | ||
//****** this.sendFlow(msgToSend, from) */ | ||
|
||
const parsePayload = queryResult['fulfillmentMessages'].find( | ||
(a) => a.message === 'payload' | ||
) | ||
// console.log(singleResponse) | ||
if (parsePayload && parsePayload.payload) { | ||
const { fields } = parsePayload.payload | ||
media = fields.media.stringValue || null | ||
} | ||
const customPayload = parsePayload ? parsePayload['payload'] : null | ||
|
||
const parseData = { | ||
replyMessage: queryResult.fulfillmentText, | ||
media, | ||
trigger: null, | ||
} | ||
|
||
// se tiene que enviar mensaje | ||
// msgToSend = [{options?.delay}] | ||
this.sendFlow(msgToSend, from) | ||
|
||
return parseData | ||
} | ||
} | ||
|
||
module.exports = DialogFlowContext |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const DialogFlowClass = require('./dialogflow.class') | ||
|
||
/** | ||
* Crear instancia de clase Bot | ||
* @param {*} args | ||
* @returns | ||
*/ | ||
const createBotDialog = async ({ flow, database, provider }) => | ||
new DialogFlowClass(flow, database, provider) | ||
|
||
module.exports = { | ||
createBotDialog, | ||
DialogFlowClass, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const MockClass = require('./mock.class') | ||
|
||
/** | ||
* Crear instancia de clase Bot | ||
* @param {*} args | ||
* @returns | ||
*/ | ||
const createBotMock = async ({ database, provider }) => | ||
new MockClass(database, provider) | ||
|
||
module.exports = { | ||
createBotMock, | ||
MockClass, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const { CoreClass } = require('@bot-whatsapp/bot') | ||
/** | ||
* Necesita extender de core.class | ||
* handleMsg(messageInComming) // const { body, from } = messageInComming | ||
*/ | ||
|
||
class MockContext extends CoreClass { | ||
constructor(_database, _provider) { | ||
super(null, _database, _provider) | ||
} | ||
|
||
init = () => {} | ||
|
||
/** | ||
* GLOSSARY.md | ||
* @param {*} messageCtxInComming | ||
* @returns | ||
*/ | ||
handleMsg = async ({ from, body }) => { | ||
console.log('DEBUG:', messageCtxInComming) | ||
} | ||
} | ||
|
||
module.exports = MockContext |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters