From f2999f462590f308a758b19cef2424c2d4dcfae2 Mon Sep 17 00:00:00 2001 From: Konstantin Trololov Date: Tue, 5 Nov 2019 15:08:23 +0100 Subject: [PATCH] Implemented Call Flow methods and updated README --- README.md | 27 ++++++- lib/messagebird.js | 106 +++++++++++++++++++++++++ lib/test.js | 189 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 320 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 03dc23b..19e98e1 100644 --- a/README.md +++ b/README.md @@ -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 ------------- diff --git a/lib/messagebird.js b/lib/messagebird.js index 04fc3f7..655c669 100644 --- a/lib/messagebird.js +++ b/lib/messagebird.js @@ -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) { @@ -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: { /** diff --git a/lib/test.js b/lib/test.js index fbb146f..9d490a2 100644 --- a/lib/test.js +++ b/lib/test.js @@ -49,7 +49,9 @@ var cache = { phoneNumber: number }, - transcription: {} + transcription: {}, + + callflow: {} }; @@ -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"}')