1515 **/
1616
1717module . exports = function ( RED ) {
18- const SERVICE_IDENTIFIER = 'natural-language-understanding' ;
19- const NaturalLanguageUnderstandingV1 = require ( 'watson-developer-cloud/natural-language-understanding/v1' ) ;
18+ const SERVICE_IDENTIFIER = 'natural-language-understanding' ,
19+ NaturalLanguageUnderstandingV1 = require ( 'ibm-watson/natural-language-understanding/v1' ) ,
20+ { IamAuthenticator } = require ( 'ibm-watson/auth' ) ;
2021
2122 const NLU_FEATURES = {
2223 'categories' : 'categories' ,
@@ -44,11 +45,6 @@ module.exports = function (RED) {
4445 endpoint = '' ,
4546 sEndpoint = 'https://gateway.watsonplatform.net/natural-language-understanding/api' ;
4647
47- function reportError ( node , msg , message ) {
48- var messageTxt = message . error ? message . error : message ;
49- node . status ( { fill :'red' , shape :'dot' , text : messageTxt } ) ;
50- node . error ( message , msg ) ;
51- }
5248
5349 function initialCheck ( u , p , k ) {
5450 if ( ! k && ( ! u || ! p ) ) {
@@ -84,7 +80,7 @@ module.exports = function (RED) {
8480 var limitCharacters = parseInt ( config . limittextcharacters ) ;
8581
8682 if ( ! isNaN ( limitCharacters ) && 0 < limitCharacters ) {
87- options . limit_text_characters = limitCharacters ;
83+ options . limitTextCharacters = limitCharacters ;
8884 }
8985
9086 return Promise . resolve ( ) ;
@@ -216,35 +212,37 @@ module.exports = function (RED) {
216212 }
217213
218214 function invokeService ( options ) {
219- var nlu = null ,
215+ let nlu = null ,
216+ authSettings = { } ;
220217 serviceSettings = {
221- version : '2018-11-16 ' ,
218+ version : '2019-07-12 ' ,
222219 headers : {
223220 'User-Agent' : pkg . name + '-' + pkg . version
224221 }
225222 } ;
226223
227224 if ( apikey ) {
228- serviceSettings . iam_apikey = apikey ;
225+ authSettings . apikey = apikey ;
229226 } else {
230- serviceSettings . username = username ;
231- serviceSettings . password = password ;
227+ authSettings . username = username ;
228+ authSettings . password = password ;
232229 }
230+ serviceSettings . authenticator = new IamAuthenticator ( authSettings ) ;
233231
234232 if ( endpoint ) {
235233 serviceSettings . url = endpoint ;
236234 }
237235
238236 nlu = new NaturalLanguageUnderstandingV1 ( serviceSettings ) ;
239237
240- var p = new Promise ( function resolver ( resolve , reject ) {
241- nlu . analyze ( options , function ( err , data ) {
242- if ( err ) {
238+ var p = new Promise ( function resolver ( resolve , reject ) {
239+ nlu . analyze ( options )
240+ . then ( ( response ) => {
241+ resolve ( response ) ;
242+ } )
243+ . catch ( ( err ) => {
243244 reject ( err ) ;
244- } else {
245- resolve ( data ) ;
246- }
247- } ) ;
245+ } ) ;
248246 } ) ;
249247 return p ;
250248 }
@@ -267,7 +265,7 @@ module.exports = function (RED) {
267265 RED . nodes . createNode ( this , config ) ;
268266 var node = this ;
269267
270- this . on ( 'input' , function ( msg ) {
268+ this . on ( 'input' , function ( msg , send , done ) {
271269 var message = '' ,
272270 options = { } ;
273271
@@ -278,7 +276,7 @@ module.exports = function (RED) {
278276 apikey = sApikey || this . credentials . apikey ;
279277
280278 endpoint = sEndpoint ;
281- if ( ( ! config [ 'default-endpoint' ] ) && config [ 'service-endpoint' ] ) {
279+ if ( config [ 'service-endpoint' ] ) {
282280 endpoint = config [ 'service-endpoint' ] ;
283281 }
284282
@@ -304,11 +302,16 @@ module.exports = function (RED) {
304302 } )
305303 . then ( function ( data ) {
306304 msg . features = data ;
307- node . send ( msg ) ;
305+ if ( data && data . result ) {
306+ msg . features = data . result ;
307+ }
308+ send ( msg ) ;
308309 node . status ( { } ) ;
310+ done ( ) ;
309311 } )
310312 . catch ( function ( err ) {
311- reportError ( node , msg , err ) ;
313+ let errMsg = payloadutils . reportError ( node , msg , err ) ;
314+ done ( errMsg ) ;
312315 } ) ;
313316
314317 } ) ;
0 commit comments