Skip to content
This repository was archived by the owner on Dec 28, 2021. It is now read-only.

Commit 8f54596

Browse files
authored
Updates
* Update packages with per npm audit * Update versions * update libs and sdk version * Some prep for making Redis optional (but not yet) * Update to node.js 8 * Misc updates to readme * travis update
1 parent d20fda4 commit 8f54596

File tree

6 files changed

+4514
-508
lines changed

6 files changed

+4514
-508
lines changed

.travis.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
language: node_js
22
dist: trusty
3-
node_js: 6
3+
node_js:
4+
- 8
5+
- node
46
script:
57
- npm run test
68
cache:

README.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ When the reader has completed this Code Pattern, they will understand how to:
2323
* Use the Weather Channel Data service to lookup locations and forecasts
2424
* Create an Alexa skill to reach tens of millions of customers
2525

26-
![](doc/source/images/architecture.png)
26+
![architecture.png](doc/source/images/architecture.png)
2727

2828
## Flow
2929

@@ -98,12 +98,12 @@ Create the service by following this link and hitting `Create`:
9898
Import the Assistant workspace.json:
9999

100100
* Find the Assistant service in your IBM Cloud Dashboard.
101-
* Click on the service and then click on `Launch tool`.
101+
* Click on the service and then click on `Launch Watson Assistant`.
102102
* Go to the `Skills` tab.
103-
* Click `Create new`
103+
* Click `Create skill`
104104
* Click the `Import skill` tab.
105105
* Click `Choose JSON file`, go to your cloned repo dir, and `Open` the workspace.json file in [`data/conversation/workspaces/workspace.json`](data/conversation/workspaces/workspace.json).
106-
* Select `Everything` and click `Import`.
106+
* Select `Everything (Intents, Entities, and Dialog)` and click `Import`.
107107

108108
To find the `WORKSPACE_ID` for Watson Assistant:
109109

@@ -185,7 +185,7 @@ Databases for Redis, and Weather Company Data), can be found in the IBM Cloud UI
185185

186186
### 6. Create the OpenWhisk action
187187

