node-red-contrib-vocalrec
Version:
Nodos personalizados de Node-RED para la API de VocalRec
53 lines (41 loc) • 1.84 kB
JavaScript
const axios = require('axios');
const https = require('https');
module.exports = function(RED) {
function VocalrecGetUrlNode(config) {
RED.nodes.createNode(this, config);
this.server = RED.nodes.getNode(config.server);
const node = this;
node.status({ fill: "grey", shape: "ring", text: "inactivo" });
node.on('input', function(msg) {
if (!node.server) {
node.error("Servidor VocalRec no configurado");
return;
}
const transactionId = msg.transactionId || config.transactionId;
if (!transactionId) {
node.error("Se requiere un Transaction ID");
return;
}
const url = `${node.server.base_url}/api/getUrlAudioTransaccion/${transactionId}`;
const headers = {};
if (node.server.credentials && node.server.credentials.token) {
headers['Authorization'] = `Bearer ${node.server.credentials.token}`;
}
node.status({ fill: "blue", shape: "dot", text: "consultando..." });
const httpsAgent = new https.Agent({
rejectUnauthorized: false
});
axios.get(url, { headers,httpsAgent })
.then(response => {
node.status({ fill: "green", shape: "dot", text: "URL recibida" });
msg.payload = response.data;
node.send(msg);
})
.catch(error => {
node.status({ fill: "red", shape: "ring", text: "error" });
node.error(`Error al obtener la URL de audio: ${error.message}`, msg);
});
});
}
RED.nodes.registerType("vocalrec-audio-url", VocalrecGetUrlNode);
};