Skip to content

Commit

Permalink
Add support for the text API
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwhitman committed Feb 12, 2022
1 parent 5f501c5 commit 9b7bcda
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

- Callback functionality has been removed. Callbacks can still be used by using `util.callbackify()`.
- `request` has been replaced by `node-fetch`. Full API responses are returned by function calls.
- Add support for for text API.

### 2.4.0

Expand Down
34 changes: 34 additions & 0 deletions README.md
Expand Up @@ -279,6 +279,40 @@ Unmute a chat.
await pusher.unmuteChat('udprOsjAsLtNTRAG');
```

### PushBullet.createText(deviceIden, addresses, message, [options])

Create a new text.

The `options` parameter can be used to add additional information to the text request.

- `file_url` is a URL of a file to send with the text.
- `file_type` is the mime type of the file being sent. Required if `file_url` is used.

Other options are available, see https://docs.pushbullet.com/#text

```javascript
await pusher.createText('udprOsjAsLtNTRAG', '+13035551212', 'Test Message', {});
```

### PushBullet.updateText(textIden, options)

Update a chat.

`options` is an object representing the text attributes to update.
See https://docs.pushbullet.com/#text for the available attributes and structure.

```javascript
await pusher.updateText('udprOsjAsLtNTRAG', {});
```

### PushBullet.deleteText(textIden)

Delete a text.

```javascript
await pusher.deleteText('udprOsjAsLtNTRAG');
```

### PushBullet.sendSMS(options)

Send an SMS through a device.
Expand Down
63 changes: 63 additions & 0 deletions lib/internal/texts.js
@@ -0,0 +1,63 @@
import PushBullet from '../pushbullet.js';

/**
* Create a new text.
*
* See https://docs.pushbullet.com/#text for additional options that can be set.
*
* @param {String} deviceIden Device IDEN of the device which can send SMS messages.
* @param {Array} addresses String or array of strings of recipient phone numbers.
* @param {String} message Message text to be sent.
* @param {Object} textOptions Additional text options.
* @returns {Promise}
*/
PushBullet.prototype.createText = async function createText(deviceIden, addresses, message, textOptions) {
const options = {
data : {
target_device_iden : deviceIden,
addresses : Array.isArray(addresses) ? addresses : [ addresses ],
message : message
}
};

if (textOptions.guid) {
options.data.guid = textOptions.guid;
}
if (textOptions.status) {
options.data.status = textOptions.status;
}
if (textOptions.file_type) {
options.data.file_type = textOptions.file_type;
}
if (textOptions.file_url) {
options.file_url = textOptions.file_url;
}
if (textOptions.skip_file_delete) {
options.skip_file_delete = textOptions.skip_file_delete;
}

return this.makeRequest('post', PushBullet.TEXTS_END_POINT, { json : options });
};

/**
* Update text.
*
* See https://docs.pushbullet.com/#text for valid attributes.
*
* @param {String} textIden The iden of the text to update.
* @param {Object} textOptions Text attributes to apply updates to.
* @returns {Promise}
*/
PushBullet.prototype.updateText = async function updateText(textIden, textOptions) {
return this.makeRequest('post', PushBullet.TEXTS_END_POINT + '/' + textIden, { json : textOptions });
};

/**
* Delete a text.
*
* @param {String} textIden The iden of the text to update.
* @returns {Promise}
*/
PushBullet.prototype.deleteText = async function deleteText(textIden) {
return this.makeRequest('delete', PushBullet.TEXTS_END_POINT + '/' + textIden, {});
};
2 changes: 2 additions & 0 deletions lib/pushbullet.js
Expand Up @@ -24,6 +24,7 @@ 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';
PushBullet.TEXTS_END_POINT = PushBullet.API_BASE + '/texts';

/**
* Enables End-to-End encryption.
Expand Down Expand Up @@ -108,4 +109,5 @@ import './internal/devices.js';
import './internal/ephemerals.js';
import './internal/pushes.js';
import './internal/subscriptions.js';
import './internal/texts.js';
import './internal/users.js';
75 changes: 75 additions & 0 deletions test/texts.js
@@ -0,0 +1,75 @@
/* global describe,it */

import nock from 'nock';
import PushBullet from '../lib/pushbullet.js';
import { should } from 'chai';
should();

const accessToken = 'test-access-token';
const requestHeaders = {
'Access-Token' : accessToken
};

describe('PushBullet.createText()', () => {
it('should create a text', async () => {
nock(PushBullet.API_BASE, { reqheaders : requestHeaders })
.post('/texts', {
data : {
target_device_iden : 'device-iden',
addresses : [ '123-456-789' ],
message : 'text-message',
file_type : 'type-of-file'
},
file_url : 'url-to-file'
})
.reply(200, {});

const pb = new PushBullet(accessToken);
const response = await pb.createText('device-iden', '123-456-789', 'text-message', {
file_url : 'url-to-file',
file_type : 'type-of-file'
});
response.status.should.equal(200);

const j = await response.json();
j.should.be.an.an('object');
});
});

describe('PushBullet.updateText()', () => {
it('should update a text', async () => {
nock(PushBullet.API_BASE, { reqheaders : requestHeaders })
.post('/texts/text-iden', {
data : {
message : 'updated-message'
}
})
.reply(200, {});

const pb = new PushBullet(accessToken);
const response = await pb.updateText('text-iden', {
data : {
message : 'updated-message'
}
});
response.status.should.equal(200);

const j = await response.json();
j.should.be.an.an('object');
});
});

describe('PushBullet.deleteText()', () => {
it('should delete a chat', async () => {
nock(PushBullet.API_BASE, { reqheaders : requestHeaders })
.delete('/texts/text-iden')
.reply(200, {});

const pb = new PushBullet(accessToken);
const response = await pb.deleteText('text-iden');
response.status.should.equal(200);

const j = await response.json();
j.should.be.an.an('object');
});
});

0 comments on commit 9b7bcda

Please sign in to comment.