Exemple #1
0
 protected virtual Task <PromptRecognizerResult <Activity> > OnRecognizeAsync(ITurnContext context, IDictionary <string, object> state, PromptOptions options)
 {
     return(Task.FromResult(new PromptRecognizerResult <Activity>
     {
         Succeeded = true,
         Value = context.Activity,
     }));
 }
Exemple #2
0
 protected abstract Task OnPromptAsync(DialogContext dc, PromptOptions options, bool isRetry);
        protected override Task <PromptRecognizerResult <FoundChoice> > OnRecognizeAsync(ITurnContext turnContext, IDictionary <string, object> state, PromptOptions options)
        {
            if (turnContext == null)
            {
                throw new ArgumentNullException(nameof(turnContext));
            }

            var choices = options.Choices ?? new List <Choice>();

            var result = new PromptRecognizerResult <FoundChoice>();

            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                var activity  = turnContext.Activity;
                var utterance = activity.Text;
                var opt       = RecognizerOptions ?? new FindChoicesOptions();
                opt.Locale = activity.Locale ?? opt.Locale ?? DefaultLocale ?? English;
                var results = ChoiceRecognizers.RecognizeChoices(utterance, choices, opt);
                if (results != null && results.Count > 0)
                {
                    result.Succeeded = true;
                    result.Value     = results[0].Resolution;
                }
            }

            return(Task.FromResult(result));
        }
 /// <summary>   Constructor for a prompt int64 dialog. </summary>
 /// <param name="promptOptions"> THe prompt options.</param>
 public PromptInt64(PromptOptions <long> promptOptions)
     : base(promptOptions)
 {
 }
 /// <summary>
 /// Constructs a choice dialog.
 /// </summary>
 /// <param name="promptOptions"> The prompt options</param>
 public PromptChoice(PromptOptions <T> promptOptions)
     : base(promptOptions)
 {
     SetField.CheckNull(nameof(promptOptions.Options), promptOptions.Options);
 }
Exemple #6
0
 protected abstract Task <PromptRecognizerResult <T> > OnRecognizeAsync(ITurnContext turnContext, IDictionary <string, object> state, PromptOptions options);
        /// <summary>
        /// Prompt for one of a set of choices.
        /// </summary>
        /// <remarks><typeparamref name="T"/> should implement <see cref="object.ToString"/></remarks>
        /// <typeparam name="T"> The type of the options.</typeparam>
        /// <param name="context"> The dialog context.</param>
        /// <param name="resume"> Resume handler.</param>
        /// <param name="promptOptions"> The prompt options.</param>
        public static void Choice <T>(IDialogContext context, ResumeAfter <T> resume, PromptOptions <T> promptOptions)
        {
            var child = new PromptChoice <T>(promptOptions);

            context.Call <T>(child, resume);
        }
        protected override Task <PromptRecognizerResult <IList <Attachment> > > OnRecognizeAsync(ITurnContext turnContext, IDictionary <string, object> state, PromptOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (turnContext == null)
            {
                throw new ArgumentNullException(nameof(turnContext));
            }

            var result = new PromptRecognizerResult <IList <Attachment> >();

            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                var message = turnContext.Activity.AsMessageActivity();
                if (message.Attachments != null && message.Attachments.Count > 0)
                {
                    result.Succeeded = true;
                    result.Value     = message.Attachments;
                }
            }

            return(Task.FromResult(result));
        }
        protected virtual async Task OnPromptAsync(ITurnContext turnContext, IDictionary <string, object> state, PromptOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (turnContext == null)
            {
                throw new ArgumentNullException(nameof(turnContext));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (options.Prompt != null)
            {
                await turnContext.SendActivityAsync(options.Prompt, cancellationToken).ConfigureAwait(false);
            }
        }
        /// <summary>
        /// Attempts to recognize the user's input.
        /// </summary>
        /// <param name="turnContext">Context for the current turn of conversation with the user.</param>
        /// <param name="state">Contains state for the current instance of the prompt on the dialog stack.</param>
        /// <param name="options">A prompt options object constructed from the options initially provided
        /// in the call to <see cref="DialogContext.PromptAsync(string, PromptOptions, CancellationToken)"/>.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects
        /// or threads to receive notice of cancellation.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        /// <remarks>If the task is successful, the result describes the result of the recognition attempt.</remarks>
        protected override Task <PromptRecognizerResult <T> > OnRecognizeAsync(ITurnContext turnContext, IDictionary <string, object> state, PromptOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (turnContext == null)
            {
                throw new ArgumentNullException(nameof(turnContext));
            }

            var result = new PromptRecognizerResult <T>();

            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                var message = turnContext.Activity.AsMessageActivity();
                var culture = turnContext.Activity.Locale ?? DefaultLocale ?? English;
                var results = RecognizeNumberWithUnit(message.Text, culture);
                if (results.Count > 0)
                {
                    // Try to parse value based on type
                    string text = string.Empty;

                    // Try to parse value based on type
                    var valueResolution = results[0].Resolution["value"];
                    if (valueResolution != null)
                    {
                        text = valueResolution.ToString();
                    }

                    if (typeof(T) == typeof(float))
                    {
                        if (float.TryParse(text, NumberStyles.Any, new CultureInfo(culture), out var value))
                        {
                            result.Succeeded = true;
                            result.Value     = (T)(object)value;
                        }
                    }
                    else if (typeof(T) == typeof(int))
                    {
                        if (int.TryParse(text, NumberStyles.Any, new CultureInfo(culture), out var value))
                        {
                            result.Succeeded = true;
                            result.Value     = (T)(object)value;
                        }
                    }
                    else if (typeof(T) == typeof(long))
                    {
                        if (long.TryParse(text, NumberStyles.Any, new CultureInfo(culture), out var value))
                        {
                            result.Succeeded = true;
                            result.Value     = (T)(object)value;
                        }
                    }
                    else if (typeof(T) == typeof(double))
                    {
                        if (double.TryParse(text, NumberStyles.Any, new CultureInfo(culture), out var value))
                        {
                            result.Succeeded = true;
                            result.Value     = (T)(object)value;
                        }
                    }
                    else if (typeof(T) == typeof(decimal))
                    {
                        if (decimal.TryParse(text, NumberStyles.Any, new CultureInfo(culture), out var value))
                        {
                            result.Succeeded = true;
                            result.Value     = (T)(object)value;
                        }
                    }
                }
            }

            return(Task.FromResult(result));
        }
