Skip to content

Commit

Permalink
feat: support rc js sdk v4 (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
embbnux committed May 15, 2020
1 parent 693d328 commit 2ec19e9
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 556 deletions.
18 changes: 10 additions & 8 deletions README.md
Expand Up @@ -45,9 +45,9 @@ CDN

## Usage

For this example you will also need to have [RingCentral JS SDK](https://github.com/ringcentral/ringcentral-js#installation) installed.
For this example you will also need to have [RingCentral JS SDK](https://github.com/ringcentral/ringcentral-js/tree/master/sdk#installation) and [RingCentral JS Subscriptions SDK](https://github.com/ringcentral/ringcentral-js/tree/master/subscriptions#installation) installed.

Configure the web-phone
Configure and new Call Control instance:

```js
// npm import
Expand All @@ -61,13 +61,15 @@ var appName = '...';
var appVersion = '...';

var sdk = new RingCentral.SDK({
appKey: appClientId,
appSecret: appClientSecret,
clientId: appClientId,
clientSecret: appClientSecret,
appName: appName,
appVersion: appVersion,
server: RingCentral.SDK.server.production // or .sandbox
});

var subscriptions = new RingCentral.Subscriptions({
sdk: rcsdk
});
var platform = sdk.platform();

platform
Expand All @@ -77,7 +79,7 @@ platform
})
.then(function() {
var rcCallControl = new RingCentralCallControl({ sdk: sdk });
var subscription = sdk.createSubscription();
var subscription = subscriptions.createSubscription();

subscription.setEventFilters(['/restapi/v1.0/account/~/extension/~/telephony/sessions']);
subscription.on(subscription.events.notification, function(msg) {
Expand All @@ -104,12 +106,12 @@ Open `http://localhost:8080/demo/`, and login with RingCentral Sandbox account t

### Init

Firstly, we need to create Call Control instance after user login with [RingCentral JS SDK](https://github.com/ringcentral/ringcentral-js/tree/v3#login). Then connect `onNotificationEvent` with subscription notification event.
Firstly, we need to create Call Control instance after user login with [RingCentral JS SDK](https://github.com/ringcentral/ringcentral-js/tree/v3#login). Then connect `onNotificationEvent` with [subscription](https://github.com/ringcentral/ringcentral-js/tree/master/subscriptions#ringcentral-subscriptions-sdk) notification event.

```js
var rcCallControl = new RingCentralCallControl({ sdk: sdk });
var initialized = false;
var subscription = sdk.createSubscription();
var subscription = subscriptions.createSubscription();
subscription.setEventFilters(['/restapi/v1.0/account/~/extension/~/telephony/sessions']);
subscription.on(subscription.events.notification, function(msg) {
rcCallControl.onNotificationEvent(msg)
Expand Down
9 changes: 5 additions & 4 deletions demo/index.html
Expand Up @@ -261,10 +261,11 @@ <h4 class="modal-title">Incoming Call</h4>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/3.2.2/es6-promise.js"></script>
<script type="text/javascript" src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.20.1.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fetch/0.11.1/fetch.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/ringcentral/ringcentral-js/3.2.2/build/ringcentral.js"></script>
<script type="text/javascript" src="https://unpkg.com/es6-promise@latest/dist/es6-promise.auto.js"></script>
<script type="text/javascript" src="https://unpkg.com/pubnub@latest/dist/web/pubnub.js"></script>
<script type="text/javascript" src="https://unpkg.com/whatwg-fetch@latest/dist/fetch.umd.js"></script>
<script type="text/javascript" src="https://unpkg.com/@ringcentral/sdk@latest/dist/ringcentral.js"></script>
<script type="text/javascript" src="https://unpkg.com/@ringcentral/subscriptions@latest/dist/ringcentral-subscriptions.js"></script>
<script src="../build/index.js"></script>
<script src="./index.js"></script>
</body>
Expand Down
10 changes: 7 additions & 3 deletions demo/index.js
@@ -1,6 +1,7 @@
$(function() {
var rcsdk = null;
var platform = null;
var subscriptions = null;
var loggedIn = false;
var subscription = null;
var rcCallControl = null;
Expand All @@ -26,7 +27,7 @@ $(function() {
}

function initCallControl() {
subscription = rcsdk.createSubscription();
subscription = subscriptions.createSubscription();
var cachedSubscriptionData = rcsdk.cache().getItem('rc-call-control-subscription-key');
if (cachedSubscriptionData) {
try {
Expand Down Expand Up @@ -334,11 +335,14 @@ $(function() {
function show3LeggedLogin(server, appKey, appSecret) {
rcsdk = new RingCentral.SDK({
cachePrefix: 'rc-call-control',
appKey: appKey,
appSecret: appSecret,
clientId: appKey,
clientSecret: appSecret,
server: server,
redirectUri: redirectUri
});
subscriptions = new RingCentral.Subscriptions({
sdk: rcsdk
});

platform = rcsdk.platform(server, appKey, appSecret);

Expand Down
10 changes: 4 additions & 6 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "ringcentral-call-control",
"version": "0.1.4",
"version": "0.2.0",
"main": "lib/index.js",
"license": "MIT",
"repository": {
Expand All @@ -20,22 +20,20 @@
"test:showCoverage": "cat ./coverage/lcov.info | coveralls"
},
"devDependencies": {
"@ringcentral/sdk": "^4.1.0",
"@types/jest": "^24.0.18",
"@types/node": "^14.0.1",
"coveralls": "^3.0.6",
"fetch-mock": "^7.3.9",
"http-server": "^0.11.1",
"http-server": "^0.12.3",
"jest": "^24.9.0",
"ringcentral": "^3.2.2",
"ts-jest": "^24.0.2",
"ts-loader": "^5.3.3",
"tslint": "^5.12.1",
"typescript": "^3.2.4",
"webpack": "^4.29.0",
"webpack-cli": "^3.2.1"
},
"peerDependencies": {
"ringcentral": "^3.2.2"
},
"keywords": [
"ringcentral",
"callcontrol",
Expand Down
56 changes: 28 additions & 28 deletions src/Session.ts
@@ -1,5 +1,5 @@
import { EventEmitter } from "events";
import RingCentral from 'ringcentral';
import { SDK as RingCentralSDK } from '@ringcentral/sdk';

import { formatParty } from './formatParty';

Expand Down Expand Up @@ -195,10 +195,10 @@ function diffParties(oldParties: Party[], updatedParties: Party[]) {
export class Session extends EventEmitter {
private _data: any;
private _eventPartySequences: any;
private _sdk: any;
private _sdk: RingCentralSDK;
private _accountLevel: boolean;

constructor(rawData: SessionData, sdk: RingCentral, accountLevel: boolean) {
constructor(rawData: SessionData, sdk: RingCentralSDK, accountLevel: boolean) {
super();
const { sequence, ...data } = rawData;
this._data = data;
Expand Down Expand Up @@ -324,8 +324,8 @@ export class Session extends EventEmitter {
}

async reload() {
const response = await this._sdk.platform().get(`/account/~/telephony/sessions/${this._data.id}`);
const data = response.json();
const response = await this._sdk.platform().get(`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}`);
const data = await response.json();
data.extensionId = this.data.extensionId;
data.accountId = this.data.accountId;
data.parties = data.parties.map(p => formatParty(p));
Expand All @@ -334,7 +334,7 @@ export class Session extends EventEmitter {

async drop() {
await this._sdk.platform().delete(
`/account/~/telephony/sessions/${this._data.id}`
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}`
);
}

Expand All @@ -348,9 +348,9 @@ export class Session extends EventEmitter {
async hold() {
const oldParty = this.party;
const response = await this._sdk.platform().post(
`/account/~/telephony/sessions/${this._data.id}/parties/${oldParty.id}/hold`
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${oldParty.id}/hold`
);
const newParty = response.json();
const newParty = await response.json();
this.saveNewPartyData(newParty);
this.emit('status', { party: this.party });
return this.party;
Expand All @@ -359,64 +359,64 @@ export class Session extends EventEmitter {
async unhold() {
const oldParty = this.party;
const response = await this._sdk.platform().post(
`/account/~/telephony/sessions/${this._data.id}/parties/${oldParty.id}/unhold`
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${oldParty.id}/unhold`
);
const newParty = response.json();
const newParty = await response.json();
this.saveNewPartyData(newParty);
this.emit('status', { party: this.party });
return this.party;
}

async toVoicemail() {
await this._sdk.platform().post(
`/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/reject`
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/reject`
);
}

async ignore(params: IgnoreParams) {
await this._sdk.platform().post(
`/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/ignore`,
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/ignore`,
params
);
}

async answer(params: AnswerParams) {
await this._sdk.platform().post(
`/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/answer`,
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/answer`,
params
);
}

async reply(params: ReplyWithTextParams) {
const response = await this._sdk.platform().post(
`/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/reply`,
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/reply`,
params,
);
const rawParty = response.json();
const rawParty = await response.json();
this.saveNewPartyData(rawParty);
this.emit('status', { party: this.party });
return this.party;
}

async forward(params: ForwardParams) {
const response = await this._sdk.platform().post(
`/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/forward`,
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/forward`,
params
);
return response.json();
}

async transfer(params: TransferParams) {
const response = await this._sdk.platform().post(
`/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/transfer`,
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/transfer`,
params
);
return response.json();
}

async park() {
const response = await this._sdk.platform().post(
`/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/park`,
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/park`,
);
return response.json();
}
Expand All @@ -436,7 +436,7 @@ export class Session extends EventEmitter {

async flip(params: FlipParams) {
const response = await this._sdk.platform().post(
`/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/flip`,
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/flip`,
params
);
return response.json();
Expand All @@ -445,11 +445,11 @@ export class Session extends EventEmitter {
async updateParty(params: PartyParams) {
const response = await this._sdk.platform().send({
method: 'PATCH',
url: `/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}`,
url: `/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}`,
query: null,
body: params
});
const rawParty = response.json();
const rawParty = await response.json();
this.saveNewPartyData(rawParty);
return rawParty;
}
Expand All @@ -468,9 +468,9 @@ export class Session extends EventEmitter {

async createRecord() {
const response = await this._sdk.platform().post(
`/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/recordings`,
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/recordings`,
);
const recording = response.json();
const recording = await response.json();
const recordings = (this.party.recordings || []).filter(r => r.id !== recording.id);
recordings.push(recording);
this.party.recordings = recordings
Expand All @@ -480,13 +480,13 @@ export class Session extends EventEmitter {
async updateRecord(params: RecordParams) {
const response = await this._sdk.platform().send({
method: 'PATCH',
url: `/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/recordings/${params.id}`,
url: `/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/recordings/${params.id}`,
query: null,
body: {
active: params.active,
}
});
const recording = response.json();
const recording = await response.json();
const recordings = (this.party.recordings || []).filter(r => r.id !== recording.id);
recordings.push(recording);
this.party.recordings = recordings
Expand All @@ -505,23 +505,23 @@ export class Session extends EventEmitter {

async supervise(params: SuperviseParams) {
const response = await this._sdk.platform().post(
`/account/~/telephony/sessions/${this._data.id}/supervise`,
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/supervise`,
params
);
return response.json();
}

async bringInParty(params: BringInParams) {
const response = await this._sdk.platform().post(
`/account/~/telephony/sessions/${this._data.id}/parties/bring-in`,
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/bring-in`,
params,
);
return response.json();
}

async removeParty(partyId: string) {
const response = await this._sdk.platform().delete(
`/account/~/telephony/sessions/${this._data.id}/parties/${partyId}`
`/restapi/v1.0/account/~/telephony/sessions/${this._data.id}/parties/${partyId}`
);
return response.json();
}
Expand Down

0 comments on commit 2ec19e9

Please sign in to comment.