Skip to content

Commit

Permalink
Update for new tone-chat endpoint (watson-developer-cloud#429)
Browse files Browse the repository at this point in the history
* Changed ToneAnalyzerV3.URL to the dev staging url and added in a new function for tone_chat.

* added sample call to tone_chat endpoint for customer care

* temp fix

* updating params check to use requiredParams and originalParams as per watson-developer-cloud#429

* removing URL from ToneAnalyzerV3 function as per watson-developer-cloud#429

* changing require statement back to watson-developer-cloud/tone-analyzer/v3 as per watson-developer-cloud#429

* updating sample utterances in tone_analyzer examples to match that of the new Customer Engagement demo

* adding jsdoc to tone-chat function as per watson-developer-cloud#429

* adding back the URL for tone analyzer as per watson-developer-cloud#429

* cleaning up lint errors

* adding unit test for tone_chat endpoint to tone_analyzer

* adding a unit test to tone_analyzer_v3 to check that the tone_chat endpoint generates a valid payload
  • Loading branch information
aprilwebster authored and germanattanasio committed Apr 18, 2017
1 parent 54db6ad commit 971f4b6
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
22 changes: 22 additions & 0 deletions examples/tone_analyzer.v3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
35 changes: 35 additions & 0 deletions test/unit/test.tone_analyzer.v3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
31 changes: 31 additions & 0 deletions tone-analyzer/v3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 971f4b6

Please sign in to comment.