188-
As a prerequisite, [install the Cloud Functions (IBM Cloud OpenWhisk) CLI](https://cloud.ibm.com/docs/openwhisk/bluemix_cli.html#cloudfunctions_cli)
188+
As a prerequisite, [install the Cloud Functions (IBM Cloud OpenWhisk) CLI](https://cloud.ibm.com/docs/openwhisk?topic=cloud-functions-cli_install)
189189

190190
#### Create the OpenWhisk action
191191

@@ -198,12 +198,12 @@ to create a raw HTTP web action in OpenWhisk.
198198
npm install
199199
rm action.zip
200200
zip -r action.zip main.js package* node_modules
201-
ibmcloud wsk action update alexa-watson action.zip --kind nodejs:6 --web raw --param-file .params
201+
ibmcloud wsk action update alexa-watson action.zip --kind nodejs:8 --web raw --param-file .params
202202
```
203203

204-
#### Determine your IBM Cloud endpoint:
204+
#### Determine your IBM Cloud endpoint
205205

206-
To find this URL, navigate to [IBM Cloud Functions - Actions](https://cloud.ibm.com/openwhisk/manage/actions), click on your
206+
To find this URL, navigate to [IBM Cloud Functions - Actions](https://cloud.ibm.com/openwhisk/actions), click on your
207207
`alexa-watson` action and use the sidebar to navigate to `Endpoints`. The Web Action URL ends with `.json`.
208208

209209
![functions_endpoints](doc/source/images/functions_endpoints.png)
@@ -333,7 +333,6 @@ The next screenshot shows how the location is automatically used in the next 'we
333333
* [Alexa/Google Home infinite loop conversation](https://www.youtube.com/watch?v=LEz9AU9c2qQ): Check out how it works.
334334
* [Award winners](https://voicebot.ai/2017/03/01/amazon-alexa-ibm-watson-won-2016-voice-assistant-wars-already-winning-2017/): Amazon Alexa and IBM Watson won the 2016 Voice Assistant Wars.
335335
* [Bluemix Stirred](https://bluemixstirred.wordpress.com/2017/05/11/use-the-amazon-echo-dot-with-the-watson-conversation-service/): Learn how to use the Amazon Echo and Dot with the Watson Assistant Service.
336-
* [Old Demo on Youtube](https://www.youtube.com/watch?v=4cTSkX0wSV8): Watch the video.
337336

338337
# Learn more
339338

main.js

+33-21
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
'use strict';
1818

1919
const alexaVerifier = require('alexa-verifier');
20-
const AssistantV1 = require('watson-developer-cloud/assistant/v1');
20+
const AssistantV1 = require('ibm-watson/assistant/v1');
2121
const redis = require('redis');
2222
const openwhisk = require('openwhisk');
2323
const request = require('request');
@@ -48,7 +48,7 @@ function verifyFromAlexa(args, rawBody) {
4848
alexaVerifier(certUrl, signature, rawBody, function(err) {
4949
if (err) {
5050
console.error('err? ' + JSON.stringify(err));
51-
throw new Error('Alexa verification failed.');
51+
throw Error('Alexa verification failed.');
5252
}
5353
resolve();
5454
});
@@ -59,52 +59,57 @@ function initClients(args) {
5959
// Connect a client to Watson Assistant
6060
if (args.ASSISTANT_IAM_APIKEY) {
6161
assistant = new AssistantV1({
62-
version: '2018-02-16',
62+
version: '2019-07-16',
6363
iam_apikey: args.ASSISTANT_IAM_APIKEY,
6464
url: args.ASSISTANT_IAM_URL
6565
});
6666
} else if (args.ASSISTANT_USERNAME) {
6767
assistant = new AssistantV1({
68-
version: '2018-02-16',
68+
version: '2019-07-16',
6969
username: args.ASSISTANT_USERNAME,
7070
password: args.ASSISTANT_PASSWORD
7171
});
7272
} else {
7373
console.error('err? ' + 'Invalid Credentials');
74-
throw new Error('Invalid Credentials');
74+
throw Error('Invalid Credentials');
7575
}
7676

7777
console.log('Connected to Watson Assistant');
7878

7979
// Connect a client to Redis
8080
const connectionString = args.REDIS_URI;
81-
if (connectionString.startsWith('rediss://')) {
82-
const convertedCert = Buffer.from(args.REDIS_CERT, 'base64').toString();
83-
redisClient = redis.createClient(connectionString, {
84-
tls: { servername: new Url(connectionString).hostname, ca: convertedCert }
81+
if (args.REDIS_URI) {
82+
if (connectionString.startsWith('rediss://')) {
83+
const convertedCert = Buffer.from(args.REDIS_CERT, 'base64').toString();
84+
redisClient = redis.createClient(connectionString, {
85+
tls: { servername: new Url(connectionString).hostname, ca: convertedCert }
86+
});
87+
} else {
88+
redisClient = redis.createClient(connectionString);
89+
}
90+
redisClient.on('error', function(err) {
91+
console.log('Redis Error - ' + err);
8592
});
93+
94+
console.log('Connected to Redis');
8695
} else {
87-
redisClient = redis.createClient(connectionString);
96+
console.error('Missing REDIS_URI');
97+
throw Error('Missing required configuration of REDIS_URI');
8898
}
89-
redisClient.on('error', function(err) {
90-
console.log('Redis Error - ' + err);
91-
});
92-
93-
console.log('Connected to Redis');
9499
}
95100

96101
function getSessionContext(sessionId) {
97-
console.log('sessionId: ' + sessionId);
102+
console.log('Alexa sessionId: ' + sessionId);
98103

99104
return new Promise(function(resolve, reject) {
100105
redisClient.get(sessionId, function(err, value) {
101106
if (err) {
102107
console.error(err);
103-
reject('Error getting context from Redis.');
108+
reject(Error('Error getting context from Redis.'));
104109
}
105110
// set global context
106111
context = value ? JSON.parse(value) : {};
107-
console.log('context:');
112+
console.log('Watson context from Redis:');
108113
console.log(context);
109114
resolve();
110115
});
@@ -126,7 +131,7 @@ function assistantMessage(request, workspaceId) {
126131
function(err, watsonResponse) {
127132
if (err) {
128133
console.error(err);
129-
reject('Error talking to Watson.');
134+
reject(Error('Error talking to Watson.'));
130135
} else {
131136
console.log(watsonResponse);
132137
context = watsonResponse.context; // Update global context
@@ -159,7 +164,7 @@ function lookupGeocode(args, location) {
159164
// console.log('Locations from Weather location services');
160165
// console.log(body.location);
161166
if (body.location.length < 1) {
162-
reject('Location not found');
167+
reject(Error('Location not found'));
163168
}
164169
// Just take the first one.
165170
const latitude = body.location.latitude[0];
@@ -253,7 +258,8 @@ function sendResponse(response, resolve) {
253258
type: 'PlainText',
254259
text: output
255260
}
256-
}
261+
},
262+
sessionAttributes: { watsonContext: context }
257263
});
258264
}
259265

@@ -283,6 +289,12 @@ function main(args) {
283289
const rawBody = Buffer.from(args.__ow_body, 'base64').toString('ascii');
284290
const body = JSON.parse(rawBody);
285291
const sessionId = body.session.sessionId;
292+
293+
// Alexa attributes hold our context (for future use)
294+
const alexaAttributes = body.session.attributes;
295+
console.log('Alexa attributes:');
296+
console.log(alexaAttributes);
297+
286298
const request = body.request;
287299

288300
verifyFromAlexa(args, rawBody)

0 commit comments

Comments
 (0)