Skip to content

Commit d6820d6

Browse files
committed
Migrate to ibm-watson
1 parent 50870f7 commit d6820d6

4 files changed

Lines changed: 57 additions & 56 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-node-watson",
3-
"version": "0.8.2",
3+
"version": "0.9.0",
44
"description": "A collection of Node-RED nodes for IBM Watson services",
55
"dependencies": {
66
"async": "^1.5.2",

services/assistant/v1.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<input type="password" id="node-input-apikey" placeholder="API Key"></input>
4040
</div>
4141

42-
<div class="form-row">
42+
<div class="form-row credentials">
4343
<label for="node-input-service-endpoint"><i class="fa fa-tag"></i> Service Endpoint</label>
4444
<input type="text" id="node-input-service-endpoint" placeholder="https://gateway.watsonplatform.net/assistant/api">
4545
</div>

services/assistant/v2.html

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@
4141
</div>
4242

4343
<div class="form-row credentials">
44-
<label>&nbsp;</label>
45-
<input type="checkbox" id="node-input-default-endpoint" style="display: inline-block; width: auto; vertical-align: top;">
46-
<label for="node-input-default-endpoint" style="width: 70%;"> Use Default Service Endpoint</label>
47-
</div>
48-
<div class="form-row">
4944
<label for="node-input-service-endpoint"><i class="fa fa-tag"></i> Service Endpoint</label>
5045
<input type="text" id="node-input-service-endpoint" placeholder="https://gateway.watsonplatform.net/assistant/api">
5146
</div>
@@ -115,7 +110,7 @@
115110
<li><code>msg.payload</code> : the message of the Assistant to analyse. Format: String </li>
116111
<li><code>msg.params.session_id</code> (optional): unique identifier for the conversation session.
117112
If this field is not provided then the node will generate a new session_id and
118-
return itt as part of the response. If the node is not used in multi-session mode, then
113+
return it as part of the response. If the node is not used in multi-session mode, then
119114
a session_id need not be provided, to reset the session_id in single-session mode
120115
send a null value as the session_id. Format: String </li>
121116
<li><code>msg.params.assistant_id</code> : unique identifier of the assistant to be used. Could be also configured in the node. Format: String </li>
@@ -171,15 +166,6 @@
171166
$('#assistantv2-form-tips').hide();
172167
}
173168
});
174-
175-
$('input#node-input-default-endpoint').change(function () {
176-
var checked = $('input#node-input-default-endpoint').prop('checked');
177-
if (checked) {
178-
$('#node-input-service-endpoint').parent().hide();
179-
} else {
180-
$('#node-input-service-endpoint').parent().show();
181-
}
182-
});
183169
}
184170

