manta_bot/funtions.js

333 lines
12 KiB
JavaScript
Raw Normal View History

2021-05-14 17:58:59 -05:00
const { error } = require('actions-on-google/dist/common');
2025-12-15 11:56:08 -05:00
const axios = require('axios');
2021-07-07 14:49:54 -05:00
const URLIC = 'https://sistemasic.manta.gob.ec/';
2024-02-08 09:26:47 -05:00
const URLPC = 'https://portalciudadano.manta.gob.ec/';
2025-12-15 11:56:08 -05:00
const API_TOKEN_PORTAL = 'de135b313b7f3b944090754470ecb67ea52987a1559dc92c734273c55bccfe9d';
2021-06-11 18:12:45 -05:00
// const URLPC = 'https://portalciudadano.manta.gob.ec/';
2021-05-14 17:58:59 -05:00
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');
}
2021-07-07 14:49:54 -05:00
function conver_name(data, recep = null) {
2021-06-02 17:55:46 -05:00
try {
var porciones = data.split(' ');
2025-12-15 11:56:08 -05:00
if (porciones.length >= 2 && porciones.length <= 4) {
2021-06-02 17:55:46 -05:00
var cadena = new String(data);
2021-07-07 14:49:54 -05:00
var accion = Math.floor(Math.random() * (1 - 4)) + 4;
2025-12-15 11:56:08 -05:00
console.log('publico' + recep);
if (recep != null) { accion = recep; }
if (accion == 1) {
cadena = porciones[(porciones.length - 2)];
} else if (accion == 2) {
cadena = porciones[(porciones.length - 1)];
} else {
cadena = porciones[(porciones.length - 2)] + ' ' + porciones[(porciones.length - 1)];
2021-07-07 14:49:54 -05:00
}
2021-06-02 17:55:46 -05:00
cadena = cadena.toLowerCase();
return cadena.replace(/\w\S*/g, (w) => (w.replace(/^\w/, (c) => c.toUpperCase())));
2025-12-15 11:56:08 -05:00
} else {
2021-06-02 17:55:46 -05:00
var cadena = new String(data);
cadena = cadena.toLowerCase();
return cadena.replace(/\w\S*/g, (w) => (w.replace(/^\w/, (c) => c.toUpperCase())));
}
2025-12-15 11:56:08 -05:00
} catch (error) {
2021-06-02 17:55:46 -05:00
return data;
2021-05-14 17:58:59 -05:00
}
}
2021-06-02 17:55:46 -05:00
function conver_capitalice(data) {
var cadena = new String(data);
cadena = cadena.toLowerCase();
return cadena.replace(/\w\S*/g, (w) => (w.replace(/^\w/, (c) => c.toUpperCase())));
}
2021-05-14 17:58:59 -05:00
2025-12-15 11:56:08 -05:00
function AxiosPOST(url, params, token = null) {
2021-05-14 17:58:59 -05:00
return new Promise((resolve, reject) => {
2025-12-15 11:56:08 -05:00
axios.post(URLPC + url, params,
{
headers: {
Authorization: `Bearer ${token}`
}
}
).then(function (response) {
2021-05-14 17:58:59 -05:00
resolve(response.data);
}).catch(function (error) {
2025-12-15 11:56:08 -05:00
resolve({ 'estado': false, 'data': [], 'msg': error });
2021-07-07 14:49:54 -05:00
}).then(function () {
2025-12-15 11:56:08 -05:00
resolve({ 'estado': false, 'data': [], 'msg': 'Lo siento, no logre terminar de procesar la solicitúd' });
2021-05-14 17:58:59 -05:00
});
});
}
2025-12-15 11:56:08 -05:00
function AxiosGET(url, params, nweurl = null) {
if (nweurl == 'sistemasic') {
2021-07-07 14:49:54 -05:00
url = URLIC + url;
2025-12-15 11:56:08 -05:00
} else {
2021-07-07 14:49:54 -05:00
url = URLPC + url;
}
2021-06-02 17:55:46 -05:00
return new Promise((resolve, reject) => {
2021-07-07 14:49:54 -05:00
axios.get(url, params).then(function (response) {
2021-06-02 17:55:46 -05:00
resolve(response.data);
}).catch(function (error) {
2025-12-15 11:56:08 -05:00
resolve({ 'estado': false, 'data': [], 'msg': error });
2021-07-07 14:49:54 -05:00
}).then(function () {
2025-12-15 11:56:08 -05:00
resolve({ 'estado': false, 'data': [], 'msg': 'Lo siento, no logre terminar de procesar la solicitúd' });
2021-06-02 17:55:46 -05:00
});
});
}
2021-05-14 17:58:59 -05:00
2025-12-15 11:56:08 -05:00
async function ValidarSession(session) {
2021-05-14 17:58:59 -05:00
return new Promise((resolve, reject) => {
2025-12-15 11:56:08 -05:00
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'] }
2021-05-14 17:58:59 -05:00
resolve(array);
}).catch(function (error) {
array['msg'] = error;
resolve(array);
});
});
}
2025-12-15 11:56:08 -05:00
async function CerrarSession(session) {
2021-05-14 17:58:59 -05:00
return new Promise((resolve, reject) => {
2025-12-15 11:56:08 -05:00
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'] }
2021-05-14 17:58:59 -05:00
resolve(array);
}).catch(function (error) {
array['msg'] = error;
resolve(array);
});
});
}
2025-12-15 11:56:08 -05:00
async function RegistrarSession(session_id, cedula) {
2021-05-14 17:58:59 -05:00
return new Promise((resolve, reject) => {
2025-12-15 11:56:08 -05:00
var heders = { 'cedula': cedula, 'name': cedula, 'session_id': session_id };
var array = { 'ok': false, 'data': [], 'msg': null };
2021-05-14 17:58:59 -05:00
AxiosPOST('apidf_RegistrarSessionDialog', heders).then(function (result) {
2025-12-15 11:56:08 -05:00
if (result['estado']) {
array['ok'] = true; array['data'] = result['data']; array['msg'] = result['msg'];
} else { array['msg'] = result['msg'] }
2021-05-14 17:58:59 -05:00
resolve(array);
}).catch(function (error) {
array['msg'] = error;
resolve(array);
});
});
}
2025-12-15 11:56:08 -05:00
async function ValidarComprobante(codigo) {
2021-06-11 18:12:45 -05:00
return new Promise((resolve, reject) => {
2025-12-15 11:56:08 -05:00
var heders = { 'codigo': codigo };
var array = { 'ok': false, 'data': [], 'msg': null };
2021-06-11 18:12:45 -05:00
AxiosPOST('ws_validar_cod_barras', heders).then(function (result) {
2025-12-15 11:56:08 -05:00
let claves = Object.keys(result);
2021-06-11 18:12:45 -05:00
claves.forEach(element => {
2025-12-15 11:56:08 -05:00
if (element == 'api') {
if (result['api']['ok']) {
array['ok'] = true; array['data'] = result['api']; array['msg'] = 'Busquera exitosa';
} else { array['msg'] = result['api']['msg'] != undefined ? result['api']['msg'] : ['Documento no encontrado']; }
} else {
2021-06-11 18:12:45 -05:00
array['msg'] = ['Documento no encontrado'];
}
});
resolve(array);
}).catch(function (error) {
array['msg'] = error;
resolve(array);
});
});
}
2025-12-15 11:56:08 -05:00
async function ConsultaClaves(cedula, chang = null) {
2021-06-02 17:55:46 -05:00
return new Promise((resolve, reject) => {
2025-12-15 11:56:08 -05:00
var array = { 'ok': false, 'data': [], 'msg': null };
var _ur = 'consulta_predios_detalle/2/' + cedula + '?boot=true';
if (chang != null) {
var _ur = 'consulta_predios_detalle/1/' + cedula + '?boot=true';
2021-07-07 14:49:54 -05:00
}
2025-12-15 11:56:08 -05:00
AxiosPOST(_ur, {}, API_TOKEN_PORTAL).then(function (result) {
2024-02-09 14:46:17 -05:00
console.log(result);
2025-12-15 11:56:08 -05:00
if (result['ok']) {
array['ok'] = true;
2024-02-08 17:03:24 -05:00
array['data'] = result['data_bot'];
2025-12-15 11:56:08 -05:00
array['msg'] = 'Consulta exitosa procedura';
} else {
array['msg'] = result['message'];
2021-07-07 14:49:54 -05:00
}
2024-02-08 17:03:24 -05:00
// if(result['ok'] && result['data_bot']['data'].length > 0){
// array['ok'] = true; array['data'] = result['data_bot']['data']; array['msg'] = 'Consulta exitosa';
// }else{
// array['msg'] = 'el número de cédula '+cedula+' no posee predios catastrales registrados en el GAD MANTA';
// if(chang != null){
// array['msg'] = 'la clave catastral '+cedula+' no existe en los registros del GAD MANTA';
// }
// }
2021-06-02 17:55:46 -05:00
resolve(array);
}).catch(function (error) {
array['msg'] = error;
resolve(array);
});
});
}
2026-01-09 10:35:08 -05:00
const fs = require('fs');
async function ConsultaTasaRecoleccion(cedula, index) {
return new Promise((resolve) => {
const array = { ok: false, data: [], msg: null };
try {
const data = fs.readFileSync('trb.json', 'utf8');
const json = JSON.parse(data);
if (!Array.isArray(json)) {
array.msg = 'El JSON no es un arreglo';
return resolve(array);
}
const response = json.find(
item => String(item.documento).trim() === String(cedula).trim()
);
if (response) {
array.ok = true;
array.data = response;
array.msg = 'Consulta exitosa';
} else {
array.msg = 'No se encontraron valores disponibles';
}
resolve(array);
} catch (error) {
array.msg = error.message;
resolve(array);
}
});
}
2025-12-15 11:56:08 -05:00
async function ConsultaComprobantes(cedula, index) {
2021-06-11 18:12:45 -05:00
return new Promise((resolve, reject) => {
2025-12-15 11:56:08 -05:00
var array = { 'ok': false, 'data': [], 'msg': null };
AxiosGET('portalciudadano/serviciosdigitales/mis_pagos?index=' + index + '&cedula=' + cedula + '&keypass=U2lzdGVtYXNJQ1NlcnZpY2VfbXJj', {}).then(function (result) {
if (result['ok'] && result['data'].length > 0) {
array['ok'] = true; array['data'] = result['data']; array['msg'] = 'Consulta exitosa';
} else { array['msg'] = 'No se encontró comprobantes de pago a su nombre'; }
2021-06-11 18:12:45 -05:00
resolve(array);
}).catch(function (error) {
array['msg'] = error;
resolve(array);
});
});
}
2025-12-15 11:56:08 -05:00
async function ConsultaNoticias() {
2021-07-07 14:49:54 -05:00
return new Promise((resolve, reject) => {
2025-12-15 11:56:08 -05:00
var array = { 'ok': false, 'data': [], 'msg': null };
2021-07-07 14:49:54 -05:00
AxiosGET('consultarPublicaciones?boot=c', {}, 'sistemasic').then(function (result) {
2025-12-15 11:56:08 -05:00
if (result['data'].length > 0) {
array['ok'] = true; array['data'] = result['data']; array['msg'] = 'Consulta exitosa';
} else { array['msg'] = 'No se encontró resultados'; }
2021-07-07 14:49:54 -05:00
resolve(array);
}).catch(function (error) {
array['msg'] = error;
resolve(array);
});
});
}
2025-12-15 11:56:08 -05:00
async function OptInfoBot(key_inf) {
2021-06-11 18:12:45 -05:00
return new Promise((resolve, reject) => {
2025-12-15 11:56:08 -05:00
var heders = { 'info_key': key_inf };
var array = { 'ok': false, 'data': [], 'msg': null };
2021-06-11 18:12:45 -05:00
AxiosPOST('api/InfoBootService', heders).then(function (result) {
2025-12-15 11:56:08 -05:00
if (result['ok']) {
array['ok'] = true; array['data'] = result['data']; array['msg'] = result['msg'];
} else { array['msg'] = result['msg'] }
2021-06-11 18:12:45 -05:00
resolve(array);
}).catch(function (error) {
array['msg'] = error;
resolve(array);
});
});
}
2021-07-07 14:49:54 -05:00
//Consulta mis tramites solicitados
2025-12-15 11:56:08 -05:00
async function GetMisTramites(key_cedula) {
2021-07-07 14:49:54 -05:00
return new Promise((resolve, reject) => {
var heders = {};
2025-12-15 11:56:08 -05:00
var array = { 'ok': false, 'data': [], 'msg': null };
AxiosGET('tramites/web_api_consulta_tramite?tipo=1&code=true&valor=' + key_cedula, heders).then(function (result) {
if (result['estado']) {
array['ok'] = true; array['data'] = result['data']; array['msg'] = result['msg'];
} else { array['msg'] = result['msg'] }
2021-07-07 14:49:54 -05:00
resolve(array);
}).catch(function (error) {
array['msg'] = error;
resolve(array);
}).finally(function (find) {
array['msg'] = 'Lo siento, no logre procesar su solicitúd';
resolve(array);
});
});
}
//Consulta historial de tramites solicitados
2025-12-15 11:56:08 -05:00
async function GetHistorialTramites(key_cedula, index) {
2021-07-07 14:49:54 -05:00
return new Promise((resolve, reject) => {
var heders = {};
2025-12-15 11:56:08 -05:00
var array = { 'ok': false, 'data': [], 'tramite': null, 'msg': null };
AxiosGET('tramites/resolver_index_tramite?tipo=1&code=true&index=' + index + '&valor=' + key_cedula, heders).then(function (result) {
if (result['estado']) {
array['ok'] = true; array['data'] = result['data']; array['msg'] = result['msg']; array['tramite'] = result['tramite'];
} else { array['msg'] = result['msg'] }
2021-07-07 14:49:54 -05:00
resolve(array);
}).catch(function (error) {
array['msg'] = error;
resolve(array);
}).finally(function (find) {
array['msg'] = 'Lo siento, no logre procesar su solicitúd';
resolve(array);
});
});
}
2021-06-11 18:12:45 -05:00
2021-05-14 17:58:59 -05:00
module.exports = {
"conver_base64": conver_base64,
"conver_utf8": conver_utf8,
"AxiosPOST": AxiosPOST,
"RegistrarSession": RegistrarSession,
"ValidarSession": ValidarSession,
"conver_name": conver_name,
2021-06-02 17:55:46 -05:00
"CerrarSession": CerrarSession,
"conver_capitalice": conver_capitalice,
2021-06-11 18:12:45 -05:00
"ValidarComprobante": ValidarComprobante,
"ConsultaComprobantes": ConsultaComprobantes,
2026-01-09 10:35:08 -05:00
"ConsultaTasaRecoleccion": ConsultaTasaRecoleccion,
2021-06-11 18:12:45 -05:00
"OptInfoBot": OptInfoBot,
2021-07-07 14:49:54 -05:00
"ConsultaNoticias": ConsultaNoticias,
"GetMisTramites": GetMisTramites,
"GetHistorialTramites": GetHistorialTramites,
2021-06-02 17:55:46 -05:00
"ConsultaClaves": ConsultaClaves
2021-05-14 17:58:59 -05:00
}