Exemple #11
0
 /// <summary>
 /// When overridden in a derived class, prompts the user for input.
 /// </summary>
 /// <param name="turnContext">Context for the current turn of conversation with the user.</param>
 /// <param name="state">Contains state for the current instance of the prompt on the dialog stack.</param>
 /// <param name="options">A prompt options object constructed from the options initially provided
 /// in the call to <see cref="DialogContext.PromptAsync(string, PromptOptions, CancellationToken)"/>.</param>
 /// <param name="cancellationToken">A cancellation token that can be used by other objects
 /// or threads to receive notice of cancellation.</param>
 /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
 protected virtual async Task OnPromptAsync(ITurnContext turnContext, IDictionary <string, object> state, PromptOptions options, CancellationToken cancellationToken = default(CancellationToken))
 => await OnPromptAsync(turnContext, state, options, false, cancellationToken).ConfigureAwait(false);
Exemple #12
0
        protected override Task <PromptRecognizerResult <IList <DateTimeResolution> > > OnRecognizeAsync(ITurnContext turnContext, IDictionary <string, object> state, PromptOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (turnContext == null)
            {
                throw new ArgumentNullException(nameof(turnContext));
            }

            var result = new PromptRecognizerResult <IList <DateTimeResolution> >();

            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                var message = turnContext.Activity.AsMessageActivity();
                var culture = turnContext.Activity.Locale ?? DefaultLocale ?? English;
                var results = DateTimeRecognizer.RecognizeDateTime(message.Text, culture);
                if (results.Count > 0)
                {
                    // Return list of resolutions from first match
                    result.Succeeded = true;
                    result.Value     = new List <DateTimeResolution>();
                    var values = (List <Dictionary <string, string> >)results[0].Resolution["values"];
                    foreach (var value in values)
                    {
                        result.Value.Add(ReadResolution(value));
                    }
                }
            }

            return(Task.FromResult(result));
        }
        /// <summary>
        /// Attempts to recognize the user's input.
        /// </summary>
        /// <param name="turnContext">Context for the current turn of conversation with the user.</param>
        /// <param name="state">Contains state for the current instance of the prompt on the dialog stack.</param>
        /// <param name="options">A prompt options object constructed from the options initially provided
        /// in the call to <see cref="DialogContext.PromptAsync(string, PromptOptions, CancellationToken)"/>.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects
        /// or threads to receive notice of cancellation.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        /// <remarks>If the task is successful, the result describes the result of the recognition attempt.</remarks>
        protected override Task <PromptRecognizerResult <T> > OnRecognizeAsync(ITurnContext turnContext, IDictionary <string, object> state, PromptOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (turnContext == null)
            {
                throw new ArgumentNullException(nameof(turnContext));
            }

            var result = new PromptRecognizerResult <T>();

            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                var message = turnContext.Activity.AsMessageActivity();
                var culture = turnContext.Activity.Locale ?? DefaultLocale ?? English;
                var results = NumberRecognizer.RecognizeNumber(message.Text, culture);
                if (results.Count > 0)
                {
                    // Try to parse value based on type
                    var text = results[0].Resolution["value"].ToString();
                    if (typeof(T) == typeof(float))
                    {
                        if (float.TryParse(text, out var value))
                        {
                            result.Succeeded = true;
                            result.Value     = (T)(object)value;
                        }
                    }
                    else if (typeof(T) == typeof(int))
                    {
                        if (int.TryParse(text, out var value))
                        {
                            result.Succeeded = true;
                            result.Value     = (T)(object)value;
                        }
                    }
                    else if (typeof(T) == typeof(long))
                    {
                        if (long.TryParse(text, out var value))
                        {
                            result.Succeeded = true;
                            result.Value     = (T)(object)value;
                        }
                    }
                    else if (typeof(T) == typeof(double))
                    {
                        if (double.TryParse(text, out var value))
                        {
                            result.Succeeded = true;
                            result.Value     = (T)(object)value;
                        }
                    }
                    else if (typeof(T) == typeof(decimal))
                    {
                        if (decimal.TryParse(text, out var value))
                        {
                            result.Succeeded = true;
                            result.Value     = (T)(object)value;
                        }
                    }
                    else
                    {
                        throw new NotSupportedException($"NumberPrompt: type argument T of type 'typeof(T)' is not supported");
                    }
                }
            }

            return(Task.FromResult(result));
        }
