Skip to content

Commit

Permalink
Merge pull request #125 from plivo/mms_changes
Browse files Browse the repository at this point in the history
MMS changes
  • Loading branch information
nixonsam committed Dec 4, 2019
2 parents 4f43195 + e683bfb commit b3c5164
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 25 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## [4.1.7](https://github.com/plivo/plivo-node/releases/tag/v4.1.7)(2019-12-04)
- Add MMS support.

## [4.1.6](https://github.com/plivo/plivo-node/releases/tag/v4.1.6)(2019-11-14)
- Fix list APIs to return meta in response.

Expand Down
32 changes: 32 additions & 0 deletions examples/messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
let plivo = require('plivo');
let client = new plivo.Client('', '');

let optionalParams = {};
client.messages.create(
"", //from
"", // to
"", // text
{
type: "", // sms or mms
method: "", // GET or POST callback method
url: "", // callback url
media_urls: [".gif", ".jpg"] // to send MMS, mention the media urls
}

).then(function (message_created) {
console.log(message_created)
}).catch(function (error) {
console.log(error);
});


// get MMS Media detail
client.messages.get("your message uuid").then(function (message) {
return client.messages.listMedia(message.messageUuid)
})
.then(function (result) {
console.log("\n============ list Media ===========\n", result)
})
.catch(function (response) {
console.log("\n============ Error :: ===========\n", response);
});
64 changes: 51 additions & 13 deletions lib/resources/messages.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@

import { extend, validate } from '../utils/common.js';
import { PlivoResource, PlivoResourceInterface } from '../base';
import {
extend,
validate
} from '../utils/common.js';
import {
PlivoResource,
PlivoResourceInterface
} from '../base';
import * as _ from "lodash";

const clientKey = Symbol();
const action = 'Message/';
const idField = 'messageUuid';

Expand All @@ -18,10 +24,20 @@ export class Message extends PlivoResource {

if (idField in data) {
this.id = data[idField];
}
};

extend(this, data);
}

listMedia() {
return super.executeAction(this.id + '/Media/', 'Get', {});
}
deleteMedia() {
return super.executeAction(this.id + '/Media/', 'Delete', {});
}
getMedia(mediaID) {
return super.executeAction(this.id + '/Media/' + mediaID + '/', 'Get', {});
}
}
/**
* Represents a Message Interface
Expand All @@ -35,6 +51,7 @@ export class MessageInterface extends PlivoResourceInterface {
constructor(client, data = {}) {
super(action, Message, idField, client);
extend(this, data);
this[clientKey] = client;
}

/**
Expand All @@ -44,9 +61,10 @@ export class MessageInterface extends PlivoResourceInterface {
* @param {string} dst - destination number
* @param {string} text - text to send
* @param {object} optionalParams - Optional Params to send message
* @param {string} [optionalParams.type] - The type of message. Should be `sms` for a text message. Defaults to `sms`.
* @param {string} [optionalParams.type] - The type of message. Should be `sms` or `mms`. Defaults to `sms`.
* @param {string} [optionalParams.url] The URL to which with the status of the message is sent.
* @param {string} [optionalParams.method] The method used to call the url. Defaults to POST.
* @param {list} [optionalParams.media_urls] For sending mms, specify the media urls in list of string
* @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.
* @promise {object} return {@link PlivoGenericMessage} object if success
* @fail {Error} return Error
Expand All @@ -62,18 +80,20 @@ export class MessageInterface extends PlivoResourceInterface {
* @param {string} dst - destination number
* @param {string} text - text to send
* @param {object} optionalParams - Optional Params to send message
* @param {string} [optionalParams.type] - The type of message. Should be `sms` for a text message. Defaults to `sms`.
* @param {string} [optionalParams.type] - The type of message. Should be `sms` or `mms`. Defaults to `sms`.
* @param {string} [optionalParams.url] The URL to which with the status of the message is sent.
* @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 PlivoGenericMessage} object if success
* @fail {Error} return Error
*/
create(src, dst, text, optionalParams, powerpackUUID) {
let errors = validate([
{ field: 'dst', value: dst, validators: ['isRequired'] },
{ field: 'text', value: text, validators: ['isRequired'] },
]);
let errors = validate([{
field: 'dst',
value: dst,
validators: ['isRequired']
}]);

if (errors) {
return errors;
Expand Down Expand Up @@ -113,14 +133,32 @@ export class MessageInterface extends PlivoResourceInterface {
* @fail {Error} return Error
*/
get(id) {
let errors = validate([
{ field: 'id', value: id, validators: ['isRequired'] }
]);
let errors = validate([{
field: 'id',
value: id,
validators: ['isRequired']
}]);

if (errors) {
return errors;
}

return super.get(id);
}

listMedia(messageUUID) {
return new Message(this[clientKey], {
id: messageUUID
}).listMedia();
}
getMedia(messageUUID, mediaID) {
return new Message(this[clientKey], {
id: messageUUID
}).getMedia(mediaID);
}
deleteMedia(messageUUID) {
return new Message(this[clientKey], {
id: messageUUID
}).deleteMedia();
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plivo",
"version": "4.1.6",
"version": "4.1.7",
"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": [
Expand Down
44 changes: 33 additions & 11 deletions test/messages.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,81 @@
import assert from 'assert';
import sinon from 'sinon';
import {Client} from '../lib/rest/client-test';
import {PlivoGenericResponse} from '../lib/base.js';
import {
Client
} from '../lib/rest/client-test';
import {
PlivoGenericResponse
} from '../lib/base.js';

let client = new Client('sampleid', 'sammpletoken', 'sampleproxy');

describe('message', function () {
it('should get message', function () {
return client.messages.get(1)
.then(function(message) {
.then(function (message) {
assert.equal(message.id, 1)
})
});

it('list messages', function () {
return client.messages.list()
.then(function(messages) {
.then(function (messages) {
assert.equal(messages.length, 2)
})
});

it('should create message via interface', function () {
return client.messages.create('src', 'dst', 'text')
.then(function(message){
assert.equal(message.message, 'message(s) queued')
.then(function (message) {
assert.equal(message.message, 'message(s) queued')
})
});

it('should send message via interface', function () {
return client.messages.send('src', 'dst', 'text')
.then(function(message){
assert.equal(message.message, 'message(s) queued')
.then(function (message) {
assert.equal(message.message, 'message(s) queued')
})
});


it('should throw error - id is required via interface', function () {
return client.messages.get()
.catch(function(err){
.catch(function (err) {
assert.equal(err.message, 'Missing mandatory field: id')
})
});

it('should throw error - src and powerpack both not present', function () {
return client.messages.send(null, 'dst', 'text', {}, null)
.catch(function(err){
.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){
.catch(function (err) {
assert.equal(err.message, 'Either of src or powerpack uuid, both of them are present')
})
});

it('list media', function (done) {
client.messages.get('xyz')
.then(function (message) {
return message.listMedia({})
})
.then(function (mmsmedia) {
assert(mmsmedia instanceof PlivoGenericResponse)
done()
})
});
it('should list media via plivo interface!', function (done) {
client.messages.listMedia('xyz')
.then(function (mmsMedia) {
assert(mmsMedia)
done()
})
});

});

0 comments on commit b3c5164

Please sign in to comment.