Skip to content

Commit 7bfab7e

Browse files
fix: Switch to use AssistantV1
1 parent 231815c commit 7bfab7e

18 files changed

+7348
-1198
lines changed

.eslintrc.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
env:
2+
es6: true
23
node: true
34
mocha: true
45
extends: 'eslint:recommended'
6+
parserOptions:
7+
ecmaVersion: 2016
58
rules:
69
indent:
710
- error
@@ -16,4 +19,4 @@ rules:
1619
- error
1720
- always
1821
no-console:
19-
- off
22+
- warn

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: node_js
22
sudo: true
3-
node_js: 8
3+
node_js: "8"
44
cache:
55
directories:
66
- node_modules

CHANGELOG.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121

2222
## v1.4.2
2323

24-
Fixed critical issue introduced in v1.4.1:
25-
updateContext fails on first write for a new user when simple storage is used
26-
(It happens because simple storage returns an error when record does not exist).
24+
* Fixed critical issue introduced in v1.4.1
25+
* updateContext fails on first write for a new user when simple storage is used (It happens because simple storage returns an error when record does not exist).
2726

2827
## v1.4.1
2928

README.md

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Use IBM Watson's Assistant service to chat with your Botkit-powered Bot! [![Build Status](https://travis-ci.org/watson-developer-cloud/botkit-middleware.svg?branch=master)](https://travis-ci.org/watson-developer-cloud/botkit-middleware)
22

