node-red-contrib-dialoqbase
Version:
Node-RED node for dialoqbase
96 lines (89 loc) • 3.31 kB
JavaScript
const axios = require("axios")
module.exports = function (RED) {
function FunctionNode(n) {
RED.nodes.createNode(this, n);
if (RED.nodes.getNode(n.creds)){
this.dialoqbase_url = RED.nodes.getNode(n.creds).credentials.dialoqbase_url;
this.api_key = RED.nodes.getNode(n.creds).credentials.api_key;
} else {
this.dialoqbase_url = "";
this.api_key = "";
}
var node = this;
this.name = n.name;
for (var key in n) {
node[key] = n[key] || "";
}
this.on('input', function (msg) {
for (var i in msg) {
if (i !== 'req' | i !== 'res' | i !== 'payload' | i !== 'send' | i !== '_msgid') {
node[i] = msg[i] || node[i];
}
}
if(!node.url){
if(node.api){
node.url = this.dialoqbase_url + node.api.toLowerCase() +'';
}else{
node.url = this.dialoqbase_url + '/bot';
}
if(node.method){
}else{
node.method = 'get';
}
}
node.error(node.url);
node.options = {};
node.options.headers = {};
node.options.headers['Content-Type'] = 'application/json';
node.options.headers['Authorization'] = '' + this.api_key;
node.error(node.method);
if(node.method === 'get'){
node.options.params = node.params;
axios.get(node.url,node.options)
.then(function (response){
msg.payload = response.data;
node.send(msg);
}).catch(function (error){
if (error.response) {
msg.payload = error.response.data;
node.send(msg);
}else {
msg.payload = error;
node.send(msg);
}
});
}else{
axios[node.method](node.url, node.params, node.options)
.then(function (response){
msg.payload = response.data;
node.send(msg);
}).catch(function (error){
if (error.response) {
msg.payload = error.response.data;
node.send(msg);
}else {
msg.payload = error;
node.send(msg);
}
});
}
});
}
RED.nodes.registerType("dialoqbase", FunctionNode, {
credentials: {
dialoqbase_url: {type:"text"},
api_key: {type:"text"}
}
});
function dialoqbaseApiKey(n){
RED.nodes.createNode(this, n);
this.dialoqbase_url = n.dialoqbase_url;
this.api_key = n.api_key;
}
RED.nodes.registerType("dialoqbaseApiKey", dialoqbaseApiKey,{
credentials: {
dialoqbase_url: {type:"text"},
api_key: {type:"text"}
}
});
};