diff --git a/examples/tone_analyzer.v3.js b/examples/tone_analyzer.v3.js index f551d566af..b5777f9a40 100644 --- a/examples/tone_analyzer.v3.js +++ b/examples/tone_analyzer.v3.js @@ -12,6 +12,28 @@ tone_analyzer.tone({ text: 'Greetings from Watson Developer Cloud!' }, function( if (err) { console.log(err); } else { + console.log('tone endpoint:'); + console.log(JSON.stringify(tone, null, 2)); + } +}); + +const utterances = { + utterances: [ + { text: 'My charger isn’t working.', user: 'customer' }, + { text: 'Thanks for reaching out. Can you give me some more detail about the issue?', user: 'agent' }, + { + text: "I put my charger in my phone last night to charge and it isn't working. Which is ridiculous, it's a new charger, I bought it yesterday.", + user: 'customer' + }, + { text: 'I’m sorry you’re having issues with charging. What kind of charger do you have?', user: 'agent' } + ] +}; + +tone_analyzer.tone_chat({ utterances: utterances }, function(err, tone) { + if (err) { + console.log(err); + } else { + console.log('tone_chat endpoint:'); console.log(JSON.stringify(tone, null, 2)); } }); diff --git a/package.json b/package.json index 7b9ca40c61..3edde583e9 100644 --- a/package.json +++ b/package.json @@ -119,4 +119,4 @@ "minify": "uglifyjs --compress --mangle --screw-ie8 dist/watson.js --output dist/watson.min.js --preamble \"// Watson Developer Cloud\n// JavaScript SDK$npm_package_version\n// Generated at `date`\n// Copyright IBM ($npm_package_license)\n// $npm_package_homepage\"", "build": "npm run browserify && npm run minify" } -} +} \ No newline at end of file diff --git a/test/unit/test.tone_analyzer.v3.js b/test/unit/test.tone_analyzer.v3.js index f626ca5166..5cf72d4ce6 100644 --- a/test/unit/test.tone_analyzer.v3.js +++ b/test/unit/test.tone_analyzer.v3.js @@ -113,4 +113,39 @@ describe('tone_analyzer.v3', function() { assert.equal(req.headers['x-custom-header'], 'foo'); assert.equal(req.headers['accept-language'], 'es'); }); + + // Tone Chat Endpoint API - test for valid payload + it('tone-chat API endpoint should generate a valid payload with utterances json payload', function(done) { + const tone_chat_path = '/v3/tone_chat'; + const tone_chat_request = { + utterances: [ + { text: 'My charger isn’t working.', user: 'customer' }, + { text: 'Thanks for reaching out. Can you give me some more detail about the issue?', user: 'agent' }, + { + text: "I put my charger in my phone last night to charge and it isn't working. Which is ridiculous, it's a new charger, I bought it yesterday.", + user: 'customer' + }, + { text: 'I’m sorry you’re having issues with charging. What kind of charger do you have?', user: 'agent' } + ] + }; + const tone_chat_response = { + tree: {} + }; + + const expectation = nock(service.url).post('/v3/tone_chat' + '?version=2016-05-19', tone_chat_request.utterances).reply(200, tone_chat_response); + + // run tests + const req = tone_analyzer.tone_chat(tone_chat_request, function(err, res) { + assert(req); + const url = service.url + tone_chat_path; + assert.equal(req.uri.href.slice(0, url.length), url); + assert.equal(req.uri.href, service.url + tone_chat_path + '?version=2016-05-19'); + assert.equal(req.method, 'POST'); + assert.equal(req.headers['content-type'], 'application/json'); + assert.equal(req.headers['accept'], 'application/json'); + assert.ifError(err); + assert(expectation.isDone()); + done(); + }); + }); }); diff --git a/tone-analyzer/v3.js b/tone-analyzer/v3.js index 719412f07c..a88d61cc7a 100644 --- a/tone-analyzer/v3.js +++ b/tone-analyzer/v3.js @@ -88,4 +88,35 @@ ToneAnalyzerV3.prototype.tone = function(params, callback) { return requestFactory(parameters, callback); }; +/** + * @param {Object} params The parameters to call the service + * @param {Object} [params.headers] - The header parameters. + * @param {string} [params.headers.accept-language=en] - The desired language of the response. + * @param {string} [params.headers.content-type=application/json] - The content type of the request: application/json (the default). + * @param {string} [params.headers.content-language=en] - The language of the input text for the request: en (English) (the default) + * @param {string} [params.headers.accept=application/json] - The desired content type of the response: application/json (the default) + * @param {string} [params.utterances] - The utterances to analyze. Utterances must be a JSON object. + * + * @param callback The callback. + */ +ToneAnalyzerV3.prototype.tone_chat = function(params, callback) { + const parameters = { + requiredParams: ['utterances'], + originalParams: params, + options: { + url: '/v3/tone_chat', + method: 'POST', + body: JSON.stringify(params.utterances) + }, + defaultOptions: extend(true, this._options, { + headers: { + accept: 'application/json', + 'content-type': 'application/json' + } + }) + }; + + return requestFactory(parameters, callback); +}; + module.exports = ToneAnalyzerV3;