3-
This middleware plugin for [Botkit](http://howdy.ai/botkit) allows developers to easily integrate a [Watson Assistant](https://www.ibm.com/watson/services/conversation/) workspace with multiple social channels like Slack, Facebook, and Twilio. Customers can have simultaneous, independent conversations with a single workspace through different channels.
3+
This middleware plugin for [Botkit](http://howdy.ai/botkit) allows developers to easily integrate a [Watson Assistant](https://www.ibm.com/watson/ai-assistant/) workspace with multiple social channels like Slack, Facebook, and Twilio. Customers can have simultaneous, independent conversations with a single workspace through different channels.
44

55
## Middleware Overview
66

@@ -18,7 +18,7 @@ This middleware plugin for [Botkit](http://howdy.ai/botkit) allows developers to
1818
* `hear`: used for [intent matching](#intent-matching).
1919
* `updateContext`: used in [implementing app actions](#implementing-app-actions) (sendToWatson does it better now).
2020
* `readContext`: used in [implementing event handlers](#implementing-event-handlers).
21-
* `before`: [pre-process](#before-and-after) requests before sending to Watson Assistant (Conversation).
21+
* `before`: [pre-process](#before-and-after) requests before sending to Watson Assistant (formerly Conversation).
2222
* `after`: [post-process](#before-and-after) responses before forwarding them to Botkit.
2323

2424

@@ -31,16 +31,14 @@ $ npm install botkit-middleware-watson --save
3131
## Prerequisites
3232

3333
1. Sign up for an [IBM Cloud account](https://console.bluemix.net/registration/).
34-
1. Download the [IBM Cloud CLI](https://console.bluemix.net/docs/cli/index.html#overview).
3534
1. Create an instance of the Watson Assistant service and get your credentials:
3635
- Go to the [Watson Assistant](https://console.bluemix.net/catalog/services/conversation) page in the IBM Cloud Catalog.
3736
- Log in to your IBM Cloud account.
3837
- Click **Create**.
39-
- Click **Show** to view the service credentials.
4038
- Copy the `apikey` value, or copy the `username` and `password` values if your service instance doesn't provide an `apikey`.
4139
- Copy the `url` value.
4240

43-
1. Create a workspace using the Watson Assistant service and copy the `workspace_id`.
41+
1. Create a workspace using the Watson Assistant service and copy the `workspace_id`. If you don't know how to create a workspace follow the [Getting Started tutorial](https://console.bluemix.net/docs/services/conversation/getting-started.html).
4442

4543

4644
### Acquire channel credentials
@@ -51,20 +49,20 @@ You need a _Slack token_ for your Slack bot to talk to Watson Assistant.
5149

5250
If you have an existing Slack bot, then copy the Slack token from your Slack settings page.
5351

54-
Otherwise, follow [Botkit's instructions](https://github.com/howdyai/botkit/blob/master/docs/readme-slack.md) to create your Slack bot from scratch. When your bot is ready, you are provided with a Slack token.
52+
Otherwise, follow [Botkit's instructions](https://botkit.ai/docs/provisioning/slack-events-api.html) to create your Slack bot from scratch. When your bot is ready, you are provided with a Slack token.
5553

5654
### Bot setup
5755

5856
This section walks you through code snippets to set up your Slack bot. If you want, you can jump straight to the [full example](/examples/simple-bot).
5957

6058
In your app, add the following lines to create your Slack controller using Botkit:
6159
```js
62-
var slackController = Botkit.slackbot();
60+
const slackController = Botkit.slackbot();
6361
```
6462

6563
Spawn a Slack bot using the controller:
6664
```js
67-
var slackBot = slackController.spawn({
65+
const slackBot = slackController.spawn({
6866
token: YOUR_SLACK_TOKEN
6967
});
7068
```
@@ -74,7 +72,7 @@ Create the middleware object which you'll use to connect to the Watson Assistant
7472
If your credentials are `username` and `password` use:
7573

7674
```js
77-
var watsonMiddleware = require('botkit-middleware-watson')({
75+
const watsonMiddleware = require('botkit-middleware-watson')({
7876
username: YOUR_ASSISTANT_USERNAME,
7977
password: YOUR_ASSISTANT_PASSWORD,
8078
url: YOUR_ASSISTANT_URL,
@@ -87,7 +85,7 @@ var watsonMiddleware = require('botkit-middleware-watson')({
8785
If your credentials is `apikey` use:
8886

8987
```js
90-
var watsonMiddleware = require('botkit-middleware-watson')({
88+
const watsonMiddleware = require('botkit-middleware-watson')({
9189
iam_apikey: YOUR_API_KEY,
9290
url: YOUR_ASSISTANT_URL,
9391
workspace_id: YOUR_WORKSPACE_ID,
@@ -102,7 +100,7 @@ slackController.middleware.receive.use(watsonMiddleware.receive);
102100
slackBot.startRTM();
103101
```
104102

105-
Finally, make your bot _listen_ to incoming messages and respond with Watson Conversation:
103+
Finally, make your bot _listen_ to incoming messages and respond with Watson Assistant:
106104
```js
107105
slackController.hears(['.*'], ['direct_message', 'direct_mention', 'mention'], function(bot, message) {
108106
if (message.watsonError) {
@@ -112,7 +110,7 @@ slackController.hears(['.*'], ['direct_message', 'direct_mention', 'mention'], f
112110
}
113111
});
114112
```
115-
The middleware attaches the `watsonData` object to _message_. This contains the text response from Conversation.
113+
The middleware attaches the `watsonData` object to _message_. This contains the text response from Assistant.
116114
If any error happened in middleware, error is assigned to `watsonError` property of the _message_.
117115

118116
Then you're all set!
@@ -141,7 +139,7 @@ slackController.hears(['.*'], ['direct_message'], function(bot, message) {
141139
#### Using middleware wrapper
142140

143141
```js
144-
var receiveMiddleware = function (bot, message, next) {
142+
const receiveMiddleware = function (bot, message, next) {
145143
if (message.type === 'direct_message') {
146144
watsonMiddleware.receive(bot, message, next);
147145
} else {
@@ -176,17 +174,18 @@ controller.hears(['.*'], ['direct_message', 'direct_mention', 'mention', 'messag
176174
```
177175

178176
#### Use the middleware's hear() function
179-
You can find the default implementation of this function [here](https://github.com/watson-developer-cloud/botkit-middleware/blob/e29b002f2a004f6df57ddf240a3fdf8cb28f95d0/lib/middleware/index.js#L40). If you want, you can redefine this function in the same way that watsonMiddleware.before and watsonMiddleware.after can be redefined. Refer to the [Botkit Middleware documentation](https://github.com/howdyai/botkit/blob/master/docs/middleware.md#hear-middleware) for an example. Then, to use this function instead of Botkit's default pattern matcher (that does not use minimum_confidence), plug it in using:
177+
You can find the default implementation of this function [here](https://github.com/watson-developer-cloud/botkit-middleware/blob/e29b002f2a004f6df57ddf240a3fdf8cb28f95d0/lib/middleware/index.js#L40). If you want, you can redefine this function in the same way that watsonMiddleware.before and watsonMiddleware.after can be redefined. Refer to the [Botkit Middleware documentation](https://botkit.ai/docs/core.html#controllerhears) for an example. Then, to use this function instead of Botkit's default pattern matcher (that does not use minimum_confidence), plug it in using:
180178
```js
181179
controller.changeEars(watsonMiddleware.hear)
182180
```
183181

184-
Note: if you want your own `hear()` function to implement pattern matching like Botkit's default one, you will likely need to implement that yourself. Botkit's default set of 'ears' is the `hears_regexp` function which is implemented [here](https://github.com/howdyai/botkit/blob/77b7d7f80c46d5c8194453667d22118b7850e252/lib/CoreBot.js#L1180).
182+
Note: if you want your own `hear()` function to implement pattern matching like Botkit's default one, you will likely need to implement that yourself. Botkit's default set of 'ears' is the `hears_regexp` function which is implemented [here](https://github.com/howdyai/botkit/blob/77b7d7f80c46d5c8194453667d22118b7850e252/lib/CoreBot.js#L1187).
185183

186184
### Implementing app actions
187185

188-
Watson Assistant side of app action is documented in [Developer Cloud](https://console.bluemix.net/docs/services/conversation/develop-app.html#building-a-client-application)
189-
A common scenario of processing actions is
186+
Watson Assistant side of app action is documented in [Developer Cloud](https://console.bluemix.net/docs/services/assistant/deploy-custom-app.html#deploy-custom-app)
187+
A common scenario of processing actions is:
188+
190189
* Send message to user "Please wait while I ..."
191190
* Perform action
192191
* Persist results in conversation context
@@ -200,17 +199,16 @@ Using sendToWatson to update context simplifies the bot code compared to solutio
200199

201200
function checkBalance(context, callback) {
202201
//do something real here
203-
204-
var contextDelta = {
202+
const contextDelta = {
205203
validAccount: true,
206204
accountBalance: 95.33
207205
};
208206
callback(null, context);
209207
}
210208

211-
var checkBalanceAsync = Promise.promisify(checkBalance);
209+
const checkBalanceAsync = Promise.promisify(checkBalance);
212210

213-
var processWatsonResponse = function (bot, message) {
211+
const processWatsonResponse = function (bot, message) {
214212
if (message.watsonError) {
215213
return bot.reply(message, "I'm sorry, but for technical reasons I can't respond to your message");
216214
}
@@ -219,7 +217,7 @@ var processWatsonResponse = function (bot, message) {
219217
bot.reply(message, message.watsonData.output.text.join('\n'));
220218

221219
if (message.watsonData.output.action === 'check_balance') {
222-
var newMessage = clone(message);
220+
const newMessage = clone(message);
223221
newMessage.text = 'balance result';
224222

225223
checkBalanceAsync(message.watsonData.context).then(function (contextDelta) {
@@ -248,9 +246,9 @@ function checkBalance(context, callback) {
248246
callback(null, context);
249247
}
250248

251-
var checkBalanceAsync = Promise.promisify(checkBalance);
249+
const checkBalanceAsync = Promise.promisify(checkBalance);
252250

253-
var processWatsonResponse = function (bot, message) {
251+
const processWatsonResponse = function (bot, message) {
254252
if (message.watsonError) {
255253
return bot.reply(message, "I'm sorry, but for technical reasons I can't respond to your message");
256254
}
@@ -259,7 +257,7 @@ var processWatsonResponse = function (bot, message) {
259257
bot.reply(message, message.watsonData.output.text.join('\n'));
260258

261259
if (message.watsonData.output.action === 'check_balance') {
262-
var newMessage = clone(message);
260+
const newMessage = clone(message);
263261
newMessage.text = 'balance result';
264262

265263
//check balance
@@ -305,7 +303,7 @@ watsonMiddleware.after = function(message, watsonResponse, callback) {
305303
callback(null, watsonResponse);
306304
};
307305

308-
var processWatsonResponse = function(bot, message) {
306+
const processWatsonResponse = function(bot, message) {
309307
if (message.watsonError) {
310308
return bot.reply(message, "I'm sorry, but for technical reasons I can't respond to your message");
311309
}
@@ -315,7 +313,7 @@ var processWatsonResponse = function(bot, message) {
315313
bot.reply(message, message.watsonData.output.text.join('\n'));
316314

317315
if (message.watsonData.output.action === 'check_balance') {
318-
var newMessage = clone(message);
316+
const newMessage = clone(message);
319317
newMessage.text = 'balance result';
320318
//send to watson
321319
watsonMiddleware.interpret(bot, newMessage, function() {
@@ -424,7 +422,7 @@ Example of setting workspace_id to id provided as a property of hello message:
424422
```js
425423
function handleHelloEvent(bot, message) {
426424
message.type = 'welcome';
427-
var contextDelta = {};
425+
const contextDelta = {};
428426

429427
if (message.workspaceId) {
430428
contextDelta.workspace_id = message.workspaceId;

examples/multi-bot/app.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
require('dotenv').load();
1818

1919
var middleware = require('botkit-middleware-watson')({
20-
username: process.env.ASSISTANT_USERNAME,
21-
password: process.env.ASSISTANT_PASSWORD,
20+
iam_apikey: process.env.ASSISTANT_IAM_APIKEY,
2221
workspace_id: process.env.WORKSPACE_ID,
2322
url: process.env.ASSISTANT_URL || 'https://gateway.watsonplatform.net/assistant/api',
2423
version: '2018-07-10'

examples/multi-bot/bot-facebook.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ controller.hears('(.*)', 'message_received', function(bot, message) {
2929
} else if (message.watsonData && 'output' in message.watsonData) {
3030
bot.reply(message, message.watsonData.output.text.join('\n'));
3131
} else {
32-
console.log('Error: received message in unknown format. (Is your connection with Watson Conversation up and running?)');
32+
console.log('Error: received message in unknown format. (Is your connection with Watson Assistant up and running?)');
3333
bot.reply(message, 'I\'m sorry, but for technical reasons I can\'t respond to your message');
3434
}
3535
});

examples/multi-bot/bot-slack.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ controller.hears(['.*'], ['direct_message', 'direct_mention', 'mention'], functi
2828
} else if (message.watsonData && 'output' in message.watsonData) {
2929
bot.reply(message, message.watsonData.output.text.join('\n'));
3030
} else {
31-
console.log('Error: received message in unknown format. (Is your connection with Watson Conversation up and running?)');
31+
console.log('Error: received message in unknown format. (Is your connection with Watson Assistant up and running?)');
3232
bot.reply(message, 'I\'m sorry, but for technical reasons I can\'t respond to your message');
3333
}
3434
});

examples/multi-bot/bot-twilio.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ controller.hears(['.*'], 'message_received', function(bot, message) {
3333
} else if (message.watsonData && 'output' in message.watsonData) {
3434
bot.reply(message, message.watsonData.output.text.join('\n'));
3535
} else {
36-
console.log('Error: received message in unknown format. (Is your connection with Watson Conversation up and running?)');
36+
console.log('Error: received message in unknown format. (Is your connection with Watson Assistant up and running?)');
3737
bot.reply(message, 'I\'m sorry, but for technical reasons I can\'t respond to your message');
3838
}
3939
});

0 commit comments

Comments
 (0)