node-red-contrib-vocalrec
Version:
Nodos personalizados de Node-RED para la API de VocalRec
64 lines (48 loc) • 2.29 kB
JavaScript
const axios = require('axios');
const https = require('https');
module.exports = function(RED) {
function VocalrecAliasNode(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("No se ha configurado el servidor VocalRec");
return;
}
const transactionId = msg.transactionId || config.transactionId;
if (!transactionId) {
node.error("Se requiere un ID de transacción para establecer el alias de la grabación");
return;
}
const alias = msg.payload.alias || config.alias;
if (!alias) {
node.error("Se requiere un alias para establecer en la grabación");
return;
}
const url = `${node.server.base_url}/api/alias/${transactionId}`;
const headers = {
'Content-Type': 'application/json'
};
if (node.server.credentials && node.server.credentials.token) {
headers['Authorization'] = `Bearer ${node.server.credentials.token}`;
}
node.status({ fill: "blue", shape: "dot", text: "estableciendo alias grabación..." });
const httpsAgent = new https.Agent({
rejectUnauthorized: false
});
axios.post(url, { value:alias }, { headers,httpsAgent })
.then(response => {
node.status({ fill: "green", shape: "dot", text: "alias establecido" });
msg.payload = response.data;
node.send(msg);
})
.catch(error => {
node.status({ fill: "red", shape: "ring", text: "error" });
node.error(`Error al establecer el alias: ${error.message}`, msg);
});
});
}
RED.nodes.registerType("vocalrec-alias", VocalrecAliasNode);
};