From e75096a8d46444aee40597020e1de28718f6e799 Mon Sep 17 00:00:00 2001 From: Alex Whitman Date: Wed, 6 Jan 2016 13:26:53 +0000 Subject: [PATCH] Add support for SMS, clipboard and dismissal --- CHANGELOG.md | 1 + README.md | 44 ++++++++++++++++++++++ lib/internal/ephemerals.js | 76 ++++++++++++++++++++++++++++++++++++++ lib/pushbullet.js | 2 + package.json | 1 + 5 files changed, 124 insertions(+) create mode 100644 lib/internal/ephemerals.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 96deb7f..b0ec403 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - Default `devices` to active only. - Default `subscriptions` to active only. - Add chats functions. +- Add ephemerals support for sending SMS messages, clipboard messages and dismissals. ### 1.4.3 diff --git a/README.md b/README.md index 74e728b..d029aee 100644 --- a/README.md +++ b/README.md @@ -274,6 +274,50 @@ Unmute a chat. ```javascript pusher.unmuteChat('udprOsjAsLtNTRAG', function(error, response) {}); +### PushBullet.sendSMS(options, callback) + +Send an SMS through a device. + +```javascript +var options = { + source_user_iden: 'ujpah72o0', // The user iden of the user sending this message + target_device_iden: 'ujpah72o0sjAoRtnM0jc', // The iden of the device corresponding to the phone that should send the SMS + conversation_iden: '+1 303 555 1212', // Phone number to send the SMS to + message: 'Hello!' // The SMS message to send +}; + +pusher.sendSMS(options, function(error, response) {}); +``` + +### PushBullet.sendClipboard(options, callback) + +Send clipboard content. + +```javascript +var options = { + source_user_iden: "ujpah72o0", // The iden of the user sending this message + source_device_iden: "ujpah72o0sjAoRtnM0jc", // The iden of the device sending this message + body: "http://www.google.com", // The text to copy to the clipboard +}; + +pusher.sendClipboard(options, function(error, response) {}); +``` + +### PushBullet.dismissEphemeral(options, callback) + +Dismiss an ephemeral. + +```javascript +var options = { + package_name: 'com.pushbullet.android', // Set to the package_name field from the mirrored notification + notification_id: '-8', // Set to the notification_id field from the mirrored notification + notification_tag: null, // Set to the notification_tag field from the mirrored notification + source_user_iden: 'ujpah72o0', // Set to the source_user_iden field from the mirrored notification +}; + +pusher.dismissEphemeral(options, function(error, response) {}); +``` + ### PushBullet.stream() Returns a new stream listener which will emit events from the stream. diff --git a/lib/internal/ephemerals.js b/lib/internal/ephemerals.js new file mode 100644 index 0000000..5e05410 --- /dev/null +++ b/lib/internal/ephemerals.js @@ -0,0 +1,76 @@ +var clone = require('clone'); + +var PushBullet = require('../pushbullet'); + +/** + * Send an SMS. + * + * The options require source_user_iden, target_device_iden, conversation_iden and message. + * See https://docs.pushbullet.com/#send-sms for details. + * + * @param {Object} smsOptions SMS options. + * @param {Function} callback Callback for when the request is complete. + */ +PushBullet.prototype.sendSMS = function sendSMS(smsOptions, callback) { + var options = clone(smsOptions); + + options.package_name = 'com.pushbullet.android'; + options.type = 'messaging_extension_reply'; + + this.sendEphemeral(options, callback); +}; + +/** + * Send clipboard content. + * + * The options require body, source_user_iden and source_device_iden. + * See https://docs.pushbullet.com/#universal-copypaste for details. + * + * @param {Object} clipOptions Clipboard options. + * @param {Function} callback Callback for when the request is complete. + */ +PushBullet.prototype.sendClipboard = function sendClipboard(clipOptions, callback) { + var options = clone(clipOptions); + + options.type = 'clip'; + + this.sendEphemeral(options, callback); +}; + +/** + * Dismiss an ephemeral. + * + * The options require package_name, notification_id, notification_tag and source_user_iden. + * See https://docs.pushbullet.com/#dismissal-ephemeral for details. + * + * @param {Object} ephemerealOptions Ephemeral dismissal options. + * @param {Function} callback Callback for when the request is complete. + */ +PushBullet.prototype.dismissEphemeral = function dismissEphemeral(ephemerealOptions, callback) { + var options = clone(ephemerealOptions); + + options.type = 'dismissal'; + + this.sendEphemeral(options, callback); +}; + +/** + * Send an ephemeral. + * + * @param {Object} ephemerealOptions Ephemeral options. + * @param {Function} callback Callback for when the request is complete. + */ +PushBullet.prototype.sendEphemeral = function sendEphemeral(ephemerealOptions, callback) { + var self = this; + + var options = { + json: { + type: 'push', + push: ephemerealOptions + } + }; + + self.request.post(PushBullet.EPHEMERALS_END_POINT, options, function(error, response, body) { + self.handleResponse(error, response, body, callback); + }); +}; diff --git a/lib/pushbullet.js b/lib/pushbullet.js index bedf5fc..5950049 100644 --- a/lib/pushbullet.js +++ b/lib/pushbullet.js @@ -33,6 +33,7 @@ PushBullet.USERS_END_POINT = PushBullet.API_BASE + '/users'; PushBullet.SUBS_END_POINT = PushBullet.API_BASE + '/subscriptions'; PushBullet.CHANNELS_END_POINT = PushBullet.API_BASE + '/channel-info'; PushBullet.CHATS_END_POINT = PushBullet.API_BASE + '/chats'; +PushBullet.EPHEMERALS_END_POINT = PushBullet.API_BASE + '/ephemerals'; module.exports = PushBullet; @@ -138,6 +139,7 @@ PushBullet.prototype.handleError = function handleError(error, response, body, c require('./internal/chats'); require('./internal/devices'); +require('./internal/ephemerals'); require('./internal/pushes'); require('./internal/subscriptions'); require('./internal/users'); diff --git a/package.json b/package.json index cabea40..215f7ef 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "author": "Alex Whitman ", "license": "BSD", "dependencies": { + "clone": "^1.0.2", "mime": "~1.2.11", "request": "~2.44.0", "websocket": "~1.0.8"