Skip to content

Commit 9a90793

Browse files
feat: add lib folder and address review
1 parent a5cdaad commit 9a90793

17 files changed

+1225
-128
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,3 @@ node_modules
3535

3636
# Optional REPL history
3737
.node_repl_history
38-
lib/

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,13 @@ A common scenario of processing actions is:
228228
Using sendToWatson to update context simplifies the bot code compared to solution using updateContext below.
229229

230230
```js
231-
const checkBalance = (context) => new Promise((resolve, reject) => {
231+
const checkBalance = async (context) => {
232232
//do something real here
233233
const contextDelta = {
234234
validAccount: true,
235235
accountBalance: 95.33
236236
};
237-
resolve(context);
237+
await Promise.resolve(context);
238238
});
239239

240240
const processWatsonResponse = async (bot, message) => {
@@ -267,7 +267,7 @@ controller.on('message_received', processWatsonResponse);
267267

268268
Events are messages having type different than `message`.
269269

270-
[Example](https://github.com/howdyai/botkit/blob/master/examples/facebook_bot.js) of handler:
270+
[Example](https://github.com/howdyai/botkit/blob/master/packages/docs/reference/facebook.md#facebookeventtypemiddleware) of handler:
271271

272272
```js
273273
controller.on('facebook_postback', async (bot, message) => {
@@ -326,16 +326,16 @@ The _before_ and _after_ async calls can be used to perform some tasks _before_
326326
They can be customized as follows:
327327

328328
```js
329-
middleware.before = (message, assistantPayload) => new Promise((resolve, reject) => {
330-
// Code here gets executed before making the call to Assistant.
331-
resolve(assistantPayload);
332-
});
329+
middleware.before = (message, assistantPayload) => async () => {
330+
// Code here gets executed before making the call to Assistant.
331+
return await Promise.resolve(assistantPayload);
332+
}
333333
```
334334

335335
```js
336-
middleware.after = (message, assistantResponse) => new Promise((resolve, reject) => {
336+
middleware.after = (message, assistantResponse) => async () => {
337337
// Code here gets executed after the call to Assistant.
338-
resolve(assistantResponse);
338+
return await Promise.resolve(assistantResponse);
339339
});
340340
```
341341

examples/simple-bot/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"dependencies": {
1111
"botbuilder-adapter-slack": "^1.0.1",
1212
"botkit": "^4.0.1",
13-
"botkit-middleware-watson": "file:../../botkit-middleware-watson-2.0.0.tgz",
13+
"botkit-middleware-watson": "^2.0.0",
1414
"dotenv": "^8.0.0",
1515
"express": "^4.16.3"
1616
}

lib/index.d.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright 2016-2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import Botkit = require('botkit');
17+
import AssistantV1 = require('ibm-watson/assistant/v1');
18+
import { BotkitMessage } from 'botkit';
19+
export interface WatsonMiddlewareConfig {
20+
version: string;
21+
workspace_id: string;
22+
url?: string;
23+
token?: string;
24+
headers?: {
25+
[index: string]: string;
26+
};
27+
use_unauthenticated?: boolean;
28+
username?: string;
29+
password?: string;
30+
iam_apikey?: string;
31+
iam_url?: string;
32+
minimum_confidence?: number;
33+
}
34+
export interface Payload extends AssistantV1.MessageRequest {
35+
workspace_id: string;
36+
}
37+
export interface Context {
38+
conversation_id: string;
39+
system: any;
40+
[index: string]: any;
41+
}
42+
export declare type BotkitWatsonMessage = BotkitMessage & {
43+
watsonData?: Payload;
44+
watsonError?: string;
45+
};
46+
export interface ContextDelta {
47+
[index: string]: any;
48+
}
49+
export declare type ErrorCallback = (err: null | Error) => null;
50+
export declare class WatsonMiddleware {
51+
private config;
52+
private conversation;
53+
private storage;
54+
private minimumConfidence;
55+
private readonly ignoreType;
56+
constructor(config: WatsonMiddlewareConfig);
57+
hear(patterns: string[], message: Botkit.BotkitMessage): boolean;
58+
before(message: Botkit.BotkitMessage, payload: Payload): Promise<Payload>;
59+
after(message: Botkit.BotkitMessage, response: any): Promise<any>;
60+
sendToWatson(bot: any, message: Botkit.BotkitMessage, contextDelta: ContextDelta): Promise<void>;
61+
receive(bot: Botkit.BotWorker, message: Botkit.BotkitMessage): Promise<void>;
62+
interpret(bot: Botkit.BotWorker, message: Botkit.BotkitMessage): Promise<void>;
63+
readContext(user: string): Promise<Context>;
64+
updateContext(user: string, contextDelta: ContextDelta): Promise<{
65+
context: Context | ContextDelta;
66+
}>;
67+
}

lib/index.js

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

lib/index.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/utils.d.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2016-2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import { Storage } from 'botbuilder';
17+
import AssistantV1 = require('ibm-watson/assistant/v1');
18+
import { Payload, Context, ContextDelta } from './index';
19+
export declare function readContext(userId: string, storage: Storage): Promise<Context | null>;
20+
export declare function updateContext(userId: string, storage: Storage, watsonResponse: {
21+
context: Context | ContextDelta;
22+
}): Promise<{
23+
context: Context | ContextDelta;
24+
}>;
25+
export declare function postMessage(conversation: AssistantV1, payload: Payload): Promise<AssistantV1.MessageResponse>;

lib/utils.js

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

0 commit comments

Comments
 (0)