Skip to content

Commit 9f36639

Browse files
authored
Merge pull request #168 from watson-developer-cloud/master
Rebasing
2 parents 51db0e8 + af69c7b commit 9f36639

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Node-RED Watson Nodes for IBM Cloud
88
<a href="https://cla-assistant.io/watson-developer-cloud/node-red-node-watson"><img src="https://cla-assistant.io/readme/badge/watson-developer-cloud/node-red-node-watson" alt="CLA assistant" /></a>
99

1010

11+
### New in version 0.9.2
12+
- Assistant V2 - Fix bug session expiry bug.
13+
14+
1115
### New in version 0.9.1
1216
- Assistant V2 - Allow flow to assign a string session id. The node maps this user specified session id to the real session id. Additional param option allow session id to be reset.
1317

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.9.1",
3+
"version": "0.9.2",
44
"description": "A collection of Node-RED nodes for IBM Watson services",
55
"dependencies": {
66
"async": "^1.5.2",

services/assistant/v2.js

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ module.exports = function(RED) {
1919
OLD_SERVICE_IDENTIFIER = 'conversation',
2020
SERVICE_VERSION = '2018-11-08',
2121
AssistantV2 = require('ibm-watson/assistant/v2'),
22-
{ IamAuthenticator } = require('ibm-watson/auth');
22+
{ IamAuthenticator } = require('ibm-watson/auth'),
23+
INVALID_SESSION = 'Invalid Session';
2324

2425
var pkg = require('../../package.json'),
2526
serviceutils = require('../../utilities/service-utils'),
@@ -313,7 +314,16 @@ module.exports = function(RED) {
313314
});
314315
}
315316

316-
function messageTurn(params) {
317+
function checkForInvalidSession(err) {
318+
if (err &&
319+
err.code && (404 === err.code) &&
320+
err.message && (INVALID_SESSION === err.message)) {
321+
return true;
322+
}
323+
return false;
324+
}
325+
326+
function messageTurn(params, repeatedTurn) {
317327
return new Promise(function resolver(resolve, reject) {
318328
node.service.message(params)
319329
.then((response) => {
@@ -324,8 +334,37 @@ module.exports = function(RED) {
324334
}
325335
})
326336
.catch((err) => {
327-
reject(err);
328-
})
337+
if ((!repeatedTurn) && checkForInvalidSession(err)) {
338+
resolve({INVALID_SESSION : true});
339+
} else {
340+
reject(err);
341+
}
342+
});
343+
});
344+
}
345+
346+
function invalidSessionTurnCheck(body, msg, params) {
347+
return new Promise(function resolver(resolve, reject) {
348+
if (body && body.INVALID_SESSION) {
349+
// Message Turn has returned an invalid session
350+
// This time round force a new session, but ensure
351+
// that the messageTurn sends a consequental invalid session
352+
// as an error.
353+
node.status({ fill: 'blue', shape: 'dot', text: 'Resetting Session ...'});
354+
params.sessionId = null;
355+
checkSession(msg, params)
356+
.then (function(){
357+
return messageTurn(params, true);
358+
})
359+
.then ((body) => {
360+
return resolve(body);
361+
})
362+
.catch ((err) => {
363+
reject(err);
364+
})
365+
} else {
366+
resolve(body);
367+
}
329368
});
330369
}
331370

@@ -357,7 +396,10 @@ module.exports = function(RED) {
357396
})
358397
.then(function(){
359398
node.status({ fill: 'blue', shape: 'dot', text: 'Calling Assistant service ...'});
360-
return messageTurn(params);
399+
return messageTurn(params, false);
400+
})
401+
.then(function(body){
402+
return invalidSessionTurnCheck(body, msg, params);
361403
})
362404
.then(function(body){
363405
body.session_id = params.sessionId;

0 commit comments

Comments
 (0)