Skip to content

Commit ca0b01d

Browse files
damoclarkdceejay
authored andcommitted
Pushover image attachment feature (#509)
* Bugfix: node defaults not applying to messages * Implemented pushover attachment feature
1 parent 73e4ae5 commit ca0b01d

File tree

4 files changed

+61
-16
lines changed

4 files changed

+61
-16
lines changed

social/pushover/57-pushover.html

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<p><code>msg.topic</code>: set the title</p>
8080
<p><code>msg.device</code>: set the device</p>
8181
<p><code>msg.priority</code>: set the priority</p>
82+
<p><code>msg.attachment</code>: attach an image (Buffer or file path)</p>
8283
<p><code>msg.url</code>: to add a web address</p>
8384
<p><code>msg.url_title</code>: to add a url title if not already set in the properties</p>
8485
<p><code>msg.sound</code>: set the notification sound, <i><a href="https://pushover.net/api#sounds" target="_new">see the available options</a></i></p>

social/pushover/57-pushover.js

+49-13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = function(RED) {
33
"use strict";
44
var PushOver = require('pushover-notifications');
55
var util = require('util');
6+
var fs = require('fs');
67

78
function PushoverNode(n) {
89
RED.nodes.createNode(this,n);
@@ -30,13 +31,14 @@ module.exports = function(RED) {
3031
var node = this;
3132

3233
this.on("input",function(msg) {
33-
var titl = this.title || msg.topic || "Node-RED";
34-
var pri = this.priority || msg.priority || 0;
35-
var dev = this.device || msg.device;
36-
var sound = this.sound || msg.sound || null;
37-
var url = this.url || msg.url || null;
38-
var url_title = this.url_title || msg.url_title || null;
39-
var html = this.html || false;
34+
var title = node.title || msg.topic || "Node-RED";
35+
var pri = node.priority || msg.priority || 0;
36+
var dev = node.device || msg.device;
37+
var sound = node.sound || msg.sound || null;
38+
var url = node.url || msg.url || null;
39+
var url_title = node.url_title || msg.url_title || null;
40+
var html = node.html || false;
41+
var attachment = msg.attachment || null;
4042
if (isNaN(pri)) {pri=0;}
4143
if (pri > 2) {pri = 2;}
4244
if (pri < -2) {pri = -2;}
@@ -47,7 +49,7 @@ module.exports = function(RED) {
4749
if (pusher) {
4850
var pushmsg = {
4951
message: msg.payload,
50-
title: titl,
52+
title: title,
5153
priority: pri,
5254
retry: 30,
5355
expire: 600,
@@ -58,16 +60,50 @@ module.exports = function(RED) {
5860
if (typeof(url) === 'string') { pushmsg.url = url; }
5961
if (typeof(url_title) === 'string') { pushmsg.url_title = url_title; }
6062
if (html) { pushmsg.html = 1; }
61-
//node.log("Sending "+JSON.stringify(pushmsg));
62-
pusher.send( pushmsg, function(err, response) {
63-
if (err) { node.error("Pushover Error: "+err); }
64-
//console.log(response);
65-
});
63+
if (typeof(attachment) === 'string') {
64+
// Treat attachment as a path
65+
fs.readFile(attachment,function(err, data) {
66+
if (err) {
67+
node.error("[57-pushover.js] Error: File Read Error: "+err);
68+
return;
69+
}
70+
pushmsg.file = { data: data };
71+
pushMessage(pushmsg);
72+
});
73+
return;
74+
}
75+
else if (attachment instanceof Buffer) {
76+
// Is it base64 encoded or binary?
77+
var attachmentString = attachment.toString();
78+
var attachmentBuffer = Buffer.from(attachmentString,'base64');
79+
if(attachmentString === attachmentBuffer.toString('base64')) {
80+
// If converts back to same, then it was base64 so set to binary
81+
// https://stackoverflow.com/a/48770228
82+
attachment = attachmentBuffer;
83+
}
84+
// Unset these temporary values
85+
attachmentBuffer = attachmentString = undefined;
86+
// attach the buffer
87+
pushmsg.file = { data: attachment };
88+
}
89+
else if (attachment) {
90+
node.error("[57-pushover.js] Error: attachment property must be a path to a local file or a Buffer containing an image");
91+
return;
92+
}
93+
pushMessage(pushmsg);
6694
}
6795
else {
6896
node.warn("Pushover credentials not set.");
6997
}
7098
});
99+
100+
function pushMessage(pushmsg) {
101+
pusher.send( pushmsg, function(err, response) {
102+
if (err) { node.error("[57-pushover.js] Error: "+err); }
103+
response = JSON.parse(response);
104+
if (response.status !== 1) { node.error("[57-pushover.js] Error: "+response); }
105+
});
106+
}
71107
}
72108
RED.nodes.registerType("pushover",PushoverNode,{
73109
credentials: {

social/pushover/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Optionally uses `msg.topic` to set the configuration, if not already set in the
2121
- `msg.device`: to set the device
2222
- `msg.priority`: to set the priority
2323
- `msg.topic`: to set the title
24+
- `msg.attachment`: to specify an image to attach to message (path as a string or Buffer containing image)
2425
- `msg.url`: to add a web address
2526
- `msg.url_title`: to add a url title
2627
- `msg.sound`: to set the alert sound, see the [available options](https://pushover.net/api#sounds)

social/pushover/package.json

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name" : "node-red-node-pushover",
3-
"version" : "0.0.13",
3+
"version" : "0.0.14",
44
"description" : "A Node-RED node to send alerts via Pushover",
55
"dependencies" : {
6-
"pushover-notifications" : "~0.2.4"
6+
"pushover-notifications" : "^1.2.0"
77
},
88
"repository" : {
99
"type":"git",
@@ -20,5 +20,12 @@
2020
"name": "Dave Conway-Jones",
2121
"email": "[email protected]",
2222
"url": "http://nodered.org"
23-
}
23+
},
24+
"contributors": [
25+
{
26+
"name": "Damien Clark",
27+
"email": "[email protected]",
28+
"url": "https://damos.world"
29+
}
30+
]
2431
}

0 commit comments

Comments
 (0)