Skip to content
This repository was archived by the owner on Sep 1, 2026. It is now read-only.

Commit ff8760b

Browse files
Steve Greengermanattanasio
authored andcommitted
feat: Refactored Code to support Assistant V2 (#6)
BREAKING CHANGE: * API code changes to Assistant to support session id * Update context to the latest version
1 parent c5ee483 commit ff8760b

10 files changed

Lines changed: 179 additions & 168 deletions

File tree

‎.env.example‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Environment variables
2-
WORKSPACE_ID=
2+
ASSISTANT_ID=
33
# You need to provide either username and password
44
ASSISTANT_USERNAME=
55
ASSISTANT_PASSWORD=

‎README.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
You can view a [demo][demo_url] of this app.
1616

17+
Please note this app uses the [Watson Assistant V2 API](https://console.bluemix.net/apidocs/assistant-v2#introduction). To access a version of the V1 app, you can go to [v1.1.0](https://github.com/watson-developer-cloud/assistant-intermediate/releases/tag/v1.1.0).
18+
19+
If you need more information about the V1 API, you can go to the [Watson Assistant V1 API page](https://cloud.ibm.com/apidocs/assistant#introduction).
20+
1721

1822
## Prerequisites
1923

@@ -83,10 +87,10 @@ You can view a [demo][demo_url] of this app.
8387
ASSISTANT_IAM_URL=https://gateway-syd.watsonplatform.net/assistant/api
8488
```
8589
86-
8. Add the `WORKSPACE_ID` to the previous properties
90+
8. Add the `ASSISTANT_ID` to the previous properties
8791
8892
```
89-
WORKSPACE_ID=522be-7b41-ab44-dec3-g1eab2ha73c6
93+
ASSISTANT_ID=522be-7b41-ab44-dec3-g1eab2ha73c6
9094
```
9195
9296
## Running locally

‎app.js‎

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
var express = require('express'); // app server
2020
var bodyParser = require('body-parser'); // parser for post requests
21-
var AssistantV1 = require('watson-developer-cloud/assistant/v1'); // watson sdk
21+
var AssistantV2 = require('watson-developer-cloud/assistant/v2'); // watson sdk
2222

2323
var Actions = require('./functions/actions');
2424
var actions = new Actions();
@@ -33,59 +33,87 @@ app.use(express.static('./public')); // load UI from public folder
3333
app.use(bodyParser.json());
3434

3535
// Create the service wrapper
36-
37-
var assistant = new AssistantV1({
38-
version: '2018-07-10'
36+
var assistant = new AssistantV2({
37+
version: '2018-11-08'
3938
});
4039
var date = new Date();
4140
date.setMonth(date.getMonth() + 1);
42-
var accountData = {
43-
acc_minamt: 50,
44-
acc_currbal: 430,
45-
acc_paydue: date.getFullYear() + '-' + (date.getMonth() + 1) + '-26 12:00:00',
46-
accnames: [
47-
5624,
48-
5893,
49-
9225,
50-
],
51-
private: {
52-
function_creds: {
53-
user: process.env.CLOUD_FUNCTION_USER,
54-
password: process.env.CLOUD_FUNCTION_PASS,
55-
},
41+
var newContext = {
42+
global : {
43+
system : {
44+
turn_count : 1
45+
}
5646
},
47+
skills: {
48+
'main skill': {
49+
user_defined: {
50+
acc_minamt: 50,
51+
acc_currbal: 430,
52+
acc_paydue: date.getFullYear() + '-' + (date.getMonth() + 1) + '-26 12:00:00',
53+
accnames: [
54+
5624,
55+
5893,
56+
9225,
57+
],
58+
private: {
59+
function_creds: {
60+
user: process.env.CLOUD_FUNCTION_USER,
61+
password: process.env.CLOUD_FUNCTION_PASS,
62+
},
63+
}
64+
}
65+
}
66+
}
5767
};
5868

5969
// Endpoint to be call from the client side
6070
app.post('/api/message', function (req, res) {
61-
var workspace = process.env.WORKSPACE_ID || '<workspace-id>';
62-
if (!workspace || workspace === '<workspace-id>') {
71+
var assistantId = process.env.ASSISTANT_ID || '<assistant-id>';
72+
if (!assistantId || assistantId === '<assistant-id>') {
6373
return res.json({
6474
'output': {
65-
'text': 'The app has not been configured with a <b>WORKSPACE_ID</b> environment variable. Please refer to the ' + '<a href="https://github.com/watson-developer-cloud/assistant-intermediate">README</a> documentation on how to set this variable. <br>' + 'Once a workspace has been defined the intents may be imported from ' + '<a href="https://github.com/watson-developer-cloud/assistant-intermediate/blob/master/training/banking_workspace.json">here</a> in order to get a working application.'
75+
'text': 'The app has not been configured with a <b>ASSISTANT_ID</b> environment variable. Please refer to the ' + '<a href="https://github.com/watson-developer-cloud/assistant-intermediate">README</a> documentation on how to set this variable. <br>' + 'Once a workspace has been defined the intents may be imported from ' + '<a href="https://github.com/watson-developer-cloud/assistant-intermediate/blob/master/training/banking_workspace.json">here</a> in order to get a working application.'
6676
}
6777
});
6878
}
6979

70-
var contextWithAcc = Object.assign({}, req.body.context, accountData);
80+
var contextWithAcc = (req.body.context) ? req.body.context : newContext;
81+
82+
if (req.body.context) {
83+
contextWithAcc.global.system.turn_count += 1;
84+
}
85+
86+
//console.log(JSON.stringify(contextWithAcc, null, 2));
87+
88+
var textIn = '';
89+
90+
if(req.body.input) {
91+
textIn = req.body.input.text;
92+
}
7193

7294
var payload = {
73-
workspace_id: workspace,
74-
context: contextWithAcc || {},
75-
input: req.body.input || {}
95+
assistant_id: assistantId,
96+
session_id: req.body.session_id,
97+
context: contextWithAcc,
98+
input: {
99+
message_type : 'text',
100+
text : textIn,
101+
options : {
102+
return_context : true
103+
}
104+
}
76105
};
77-
106+
78107
// Send the input to the assistant service
79108
assistant.message(payload, function (err, data) {
80109
if (err) {
81110
return res.status(err.code || 500).json(err);
82111
}
83-
actions.testForAction(data).then(function (d) {
112+
actions.testForAction(data, req.body.session_id).then(function (d) {
84113
return res.json(d);
85114
}).catch(function (error) {
86115
return res.json(error);
87116
});
88-
89117
});
90118
});
91119

@@ -105,4 +133,16 @@ app.get('/bank/locate', function (req, res) {
105133
res.send({ result: 'zip123retrieved' });
106134
});
107135

136+
app.get('/api/session', function (req, res) {
137+
assistant.createSession({
138+
assistant_id: process.env.ASSISTANT_ID || '{assistant_id}',
139+
}, function (error, response) {
140+
if (error) {
141+
return res.send(error);
142+
} else {
143+
return res.send(response);
144+
}
145+
});
146+
});
147+
108148
module.exports = app;

‎casper-runner.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
require('dotenv').config({ silent: true });
1818

19-
if (!process.env.WORKSPACE_ID) {
19+
if (!process.env.ASSISTANT_ID) {
2020
// eslint-disable-next-line
21-
console.warn('Skipping casper tests because WORKSPACE_ID is null');
21+
console.warn('Skipping casper tests because ASSISTANT_ID is null');
2222
return;
2323
}
2424

‎functions/actions.js‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,23 @@ function actions() {
1919

2020
var http = require('http');
2121

22-
this.testForAction = function (data) {
22+
this.testForAction = function (data, sessionId) {
2323
return new Promise(function (resolve, reject) {
24-
if (data.hasOwnProperty('actions')) {
25-
//console.log(JSON.stringify(data.actions, null, 2));
26-
data.actions.forEach(function (action) {
24+
if (data.output.hasOwnProperty('actions')) {
25+
data.output.actions.forEach(function (action) {
2726
var endPoint = '';
2827
var value = '';
2928
var sendType = '';
3029
if (action.name === 'ValidateAcc') {
3130
endPoint = '/bank/validate';
3231
value = action.parameters.chosen_acc;
3332
sendType = action.result_variable;
34-
bankCalls(endPoint, value, sendType, data, resolve, reject);
33+
bankCalls(endPoint, value, sendType, data, sessionId, resolve, reject);
3534
} else if (action.name === 'RetrieveZip') {
3635
endPoint = '/bank/locate';
3736
value = action.parameters.zip_value;
3837
sendType = action.result_variable;
39-
bankCalls(endPoint, value, sendType, data, resolve, reject);
38+
bankCalls(endPoint, value, sendType, data, sessionId, resolve, reject);
4039
}
4140
});
4241
} else {
@@ -45,10 +44,9 @@ function actions() {
4544
});
4645
};
4746

48-
function bankCalls(endPoint, value, sendType, data, resolve, reject) {
47+
function bankCalls(endPoint, value, sendType, data, sessionId, resolve, reject) {
4948
// construct the endpoint based on which client action
5049
var parameterizedEndpoint = endPoint + '?value=' + value;
51-
5250
http.get({
5351
path: parameterizedEndpoint,
5452
port: process.env.PORT || 3000,
@@ -57,7 +55,7 @@ function actions() {
5755
}
5856
}, function (resp) {
5957
resp.on('data', function (res) {
60-
sendMessage(JSON.parse(res), value, sendType, data.context, resolve, reject);
58+
sendMessage(JSON.parse(res), value, sendType, data.context, sessionId, resolve, reject);
6159
});
6260
resp.on('end', function () {
6361
});
@@ -66,14 +64,15 @@ function actions() {
6664
});
6765
}
6866

69-
function sendMessage(res, value, sendType, context, resolve, reject) {
67+
function sendMessage(res, value, sendType, context, sessionId, resolve, reject) {
7068
var parameterizedEndpoint = '/api/message';
7169

7270
var payloadToWatson = {
73-
'context': context
71+
'context': context,
72+
'session_id': sessionId
7473
};
7574

76-
payloadToWatson.input = {};
75+
payloadToWatson.input = {text : ''};
7776

7877
if (sendType === 'input.text') {
7978
payloadToWatson.input.text = res.result;

‎package-lock.json‎

Lines changed: 12 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"body-parser": "^1.18.3",
2323
"dotenv": "^6.1.0",
2424
"express": "^4.16.4",
25-
"watson-developer-cloud": "^3.13.0"
25+
"watson-developer-cloud": "^3.13.1"
2626
},
2727
"publishConfig": {
2828
"registry": "https://registry.npmjs.org/",

0 commit comments

Comments
 (0)