Skip to content

Commit f4b86da

Browse files
fix: UpdateContext documentation (#182)
UpdateContext documentation Co-authored-by: German Attanasio <[email protected]>
2 parents 046f901 + ad37e8d commit f4b86da

File tree

9 files changed

+36
-30
lines changed

9 files changed

+36
-30
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,6 @@ A common scenario of processing actions is:
220220

221221
### Using sendToWatson to update context
222222

223-
Using sendToWatson to update context simplifies the bot code compared to solution using updateContext below.
224-
225223
```js
226224
const checkBalance = async (context) => {
227225
//do something real here
@@ -258,6 +256,20 @@ const processWatsonResponse = async (bot, message) => {
258256
controller.on('message_received', processWatsonResponse);
259257
```
260258

259+
### Using updateContext to update context
260+
261+
sendToWatson should cover majority of use cases,
262+
but updateContext method can be useful when you want to update context from bot code,
263+
but there is no need to make a special request to Watson.
264+
265+
```js
266+
if (params.amount) {
267+
const context = message.watsonData.context;
268+
context.paymentAmount = params.amount;
269+
await watsonMiddleware.updateContext(message.user, context);
270+
}
271+
```
272+
261273
## Implementing event handlers
262274

263275
Events are messages having type different than `message`.

lib/index.d.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
import Botkit = require('botkit');
1717
import AssistantV1 = require('ibm-watson/assistant/v1');
18+
import { Context } from 'ibm-watson/assistant/v1';
1819
import { BotkitMessage } from 'botkit';
1920
export interface WatsonMiddlewareConfig {
2021
version: string;
@@ -34,11 +35,6 @@ export interface WatsonMiddlewareConfig {
3435
export interface Payload extends AssistantV1.MessageRequest {
3536
workspace_id: string;
3637
}
37-
export interface Context {
38-
conversation_id: string;
39-
system: any;
40-
[index: string]: any;
41-
}
4238
export declare type BotkitWatsonMessage = BotkitMessage & {
4339
watsonData?: Payload;
4440
watsonError?: string;
@@ -60,7 +56,7 @@ export declare class WatsonMiddleware {
6056
receive(bot: Botkit.BotWorker, message: Botkit.BotkitMessage): Promise<void>;
6157
interpret(bot: Botkit.BotWorker, message: Botkit.BotkitMessage): Promise<void>;
6258
readContext(user: string): Promise<Context>;
63-
updateContext(user: string, contextDelta: ContextDelta): Promise<{
64-
context: Context | ContextDelta;
59+
updateContext(user: string, context: Context): Promise<{
60+
context: Context;
6561
}>;
6662
}

lib/index.js

Lines changed: 2 additions & 2 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 & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/utils.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
*/
1616
import { Storage } from 'botbuilder';
1717
import AssistantV1 = require('ibm-watson/assistant/v1');
18-
import { Payload, Context, ContextDelta } from './index';
18+
import { Context } from 'ibm-watson/assistant/v1';
19+
import { Payload } from './index';
1920
export declare function readContext(userId: string, storage: Storage): Promise<Context | null>;
2021
export declare function updateContext(userId: string, storage: Storage, watsonResponse: {
21-
context: Context | ContextDelta;
22+
context: Context;
2223
}): Promise<{
23-
context: Context | ContextDelta;
24+
context: Context;
2425
}>;
2526
export declare function postMessage(conversation: AssistantV1, payload: Payload): Promise<AssistantV1.MessageResponse>;

lib/utils.js.map

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

src/index.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
const debug = require('debug')('watson-middleware:index');
1919
import Botkit = require('botkit');
2020
import AssistantV1 = require('ibm-watson/assistant/v1');
21+
import { Context } from 'ibm-watson/assistant/v1';
2122
import { Storage } from 'botbuilder';
2223
import { readContext, updateContext, postMessage } from './utils';
2324
import deepMerge = require('deepmerge');
@@ -43,12 +44,6 @@ export interface Payload extends AssistantV1.MessageRequest {
4344
workspace_id: string;
4445
}
4546

46-
export interface Context {
47-
conversation_id: string;
48-
system: any;
49-
[index: string]: any;
50-
}
51-
5247
export type BotkitWatsonMessage = BotkitMessage & {
5348
watsonData?: Payload;
5449
watsonError?: string;
@@ -208,15 +203,15 @@ export class WatsonMiddleware {
208203

209204
public async updateContext(
210205
user: string,
211-
contextDelta: ContextDelta,
212-
): Promise<{ context: Context | ContextDelta }> {
206+
context: Context,
207+
): Promise<{ context: Context }> {
213208
if (!this.storage) {
214209
throw new Error(
215210
'updateContext is called before the first this.receive call',
216211
);
217212
}
218213
return updateContext(user, this.storage, {
219-
context: contextDelta,
214+
context: context,
220215
});
221216
}
222217
}

src/utils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
const debug = require('debug')('watson-middleware:utils');
1919
import { Storage } from 'botbuilder';
2020
import AssistantV1 = require('ibm-watson/assistant/v1');
21-
import { Payload, Context, ContextDelta } from './index';
21+
import { Context } from 'ibm-watson/assistant/v1';
22+
import { Payload } from './index';
2223

2324
const storagePrefix = 'user.';
2425

@@ -47,8 +48,8 @@ export async function readContext(
4748
export async function updateContext(
4849
userId: string,
4950
storage: Storage,
50-
watsonResponse: { context: Context | ContextDelta },
51-
): Promise<{ context: Context | ContextDelta }> {
51+
watsonResponse: { context: Context },
52+
): Promise<{ context: Context }> {
5253
const itemId = storagePrefix + userId;
5354

5455
let userData: any = {};

test/middleware-send-to-watson.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Botkit, BotkitMessage } from 'botkit';
17+
import { Botkit } from 'botkit';
1818
import { updateContext } from '../lib/utils';
1919
import { clonePrototype } from 'clone';
2020
import { MemoryStorage } from 'botbuilder';
2121
import { WebAdapter } from 'botbuilder-adapter-web';
22-
import { WatsonMiddleware, BotkitWatsonMessage, Context } from '../lib/index';
22+
import { WatsonMiddleware, BotkitWatsonMessage } from '../lib/index';
23+
import { Context } from 'ibm-watson/assistant/v1';
2324
import nock = require('nock');
2425

2526
//Watson Assistant params

0 commit comments

Comments
 (0)