Exemple #14
0
        protected override async Task OnPromptAsync(ITurnContext context, IDictionary <string, object> state, PromptOptions options, bool isRetry)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (isRetry && options.RetryPrompt != null)
            {
                await context.SendActivityAsync(options.RetryPrompt).ConfigureAwait(false);
            }
            else if (options.Prompt != null)
            {
                await context.SendActivityAsync(options.Prompt).ConfigureAwait(false);
            }
        }
Exemple #15
0
        /// <summary>
        /// Called when a prompt dialog is pushed onto the dialog stack and is being activated.
        /// </summary>
        /// <param name="dc">The dialog context for the current turn of the conversation.</param>
        /// <param name="options">Optional, additional information to pass to the prompt being started.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects
        /// or threads to receive notice of cancellation.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        /// <remarks>If the task is successful, the result indicates whether the prompt is still
        /// active after the turn has been processed by the prompt.</remarks>
        public override async Task <DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (dc == null)
            {
                throw new ArgumentNullException(nameof(dc));
            }

            if (options is CancellationToken)
            {
                throw new ArgumentException($"{nameof(options)} cannot be a cancellation token");
            }

            PromptOptions opt = null;

            if (options != null)
            {
                if (options is PromptOptions)
                {
                    // Ensure prompts have input hint set
                    opt = options as PromptOptions;
                    if (opt.Prompt != null && string.IsNullOrEmpty(opt.Prompt.InputHint))
                    {
                        opt.Prompt.InputHint = InputHints.AcceptingInput;
                    }

                    if (opt.RetryPrompt != null && string.IsNullOrEmpty(opt.RetryPrompt.InputHint))
                    {
                        opt.RetryPrompt.InputHint = InputHints.AcceptingInput;
                    }
                }
                else
                {
                    throw new ArgumentException(nameof(options));
                }
            }

            // Initialize state
            var timeout = _settings.Timeout ?? (int)TurnStateConstants.OAuthLoginTimeoutValue.TotalMilliseconds;
            var state   = dc.ActiveDialog.State;

            state[PersistedOptions] = opt;
            state[PersistedState]   = new Dictionary <string, object>
            {
                { Prompt <int> .AttemptCountKey, 0 },
            };

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

            // Attempt to get the users token
            if (!(dc.Context.Adapter is IExtendedUserTokenProvider adapter))
            {
                throw new InvalidOperationException("OAuthPrompt.Recognize(): not supported by the current adapter");
            }

            var output = await adapter.GetUserTokenAsync(dc.Context, _settings.OAuthAppCredentials, _settings.ConnectionName, null, cancellationToken).ConfigureAwait(false);

            if (output != null)
            {
                // Return token
                return(await dc.EndDialogAsync(output, cancellationToken).ConfigureAwait(false));
            }

            // Prompt user to login
            await SendOAuthCardAsync(dc.Context, opt?.Prompt, cancellationToken).ConfigureAwait(false);

            return(EndOfTurn);
        }
 protected virtual Task <PromptRecognizerResult <Activity> > OnRecognizeAsync(ITurnContext turnContext, IDictionary <string, object> state, PromptOptions options, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(Task.FromResult(new PromptRecognizerResult <Activity>
     {
         Succeeded = true,
         Value = turnContext.Activity,
     }));
 }
