1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00
node-red-nodes/social/nma/57-nma.js

46 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

module.exports = function(RED) {
"use strict";
var nma = require('nma');
function NMANode(n) {
RED.nodes.createNode(this,n);
this.title = n.title;
var credentials = this.credentials;
if ((credentials) && (credentials.hasOwnProperty("pushkey"))) { this.pushkey = credentials.pushkey; }
else { this.error("No NMA API key set"); }
var node = this;
this.on("input",function(msg) {
var titl = this.title||msg.topic||"Node-RED";
if (typeof(msg.payload) === 'object') {
msg.payload = JSON.stringify(msg.payload);
}
else {
msg.payload = msg.payload.toString();
}
if (node.pushkey) {
nma({
2014-12-13 00:15:09 +01:00
"apikey": node.pushkey,
"application": "Node-RED",
"event": titl,
"description": msg.payload,
"priority": 0
}, function (error) {
if (error) {
node.error("NMA error: " + error,msg);
2014-12-13 00:15:09 +01:00
}
});
}
else {
node.warn("NMA credentials not set.");
}
});
}
RED.nodes.registerType("nma",NMANode, {
credentials: {
pushkey: {type: "password"}
}
});
}