Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActivityPrompt wasn't using retryPrompt if specified. #673

Merged
merged 3 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions libraries/botbuilder-dialogs/src/prompts/activityPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { Activity, InputHints, TurnContext } from 'botbuilder-core';
import { Activity, InputHints, TurnContext, ActivityTypes } from 'botbuilder-core';
import { Dialog, DialogInstance, DialogReason, DialogTurnResult } from '../dialog';
import { DialogContext } from '../dialogContext';
import { PromptOptions, PromptRecognizerResult, PromptValidator } from './prompt';
Expand Down Expand Up @@ -45,7 +45,7 @@ export abstract class ActivityPrompt extends Dialog {
state.state = {};

// Send initial prompt
await this.onPrompt(dc.context, state.state, state.options);
await this.onPrompt(dc.context, state.state, state.options, false);

return Dialog.EndOfTurn;
}
Expand All @@ -69,6 +69,10 @@ export abstract class ActivityPrompt extends Dialog {
if (isValid) {
return await dc.endDialog(recognized.value);
} else {
if (dc.context.activity.type === ActivityTypes.Message && !dc.context.responded) {
await this.onPrompt(dc.context, state.state, state.options, true);
}

return Dialog.EndOfTurn;
}
}
Expand All @@ -86,11 +90,13 @@ export abstract class ActivityPrompt extends Dialog {

public async repromptDialog(context: TurnContext, instance: DialogInstance): Promise<void> {
const state: ActivityPromptState = instance.state as ActivityPromptState;
await this.onPrompt(context, state.state, state.options);
await this.onPrompt(context, state.state, state.options, true);
}

protected async onPrompt(context: TurnContext, state: object, options: PromptOptions): Promise<void> {
if (options.prompt) {
protected async onPrompt(context: TurnContext, state: object, options: PromptOptions, isRetry: boolean): Promise<void> {
if (isRetry && options.retryPrompt) {
await context.sendActivity(options.retryPrompt, undefined, InputHints.ExpectingInput);
} else if (options.prompt) {
await context.sendActivity(options.prompt, undefined, InputHints.ExpectingInput);
}
}
Expand Down
36 changes: 31 additions & 5 deletions libraries/botbuilder-dialogs/tests/activityPrompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('ActivityPrompt', function () {
.assertReply('You said test');
});

it('should return Dialog.EndOfTurn if validator returned false.', async function () {
it('should re-prompt with original prompt if validator returned false.', async function () {
const adapter = new TestAdapter(async turnContext => {
const dc = await dialogs.createContext(turnContext);

Expand All @@ -57,9 +57,6 @@ describe('ActivityPrompt', function () {
} else if (results.status === DialogTurnStatus.complete) {
const reply = results.result.text;
await turnContext.sendActivity(`You said ${reply}`);
} else if (results.status === DialogTurnStatus.waiting) {
// This status is reached if the ActivityPrompt fails the validation process.
await turnContext.sendActivity('Test complete.');
}
await convoState.saveChanges(turnContext);
});
Expand All @@ -76,7 +73,36 @@ describe('ActivityPrompt', function () {
await adapter.send('Hello')
.assertReply('Please send an activity.')
.send('test')
.assertReply('Test complete.');
.assertReply('Please send an activity.');
});

it('should re-prompt with custom retyrPrompt if validator returned false.', async function () {
const adapter = new TestAdapter(async turnContext => {
const dc = await dialogs.createContext(turnContext);

const results = await dc.continueDialog();
if (results.status === DialogTurnStatus.empty) {
await dc.prompt('prompt', { prompt: 'Please send activity.', retryPrompt: 'Activity not received.' });
} else if (results.status === DialogTurnStatus.complete) {
const reply = results.result.text;
await turnContext.sendActivity(`You said ${reply}`);
}
await convoState.saveChanges(turnContext);
});
const convoState = new ConversationState(new MemoryStorage());

const dialogState = convoState.createProperty('dialogState');
const dialogs = new DialogSet(dialogState);
dialogs.add(new SimpleActivityPrompt('prompt', async prompt => {
assert(prompt, `validator missing PromptValidatorContext.`);
assert(typeof prompt.recognized.value === 'object', 'recognized.value was not an object.');
return false;
}));

await adapter.send('Hello')
.assertReply('Please send activity.')
.send('test')
.assertReply('Activity not received.');
});

it('should have resumeDialog() prompt user and return Dialog.EndOfTurn.', async function () {
Expand Down