manta_bot/funtions.js
Victor Sanchez Sosa fd7abe68a5 inicia
2021-05-14 17:58:59 -05:00

104 lines
3.4 KiB
JavaScript

const { error } = require('actions-on-google/dist/common');
const axios= require('axios');
const URLPC = 'http://mantaentusmanos.test/';
const { WebhookClient } = require("dialogflow-fulfillment");
function conver_base64(data) {
return Buffer.from(data).toString('base64')
}
function conver_utf8(data) {
return Buffer.from(data, 'base64').toString('ascii');
}
function conver_name(data) {
var porciones = data.split(' ');
if(porciones.length >= 2 && porciones.length <= 4){
var cadena = new String(data);
cadena = porciones[(porciones.length -2)]+ ' ' +porciones[(porciones.length -1)];
cadena = cadena.toLowerCase();
return cadena.replace(/\w\S*/g, (w) => (w.replace(/^\w/, (c) => c.toUpperCase())));
}else{
var cadena = new String(data);
cadena = cadena.toLowerCase();
return cadena.replace(/\w\S*/g, (w) => (w.replace(/^\w/, (c) => c.toUpperCase())));
}
}
function AxiosPOST(url, params){
return new Promise((resolve, reject) => {
axios.post(URLPC+url, params).then(function (response) {
resolve(response.data);
}).catch(function (error) {
resolve({'estado':false, 'data':[], 'msg':error});
});
});
}
async function ValidarSession(session){
return new Promise((resolve, reject) => {
var array = {'ok': false, 'data':[], 'msg':null};
AxiosPOST('apidf_ValidarSessionDialog', {'session_id':session}).then(function (result) {
if(result['estado']){
if(result['data']['session_id'] != undefined && result['data']['session_id'] != null){
array['ok'] = true; array['data'] = result['data']; array['msg'] = result['msg'];
}else{ array['msg'] = result['msg'] }
}else{ array['msg'] = result['msg'] }
resolve(array);
}).catch(function (error) {
array['msg'] = error;
resolve(array);
});
});
}
async function CerrarSession(session){
return new Promise((resolve, reject) => {
var array = {'ok': false, 'data':[], 'msg':null};
AxiosPOST('apidf_CerrarSessionDialog', {'session_id':session}).then(function (result) {
if(result['estado']){
array['ok'] = true; array['msg'] = result['msg'];
}else{ array['msg'] = result['msg'] }
resolve(array);
}).catch(function (error) {
array['msg'] = error;
resolve(array);
});
});
}
async function RegistrarSession(session_id, cedula){
return new Promise((resolve, reject) => {
var heders = {'cedula': cedula, 'name':cedula, 'session_id':session_id};
var array = {'ok': false, 'data':[], 'msg':null};
AxiosPOST('apidf_RegistrarSessionDialog', heders).then(function (result) {
if(result['estado']){
array['ok'] = true; array['data'] = result['data']; array['msg'] = result['msg'];
}else{ array['msg'] = result['msg'] }
resolve(array);
}).catch(function (error) {
array['msg'] = error;
resolve(array);
});
});
}
module.exports = {
"conver_base64": conver_base64,
"conver_utf8": conver_utf8,
"AxiosPOST": AxiosPOST,
"RegistrarSession": RegistrarSession,
"ValidarSession": ValidarSession,
"conver_name": conver_name,
"CerrarSession": CerrarSession
}