Skip to content

Commit

Permalink
Add PHLO support (#112)
Browse files Browse the repository at this point in the history
* Added phlo client.

* Finished implementaiton of initializing phlo client.

* Finished logic for v2 methods of multiparty call(not members).

* Added test cases for multiparty call.

* Finished logic for phlo get api.

* Added member method structure.

* Finished implementation of phlo.run().

* Finished api calls for multi party(except abort transfer).

* Deleted old rest.js file.

* Finished abortTranfer implementation with test cases.

* Finished implementation of member.resumeCall().

* Finished all member functions.

* Reverted debugging logic.

* Removed console.log

* Added samples for all apis. method structure change for abort transfer.

* Resolved nsp issue.

* Audit fix.

* Package Lock Fix.

* Added test proxy for test cases.

* Resolved issue in request.js json format.

* Improved comments.

* Added test cases for .get() methods.

* Removed async await to support older browsers.

* Modified examples of phlo, removed async await.

* Resolved example issues, remove auth credentials.

* Removed es6 declaration syntaxes from phlo example.

* Added logic to pass payload.

* changed beta version 4.1-beta.1

* changed beta version 4.1-beta.1

* changed beta version 4.1-beta.1

* changes in changelog & readme- Add PHLO support
  • Loading branch information
nixonsam authored Mar 11, 2019
1 parent b8d2b11 commit a4e3f9f
Show file tree
Hide file tree
Showing 15 changed files with 6,522 additions and 103 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [4.1.0](https://github.com/plivo/plivo-node/releases/tag/v4.1.0)(2019-03-11)
- Add PHLO support
- Add Multiparty call triggers

## [4.0.6](https://github.com/plivo/plivo-node/releases/tag/v4.0.6)(2019-02-04)
- Bugfix: ListAllMessages API: Fixed filter-by message_time parameter.

Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Install the SDK using [npm](https://www.npmjs.com/package/plivo)

If you have the `0.4.1` version (a.k.a legacy) already installed, you may have to first uninstall it before installing the new version.

For features in beta, use the beta branch:

$ npm install plivo@beta

## Getting started

### Authentication
Expand Down Expand Up @@ -92,6 +96,26 @@ This generates the following XML:
</Response>
```

### Run a PHLO

```javascript
var plivo = require('../dist/rest/client.js');
var PhloClient = plivo.PhloClient;

var authId = 'auth-id';
var authToken = 'auth-token';
var phloId = 'PHLO_ID';
var phloClient = phlo = null;

// Run phlo
phloClient = new PhloClient(authId, authToken);
phloClient.phlo(phloId).run().then(function (result) {
console.log('Phlo run result', result);
}).catch(function (err) {
console.error('Phlo run failed', err);
});
```

### More examples
Refer to the [Plivo API Reference](https://api-reference.plivo.com/latest/node/introduction/overview) for more examples. Also refer to the [guide to setting up dev environment](https://developers.plivo.com/getting-started/setting-up-dev-environment/) on [Plivo Developers Portal](https://developers.plivo.com) to setup an Express server & use it to test out your integration in under 5 minutes.

Expand Down
127 changes: 127 additions & 0 deletions examples/phlo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
var plivo = require('../dist/rest/client-test.js');
var PhloClient = plivo.PhloClient;


var authId = 'auth-id';
var authToken = 'auth-token';
var phloId = 'sample-phlo-id';
var mpcId = 'sample-mpc-id';

var mpcSourceNo = '919920700964';
var mpcTargetNo = '919620074923';
var role = 'agent';
var phloClient = phlo = null;

//Get Phlo details by phlo id
phloClient = new PhloClient(authId, authToken);
phloClient.phlo.get(phloId).then(function (result) {
console.log('phlo details =>', result);
}).catch(function (err) {
console.log('Failed to fetch phlo details', err);
});;


// Run phlo
phloClient = new PhloClient(authId, authToken);
phloClient.phlo(phloId).run().then(function (result) {
console.log('Phlo run result', result);
}).catch(function (err) {
console.error('Phlo run failed', err);
});


/**************************** Multiparty call examples **************************/

// Get multi-party call details
phloClient = new PhloClient(authId, authToken);
phloClient.phlo(phloId).multiPartyCall.get(mpcId).then(function (result) {
console.log('multi party call details api result', result);
}).catch(function (err) {
console.log('multi party call details api failed', err);
})


// Add member to multi party call
phloClient = new PhloClient(authId, authToken);
phloClient.phlo(phloId).multiPartyCall(mpcId).call(mpcSourceNo, mpcTargetNo, role).then(function (result) {
console.log('Multiparty call result', result);
}).catch(function (err) {
console.log('Multiparty call failed', err);
});


// Warm Transfer - multi party call
phloClient = new PhloClient(authId, authToken);
phloClient.phlo(phloId).multiPartyCall(mpcId).warmTransfer(mpcSourceNo, mpcTargetNo, role).then(function (result) {
console.log('Warm transfer result', result);
}).catch(function (err) {
console.log('Warm transfer failed', err);
});


// Cold Transfer - multi party call
phloClient = new PhloClient(authId, authToken);
phloClient.phlo(phloId).multiPartyCall(mpcId).coldTransfer(mpcSourceNo, mpcTargetNo, role).then(function (result) {
console.log('Cold transfer result', result);
}).catch(function (err) {
console.log('Cold transfer failed', err);
});


// Abort Transfer - multi party call
phloClient = new PhloClient(authId, authToken);
phloClient.phlo(phloId).multiPartyCall(mpcId).abortTransfer(mpcSourceNo).then(function (result) {
console.log('abort transfer result', result);
}).catch(function (err) {
console.log('abort transfer failed', err);
});

/**************************** Multiparty call member examples **************************/


/******************** Phlo resource test cases *********************/

// Resume Call - Phlo Member
phloClient = new PhloClient(authId, authToken);
phloClient.phlo(phloId).multiPartyCall(mpcId).member(mpcSourceNo).resumeCall().then(function (result) {
console.log('resume call result', result);
}).catch(function (err) {
console.log('resume call failed', err);
});


// Voice mail drop - Phlo Member
phloClient = new PhloClient(authId, authToken);
phloClient.phlo(phloId).multiPartyCall(mpcId).member(mpcSourceNo).voicemailDrop().then(function (result) {
console.log('voicemail Drop call result -', result);
}).catch(function (err) {
console.log('voicemail Drop call failed', err);
});


// Hangup - Phlo Member
phloClient = new PhloClient(authId, authToken);
phloClient.phlo(phloId).multiPartyCall(mpcId).member(mpcSourceNo).hangup().then(function (result) {
console.log('hangup result - ', result);
}).catch(function (err) {
console.log('hangup failed', err);
});


// Hold - Phlo Member
phloClient = new PhloClient(authId, authToken);
phloClient.phlo(phloId).multiPartyCall(mpcId).member(mpcSourceNo).hold().then(function (result) {
console.log('hold result -', result);
}).catch(function (err) {
console.log('hold failed', err);
});


// Unhold - Phlo Member
phloClient = new PhloClient(authId, authToken);
phloClient.phlo(phloId).multiPartyCall(mpcId).member(mpcSourceNo).unhold().then(function (result) {
console.log('unhold result -', result);
}).catch(function (err) {
console.log('unhold failed', err);
});

4 changes: 2 additions & 2 deletions lib/base.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {extend} from './utils/common.js';
import { extend } from './utils/common.js';

let actionKey = Symbol('api action');
let klassKey = Symbol('constructor');
Expand Down Expand Up @@ -113,7 +113,7 @@ export class PlivoResourceInterface {
client('GET', action, params)
.then(response => {
let objects = [];
Object.defineProperty(objects, 'meta', {value: response.body.meta});
Object.defineProperty(objects, 'meta', { value: response.body.meta });
response.body.objects.forEach(item => {
objects.push(new Klass(client, item));
});
Expand Down
94 changes: 94 additions & 0 deletions lib/resources/phlo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { extend, validate } from '../utils/common.js';
import { PlivoResource, PlivoResourceInterface } from '../base';
import { PhloMultiPartyCall, PhloMultiPartyCallInterface } from "../resources/phloMultipartyCall";
import * as _ from "lodash";

const clientKey = Symbol();
const action = 'phlo/';
const idField = 'phloUuid';

/**
* Represents a Phlo
* @constructor
* @param {function} client - make api call
* @param {object} [data] - data of phlo
*/
export class Phlo extends PlivoResource {
constructor(client, data = {}) {
super(action, Phlo, idField, client);
extend(this, data);

this.client = client;

// Define multiparty call getters
let item = this;
this.multiPartyCall = function (nodeId) {
let dd = new PhloMultiPartyCall(client, { phloId: item.phloId, nodeId: nodeId });
return dd;
};

this.multiPartyCall.get = function (nodeId) {
let dd = new PhloMultiPartyCallInterface(client, { phloId: item.phloId, nodeId: nodeId });
return dd.get(item.phloId, nodeId);
}

}

/**
* run phlo
* @method
* @promise {Boolean} return true if phlo is complete
* @fail {Error} return Error
*/
run(params) {

//Url for phlo running
// https://phlorunner.plivo.com/v1/account/{AUTH_ID}/phlo/{PHLO_ID}
let action = 'account/' + this.authId + '/phlo/' + this.phloId;
return super.executeAction(action, 'POST', params, '');
}

}

/**
* Represents a Phlo Interface
* @constructor
* @param {function} client - make api call
* @param {object} [data] - data of call
*/
export class PhloInterface extends PlivoResourceInterface {

constructor(client, data = {}) {
super(action, Phlo, idField, client);
extend(this, data);
}

/**
* Get A Phlo Detail
* @method
* @param {string} id - phlo uuid to get information of.
* @promise {object} returns Phlo Object
* @fail {Error} returns Error
*/
get(id) {

//Validate id first
let errors = validate([{
field: 'id',
value: id,
validators: ['isRequired']
}]);

if (errors) {
return errors;
}

let params = {
phlo_id: id
};

// Url pattern for getting phlo resource by id
// https://phlorunner.plivo.com/v1/phlo/{phlo_id}
return super.get(id, params);
}
}
85 changes: 85 additions & 0 deletions lib/resources/phloMultiPartyCallMember.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { extend, validate } from '../utils/common.js';
import { PlivoResource, PlivoResourceInterface } from '../base';

const clientKey = Symbol();
const action = 'Phlo/';
const idField = 'phloUuid';

/**
* Represents a Multiparty Call Member
* @constructor
* @param {function} client - make api call
* @param {object} [data] - data of phlo
*/
export class PhloMultiPartyCallMember extends PlivoResource {

constructor(client, data = {}) {
let action = 'phlo/' + data.phloId + '/multi_party_call/' + data.nodeId + '/members/';
super(action, PhloMultiPartyCallMember, idField, client);
extend(this, data);
this.action = action;
this.client = client;
}

resumeCall() {
return this.update('resume_call');
}

voicemailDrop() {
return this.update('voicemail_drop');
}

hangup() {
return this.update('hangup');
}

hold() {
return this.update('hold');
}

unhold() {
return this.update('unhold');
}

update(action) {

let params = {
action: action
};

// Build Url
// https://phlorunner.plivo.com/v1/phlo/{PHLO_ID}/multi_party_call/{NODE_ID}/members/{MemberAddress}
let task = this.action + this.memberAddress;

return super.executeAction(task, 'POST', params, '');
}

}

export class PhloMultiPartyCallMemberInterface extends PlivoResourceInterface {


constructor(client, data = {}) {
let action = 'phlo/' + data.phloId + '/multi_party_call/' + data.nodeId + '/members/';
super(action, PhloMultiPartyCallMember, idField, client);
extend(this, data);
this.action = action;
this.client = client;
}

get(phloId, nodeId, memberAddress) {

//Validate memberAddress first
let errors = validate([{
field: 'memberAddress',
value: memberAddress,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}

return new PhloMultiPartyCallMember(this.client, { phloId: phloId, nodeId: nodeId, memberAddress: memberAddress });

}
}
Loading

0 comments on commit a4e3f9f

Please sign in to comment.