Skip to content

Commit

Permalink
feat(Context): add context.setAsHandled() and context.setAsNotHandled()
Browse files Browse the repository at this point in the history
  • Loading branch information
chentsulin committed Jan 13, 2020
1 parent c1dc152 commit 2de9e12
Show file tree
Hide file tree
Showing 19 changed files with 70 additions and 871 deletions.
3 changes: 3 additions & 0 deletions packages/bottender-dialogflow/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ module.exports = function dialogflow({

if (intent) {
context.setIntent(intent.displayName);
context.setAsHandled();
} else {
context.setAsNotHandled();
}

const Action = actions[intent.name] || actions[intent.displayName];
Expand Down
3 changes: 3 additions & 0 deletions packages/bottender-luis/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ module.exports = function luis({

if (topScoringIntent && topScoringIntent.score > scoreThreshold) {
context.setIntent(topScoringIntent.intent);
context.setAsHandled();

const Action = actions[topScoringIntent.intent];

Expand All @@ -85,6 +86,8 @@ module.exports = function luis({
sentimentAnalysis,
});
}
} else {
context.setAsNotHandled();
}

return next;
Expand Down
3 changes: 3 additions & 0 deletions packages/bottender-qna-maker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ module.exports = function qnaMaker({
return async function TopAnswer() {
if (topAnswer.id !== -1) {
context.setIntent(`qna_${topAnswer.id}`);
context.setAsHandled();
} else {
context.setAsNotHandled();
}

await context.sendText(topAnswer.answer);
Expand Down
3 changes: 3 additions & 0 deletions packages/bottender-rasa/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ module.exports = function rasa({

if (intent && intent.confidence > confidenceThreshold) {
context.setIntent(intent.name);
context.setAsHandled();

const Action = actions[intent.name];
if (Action) {
return withProps(Action as any, { intent, entities });
}
} else {
context.setAsNotHandled();
}

return next;
Expand Down
2 changes: 0 additions & 2 deletions packages/bottender/src/console/ConsoleContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ export default class ConsoleContext extends Context<
*
*/
async sendText(text: string, ...args: any[]): Promise<void> {
this._isHandled = true;
if (args.length > 0 && this._fallbackMethods) {
this._client.sendText(
`${text}\nwith other args:\n${JSON.stringify(args, null, 2)}`
Expand All @@ -102,7 +101,6 @@ export default class ConsoleContext extends Context<
}

async _methodMissing(method: string, args: any[]): Promise<void> {
this._isHandled = true;
this._client.sendText(
`${method} with args:\n${JSON.stringify(args, null, 2)}`
);
Expand Down
16 changes: 0 additions & 16 deletions packages/bottender/src/console/__tests__/ConsoleContext.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,6 @@ describe('#sendText', () => {
expect(client.sendText).toBeCalledWith('hello');
});

it('should mark context as handled', async () => {
const { context } = setup();

await context.sendText('hello');

expect(context.isHandled).toBe(true);
});

it('should support fallbackMethods with other args', async () => {
const { context, client } = setup({ fallbackMethods: true });

Expand Down Expand Up @@ -131,14 +123,6 @@ describe('method missing', () => {
);
});

it('should mark context as handled', async () => {
const { context } = setup({ fallbackMethods: true });

await context.sendABC('hello');

expect(context.isHandled).toBe(true);
});

it('should not proxy blacklisted methods', async () => {
const { context } = setup({ fallbackMethods: true });

Expand Down
20 changes: 18 additions & 2 deletions packages/bottender/src/context/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default abstract class Context<C extends Client, E extends Event> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
abstract sendText(text: string, options?: Record<string, any>): any;

_isHandled = false;
_isHandled: boolean | null = null;

_isSessionWritten = false;

Expand Down Expand Up @@ -117,7 +117,7 @@ export default abstract class Context<C extends Client, E extends Event> {
return this._session;
}

get isHandled(): boolean {
get isHandled(): boolean | null {
return this._isHandled;
}

Expand Down Expand Up @@ -205,6 +205,22 @@ export default abstract class Context<C extends Client, E extends Event> {
this._intent = intent;
}

/**
* Set the conversation context as handled or not handled by boolean.
*
*/
setAsHandled(handled = true) {
this._isHandled = handled;
}

/**
* Set the conversation context as not handled.
*
*/
setAsNotHandled() {
this.setAsHandled(false);
}

emitError(err: Error): void {
if (this._emitter) {
this._emitter.emit('error', err, this);
Expand Down
40 changes: 40 additions & 0 deletions packages/bottender/src/context/__tests__/Context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,43 @@ describe('intent', () => {
expect(context.intent).toEqual('hello-world');
});
});

describe('handled', () => {
it('should default to null', () => {
const context = new TestContext({ client: {}, event: {} });

expect(context.isHandled).toBeNull();
});

it('should be true after calling context.setAsHandled()', () => {
const context = new TestContext({ client: {}, event: {} });

context.setAsHandled();

expect(context.isHandled).toEqual(true);
});

it('should be true after calling context.setAsHandled(true)', () => {
const context = new TestContext({ client: {}, event: {} });

context.setAsHandled(true);

expect(context.isHandled).toEqual(true);
});

it('should be false after calling context.setAsHandled(false)', () => {
const context = new TestContext({ client: {}, event: {} });

context.setAsHandled(false);

expect(context.isHandled).toEqual(false);
});

it('should be false after calling context.setAsNotHandled()', () => {
const context = new TestContext({ client: {}, event: {} });

context.setAsNotHandled();

expect(context.isHandled).toEqual(false);
});
});
6 changes: 0 additions & 6 deletions packages/bottender/src/line/LineContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,12 @@ class LineContext extends Context<LineClient, LineEvent> {

switch (this._session.type) {
case 'room':
this._isHandled = true;
return this._client.leaveRoom(this._session.room.id, {
...(this._customAccessToken
? { accessToken: this._customAccessToken }
: undefined),
} as any);
case 'group':
this._isHandled = true;
return this._client.leaveGroup(this._session.group.id, {
...(this._customAccessToken
? { accessToken: this._customAccessToken }
Expand Down Expand Up @@ -446,8 +444,6 @@ class LineContext extends Context<LineClient, LineEvent> {
reply(messages: LineTypes.Message[], options: LineTypes.MessageOptions = {}) {
invariant(!this._isReplied, 'Can not reply event multiple times');

this._isHandled = true;

if (this._shouldBatch) {
this._replyMessages.push(...messages);

Expand Down Expand Up @@ -614,8 +610,6 @@ class LineContext extends Context<LineClient, LineEvent> {
return;
}

this._isHandled = true;

if (this._shouldBatch) {
this._pushMessages.push(...messages);
return;
Expand Down
36 changes: 0 additions & 36 deletions packages/bottender/src/line/__tests__/LineContext.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ describe('#leave', () => {
expect(client.leaveGroup).not.toBeCalled();
expect(client.leaveRoom).not.toBeCalled();
expect(warning).toBeCalled();
expect(context.isHandled).toBe(false);
});

it('leave group', async () => {
Expand All @@ -198,7 +197,6 @@ describe('#leave', () => {
await context.leave();

expect(client.leaveGroup).toBeCalledWith('fakeGroupId', {});
expect(context.isHandled).toBe(true);
});

it('leave room', async () => {
Expand All @@ -207,7 +205,6 @@ describe('#leave', () => {
await context.leave();

expect(client.leaveRoom).toBeCalledWith('fakeRoomId', {});
expect(context.isHandled).toBe(true);
});

it('not leave user', async () => {
Expand All @@ -218,7 +215,6 @@ describe('#leave', () => {
expect(client.leaveGroup).not.toBeCalled();
expect(client.leaveRoom).not.toBeCalled();
expect(warning).toBeCalled();
expect(context.isHandled).toBe(false);
});
});

Expand Down Expand Up @@ -321,14 +317,6 @@ describe('#replyText', () => {
expect(error.message).toEqual('Can not reply event multiple times');
});

it('should mark context as handled', async () => {
const { context } = setup();

await context.replyText('hello');

expect(context.isHandled).toBe(true);
});

it('should support custom token', async () => {
const { context, client } = setup({
customAccessToken: 'anyToken',
Expand Down Expand Up @@ -457,14 +445,6 @@ describe('#pushText', () => {
);
});

it('should mark context as handled', async () => {
const { context } = setup();

await context.pushText('hello');

expect(context.isHandled).toBe(true);
});

it('should call warning and not to send if dont have session', async () => {
const { context, client } = setup({ session: false });

Expand Down Expand Up @@ -513,14 +493,6 @@ describe('send APIs', () => {
{}
);
});

it('should mark context as handled', async () => {
const { context } = setup();

await context.send([Line.createText('2'), Line.createText('3')]);

expect(context.isHandled).toBe(true);
});
});

describe('#sendText', () => {
Expand Down Expand Up @@ -1329,7 +1301,6 @@ describe('profile APIs', () => {
expect(client.getGroupMemberProfile).not.toBeCalled();
expect(client.getRoomMemberProfile).not.toBeCalled();
expect(warning).toBeCalled();
expect(context.isHandled).toBe(false);
});

it('not get profile without user in session', async () => {
Expand All @@ -1349,7 +1320,6 @@ describe('profile APIs', () => {
expect(client.getGroupMemberProfile).not.toBeCalled();
expect(client.getRoomMemberProfile).not.toBeCalled();
expect(warning).toBeCalled();
expect(context.isHandled).toBe(false);
});

it('get user profile in group', async () => {
Expand Down Expand Up @@ -1394,7 +1364,6 @@ describe('profile APIs', () => {
expect(client.getGroupMemberProfile).not.toBeCalled();
expect(client.getRoomMemberProfile).not.toBeCalled();
expect(warning).toBeCalled();
expect(context.isHandled).toBe(false);
});

it('get member profile in group', async () => {
Expand Down Expand Up @@ -1429,7 +1398,6 @@ describe('profile APIs', () => {
expect(client.getGroupMemberProfile).not.toBeCalled();
expect(client.getRoomMemberProfile).not.toBeCalled();
expect(warning).toBeCalled();
expect(context.isHandled).toBe(false);
});
});
});
Expand All @@ -1444,7 +1412,6 @@ describe('member IDs APIs', () => {
expect(client.getRoomMemberIds).not.toBeCalled();
expect(client.getGroupMemberIds).not.toBeCalled();
expect(warning).toBeCalled();
expect(context.isHandled).toBe(false);
});

it('get memeber ids in group', async () => {
Expand Down Expand Up @@ -1479,7 +1446,6 @@ describe('member IDs APIs', () => {
expect(client.getRoomMemberIds).not.toBeCalled();
expect(client.getGroupMemberIds).not.toBeCalled();
expect(warning).toBeCalled();
expect(context.isHandled).toBe(false);
});
});

Expand All @@ -1492,7 +1458,6 @@ describe('member IDs APIs', () => {
expect(client.getAllRoomMemberIds).not.toBeCalled();
expect(client.getAllGroupMemberIds).not.toBeCalled();
expect(warning).toBeCalled();
expect(context.isHandled).toBe(false);
});

it('get memeber ids in group', async () => {
Expand All @@ -1519,7 +1484,6 @@ describe('member IDs APIs', () => {
expect(client.getAllRoomMemberIds).not.toBeCalled();
expect(client.getAllGroupMemberIds).not.toBeCalled();
expect(warning).toBeCalled();
expect(context.isHandled).toBe(false);
});
});
});
Expand Down
Loading

0 comments on commit 2de9e12

Please sign in to comment.