1616
1717module . exports = function ( RED ) {
1818 const SERVICE_IDENTIFIER = 'assistant' ,
19- OLD_SERVICE_IDENTIFIER = 'conversation' ;
19+ OLD_SERVICE_IDENTIFIER = 'conversation' ,
20+ SERVICE_VERSION = '2018-11-08' ;
2021
2122 var pkg = require ( '../../package.json' ) ,
2223 AssistantV2 = require ( 'watson-developer-cloud/assistant/v2' ) ,
@@ -84,14 +85,14 @@ module.exports = function(RED) {
8485 return Promise . resolve ( ) ;
8586 }
8687
87- function idCheck ( msg , config ) {
88+ function idCheck ( msg ) {
8889 if ( ! config . assistant_id && ! ( msg . params && msg . params . assistant_id ) ) {
8990 return Promise . reject ( 'Missing assistant_id. Check node documentation.' ) ;
9091 }
9192 return Promise . resolve ( ) ;
9293 }
9394
94- function setSessionID ( msg , config ) {
95+ function setSessionID ( msg ) {
9596 let session_id = null ;
9697
9798 if ( ! config . multisession ) {
@@ -128,6 +129,8 @@ module.exports = function(RED) {
128129 }
129130
130131 function setAdditionalContext ( msg , params ) {
132+ // needs to go in context.skills['main skill']['user_defined']
133+
131134 if ( msg . additional_context ) {
132135 params . context = params . context ? params . context : { } ;
133136
@@ -139,16 +142,16 @@ module.exports = function(RED) {
139142 }
140143 }
141144
142- function setAssistantID ( msg , params , config ) {
145+ function setAssistantID ( msg , params ) {
143146 checkAndSet ( config , params , 'assistant_id' ) ;
144147 if ( msg . params ) {
145148 checkAndSet ( msg . params , params , 'assistant_id' ) ;
146149 }
147150 }
148151
149- function setInputOptions ( msg , params , config ) {
152+ function setInputOptions ( msg , params ) {
150153 // Setting the flags this way works as their default
151- // values are false.
154+ // values are false.
152155 [ 'alternate_intents' ,
153156 'return_context' ,
154157 'restart' ,
@@ -160,48 +163,170 @@ module.exports = function(RED) {
160163 } ) ;
161164 }
162165
163- function buildInputParams ( msg , config ) {
166+ function setParamInputs ( msg , params ) {
167+ if ( msg . params ) {
168+ [ 'intents' ,
169+ 'entities' ] . forEach ( ( f ) => {
170+ checkAndSet ( msg . params , params , f ) ;
171+ } ) ;
172+ }
173+ }
174+
175+ function buildInputParams ( msg ) {
164176 let params = {
165177 'input' : {
166178 'message_type' : 'text' ,
167179 'text' : msg . payload ,
168180 'options' : { }
169181 } ,
170- 'session_id' : setSessionID ( msg , config )
182+ 'session_id' : setSessionID ( msg )
171183 } ;
172184
173185 let context = setContext ( msg , params . session_id ) ;
174186 if ( context ) {
175187 params . context = context ;
176188 }
177189 setAdditionalContext ( msg , params ) ;
178- setAssistantID ( msg , params , config ) ;
179- setInputOptions ( msg , params , config ) ;
190+ setAssistantID ( msg , params ) ;
191+ setInputOptions ( msg , params ) ;
192+ setParamInputs ( msg , params ) ;
180193
181194 //verifyOptionalInputs(node, msg, config, params);
182195
183196 return Promise . resolve ( params ) ;
184197 }
185198
199+ function setServiceSettings ( msg , creds ) {
200+ const serviceSettings = {
201+ headers : {
202+ 'User-Agent' : pkg . name + '-' + pkg . version
203+ }
204+ } ;
205+ let endpoint = '' ,
206+ optoutLearning = false ,
207+ version = SERVICE_VERSION ;
208+
209+ if ( creds . apikey ) {
210+ serviceSettings . iam_apikey = creds . apikey ;
211+ } else {
212+ serviceSettings . username = creds . username ;
213+ serviceSettings . password = creds . password ;
214+ }
215+
216+ if ( service ) {
217+ endpoint = service . url ;
218+ }
219+ if ( ! config [ 'default-endpoint' ] && config [ 'service-endpoint' ] ) {
220+ endpoint = config [ 'service-endpoint' ] ;
221+ }
222+
223+ if ( config [ 'optout-learning' ] ) {
224+ optoutLearning = true ;
225+ }
226+
227+ if ( config [ 'timeout' ] && config [ 'timeout' ] !== '0' && isFinite ( config [ 'timeout' ] ) ) {
228+ serviceSettings . timeout = parseInt ( config [ 'timeout' ] ) ;
229+ }
230+
231+ // Look for message overrides
232+ if ( msg . params ) {
233+ if ( msg . params . endpoint ) {
234+ endpoint = msg . params . endpoint ;
235+ }
236+ if ( msg . params . version ) {
237+ version = msg . params . version ;
238+ }
239+ if ( ( msg . params [ 'optout_learning' ] ) ) {
240+ optoutLearning = true ;
241+ }
242+ if ( msg . params . timeout !== '0' && isFinite ( msg . params . timeout ) ) {
243+ serviceSettings . timeout = parseInt ( msg . params . timeout ) ;
244+ }
245+ if ( msg . params . disable_ssl_verification ) {
246+ serviceSettings . disable_ssl_verification = true ;
247+ }
248+ }
249+
250+ serviceSettings . version = version ;
251+ if ( endpoint ) {
252+ serviceSettings . url = endpoint ;
253+ }
254+ if ( optoutLearning ) {
255+ serviceSettings . headers = serviceSettings . headers || { } ;
256+ serviceSettings . headers [ 'X-Watson-Learning-Opt-Out' ] = '1' ;
257+ }
258+
259+ return Promise . resolve ( serviceSettings ) ;
260+ }
261+
262+ function buildService ( settings ) {
263+ node . service = new AssistantV2 ( settings ) ;
264+ return Promise . resolve ( )
265+ }
266+
267+ function checkSession ( params ) {
268+ return new Promise ( function resolver ( resolve , reject ) {
269+ if ( params . session_id ) {
270+ resolve ( ) ;
271+ } else {
272+ node . service . createSession ( {
273+ assistant_id : params . assistant_id
274+ } , function ( err , response ) {
275+ if ( err ) {
276+ console . log ( 'Error Detected' ) ;
277+ reject ( err ) ;
278+ } else {
279+ console . log ( 'Data returned' )
280+ console . log ( response ) ;
281+ if ( response && response . session_id ) {
282+ params . session_id = response . session_id ;
283+ if ( ! config . multisession ) {
284+ node . context ( ) . flow . set ( 'session_id' , params . session_id ) ;
285+ }
286+ resolve ( ) ;
287+ } else {
288+ reject ( 'Unable to set session' ) ;
289+ }
290+ }
291+ } ) ;
292+ }
293+ } ) ;
294+ }
295+
186296 this . on ( 'input' , function ( msg ) {
187- node . status ( { } ) ;
297+ var creds = setCredentials ( msg ) ,
298+ params = { } ;
188299
189- var creds = setCredentials ( msg ) ;
300+ node . status ( { } ) ;
190301
191302 credentialCheck ( creds . username , creds . password , creds . apikey )
192303 . then ( function ( ) {
193304 return payloadCheck ( msg ) ;
194305 } )
195306 . then ( function ( ) {
196- return idCheck ( msg , config ) ;
307+ return idCheck ( msg ) ;
197308 } )
198309 . then ( function ( ) {
199- return buildInputParams ( msg , config ) ;
310+ return buildInputParams ( msg ) ;
200311 } )
201- . then ( function ( params ) {
312+ . then ( function ( p ) {
313+ params = p ;
202314 console . log ( 'params have been built' ) ;
203315 console . log ( params ) ;
204- msg . payload = 'No functionality yet' ;
316+ return setServiceSettings ( msg , creds ) ;
317+
318+ } )
319+ . then ( function ( settings ) {
320+ console . log ( 'service settings have been built' ) ;
321+ console . log ( settings ) ;
322+ return buildService ( settings ) ;
323+ } )
324+ . then ( function ( ) {
325+ console . log ( 'service is ready' ) ;
326+ return checkSession ( params ) ;
327+ } )
328+ . then ( function ( ) {
329+ msg . payload = 'Not complete yet' ;
205330 return Promise . resolve ( ) ;
206331 } )
207332 . then ( function ( ) {
0 commit comments