Exemple #17
0
 protected abstract Task OnPromptAsync(ITurnContext turnContext, IDictionary <string, object> state, PromptOptions options, bool isRetry);
Exemple #18
0
        protected override async Task <ConfirmResult> OnRecognizeAsync(DialogContext dc, PromptOptions options)
        {
            if (dc == null)
            {
                throw new ArgumentNullException(nameof(dc));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            return(await _prompt.RecognizeAsync(dc.Context).ConfigureAwait(false));
        }
        /// <summary>
        /// Ask a yes/no questions.
        /// </summary>
        /// <param name="context"> The dialog context.</param>
        /// <param name="resume"> Resume handler.</param>
        /// <param name="promptOptions"> The options for the prompt, <see cref="PromptOptions{T}"/>.</param>
        public static void Confirm(IDialogContext context, ResumeAfter <bool> resume, PromptOptions <string> promptOptions)
        {
            var child = new PromptConfirm(promptOptions);

            context.Call <bool>(child, resume);
        }
Exemple #20
0
 internal PromptValidatorContext(DialogContext dc, IDictionary <string, object> state, PromptOptions options, PromptRecognizerResult <T> recognized)
 {
     _dc        = dc;
     HasEnded   = false;
     EndResult  = null;
     Options    = options;
     Recognized = recognized;
 }
 /// <summary>
 /// Constructor for a prompt confirmation dialog.
 /// </summary>
 /// <param name="promptOptions"> THe prompt options.</param>
 public PromptConfirm(PromptOptions <string> promptOptions)
     : base(promptOptions)
 {
     this.promptOptions.DefaultRetry = this.DefaultRetry;
 }
Exemple #22
0
 /// <summary>
 /// When overridden in a derived class, prompts the user for input.
 /// </summary>
 /// <param name="turnContext">Context for the current turn of conversation with the user.</param>
 /// <param name="state">Contains state for the current instance of the prompt on the dialog stack.</param>
 /// <param name="options">A prompt options object constructed from the options initially provided
 /// in the call to <see cref="DialogContext.PromptAsync(string, PromptOptions, CancellationToken)"/>.</param>
 /// <param name="isRetry">true if this is the first time this prompt dialog instance
 /// on the stack is prompting the user for input; otherwise, false.</param>
 /// <param name="cancellationToken">A cancellation token that can be used by other objects
 /// or threads to receive notice of cancellation.</param>
 /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
 protected abstract Task OnPromptAsync(ITurnContext turnContext, IDictionary <string, object> state, PromptOptions options, bool isRetry, CancellationToken cancellationToken = default);
 /// <summary>   Constructor for a prompt double dialog. </summary>
 /// <param name="promptOptions"> THe prompt options.</param>
 public PromptDouble(PromptOptions <double> promptOptions)
     : base(promptOptions)
 {
 }
Exemple #24
0
 /// <summary>
 /// When overridden in a derived class, attempts to recognize the user's input.
 /// </summary>
 /// <param name="turnContext">Context for the current turn of conversation with the user.</param>
 /// <param name="state">Contains state for the current instance of the prompt on the dialog stack.</param>
 /// <param name="options">A prompt options object constructed from the options initially provided
 /// in the call to <see cref="DialogContext.PromptAsync(string, PromptOptions, CancellationToken)"/>.</param>
 /// <param name="cancellationToken">A cancellation token that can be used by other objects
 /// or threads to receive notice of cancellation.</param>
 /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
 /// <remarks>If the task is successful, the result describes the result of the recognition attempt.</remarks>
 protected abstract Task <PromptRecognizerResult <T> > OnRecognizeAsync(ITurnContext turnContext, IDictionary <string, object> state, PromptOptions options, CancellationToken cancellationToken = default);
        protected override async Task <AttachmentResult> OnRecognize(DialogContext dc, PromptOptions options)
        {
            if (dc == null)
            {
                throw new ArgumentNullException(nameof(dc));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            return(await _prompt.Recognize(dc.Context));
        }
Exemple #26
0
 internal PromptValidatorContext(ITurnContext turnContext, PromptRecognizerResult <T> recognized, IDictionary <string, object> state, PromptOptions options)
 {
     Context    = turnContext;
     Options    = options;
     Recognized = recognized;
     State      = state;
 }
Exemple #27
0
 protected abstract Task <T> OnRecognizeAsync(DialogContext dc, PromptOptions options);
        /// <summary>
        /// Prompts the user for input.
        /// </summary>
        /// <param name="turnContext">Context for the current turn of conversation with the user.</param>
        /// <param name="state">Contains state for the current instance of the prompt on the dialog stack.</param>
        /// <param name="options">A prompt options object constructed from the options initially provided
        /// in the call to <see cref="DialogContext.PromptAsync(string, PromptOptions, CancellationToken)"/>.</param>
        /// <param name="isRetry">true  if this is the first time this prompt dialog instance
        /// on the stack is prompting the user for input; otherwise, false.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects
        /// or threads to receive notice of cancellation.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        protected override async Task OnPromptAsync(ITurnContext turnContext, IDictionary <string, object> state, PromptOptions options, bool isRetry, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (turnContext == null)
            {
                throw new ArgumentNullException(nameof(turnContext));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var culture = DetermineCulture(turnContext.Activity);

            // Format prompt to send
            IMessageActivity prompt;
            var choices       = options.Choices ?? new List <Choice>();
            var channelId     = turnContext.Activity.ChannelId;
            var choiceOptions = ChoiceOptions ?? _choiceDefaults[culture];
            var choiceStyle   = options.Style ?? Style;

            if (isRetry && options.RetryPrompt != null)
            {
                prompt = AppendChoices(options.RetryPrompt, channelId, choices, choiceStyle, choiceOptions);
            }
            else
            {
                prompt = AppendChoices(options.Prompt, channelId, choices, choiceStyle, choiceOptions);
            }

            // Send prompt
            await turnContext.SendActivityAsync(prompt, cancellationToken).ConfigureAwait(false);
        }
        protected override async Task OnPromptAsync(ITurnContext turnContext, IDictionary <string, object> state, PromptOptions options, bool isRetry)
        {
            if (turnContext == null)
            {
                throw new ArgumentNullException(nameof(turnContext));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            // Determine culture
            var culture = turnContext.Activity.Locale ?? DefaultLocale;

            if (string.IsNullOrEmpty(culture) || !DefaultChoiceOptions.ContainsKey(culture))
            {
                culture = English;
            }

            // Format prompt to send
            IMessageActivity prompt;
            var choices       = options.Choices ?? new List <Choice>();
            var channelId     = turnContext.Activity.ChannelId;
            var choiceOptions = ChoiceOptions ?? DefaultChoiceOptions[culture];

            if (isRetry && options.RetryPrompt != null)
            {
                prompt = AppendChoices(options.RetryPrompt, channelId, choices, Style, choiceOptions);
            }
            else
            {
                prompt = AppendChoices(options.Prompt, channelId, choices, Style, choiceOptions);
            }

            // Send prompt
            await turnContext.SendActivityAsync(prompt).ConfigureAwait(false);
        }
Exemple #30
0
        protected virtual async Task OnPromptAsync(ITurnContext context, IDictionary <string, object> state, PromptOptions options)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (options.Prompt != null)
            {
                await context.SendActivityAsync(options.Prompt).ConfigureAwait(false);
            }
        }