Skip to content

Commit

Permalink
Implemented Call Flow methods and updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
trololov committed Nov 12, 2019
1 parent ffcc214 commit f2999f4
Show file tree
Hide file tree
Showing 3 changed files with 320 additions and 2 deletions.
27 changes: 26 additions & 1 deletion README.md
Expand Up @@ -61,7 +61,32 @@ Or in case of an error:
]
}
```

Notes
-------------
Messaging and Voice API use different pagination semantics:

**Messaging API** uses limit and offset params for list methods (where applicable)
````javascript
// list conversations
//In this case 20 is limit and 0 is offset
messagebird.conversations.list(20, 0, function (err, response) {
if (err) {
return console.log(err);
}
console.log(response);
});
````
**Voice API** uses page and perPage params for list methods (where applicable)
````javascript
// list Call Flows
// In this case 1 is page, 2 is items per page
messagebird.callflows.list(1, 2, function (err, response) {
if (err) {
return console.log(err);
}
console.log(response);
});
````
Verifying Signatures
-------------

Expand Down
106 changes: 106 additions & 0 deletions lib/messagebird.js
Expand Up @@ -93,6 +93,7 @@ module.exports = function (accessKey, timeout, features) {
options.headers = extend(options.headers || {}, requestParams.headers || {})

request = http.request(options);
// console.log(options)

// set timeout
request.on('socket', function (socket) {
Expand Down Expand Up @@ -875,6 +876,111 @@ module.exports = function (accessKey, timeout, features) {

},

callflows: {

/**
* Lists existing call flows.
*
* @param {Function} callback
* @return void
*/
list: function (page, perpage, callback) {
var params = null;

if (typeof callback === 'function') {
params = {
page: page,
perPage: perpage
};
} else {
callback = page;
}

httpRequest({
hostname: VOICE_ENDPOINT,
method: 'GET',
path: '/call-flows',
params: params},
callback);
},

/**
* Creates a new call flow, params are mandatory.
*
* @param {String} name
* @param {Object} params
* @param {Function} callback
* @return void
*/
create: function (params, callback) {
httpRequest(
{
hostname: VOICE_ENDPOINT,
method: 'POST',
path: '/call-flows',
params: params},
callback);
},

/**
* Get a call flow
*
* @param {String} flowId
* @param {Function} callback
* @return {void}
*/
read: function (flowId, callback) {
httpRequest(
{
hostname: VOICE_ENDPOINT,
method: 'GET',
path: '/call-flows/'+flowId,
},
callback
);
},

/**
* Deletes an existing call flow. The callback is invoked with an error if
* applicable, but the data will never contain anything meaningful as the
* API returns an empty response for successful deletes.
*
* @param {String} flowId
* @param {Function} callback
* @return void
*/
delete: function (flowId, callback) {
httpRequest(
{
hostname: VOICE_ENDPOINT,
method: 'DELETE',
path: '/call-flows/'+flowId
},
callback,
);
},

/**
* Updates an existing call flow. Params are required.
*
* @param {String} flowId
* @param {Object} params
* @param {Function} callback
* @return void
*/
update: function (flowId, params, callback) {

httpRequest(
{
hostname: VOICE_ENDPOINT,
method: 'PUT',
path: '/call-flows/'+flowId,
params: params
},
callback
);
}
},
groups: {

/**
Expand Down
189 changes: 188 additions & 1 deletion lib/test.js
Expand Up @@ -49,7 +49,9 @@ var cache = {
phoneNumber: number
},

transcription: {}
transcription: {},

callflow: {}
};


Expand Down Expand Up @@ -1499,6 +1501,191 @@ queue.push(function () {
});
});

CALLFLOW_EXAMPLE={
data: [
{
id: "id#1",
title: "title #1",
steps: [
{
id: "step #1",
action: "action",
options: {
destination: "dest $1"
}
}
],
record: false,
default: false,
createdAt: "2019-11-04T15:38:01Z",
updatedAt: "2019-11-04T15:38:01Z",
_links: {
self: "/call-flows/id#1"
}
},
{
id: "id#2",
title: "title #2",
steps: [
{
id: "step #1",
action: "action",
options: {
destination: "dest $2"
}
}
],
record: false,
default: false,
createdAt: "2019-11-04T15:38:01Z",
updatedAt: "2019-11-04T15:38:01Z",
_links: {
self: "/call-flows/id#2"
}
},
{
id: "id#3",
title: "title #3",
steps: [
{
id: "step #1",
action: "action",
options: {
destination: "dest $3"
}
}
],
record: false,
default: false,
createdAt: "2019-11-04T15:38:01Z",
updatedAt: "2019-11-04T15:38:01Z",
_links: {
self: "/call-flows/id#3"
}
}
],
pagination: {
totalCount: 3,
pageCount: 1,
currentPage: 1,
perPage: 10
}
}

CALLFLOW_EXAMPLE_PAGE={
data: [
{
id: 'id#1',
title: 'title #1',
steps: [
{
id: 'step #1',
action: 'action',
options: {
destination: 'dest $1'
}
}
],
record: false,
default: false,
createdAt: '2019-11-04T15:38:01Z',
updatedAt: '2019-11-04T15:38:01Z',
_links: {
self: '/call-flows/id#1'
}
}
],
pagination: {
totalCount: 1,
pageCount: 1,
currentPage: 1,
perPage: 10
}
}

queue.push(function () {
nock(VOICE_ENDPOINT)
.get('/call-flows')
.reply(200,{});
messagebird.callflows.list(function (err, data) {
doTest(err, 'callflows.list.empty', []);
})
})

queue.push(function () {
nock(VOICE_ENDPOINT)
.get('/call-flows')
.reply(200,CALLFLOW_EXAMPLE);
messagebird.callflows.list(function (err, response) {
doTest(err, 'callflows.list.default', [
['.response.data[0].id', response.data[0].id === 'id#1'],
['.response.data[0].id', response.data[0].title === 'title #1'],
['length of array response == 3', response.data.length === 3],
['totalCount == 3', response.pagination.totalCount === 3]
]);
})
})

queue.push(function () {
nock(VOICE_ENDPOINT)
.get('/call-flows')
.reply(200,CALLFLOW_EXAMPLE_PAGE);
messagebird.callflows.list(1, 1, function (err, response) {
doTest(err, 'callflows.list.paged', [
['.response.data[0].id', response.data[0].id === 'id#1'],
['.response.data[0].id', response.data[0].title === 'title #1'],
['length of array response == 1', response.data.length === 1],
['totalCount == 1', response.pagination.totalCount === 1]
]);
})
})

queue.push(function () {
nock(VOICE_ENDPOINT)
.get('/call-flows/id#1')
.reply(200,CALLFLOW_EXAMPLE_PAGE);
messagebird.callflows.read("id#1", function (err, response) {
doTest(err, 'callflows.read', [
['.response.data[0].id', response.data[0].id === 'id#1'],
['.response.data[0].id', response.data[0].title === 'title #1'],
['length of array response == 1', response.data.length === 1]
]);
})
})

queue.push(function () {
nock(VOICE_ENDPOINT)
.delete('/call-flows/id#1')
.reply(204, '');

messagebird.callflows.delete('id#1', function (err) {
doTest(err, 'callflows.delete', []);
});
});

queue.push(function () {
nock(VOICE_ENDPOINT)
.post('/call-flows')
.reply(200, CALLFLOW_EXAMPLE_PAGE);

messagebird.callflows.create(CALLFLOW_EXAMPLE_PAGE.data[0], function (err, response) {
doTest(err, 'callflows.create', [
['.response.data[0].id', response.data[0].id === 'id#1'],
['.response.data[0].id', response.data[0].title === 'title #1']
]);
});
});

queue.push(function () {
nock(VOICE_ENDPOINT)
.put('/call-flows/id#1')
.reply(204, '');

messagebird.callflows.update('id#1', {title: 'title_new'}, function (err, response) {
doTest(err, 'callflows.update', []);
});
});

queue.push(function () {
nock('https://rest.messagebird.com')
.post('/groups', '{"name":"friends"}')
Expand Down

0 comments on commit f2999f4

Please sign in to comment.