185171
assistantV2.checkCredentials = function () {
@@ -205,8 +191,7 @@
205191
category: 'IBM Watson',
206192
defaults: {
207193
name: { value: '' },
208-
'default-endpoint' : {value: true},
209-
'service-endpoint' : {value: 'https://gateway.watsonplatform.net/assistant/api'},
194+
'service-endpoint' : {value: ''},
210195
assistant_id: {value: ''},
211196
debug: {value: false},
212197
restart: {value: false},

services/assistant/v2.js

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
module.exports = function(RED) {
1818
const SERVICE_IDENTIFIER = 'assistant',
1919
OLD_SERVICE_IDENTIFIER = 'conversation',
20-
SERVICE_VERSION = '2018-11-08';
20+
SERVICE_VERSION = '2018-11-08',
21+
AssistantV2 = require('ibm-watson/assistant/v2'),
22+
{ IamAuthenticator } = require('ibm-watson/auth');
2123

2224
var pkg = require('../../package.json'),
23-
AssistantV2 = require('watson-developer-cloud/assistant/v2'),
2425
serviceutils = require('../../utilities/service-utils'),
2526
payloadutils = require('../../utilities/payload-utils'),
2627
service = null,
@@ -64,7 +65,7 @@ module.exports = function(RED) {
6465
creds.password = msg.params.password;
6566
}
6667
if (msg.params.apikey) {
67-
creds.apiKey = msg.params.apikey;
68+
creds.apikey = msg.params.apikey;
6869
}
6970
}
7071

@@ -141,6 +142,10 @@ module.exports = function(RED) {
141142
if (msg.params) {
142143
checkAndSet(msg.params, params, 'assistant_id');
143144
}
145+
if (params && params['assistant_id']) {
146+
params.assistantId = params['assistant_id'];
147+
delete params['assistant_id'];
148+
}
144149
}
145150

146151
function setInputOptions(msg, params) {
@@ -173,7 +178,7 @@ module.exports = function(RED) {
173178
'text' : msg.payload,
174179
'options' : {}
175180
},
176-
'session_id' : setSessionID(msg)
181+
'sessionId' : setSessionID(msg)
177182
};
178183

179184
let context = setContext(msg, params);
@@ -190,8 +195,9 @@ module.exports = function(RED) {
190195
}
191196

192197
function setServiceSettings(msg, creds) {
193-
const serviceSettings = {
194-
headers: {
198+
let authSettings = {};
199+
let serviceSettings = {
200+
headers: {
195201
'User-Agent': pkg.name + '-' + pkg.version
196202
}
197203
};
@@ -201,16 +207,17 @@ module.exports = function(RED) {
201207
version = SERVICE_VERSION;
202208

203209
if (creds.apikey) {
204-
serviceSettings.iam_apikey = creds.apikey;
210+
authSettings.apikey = creds.apikey;
205211
} else {
206-
serviceSettings.username = creds.username;
207-
serviceSettings.password = creds.password;
212+
authSettings.username = creds.username;
213+
authSettings.password = creds.password;
208214
}
215+
serviceSettings.authenticator = new IamAuthenticator(authSettings);
209216

210217
if (service) {
211218
endpoint = service.url;
212219
}
213-
if (!config['default-endpoint'] && config['service-endpoint']) {
220+
if (config['service-endpoint']) {
214221
endpoint = config['service-endpoint'];
215222
}
216223

@@ -260,41 +267,49 @@ module.exports = function(RED) {
260267

261268
function checkSession(params) {
262269
return new Promise(function resolver(resolve, reject){
263-
if (params.session_id) {
270+
if (params.sessionId) {
264271
resolve();
265272
} else {
266-
node.service.createSession({
267-
assistant_id: params.assistant_id
268-
}, function(err, response) {
269-
if (err) {
270-
reject(err);
271-
} else if (response && response.session_id) {
272-
params.session_id = response.session_id;
273-
if (!config.multisession) {
274-
node.context().flow.set('session_id', params.session_id);
273+
node.service.createSession({assistantId: params.assistantId})
274+
.then((response) => {
275+
if (response && response.result && response.result.session_id) {
276+
params.sessionId = response.result.session_id;
277+
} else if (response && response.session_id) {
278+
params.sessionId = response.session_id;
275279
}
276-
resolve();
277-
} else {
278-
reject('Unable to set session');
279-
}
280-
});
280+
if (params.sessionId) {
281+
if (!config.multisession) {
282+
node.context().flow.set('session_id', params.sessionId);
283+
}
284+
resolve();
285+
} else {
286+
reject('Unable to set session');
287+
}
288+
})
289+
.catch((err) => {
290+
reject(err);
291+
});
281292
}
282293
});
283294
}
284295

285296
function messageTurn(params) {
286-
return new Promise(function resolver(resolve, reject){
287-
node.service.message(params, function(err, body) {
288-
if (err) {
297+
return new Promise(function resolver(resolve, reject) {
298+
node.service.message(params)
299+
.then((response) => {
300+
if (response.result) {
301+
resolve(response.result);
302+
} else {
303+
resolve(response);
304+
}
305+
})
306+
.catch((err) => {
289307
reject(err);
290-
} else {
291-
resolve(body);
292-
}
293-
});
308+
})
294309
});
295310
}
296311

297-
this.on('input', function(msg) {
312+
this.on('input', function(msg, send, done) {
298313
var creds = setCredentials(msg),
299314
params = {};
300315

@@ -325,17 +340,18 @@ module.exports = function(RED) {
325340
return messageTurn(params);
326341
})
327342
.then(function(body){
328-
body.session_id = params.session_id;
343+
body.session_id = params.sessionId;
329344
msg.payload = body;
330345
return Promise.resolve();
331346
})
332347
.then(function(){
333348
node.status({});
334-
node.send(msg);
349+
send(msg);
350+
done();
335351
})
336352
.catch(function(err){
337-
payloadutils.reportError(node,msg,err);
338-
node.send(msg);
353+
let errMsg = payloadutils.reportError(node, msg, err);
354+
done(errMsg);
339355
});
340356

341357
});

0 commit comments

Comments
 (0)