diff --git a/CHANGELOG.md b/CHANGELOG.md index d1ff29e..11b4683 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +## [v4.23.0](https://github.com/plivo/plivo-node/tree/v4.23.0) (2021-10-11) +**Features - Messaging** +- This version includes advancements to the Messaging Interface that deals with the [Send SMS/MMS](https://www.plivo.com/docs/sms/api/message#send-a-message) interface, Creating a standard structure for `request/input` arguments to make implementation easier and incorporating support for the older interface. + + Example for [send SMS](https://github.com/plivo/plivo-node#send-a-message) + ## [v4.22.4](https://github.com/plivo/plivo-node/tree/v4.22.4) (2021-09-27) **Bug Fix** - Handle invalid destination number API response for send [SMS API](https://www.plivo.com/docs/sms/api/message/send-a-message/). diff --git a/README.md b/README.md index 2c454a1..5574ae6 100644 --- a/README.md +++ b/README.md @@ -55,12 +55,12 @@ Also, using `client.resources.list()` would list the first 20 resources by defau let plivo = require('plivo'); let client = new plivo.Client(); -client.messages.create( - '+14156667778', - '+14156667777', - 'Hello, world!' -).then(function(response) { - console.log(response) +client.messages.create({ + src: '+14156667778', + dst: '14156667777', + text: 'Hello, this is a sample text from Plivo', +}).then(function(response) { + console.log(response) }); ``` diff --git a/lib/resources/messages.js b/lib/resources/messages.js index 57bba6c..4e353bf 100644 --- a/lib/resources/messages.js +++ b/lib/resources/messages.js @@ -127,15 +127,14 @@ export class Message extends PlivoResource { } } + /** * Represents a Message Interface * @constructor * @param {function} client - make api call * @param {object} [data] - data of call */ - export class MessageInterface extends PlivoResourceInterface { - constructor(client, data = {}) { super(action, Message, idField, client); extend(this, data); @@ -143,7 +142,6 @@ export class MessageInterface extends PlivoResourceInterface { this[actionKey] = action; this[klassKey] = Message; this[idKey] = idField; - } /** @@ -161,12 +159,13 @@ export class MessageInterface extends PlivoResourceInterface { * @promise {object} return {@link PlivoGenericMessage} object if success * @fail {Error} return Error */ - send(src, dst, text, optionalParams) { + + send(src, dst, text, optionalParams) { return this.create(src, dst, text, optionalParams); } /** - * Create Message + * Send Message * @method * @param {string} src - source number * @param {string} dst - destination number @@ -177,15 +176,36 @@ export class MessageInterface extends PlivoResourceInterface { * @param {string} [optionalParams.method] The method used to call the url. Defaults to POST. * @param {boolean} [optionalParams.log] If set to false, the content of this message will not be logged on the Plivo infrastructure and the dst value will be masked (e.g., 141XXXXX528). Default is set to true. * @param {Array} [optionalParams.media_urls] For sending mms, specify the media urls in list of string - * @promise {object} return {@link MessageResponse} object if success + * @promise {object} return {@link PlivoGenericMessage} object if success * @fail {Error} return Error */ create(src, dst, text, optionalParams, powerpackUUID) { + var isObject = arguments.length; + if (isObject == 1) { + var powerpackUUID = src.powerpackUUID; + var text = src.text; + var dst = src.dst; + var url = src.url; + var method = src.method; + var type = src.type; + var media_urls = src.media_urls; + var media_ids = src.media_ids; + var log = src.log; + var trackable = src.trackable; + var src = src.src; + } + let errors = validate([{ - field: 'dst', - value: dst, - validators: ['isRequired'] - }]); + field: 'dst', + value: dst, + validators: ['isRequired'] + }, + { + field: 'text', + value: text, + validators: ['isRequired'] + }, + ]); if (errors) { return errors; @@ -206,6 +226,31 @@ export class MessageInterface extends PlivoResourceInterface { } let params = optionalParams || {}; + + if (isObject == 1) { + if (url) { + params.url = url; + } + if (method) { + params.method = method; + } + if (type) { + params.type = type; + } + if (media_urls) { + params.media_urls = media_urls; + } + if (media_ids) { + params.media_ids = media_ids; + } + if (log) { + params.log = log; + } + if (trackable) { + params.trackable = trackable; + } + } + if (src) { params.src = src; } @@ -214,7 +259,7 @@ export class MessageInterface extends PlivoResourceInterface { if (powerpackUUID) { params.powerpackUUID = powerpackUUID; } - + let client = this[clientKey]; let idField = this[idKey]; let action = this[actionKey] + (this.id ? this.id + '/' : ''); @@ -292,5 +337,4 @@ export class MessageInterface extends PlivoResourceInterface { id: messageUUID }).listMedia(); } - } \ No newline at end of file diff --git a/package.json b/package.json index e0851d5..b01082e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "plivo", - "version": "4.22.4", + "version": "4.23.0", "description": "A Node.js SDK to make voice calls and send SMS using Plivo and to generate Plivo XML", "homepage": "https://github.com/plivo/plivo-node", "files": [ diff --git a/test/messages.js b/test/messages.js index 7a43d98..67e750a 100644 --- a/test/messages.js +++ b/test/messages.js @@ -32,9 +32,16 @@ describe('message', function () { }); it('should send message via interface', function () { - return client.messages.send('src', 'dst', 'text') - .then(function (message) { - assert.equal(message.message, 'message(s) queued') + return client.messages.send({src:'src', dst:'dst', text:'text',powerpackUUID: null}) + .then(function(message){ + assert.equal(message.message, 'message(s) queued') + }) + }); + + it('should send message via interface', function () { + return client.messages.create({src:'src', dst:'dst', text:'text'}) + .then(function(message){ + assert.equal(message.message, 'message(s) queued') }) }); @@ -47,15 +54,15 @@ describe('message', function () { }); it('should throw error - src and powerpack both not present', function () { - return client.messages.send(null, 'dst', 'text', {}, null) - .catch(function (err) { + return client.messages.send({src:null,dst:'dst',text:'text',powerpackUUID:null}) + .catch(function(err){ assert.equal(err.message, 'Neither of src or powerpack uuid present, either one is required') }) }); it('should throw error - src and powerpack both are present', function () { - return client.messages.send('91235456917375', 'dst', 'text', {}, '916386027476') - .catch(function (err) { + return client.messages.send({src:'91235456917375', dst:'dst', text:'text', powerpackUUID:'916386027476'}) + .catch(function(err){ assert.equal(err.message, 'Either of src or powerpack uuid, both of them are present') }) });