Skip to content

Commit

Permalink
feat: add lib folder and address review
Browse files Browse the repository at this point in the history
  • Loading branch information
germanattanasio committed May 6, 2019
1 parent a5cdaad commit 9a90793
Show file tree
Hide file tree
Showing 17 changed files with 1,225 additions and 128 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -35,4 +35,3 @@ node_modules

# Optional REPL history
.node_repl_history
lib/
18 changes: 9 additions & 9 deletions README.md
Expand Up @@ -228,13 +228,13 @@ A common scenario of processing actions is:
Using sendToWatson to update context simplifies the bot code compared to solution using updateContext below.

```js
const checkBalance = (context) => new Promise((resolve, reject) => {
const checkBalance = async (context) => {
//do something real here
const contextDelta = {
validAccount: true,
accountBalance: 95.33
};
resolve(context);
await Promise.resolve(context);
});

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

Events are messages having type different than `message`.

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

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

```js
middleware.before = (message, assistantPayload) => new Promise((resolve, reject) => {
// Code here gets executed before making the call to Assistant.
resolve(assistantPayload);
});
middleware.before = (message, assistantPayload) => async () => {
// Code here gets executed before making the call to Assistant.
return await Promise.resolve(assistantPayload);
}
```

```js
middleware.after = (message, assistantResponse) => new Promise((resolve, reject) => {
middleware.after = (message, assistantResponse) => async () => {
// Code here gets executed after the call to Assistant.
resolve(assistantResponse);
return await Promise.resolve(assistantResponse);
});
```

Expand Down
2 changes: 1 addition & 1 deletion examples/simple-bot/package.json
Expand Up @@ -10,7 +10,7 @@
"dependencies": {
"botbuilder-adapter-slack": "^1.0.1",
"botkit": "^4.0.1",
"botkit-middleware-watson": "file:../../botkit-middleware-watson-2.0.0.tgz",
"botkit-middleware-watson": "^2.0.0",
"dotenv": "^8.0.0",
"express": "^4.16.3"
}
Expand Down
67 changes: 67 additions & 0 deletions lib/index.d.ts
@@ -0,0 +1,67 @@
/**
* Copyright 2016-2019 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Botkit = require('botkit');
import AssistantV1 = require('ibm-watson/assistant/v1');
import { BotkitMessage } from 'botkit';
export interface WatsonMiddlewareConfig {
version: string;
workspace_id: string;
url?: string;
token?: string;
headers?: {
[index: string]: string;
};
use_unauthenticated?: boolean;
username?: string;
password?: string;
iam_apikey?: string;
iam_url?: string;
minimum_confidence?: number;
}
export interface Payload extends AssistantV1.MessageRequest {
workspace_id: string;
}
export interface Context {
conversation_id: string;
system: any;
[index: string]: any;
}
export declare type BotkitWatsonMessage = BotkitMessage & {
watsonData?: Payload;
watsonError?: string;
};
export interface ContextDelta {
[index: string]: any;
}
export declare type ErrorCallback = (err: null | Error) => null;
export declare class WatsonMiddleware {
private config;
private conversation;
private storage;
private minimumConfidence;
private readonly ignoreType;
constructor(config: WatsonMiddlewareConfig);
hear(patterns: string[], message: Botkit.BotkitMessage): boolean;
before(message: Botkit.BotkitMessage, payload: Payload): Promise<Payload>;
after(message: Botkit.BotkitMessage, response: any): Promise<any>;
sendToWatson(bot: any, message: Botkit.BotkitMessage, contextDelta: ContextDelta): Promise<void>;
receive(bot: Botkit.BotWorker, message: Botkit.BotkitMessage): Promise<void>;
interpret(bot: Botkit.BotWorker, message: Botkit.BotkitMessage): Promise<void>;
readContext(user: string): Promise<Context>;
updateContext(user: string, contextDelta: ContextDelta): Promise<{
context: Context | ContextDelta;
}>;
}
153 changes: 153 additions & 0 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions lib/utils.d.ts
@@ -0,0 +1,25 @@
/**
* Copyright 2016-2019 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Storage } from 'botbuilder';
import AssistantV1 = require('ibm-watson/assistant/v1');
import { Payload, Context, ContextDelta } from './index';
export declare function readContext(userId: string, storage: Storage): Promise<Context | null>;
export declare function updateContext(userId: string, storage: Storage, watsonResponse: {
context: Context | ContextDelta;
}): Promise<{
context: Context | ContextDelta;
}>;
export declare function postMessage(conversation: AssistantV1, payload: Payload): Promise<AssistantV1.MessageResponse>;
78 changes: 78 additions & 0 deletions lib/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9a90793

Please sign in to comment.