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

added numberOfAttempts to promptOptions #870

Merged
merged 3 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions libraries/botbuilder-dialogs/src/prompts/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export interface PromptOptions {
* (Optional) Additional validation rules to pass the prompts validator routine.
*/
validations?: object;

/**
* (Optional) Count of the number of times the prompt has retried.
*/
numberOfAttempts?: number;
}

/**
Expand Down Expand Up @@ -195,12 +200,18 @@ export abstract class Prompt<T> extends Dialog {
// Validate the return value
let isValid = false;
if (this.validator) {
if (state.options.numberOfAttempts === undefined) {
state.options.numberOfAttempts = 0;
}
isValid = await this.validator({
context: dc.context,
recognized: recognized,
state: state.state,
options: state.options
});
if (state.options.numberOfAttempts !== undefined) {
state.options.numberOfAttempts++;
}
} else if (recognized.succeeded) {
isValid = true;
}
Expand Down
49 changes: 48 additions & 1 deletion libraries/botbuilder-dialogs/tests/numberPrompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ describe('NumberPrompt', function () {
const dialogs = new DialogSet(dialogState);
dialogs.add(new NumberPrompt('prompt', async (prompt) => {
assert(prompt);
console.log('recognized value:', prompt.recognized.value);
return prompt.recognized.value === 0;
}));

Expand All @@ -202,4 +201,52 @@ describe('NumberPrompt', function () {
.send('zero')
.assertReply('0')
});

it ('should see numberOfAttempts counter increment', 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: 'Send me a zero', retryPrompt: 'Send 0 or zero'});
} else if (results.status === DialogTurnStatus.complete) {
await turnContext.sendActivity('ok');
}
await convoState.saveChanges(turnContext);
});

const convoState = new ConversationState(new MemoryStorage());

const dialogState = convoState.createProperty('dialogState');
const dialogs = new DialogSet(dialogState);

dialogs.add(new NumberPrompt('prompt', async (prompt) => {
if (prompt.recognized.value !== 0) {
prompt.context.sendActivity(`numberOfAttempts ${prompt.options.numberOfAttempts}`);
return false;
}
return true;
}));

await adapter.send('Hello')
.assertReply('Send me a zero')
.send('100')
.assertReply('numberOfAttempts 0')
.send('200')
.assertReply('numberOfAttempts 1')
.send('300')
.assertReply('numberOfAttempts 2')
.send('0')
.assertReply('ok')
.send('Another!')
.assertReply('Send me a zero')
.send('100')
.assertReply('numberOfAttempts 0')
.send('200')
.assertReply('numberOfAttempts 1')
.send('300')
.assertReply('numberOfAttempts 2')
.send('0')
.assertReply('ok')
});
});