From 95ecc766a9f742bb76a4ac5b14ce4bb3171618ea Mon Sep 17 00:00:00 2001 From: Damien Clark Date: Tue, 16 Oct 2018 21:02:01 +1000 Subject: [PATCH 1/5] Implement attachment support using form-data module --- lib/pushover.js | 77 ++++++++++++++++++----------------------- package.json | 4 ++- test/test_attachment.js | 25 +++++++++++++ 3 files changed, 61 insertions(+), 45 deletions(-) create mode 100644 test/test_attachment.js diff --git a/lib/pushover.js b/lib/pushover.js index 04c7964..dcd34c1 100644 --- a/lib/pushover.js +++ b/lib/pushover.js @@ -3,6 +3,7 @@ var http = require('http') var url = require('url') var qs = require('querystring') var pUrl = 'https://api.pushover.net/1/messages.json' +var FormData = require('form-data') function setDefaults (o) { var def = [ @@ -113,26 +114,21 @@ Pushover.prototype.updateSounds = function () { Pushover.prototype.send = function (obj, fn) { var self = this var o = url.parse(pUrl) + var form = new FormData() var proxy o.method = 'POST' obj = setDefaults(obj) - var reqString = { - token: self.token || obj.token, - user: self.user || obj.user - } + form.append('token',self.token || obj.token) + form.append('user',self.user || obj.user) var p for (p in obj) { - reqString[ p ] = obj[p] + form.append(p,obj[p]) } - reqString = qs.stringify(reqString) - - o.headers = { - 'Content-Length': reqString.length - } + o.headers = form.getHeaders() var httpOpts = self.httpOptions || {} if (httpOpts) { @@ -150,45 +146,38 @@ Pushover.prototype.send = function (obj, fn) { o.protocol = proxy.protocol } - var request - if (httpOpts.proxy && httpOpts.proxy !== '') { - request = http.request - } else { - request = https.request - } - - var req = request(o, function (res) { - if (self.debug) { - console.log(res.statusCode) + form.submit(o,function(err,res) { + if(err) { + if (fn) { + fn(err) + } + // In the tests the "end" event did not get emitted if "error" was emitted, + // but to be sure that the callback is not get called twice, null the callback function + fn = null } - var err - var data = '' - res.on('end', function () { - self.errors(data, res) - if (fn) { - fn(err, data, res) - } - }) - res.on('data', function (chunk) { - data += chunk - }) + if (self.debug) { + console.log(res.statusCode) + } + + var data = '' + res.on('end', function () { + self.errors(data, res) + if (fn) { + fn(err, data, res) + } + }) + + res.on('data', function (chunk) { + data += chunk + }) }) - req.on('error', function (err) { - if (fn) { - fn(err) - } - // In the tests the "end" event did not get emitted if "error" was emitted, - // but to be sure that the callback is not get called twice, null the callback function - fn = null - }) - if (self.debug) { - console.log(reqString.replace(self.token, 'XXXXX').replace(self.user, 'XXXXX')) - } - req.write(reqString) - req.end() + + // if (self.debug) { + // console.log(form.replace(self.token, 'XXXXX').replace(self.user, 'XXXXX')) + // } } exports = module.exports = Pushover diff --git a/package.json b/package.json index 42d2c93..1492f19 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,9 @@ "type": "git", "url": "https://github.com/qbit/node-pushover.git" }, - "dependencies": {}, + "dependencies": { + "form-data": "^2.3.2" + }, "devDependencies": {}, "optionalDependencies": {}, "engines": { diff --git a/test/test_attachment.js b/test/test_attachment.js new file mode 100644 index 0000000..215b580 --- /dev/null +++ b/test/test_attachment.js @@ -0,0 +1,25 @@ +var Push = require('../lib/pushover.js') +var fs = require('fs') + +var p = new Push({ + user: process.env['PUSHOVER_USER'], + token: process.env['PUSHOVER_TOKEN'], + update_sounds: false, + debug: true +}) + +var msg = { + message: 'test from ' + process.argv[1], + sound: 'magic', + title: 'Well - this is fantastic', + attachment: fs.createReadStream('../img/pushover-header.png') +} + +// console.log( p ); + +p.send(msg, function (err, result, res) { + console.log('error', err) + console.log('result', result) + console.log('res.headers', res.headers) + // process.exit(0); +}) From daef80d9ea1e248d48a8b3dbc4fdf7baefdc1c13 Mon Sep 17 00:00:00 2001 From: Damien Clark Date: Wed, 17 Oct 2018 13:48:09 +1000 Subject: [PATCH 2/5] Added support for image attachments provided by Buffer objects --- lib/pushover.js | 6 ++++++ test/test_attachment.js | 18 +++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/pushover.js b/lib/pushover.js index dcd34c1..de6d5e2 100644 --- a/lib/pushover.js +++ b/lib/pushover.js @@ -123,6 +123,12 @@ Pushover.prototype.send = function (obj, fn) { form.append('token',self.token || obj.token) form.append('user',self.user || obj.user) + if('attachment' in obj && obj.attachment instanceof Buffer) { + // Sending a file from a buffer + // https://stackoverflow.com/a/43914175 + form.append('attachment',obj.attachment,{filename: 'filename'}) + delete obj.attachment + } var p for (p in obj) { form.append(p,obj[p]) diff --git a/test/test_attachment.js b/test/test_attachment.js index 215b580..62c3478 100644 --- a/test/test_attachment.js +++ b/test/test_attachment.js @@ -11,7 +11,7 @@ var p = new Push({ var msg = { message: 'test from ' + process.argv[1], sound: 'magic', - title: 'Well - this is fantastic', + title: 'Testing attachment', attachment: fs.createReadStream('../img/pushover-header.png') } @@ -23,3 +23,19 @@ p.send(msg, function (err, result, res) { console.log('res.headers', res.headers) // process.exit(0); }) + +fs.readFile('../img/pushover-header.png', function (err, data) { + if (err) throw err; + + msg = { + message: 'test from ' + process.argv[1], + title: 'Testing attachment from buffer', + attachment: data + } + + p.send(msg, function (err, result, res) { + console.log('error', err) + console.log('result', result) + console.log('res.headers', res.headers) + }) +}); From 6bfe5d2274acb3f26092e6640abecbe4abf2f045 Mon Sep 17 00:00:00 2001 From: Damien Clark Date: Wed, 17 Oct 2018 14:06:04 +1000 Subject: [PATCH 3/5] Code formatting tidy up --- lib/pushover.js | 67 +++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 36 deletions(-) diff --git a/lib/pushover.js b/lib/pushover.js index de6d5e2..c120a18 100644 --- a/lib/pushover.js +++ b/lib/pushover.js @@ -120,21 +120,21 @@ Pushover.prototype.send = function (obj, fn) { obj = setDefaults(obj) - form.append('token',self.token || obj.token) - form.append('user',self.user || obj.user) + form.append('token', self.token || obj.token) + form.append('user', self.user || obj.user) - if('attachment' in obj && obj.attachment instanceof Buffer) { - // Sending a file from a buffer - // https://stackoverflow.com/a/43914175 - form.append('attachment',obj.attachment,{filename: 'filename'}) + if ('attachment' in obj && obj.attachment instanceof Buffer) { + // Sending a file from a buffer + // https://stackoverflow.com/a/43914175 + form.append('attachment', obj.attachment, {filename: 'filename'}) delete obj.attachment - } + } var p for (p in obj) { - form.append(p,obj[p]) + form.append(p, obj[p]) } - o.headers = form.getHeaders() + o.headers = form.getHeaders() var httpOpts = self.httpOptions || {} if (httpOpts) { @@ -152,38 +152,33 @@ Pushover.prototype.send = function (obj, fn) { o.protocol = proxy.protocol } - form.submit(o,function(err,res) { - if(err) { - if (fn) { - fn(err) - } - // In the tests the "end" event did not get emitted if "error" was emitted, - // but to be sure that the callback is not get called twice, null the callback function - fn = null + form.submit(o, function (err, res) { + if (err) { + if (fn) { + fn(err) + } + // In the tests the "end" event did not get emitted if "error" was emitted, + // but to be sure that the callback is not get called twice, null the callback function + fn = null } - if (self.debug) { - console.log(res.statusCode) - } - - var data = '' - res.on('end', function () { - self.errors(data, res) - if (fn) { - fn(err, data, res) - } - }) - - res.on('data', function (chunk) { - data += chunk - }) - }) + if (self.debug) { + console.log(res.statusCode) + } + var data = '' + res.on('end', function () { + self.errors(data, res) + if (fn) { + fn(err, data, res) + } + }) + res.on('data', function (chunk) { + data += chunk + }) + }) - // if (self.debug) { - // console.log(form.replace(self.token, 'XXXXX').replace(self.user, 'XXXXX')) - // } } exports = module.exports = Pushover From cccf547bc3f3828fd1f53f4e114c0f4196c35cdd Mon Sep 17 00:00:00 2001 From: Damien Clark Date: Wed, 17 Oct 2018 14:24:02 +1000 Subject: [PATCH 4/5] Update README with examples for attaching images --- README.md | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/README.md b/README.md index d9f30ca..6499683 100644 --- a/README.md +++ b/README.md @@ -95,3 +95,69 @@ for ( var i = 0, l = users.length; i < l; i++ ) { }) } ``` + +### Sending a message with an image attachment + +```javascript + +var fs = require( 'fs' ) +var Push = require( 'pushover-notifications' ) + +var p = new Push( { + user: process.env['PUSHOVER_USER'], + token: process.env['PUSHOVER_TOKEN'], +}) + +var msg = { + message: 'omg node test', // required + title: "Well - this is fantastic - an attachment", + sound: 'magic', + device: 'devicename', + attachment: fs.createReadStream('path/to/image.jpg'), + priority: 1 +} + +p.send( msg, function( err, result ) { + if ( err ) { + throw err + } + + console.log( result ) +}) +``` + +### Sending a message with an image attachment from a Buffer + +```javascript + +var fs = require( 'fs' ) +var Push = require( 'pushover-notifications' ) + +var p = new Push( { + user: process.env['PUSHOVER_USER'], + token: process.env['PUSHOVER_TOKEN'], +}) + +// data is a Buffer object holding contents of image.jpg +fs.readFile('path/to/image.jpg', function (err, data) { + if (err) throw err; + + var msg = { + message: 'omg node test', // required + title: "Well - this is fantastic - an attachment from a buffer", + sound: 'magic', + device: 'devicename', + attachment: data + priority: 1 + } + + p.send( msg, function( err, result ) { + if ( err ) { + throw err + } + + console.log( result ) + }) +}) + +``` From 44d9e174cfec5c61fb533abc41545f7e136cb4af Mon Sep 17 00:00:00 2001 From: Damien Clark Date: Wed, 17 Oct 2018 17:32:02 +1000 Subject: [PATCH 5/5] Fixed path in test script --- test/test_attachment.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_attachment.js b/test/test_attachment.js index 62c3478..5b00d43 100644 --- a/test/test_attachment.js +++ b/test/test_attachment.js @@ -12,7 +12,7 @@ var msg = { message: 'test from ' + process.argv[1], sound: 'magic', title: 'Testing attachment', - attachment: fs.createReadStream('../img/pushover-header.png') + attachment: fs.createReadStream('img/pushover-header.png') } // console.log( p ); @@ -24,7 +24,7 @@ p.send(msg, function (err, result, res) { // process.exit(0); }) -fs.readFile('../img/pushover-header.png', function (err, data) { +fs.readFile('img/pushover-header.png', function (err, data) { if (err) throw err; msg = {