diff --git a/README.md b/README.md index f406317..6d45239 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # node-pushover-client Send push notifications to iOS and Android using [Pushover][site]. +Compatible with Pushover 3.0 api, it now can send an attached image. + ## Usage [Register an application with Pushover.net][site-app] to get your application and user tokens. @@ -33,6 +35,18 @@ Pushover.send({ }); ``` +You also can attach an image. Full filepath must be specified. +Pushover's api limits must be followed: "Each message may only include one attachment, and attachments are currently limited to 2,621,440 bytes (2.5 megabytes)." + +```js +Pushover.send({ + token: 'KzGDORePK8gMaC0QOYAMyEEuzJnyUi', + user: 'uQiRzpo4DXghDmr9QzzfQu27cmVRsG', + message: 'Hello World', + attachment: FILEPATH +}); +``` + ## Command Line Usage ```sh $ npm -g install node-pushover-client diff --git a/bin/pushover b/bin/pushover index 5f74780..4ff0086 100755 --- a/bin/pushover +++ b/bin/pushover @@ -15,6 +15,7 @@ program .option('-i, --timestamp [timestamp]', 'a unix timestamp', parseInt) .option('-p, --priority [n]', 'priority (-1 to 2)', parseInt) .option('-d, --device [id]', 'device to send to', String) + .option('-f, --file [file]', 'file to attach', String) .parse(process.argv); new PushoverClient() @@ -26,7 +27,8 @@ new PushoverClient() expires: program.expires, retry: program.retry, timestamp: program.timestamp, - priority: program.priority + priority: program.priority, + attachment: program.attachment }) .then(function (res) { console.log(`Sent request: ${res.request}`); diff --git a/lib/pushover-client.js b/lib/pushover-client.js index 69cad27..f61eb28 100644 --- a/lib/pushover-client.js +++ b/lib/pushover-client.js @@ -1,6 +1,8 @@ +const fs = require('fs'); const fetch = require('node-fetch'); const FormData = require('form-data'); const snakeCase = require('lodash/snakeCase'); +require('babel-polyfill'); const endpoint = 'https://api.pushover.net/1/messages.json'; @@ -20,7 +22,17 @@ class Pushover { } for (let [key, value] of filterOptions(this.defaults, options)) { - form.append(key, value); + // If attachment is specified, create a file stream and include it into the form-data object + if (key === 'attachment') { + var stream = fs.readFileSync(value, options); + form.append(key, stream, { + filename: 'image.jpg', + contentType: stream.contentType, + knownLength: stream.byteLength + }); + } else { + form.append(key, value); + } } return fetch(endpoint, { diff --git a/package.json b/package.json index 0931299..3739c28 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,10 @@ { "name": "node-pushover-client", - "version": "2.1.0", - "description": "Send push notifications to iOS and Android using Pushover.", + "version": "2.1.1", + "description": "Send push notifications to iOS and Android using Pushover. Include image attachment support.", "main": "index.js", "dependencies": { + "babel-polyfill": "^6.26.0", "commander": "^2.9.0", "form-data": "^2.1.2", "lodash": "^4.17.2", @@ -20,6 +21,12 @@ "url": "git://github.com/dvdln/node-pushover-client.git" }, "author": "David Lane ", + "contributors": [ + { + "name": "Carlos Cordero", + "url": "http://www.carloscordero.com" + } + ], "license": "ISC", "xo": { "space": true