UNPKG

node-red-contrib-vocalrec

Version:

Nodos personalizados de Node-RED para la API de VocalRec

67 lines (49 loc) 2.28 kB
const axios = require('axios'); const https = require('https'); module.exports = function(RED) { function VocalrecStartRecNode(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 url = `${node.server.base_url}/api/startRecEx`; const call_id = msg.callId || config.callId; if (!call_id) { node.error("Se requiere un ID de llamada para iniciar la grabación"); return; } const body = { expression: `\${call_id}="${call_id}"` }; if (msg.program_id !== undefined && msg.program_id !== null) { body.ProgramId = msg.program_id; } 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: "iniciando grabación..." }); const httpsAgent = new https.Agent({ rejectUnauthorized: false }); axios.post(url, body, { headers, httpsAgent }) .then(response => { node.status({ fill: "green", shape: "dot", text: "grabación iniciada" }); msg.payload = response.data; node.send(msg); }) .catch(error => { node.status({ fill: "red", shape: "ring", text: "error" }); node.error(`Error al iniciar grabación: ${error.message}`, msg); }); }); } RED.nodes.registerType("vocalrec-start-rec", VocalrecStartRecNode); };