mirror of
https://github.com/cheveguerra/botGuna.git
synced 2026-04-17 19:37:07 +00:00
add templates from main
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
const dialogflow = require('@google-cloud/dialogflow');
|
||||
const fs = require('fs')
|
||||
const { nanoid } = require('nanoid')
|
||||
const {struct} = require('pb-util');
|
||||
|
||||
/**
|
||||
* Debes de tener tu archivo con el nombre "chatbot-account.json" en la raíz del proyecto
|
||||
*/
|
||||
@@ -30,9 +31,10 @@ const checkFileCredentials = () => {
|
||||
|
||||
|
||||
// Detect intent method
|
||||
const detectIntent = async (queryText) => {
|
||||
const detectIntent = async (queryText, waPhoneNumber) => {
|
||||
let media = null;
|
||||
const sessionId = KEEP_DIALOG_FLOW ? 1 : nanoid();
|
||||
let actions = null;
|
||||
const sessionId = KEEP_DIALOG_FLOW ? 1 : waPhoneNumber;
|
||||
const sessionPath = sessionClient.projectAgentSessionPath(PROJECID, sessionId);
|
||||
const languageCode = process.env.LANGUAGE
|
||||
const request = {
|
||||
@@ -54,24 +56,26 @@ const detectIntent = async (queryText) => {
|
||||
// console.log(singleResponse)
|
||||
if (parsePayload && parsePayload.payload) {
|
||||
const { fields } = parsePayload.payload
|
||||
actions = struct.decode(fields.actions.structValue) || null;
|
||||
media = fields.media.stringValue || null
|
||||
}
|
||||
const customPayload = parsePayload['payload']
|
||||
const customPayload = parsePayload ? parsePayload['payload'] : null
|
||||
|
||||
const parseData = {
|
||||
replyMessage: queryResult.fulfillmentText,
|
||||
media,
|
||||
actions,
|
||||
trigger: null
|
||||
}
|
||||
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 }
|
||||
103
adapter/gdrive,.js
Normal file
103
adapter/gdrive,.js
Normal file
@@ -0,0 +1,103 @@
|
||||
require('dotenv').config({ path: `${__dirname}/../.env` });
|
||||
const { google } = require('googleapis');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
//const clientEmail = require(`${__dirname}/../chatbot-account.json`);
|
||||
|
||||
/**
|
||||
* La funcion 'generatePublicUrl' genera un error muy menor al enviar el 'requestBody'
|
||||
* siempre y cuando necesites que el acceso sea restringido y solo ciertos usuarios puedan acceder.
|
||||
* Esto se logra con la combinacion requerida: 'reader', 'user' y 'emailAddress':
|
||||
* requestBody: {
|
||||
* role: 'reader',
|
||||
* type: 'user',
|
||||
* emailAddress: usuario@gmail.com,
|
||||
* },
|
||||
* Segun la documentacion https://developers.google.com/drive/api/v3/reference/permissions/create#request-body,
|
||||
* los datos se envian correctamente, pero la respuesta del API regresa este error:
|
||||
* Bad Request. User message: "You cannot share this item because it has been flagged as inappropriate."
|
||||
* Al parecer, es un error conocido en stackoverflow.com entre varios usuarios del API.
|
||||
*/
|
||||
|
||||
if (process.env.DATABASE === 'dialogflow') {
|
||||
|
||||
/**
|
||||
* Debes de tener tu archivo con el nombre "chatbot-account.json" en la raíz del proyecto
|
||||
*/
|
||||
|
||||
const KEYFILEPATH = path.join(`${__dirname}/../chatbot-account.json`);
|
||||
const SCOPES = ['https://www.googleapis.com/auth/drive'];
|
||||
|
||||
const auth = new google.auth.GoogleAuth({
|
||||
keyFile: KEYFILEPATH,
|
||||
scopes: SCOPES,
|
||||
});
|
||||
|
||||
const drive = google.drive({
|
||||
version: 'v3',
|
||||
auth,
|
||||
});
|
||||
|
||||
const uploadSingleFile = async (fileName, filePath) => {
|
||||
const folderId = process.env.GDRIVE_FOLDER_ID;
|
||||
const { data: { id, name } = {} } = await drive.files.create({
|
||||
resource: {
|
||||
name: fileName,
|
||||
parents: [folderId],
|
||||
},
|
||||
media: {
|
||||
mimeType: 'image/jpg',
|
||||
body: fs.createReadStream(filePath),
|
||||
},
|
||||
fields: 'id,name',
|
||||
});
|
||||
generatePublicUrl(id).then(() => {
|
||||
console.log(`Se generó enlace https://drive.google.com/open?id=${id} para el archivo ${name}`);
|
||||
});
|
||||
return `https://drive.google.com/open?id=${id}`
|
||||
};
|
||||
|
||||
const scanFolderForFiles = async (folderPath) => {
|
||||
const folder = await fs.promises.opendir(folderPath);
|
||||
for await (const dirent of folder) {
|
||||
if (dirent.isFile() && dirent.name.endsWith('.jpeg')) {
|
||||
await uploadSingleFile(dirent.name, path.join(folderPath, dirent.name));
|
||||
await fs.promises.rm(filePath);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
async function generatePublicUrl(id) {
|
||||
try {
|
||||
const fileId = id;
|
||||
await drive.permissions.create({
|
||||
fileId: fileId,
|
||||
supportsAllDrives: true,
|
||||
requestBody: {
|
||||
role: 'reader',
|
||||
type: 'domain', // 'anyone' da acceso al publico vía enlace https://drive.google.com...
|
||||
domain: 'gserviceaccount.com', // Si tu cuenta esta bajo un dominio (usuario@empresa.com) y no bajo gmail.com
|
||||
allowFileDiscovery: false,
|
||||
},
|
||||
});
|
||||
|
||||
/*
|
||||
webViewLink: Ver el archivo en el navegador
|
||||
webContentLink: Enlace de descarga directa
|
||||
*/
|
||||
const result = await drive.files.get({
|
||||
fileId: fileId,
|
||||
fields: 'webViewLink, webContentLink',
|
||||
});
|
||||
console.log(result.data);
|
||||
} catch (error) {
|
||||
//console.log(error.message); // Imprime 'Internal Error', pero aún así genera el enlace
|
||||
console.error = () => { }; // No muestra el error anterior
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { uploadSingleFile, scanFolderForFiles }
|
||||
|
||||
} else {
|
||||
console.log(`Actualmente, la base de datos es:\n\t'DATABASE=${process.env.DATABASE}'\nPara usar Google Drive, cambiar a:\n\t'DATABASE=dialogflow'`);
|
||||
}
|
||||
103
adapter/gdrive.js
Normal file
103
adapter/gdrive.js
Normal file
@@ -0,0 +1,103 @@
|
||||
require('dotenv').config({ path: `${__dirname}/../.env` });
|
||||
const { google } = require('googleapis');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
//const clientEmail = require(`${__dirname}/../chatbot-account.json`);
|
||||
|
||||
/**
|
||||
* La funcion 'generatePublicUrl' genera un error muy menor al enviar el 'requestBody'
|
||||
* siempre y cuando necesites que el acceso sea restringido y solo ciertos usuarios puedan acceder.
|
||||
* Esto se logra con la combinacion requerida: 'reader', 'user' y 'emailAddress':
|
||||
* requestBody: {
|
||||
* role: 'reader',
|
||||
* type: 'user',
|
||||
* emailAddress: usuario@gmail.com,
|
||||
* },
|
||||
* Segun la documentacion https://developers.google.com/drive/api/v3/reference/permissions/create#request-body,
|
||||
* los datos se envian correctamente, pero la respuesta del API regresa este error:
|
||||
* Bad Request. User message: "You cannot share this item because it has been flagged as inappropriate."
|
||||
* Al parecer, es un error conocido en stackoverflow.com entre varios usuarios del API.
|
||||
*/
|
||||
|
||||
if (process.env.DATABASE === 'dialogflow') {
|
||||
|
||||
/**
|
||||
* Debes de tener tu archivo con el nombre "chatbot-account.json" en la raíz del proyecto
|
||||
*/
|
||||
|
||||
const KEYFILEPATH = path.join(`${__dirname}/../chatbot-account.json`);
|
||||
const SCOPES = ['https://www.googleapis.com/auth/drive'];
|
||||
|
||||
const auth = new google.auth.GoogleAuth({
|
||||
keyFile: KEYFILEPATH,
|
||||
scopes: SCOPES,
|
||||
});
|
||||
|
||||
const drive = google.drive({
|
||||
version: 'v3',
|
||||
auth,
|
||||
});
|
||||
|
||||
const uploadSingleFile = async (fileName, filePath) => {
|
||||
const folderId = process.env.GDRIVE_FOLDER_ID;
|
||||
const { data: { id, name } = {} } = await drive.files.create({
|
||||
resource: {
|
||||
name: fileName,
|
||||
parents: [folderId],
|
||||
},
|
||||
media: {
|
||||
mimeType: 'image/jpg',
|
||||
body: fs.createReadStream(filePath),
|
||||
},
|
||||
fields: 'id,name',
|
||||
});
|
||||
generatePublicUrl(id).then(() => {
|
||||
console.log(`Se generó enlace https://drive.google.com/open?id=${id} para el archivo ${name}`);
|
||||
});
|
||||
return `https://drive.google.com/open?id=${id}`
|
||||
};
|
||||
|
||||
const scanFolderForFiles = async (folderPath) => {
|
||||
const folder = await fs.promises.opendir(folderPath);
|
||||
for await (const dirent of folder) {
|
||||
if (dirent.isFile() && dirent.name.endsWith('.jpeg')) {
|
||||
await uploadSingleFile(dirent.name, path.join(folderPath, dirent.name));
|
||||
await fs.promises.rm(filePath);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
async function generatePublicUrl(id) {
|
||||
try {
|
||||
const fileId = id;
|
||||
await drive.permissions.create({
|
||||
fileId: fileId,
|
||||
supportsAllDrives: true,
|
||||
requestBody: {
|
||||
role: 'reader',
|
||||
type: 'domain', // 'anyone' da acceso al publico vía enlace https://drive.google.com...
|
||||
domain: 'gserviceaccount.com', // Si tu cuenta esta bajo un dominio (usuario@empresa.com) y no bajo gmail.com
|
||||
allowFileDiscovery: false,
|
||||
},
|
||||
});
|
||||
|
||||
/*
|
||||
webViewLink: Ver el archivo en el navegador
|
||||
webContentLink: Enlace de descarga directa
|
||||
*/
|
||||
const result = await drive.files.get({
|
||||
fileId: fileId,
|
||||
fields: 'webViewLink, webContentLink',
|
||||
});
|
||||
console.log(result.data);
|
||||
} catch (error) {
|
||||
//console.log(error.message); // Imprime 'Internal Error', pero aún así genera el enlace
|
||||
console.error = () => { }; // No muestra el error anterior
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { uploadSingleFile, scanFolderForFiles }
|
||||
|
||||
} else {
|
||||
console.log(`Actualmente, la base de datos es:\n\t'DATABASE=${process.env.DATABASE}'\nPara usar Google Drive, cambiar a:\n\t'DATABASE=dialogflow'`);
|
||||
}
|
||||
Reference in New Issue
Block a user