UNPKG

node-red-contrib-vocalrec

Version:

Nodos personalizados de Node-RED para la API de VocalRec

62 lines (48 loc) 2.15 kB
const axios = require('axios'); const https = require('https'); module.exports = function(RED) { function VocalrecBookmarkNode(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 insertar el bookmark"); return; } const label = msg.label || config.label; if (!label) { node.error("Se requiere una etiqueta (label) para el bookmark"); return; } const url = `${node.server.base_url}/api/bookmark/${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: "insertando bookmark..." }); const httpsAgent = new https.Agent({ rejectUnauthorized: false }); axios.post(url, { label }, { headers,httpsAgent }) .then(response => { node.status({ fill: "green", shape: "dot", text: "bookmark insertado" }); msg.payload = response.data; node.send(msg); }) .catch(error => { node.status({ fill: "red", shape: "ring", text: "error" }); node.error(`Error al insertar bookmark: ${error.message}`, msg); }); }); } RED.nodes.registerType("vocalrec-bookmark", VocalrecBookmarkNode); };