Skip to content
This repository was archived by the owner on Jan 5, 2026. It is now read-only.
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
21 changes: 18 additions & 3 deletions libraries/Microsoft.Bot.Builder.Dialogs/Prompts/ActivityPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ public ActivityPrompt(string dialogId, PromptValidator<Activity> validator)
// Initialize prompt state
var state = dc.ActiveDialog.State;
state[PersistedOptions] = opt;
state[PersistedState] = new Dictionary<string, object>();
state[PersistedState] = new Dictionary<string, object>
{
{ Prompt<int>.AttemptCountKey, 0 },
};

// Send initial prompt
await OnPromptAsync(dc.Context, (IDictionary<string, object>)state[PersistedState], (PromptOptions)state[PersistedOptions], cancellationToken).ConfigureAwait(false);
Expand All @@ -82,9 +85,21 @@ public ActivityPrompt(string dialogId, PromptValidator<Activity> validator)
var options = (PromptOptions)instance.State[PersistedOptions];
var recognized = await OnRecognizeAsync(dc.Context, state, options, cancellationToken).ConfigureAwait(false);

// Increment attempt count
// Convert.ToInt32 For issue https://github.com/Microsoft/botbuilder-dotnet/issues/1859
state[Prompt<int>.AttemptCountKey] = Convert.ToInt32(state[Prompt<int>.AttemptCountKey]) + 1;

// Validate the return value
var promptContext = new PromptValidatorContext<Activity>(dc.Context, recognized, state, options);
var isValid = await _validator(promptContext, cancellationToken).ConfigureAwait(false);
var isValid = false;
if (_validator != null)
{
var promptContext = new PromptValidatorContext<Activity>(dc.Context, recognized, state, options);
isValid = await _validator(promptContext, cancellationToken).ConfigureAwait(false);
}
else if (recognized.Succeeded)
{
isValid = true;
}

// Return recognized value or re-prompt
if (isValid)
Expand Down
10 changes: 9 additions & 1 deletion libraries/Microsoft.Bot.Builder.Dialogs/Prompts/OAuthPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ public OAuthPrompt(string dialogId, OAuthPromptSettings settings, PromptValidato
var timeout = _settings.Timeout ?? DefaultPromptTimeout;
var state = dc.ActiveDialog.State;
state[PersistedOptions] = opt;
state[PersistedState] = new Dictionary<string, object>();
state[PersistedState] = new Dictionary<string, object>
{
{ Prompt<int>.AttemptCountKey, 0 },
};

state[PersistedExpires] = DateTime.Now.AddMilliseconds(timeout);

// Attempt to get the users token
Expand Down Expand Up @@ -152,6 +156,10 @@ public OAuthPrompt(string dialogId, OAuthPromptSettings settings, PromptValidato
var promptState = (IDictionary<string, object>)state[PersistedState];
var promptOptions = (PromptOptions)state[PersistedOptions];

// Increment attempt count
// Convert.ToInt32 For issue https://github.com/Microsoft/botbuilder-dotnet/issues/1859
promptState[Prompt<int>.AttemptCountKey] = Convert.ToInt32(promptState[Prompt<int>.AttemptCountKey]) + 1;

// Validate the return value
var isValid = false;
if (_validator != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ internal PromptValidatorContext(ITurnContext turnContext, PromptRecognizerResult
/// Gets the number of times the prompt has been executed.
/// </summary>
/// <value>A number indicating how many times the prompt was invoked (starting at 1 for the first time it was called).</value>
public int AttemptCount => Convert.ToInt32(State[Prompt<T>.AttemptCountKey]);
public int AttemptCount
{
get
{
if (!State.ContainsKey(Prompt<T>.AttemptCountKey))
{
return 0;
}

return Convert.ToInt32(State[Prompt<T>.AttemptCountKey]);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ public async Task ActivityPromptResumeDialogShouldReturnDialogEndOfTurn()

private async Task<bool> Validator(PromptValidatorContext<Activity> promptContext, CancellationToken cancellationToken)
{
Assert.IsTrue(promptContext.AttemptCount > 0);

var activity = promptContext.Recognized.Value;
if (activity.Type == ActivityTypes.Event)
{
Expand Down