const { error } = require('actions-on-google/dist/common'); const axios = require('axios'); const URLIC = 'https://sistemasic.manta.gob.ec/'; const URLPC = 'https://portalciudadano.manta.gob.ec/'; const API_TOKEN_PORTAL = 'de135b313b7f3b944090754470ecb67ea52987a1559dc92c734273c55bccfe9d'; // const URLPC = 'https://portalciudadano.manta.gob.ec/'; 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, recep = null) { try { var porciones = data.split(' '); if (porciones.length >= 2 && porciones.length <= 4) { var cadena = new String(data); var accion = Math.floor(Math.random() * (1 - 4)) + 4; 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)]; } 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()))); } } catch (error) { return data; } } 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()))); } function AxiosPOST(url, params, token = null) { return new Promise((resolve, reject) => { axios.post(URLPC + url, params, { headers: { Authorization: `Bearer ${token}` } } ).then(function (response) { resolve(response.data); }).catch(function (error) { resolve({ 'estado': false, 'data': [], 'msg': error }); }).then(function () { resolve({ 'estado': false, 'data': [], 'msg': 'Lo siento, no logre terminar de procesar la solicitúd' }); }); }); } function AxiosGET(url, params, nweurl = null) { if (nweurl == 'sistemasic') { url = URLIC + url; } else { url = URLPC + url; } return new Promise((resolve, reject) => { axios.get(url, params).then(function (response) { resolve(response.data); }).catch(function (error) { resolve({ 'estado': false, 'data': [], 'msg': error }); }).then(function () { resolve({ 'estado': false, 'data': [], 'msg': 'Lo siento, no logre terminar de procesar la solicitúd' }); }); }); } 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': String(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); }); }); } async function ValidarComprobante(codigo) { return new Promise((resolve, reject) => { var heders = { 'codigo': codigo }; var array = { 'ok': false, 'data': [], 'msg': null }; AxiosPOST('ws_validar_cod_barras', heders).then(function (result) { let claves = Object.keys(result); claves.forEach(element => { 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 { array['msg'] = ['Documento no encontrado']; } }); resolve(array); }).catch(function (error) { array['msg'] = error; resolve(array); }); }); } async function ConsultaClaves(cedula, chang = null) { return new Promise((resolve, reject) => { 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'; } AxiosPOST(_ur, {'api_token' : API_TOKEN_PORTAL}, API_TOKEN_PORTAL).then(function (result) { if (result['ok']) { array['ok'] = true; array['data'] = result['data_bot']; array['msg'] = 'Consulta exitosa procedura'; } else { array['msg'] = result['message']; } resolve(array); }).catch(function (error) { array['msg'] = error; resolve(array); }); }); } async function ConsultaTasaRecoleccion(documento, type = 2) { return new Promise((resolve, reject) => { var array = { 'ok': false, 'data': [], 'msg': 'Consulta exitosa' }; var _ur = 'consulta_trb'; let params = { boot: true, tipo_doc: type, api_token : API_TOKEN_PORTAL, valor: documento, } AxiosPOST(_ur, params, API_TOKEN_PORTAL).then(function (result) { if(result.length > 0){ array['ok'] = true; array['data'] = result; }else{ array['msg'] = 'No se encontrarón valores pendientes de pago' } resolve(array); }).catch(function (error) { array['msg'] = error; resolve(array); }); }); } // const fs = require('fs'); // async function ConsultaTasaRecoleccion(cedula, index) { // return new Promise((resolve) => { // const result = { ok: false, data: [], msg: null }; // try { // const data = fs.readFileSync('trb.json', 'utf8'); // const json = JSON.parse(data); // if (!Array.isArray(json)) { // result.msg = 'El JSON no es un arreglo'; // return resolve(result); // } // const response = json.filter( // item => String(item.documento).trim() === String(cedula).trim() // ); // if (response.length > 0) { // result.ok = true; // result.data = response; // ← ahora es un array // result.msg = 'Consulta exitosa'; // } else { // result.msg = 'No se encontraron valores disponibles'; // } // resolve(result); // } catch (error) { // result.msg = error.message; // resolve(result); // } // }); // } async function ConsultaComprobantes(cedula, index) { return new Promise((resolve, reject) => { 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'; } resolve(array); }).catch(function (error) { array['msg'] = error; resolve(array); }); }); } async function ConsultaNoticias() { return new Promise((resolve, reject) => { var array = { 'ok': false, 'data': [], 'msg': null }; AxiosGET('consultarPublicaciones?boot=c', {}, 'sistemasic').then(function (result) { if (result['data'].length > 0) { array['ok'] = true; array['data'] = result['data']; array['msg'] = 'Consulta exitosa'; } else { array['msg'] = 'No se encontró resultados'; } resolve(array); }).catch(function (error) { array['msg'] = error; resolve(array); }); }); } async function OptInfoBot(key_inf) { return new Promise((resolve, reject) => { var heders = { 'info_key': key_inf, 'api_token' : API_TOKEN_PORTAL }; var array = { 'ok': false, 'data': [], 'msg': null }; AxiosPOST('api/InfoBootService', heders, API_TOKEN_PORTAL).then(function (result) { if (result['ok']) { 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); }); }); } //Consulta mis tramites solicitados async function GetMisTramites(key_cedula) { return new Promise((resolve, reject) => { var heders = {}; 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'] } 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 async function GetHistorialTramites(key_cedula, index) { return new Promise((resolve, reject) => { var heders = {}; 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'] } 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); }); }); } module.exports = { "conver_base64": conver_base64, "conver_utf8": conver_utf8, "AxiosPOST": AxiosPOST, "RegistrarSession": RegistrarSession, "ValidarSession": ValidarSession, "conver_name": conver_name, "CerrarSession": CerrarSession, "conver_capitalice": conver_capitalice, "ValidarComprobante": ValidarComprobante, "ConsultaComprobantes": ConsultaComprobantes, "ConsultaTasaRecoleccion": ConsultaTasaRecoleccion, "OptInfoBot": OptInfoBot, "ConsultaNoticias": ConsultaNoticias, "GetMisTramites": GetMisTramites, "GetHistorialTramites": GetHistorialTramites, "ConsultaClaves": ConsultaClaves }