Skip to content

Commit

Permalink
Added file checker
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoEstevo committed Nov 12, 2023
1 parent 9e31174 commit 6caee26
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions packages/contexts/src/dialogflow-cx/dialogflow-cx.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@ 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
*/
//Darle al usuario la opcion de no publicar sus claves de Google y pasarlas como variable de entorno
let GOOGLE_ACCOUNT_PATH = join(process.cwd(), 'google-key.json');

if (!fs.existsSync(googleKeyFilePath)) {
GOOGLE_ACCOUNT_PATH = JSON.parse(process.env.GOOGLE_KEY_JSON);
}

class DialogFlowCXContext extends CoreClass {
// Opciones del usuario
optionsDX = {
Expand All @@ -22,7 +11,6 @@ class DialogFlowCXContext extends CoreClass {
agentId: '',
}
projectId = null
configuration = null
sessionClient = null

constructor(_database, _provider, _optionsDX = {}) {
Expand All @@ -35,27 +23,33 @@ class DialogFlowCXContext extends CoreClass {
* 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
* */
let credentials;
const googleKeyFilePath = join(process.cwd(), 'google-key.json');

if (existsSync(googleKeyFilePath)) {
const rawJson = readFileSync(googleKeyFilePath, 'utf-8');
credentials = JSON.parse(rawJson);
} else if (process.env.GOOGLE_KEY_JSON) {
credentials = JSON.parse(process.env.GOOGLE_KEY_JSON);
} else {
throw new Error('Google key configuration not found');
}

if (!this.optionsDX.location.length) throw new Error('LOCATION_NO_ENCONTRADO')
if (!this.optionsDX.agentId.length) throw new Error('AGENTID_NO_ENCONTRADO')
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)
const { project_id, private_key, client_email } = credentials;

this.projectId = project_id
this.projectId = project_id;

this.sessionClient = new SessionsClient({
credentials: { private_key, client_email },
apiEndpoint: `${this.optionsDX.location}-dialogflow.googleapis.com`,
})
});
}



/**
* GLOSSARY.md
* @param {*} messageCtxInComming
Expand Down

0 comments on commit 6caee26

Please sign in to comment.