public void Call <R>(IDialog <R> child, ResumeAfter <R> resume)
        {
            if (child is EditablePromptChoice <string> p)
            {
                var language = _translatorService.GetLanguage(_original);

                if (string.IsNullOrWhiteSpace(language))
                {
                    _original.Call <R>(child, resume);
                    return;
                }

                var optOld = p.PromptOptions;

                var prompt = AsyncHelpers.RunSync(() => _translatorService.Translate(optOld.Prompt, "en", language));
                var retry  = AsyncHelpers.RunSync(() => _translatorService.Translate(optOld.Retry, "en", language));

                var optNew = new PromptOptions <string>(prompt, retry, optOld.TooManyAttempts, optOld.Options, optOld.Attempts, optOld.PromptStyler, optOld.Descriptions);

                var newDialog = new PromptDialog.PromptChoice <string>(optNew);
                _original.Call <string>(newDialog, resume as ResumeAfter <string>);

                return;
            }
            _original.Call <R>(child, resume);
        }
        public async Task GetIntent(ResumeAfter <object> messageReceivedAsync, Activity activity, IDialogContext context, object obj = null)
        {
            var message = activity.Text.ToLower();

            var luis = await Rest.LUIS.GetAsync(message.Replace("-", null));

            string topScoringIntent = luis.topScoringIntent.intent.ToLower();

            if (topScoringIntent == "quiz")
            {
                await context.Forward(new QuizDialog(), messageReceivedAsync, activity, CancellationToken.None);
            }
            else if (topScoringIntent == "greeting")
            {
                await context.Forward(new InitDialog(), messageReceivedAsync, activity, CancellationToken.None);
            }
            else if (topScoringIntent == "thanks")
            {
                await context.Forward(new ThanksDialog(), messageReceivedAsync, activity, CancellationToken.None);
            }
            else
            {
                await context.Forward(new NoneDialog(), messageReceivedAsync, activity, CancellationToken.None);
            }
        }
Exemple #3
0
        public static async Task RequiresAuthentication(this IDialogContext context, ResumeAfter <bool> resumeAfter, string loginOperationDescription = "carry out this operation")
        {
            if (resumeAfter == null)
            {
                throw new ArgumentNullException(nameof(resumeAfter));
            }

            var dialog = GetLoginDialog();

            if (dialog != null)
            {
                dialog.LoginRequiredMessage      = $"You are required to login before you can {loginOperationDescription}.";
                dialog.LoginOperationDescription = loginOperationDescription;
                dialog.RequiredRoles             = new string[] { };
            }

            if (!context.IsAuthenticated())
            {
                if (dialog != null)
                {
                    context.Call(
                        dialog,
                        resumeAfter
                        );
                }
                else
                {
                    await resumeAfter(context, new MockupAwaitable <bool>(false));
                }
            }
            else
            {
                await resumeAfter(context, new MockupAwaitable <bool>(true));
            }
        }
        // Choice
        public static void Choice <T>(IDialogContext context, ResumeAfter <T> resume, IPromptOptions <T> promptOptions, bool isAdaptiveCard = true, PromptStyle promptStyle = PromptStyle.Auto, bool recognizeChoices = true, bool recognizeNumbers = true, bool recognizeOrdinals = true, double minScore = 0.4)
        {
            IPromptOptions <T> adPromptOptions;

            if (promptOptions.GetType() == typeof(PromptOptions <T>))
            {
                adPromptOptions = new PromptOptions <T>(promptOptions.Prompt,
                                                        promptOptions.Retry,
                                                        attempts: promptOptions.Attempts,
                                                        options: promptOptions.Options.ToList(),
                                                        promptStyler: new AdaptivePromptStyler(promptStyle, isAdaptiveCard),
                                                        descriptions: promptOptions.Descriptions?.ToList());
            }
            else
            {
                adPromptOptions = new PromptOptionsWithSynonyms <T>(promptOptions.Prompt,
                                                                    promptOptions.Retry,
                                                                    attempts: promptOptions.Attempts,
                                                                    choices: promptOptions.Choices.ToDictionary(x => x.Key,
                                                                                                                x => (IReadOnlyList <T>)x.Value.ToList().AsReadOnly()),
                                                                    promptStyler: new AdaptivePromptStyler(promptStyle, isAdaptiveCard),
                                                                    descriptions: promptOptions.Descriptions?.ToList());
            }
            PromptDialog.Choice(context, resume, adPromptOptions, recognizeChoices, recognizeNumbers, recognizeOrdinals, minScore);
        }
Exemple #5
0
        /// <summary>   Prompt for one of a set of choices. </summary>
        /// <param name="context">  The context. </param>
        /// <param name="resume">   Resume handler. </param>
        /// <param name="options">  The possible options all of which must be convertible to a string.</param>
        /// <param name="prompt">   The prompt to show to the user. </param>
        /// <param name="retry">    What to show on retry. </param>
        /// <param name="attempts"> The number of times to retry. </param>
        /// <param name="promptStyle"> Style of the prompt <see cref="PromptStyle" /> </param>
        /// <param name="descriptions">Descriptions to display for choices.</param>
        public static void Choice <T>(IDialogContext context, ResumeAfter <T> resume, IEnumerable <T> options, string prompt, string retry = null, int attempts = 3, PromptStyle promptStyle = PromptStyle.Auto, IEnumerable <string> descriptions = null)
        {
            List <T> l_options = options.ToList();

            Shuffle(ref l_options);
            Choice(context, resume, new PromptOptions <T>(prompt, retry, attempts: attempts, options: l_options, promptStyler: new PromptStyler(promptStyle), descriptions: descriptions?.ToList()));
        }
 // Wait
 public async Task Wait <T>(ResumeAfter <T> resumeAfter)
 {
     await StartAsync((context) =>
     {
         context.Task.Wait(resumeAfter);
     }, CancellationToken.None);
 }
        public static void Choice(IDialogContext context, ResumeAfter <T> resume,
                                  CancelablePromptOptions <T> promptOptions)
        {
            var child = new CancelablePromptChoice <T>(promptOptions);

            context.Call(child, resume);
        }
 private void RegisterResultHandler <U>(Command command, ResumeAfter <U> resultHandler)
 {
     if (resultHandler != null)
     {
         resultHandlers.Add(command.CommandId, resultHandler);
     }
 }
        public async Task <bool> checkOutdatedMessage <R>(IDialogContext context, ResumeAfter <R> resume, IAwaitable <IMessageActivity> message)
        {
            updateUserSession(context);



            var mes = await message;

            if (mes.Text.Length == 0)
            {
                return(false);
            }
            if (mes.Timestamp <= Request)
            {
                //   mes.Summary = getDialogContext();
                context.Wait(resume);
                //await context.Forward<IMessageActivity, IMessageActivity>(new SideDialog(), resume, mes, CancellationToken.None);
                return(true);
            }
            else if (context.Activity.Timestamp >= Request.AddHours(1))
            {
                await writeMessageToUser(context, conv().getPhrase(Pkey.whereDidYouGone));

                context.ConversationData.SetValue <ResumeAfter <R> >("resume", resume);
                //   await context.Forward<bool,string[]>(new YesNoQuestionDialog(), continuFromLastPlace, conv().getPhrase(Pkey.youWantToContinue),new CancellationToken());
            }

            return(false);
        }
 public new static void Choice <T>(IDialogContext context, ResumeAfter <T> resume, IEnumerable <T> options, string prompt, string retry = null, int attempts = 3, PromptStyle promptStyle = PromptStyle.Auto, IEnumerable <string> descriptions = null)
 {
     if (!(context is TranslatingDialogContext))
     {
         context = new TranslatingDialogContext(context);
     }
     Choice(context, resume, new PromptOptions <T>(prompt, retry, attempts: attempts, options: options.ToList(), promptStyler: new PromptStyler(promptStyle), descriptions: descriptions?.ToList()));
 }
Exemple #11
0
 public static void Choice(IDialogContext context, ResumeAfter <T> resume, IEnumerable <T> options, string prompt,
                           string cancelPrompt     = null, string retry = null, int attempts = 3,
                           PromptStyle promptStyle = PromptStyle.Auto)
 {
     Choice(context, resume,
            new CancelablePromptOptions <T>(prompt, cancelPrompt, retry, attempts : attempts,
                                            options : options.ToList(), promptStyler : new PromptStyler(promptStyle)));
 }
Exemple #12
0
        /// <summary>   Define the default action if no match. </summary>
        /// <param name="handler">  Handler to call if no match. </param>
        /// <returns>   A CommandDialog. </returns>
        #endregion
        public CommandDialog OnDefault(ResumeAfter <Connector.Message> handler)
        {
            var command = new Command {
                CommandHandler = handler
            };

            this.defaultCommand = command;
            return(this);
        }
 // Forward Dialog
 public async Task Forward(IDialog <object> dialog, ResumeAfter <object> resumeAfter = null)
 {
     await StartAsync(async (context) =>
     {
         var interruption = dialog.Void <object, IMessageActivity>();
         await context.Task.Forward(interruption, resumeAfter, Activity, CancellationToken.None);
         await context.Task.PollAsync(CancellationToken.None);
     }, CancellationToken.None);
 }
Exemple #14
0
        async Task IDialogStack.Forward <R, T>(IDialog <R> child, ResumeAfter <R> resume, T item, CancellationToken token)
        {
            IDialogStack stack = this;

            stack.Call(child, resume);
            await stack.PollAsync(token);

            await(this as IPostToBot).PostAsync(item, token);
        }
        public static void Choice <T>(IDialogContext context, ResumeAfter <T> resume, IEnumerable <T> options, string prompt, string retry = null, int attempts = 3, bool isAdaptiveCard = true, PromptStyle promptStyle = PromptStyle.Auto, IEnumerable <string> descriptions = null)
        {
            var adPromptOptions = new PromptOptions <T>(prompt, retry, attempts: attempts,
                                                        options: options.ToList(),
                                                        promptStyler: new AdaptivePromptStyler(promptStyle, isAdaptiveCard),
                                                        descriptions: descriptions?.ToList());

            PromptDialog.Choice(context, resume, adPromptOptions);
        }
        public static void Confirm(IDialogContext context, ResumeAfter <bool> resume, string prompt, string retry = null, int attempts = 3, bool isAdaptiveCard = true, PromptStyle promptStyle = PromptStyle.Auto, string[] options = null, string[][] patterns = null)
        {
            var adPromptOptions = new PromptOptions <string>(prompt,
                                                             retry,
                                                             attempts: attempts,
                                                             options: options ?? PromptDialog.PromptConfirm.Options,
                                                             promptStyler: new AdaptivePromptStyler(promptStyle, isAdaptiveCard));

            PromptDialog.Confirm(context, resume, adPromptOptions, patterns);
        }
Exemple #17
0
        public static async Task CreateReply(ResumeAfter <IMessageActivity> resume, IDialogContext context, Activity activity, string message)
        {
            var reply = activity.CreateReply(message);

            reply.Type       = ActivityTypes.Message;
            reply.TextFormat = TextFormatTypes.Plain;

            await context.PostAsync(reply);

            context.Wait(resume);
        }
        /// <summary> Define the default action if no match. </summary>
        /// <typeparam name="U"> Type of input to result handler. </typeparam>
        /// <param name="handler"> Handler to call if no match. </param>
        /// <param name="resultHandler"> Optional result handler to be called if handler is creating a chaild dialog. </param>
        /// <returns> A CommandDialog. </returns>
        #endregion
        public CommandDialog <T> OnDefault <U>(ResumeAfter <Connector.IMessageActivity> handler, ResumeAfter <U> resultHandler = null)
        {
            var command = new Command {
                CommandId = "defaultResultHandler", CommandHandler = handler
            };

            this.defaultCommand = command;
            RegisterResultHandler(command, resultHandler);

            return(this);
        }
 private async Task GetIssue(IDialogContext context, ResumeAfter <double> resume)
 {
     if (_lastIssue == 0)
     {
         PromptDialog.Number(context, resume, "What issue are you referring to?", "I didn't catch that...try again?");
     }
     else
     {
         await resume(context, Awaitable.FromItem((double)_lastIssue));
     }
 }
Exemple #20
0
        /// <summary>   Define a handler that is fired on a regular expression match of a message. </summary>
        /// <param name="expression">       Regular expression to match. </param>
        /// <param name="handler">          Handler to call on match. </param>
        /// <returns>   A CommandDialog. </returns>
        #endregion
        public CommandDialog On(Regex expression, ResumeAfter <Connector.Message> handler)
        {
            var command = new Command
            {
                Expression     = expression,
                CommandHandler = handler,
            };

            commands.Add(command);
            return(this);
        }
Exemple #21
0
 public void ConfirmDialog(IDialogContext context, ResumeAfter <bool> executeAfterUserResponse, string promptMessage, string retryMessage = "", int attemptsAllowed = 3, PromptStyle style = PromptStyle.Auto)
 {
     PromptDialog.Confirm(
         context: context,
         resume: executeAfterUserResponse,
         prompt: promptMessage,
         retry: retryMessage.HasValue() ? retryMessage : defaultRetryMessage,
         attempts: attemptsAllowed,
         promptStyle: style
         );
 }
Exemple #22
0
 public static void Choice(IDialogContext context, ResumeAfter <T> resume,
                           IEnumerable <T> options, IEnumerable <T> validOptions,
                           string prompt, string retry = null, string tooManyAttempts = null,
                           int attempts = 3, PromptStyle promptStyle = PromptStyle.Auto)
 {
     Choice(context, resume, new FuzzyPromptOptions <T>(
                prompt, retry, tooManyAttempts,
                attempts : attempts, options : options.ToList(),
                validOptions : validOptions.ToList(),
                promptStyler : new PromptStyler(promptStyle)));
 }
Exemple #23
0
        private void HandleMultiAddress(IDialogContext context, BaseRequest request, ResumeAfter <string> onMultiAddressTask)
        {
            string        multiAddressMessage = $"I narrowed down {request.AddressesArray.Length} possible locations. Please choose one below.";
            List <string> list = new List <string>(request.AddressesArray)
            {
                "None"
            };

            context.UserData.Replace(request.Key, request);

            PromptDialog.Choice(context, onMultiAddressTask, list, multiAddressMessage);
        }
        public static void Choice <T>(IDialogContext context, ResumeAfter <T> resume, IDictionary <T, IEnumerable <T> > choices, string prompt, string retry = null, int attempts = 3, bool isAdaptiveCard = true, PromptStyle promptStyle = PromptStyle.Auto, IEnumerable <string> descriptions = null, bool recognizeChoices = true, bool recognizeNumbers = true, bool recognizeOrdinals = true, double minScore = 0.4)
        {
            var adPromptOptions = new PromptOptionsWithSynonyms <T>(prompt,
                                                                    retry,
                                                                    attempts: attempts,
                                                                    choices: choices.ToDictionary(x => x.Key,
                                                                                                  x => (IReadOnlyList <T>)x.Value.ToList().AsReadOnly()),
                                                                    promptStyler: new AdaptivePromptStyler(promptStyle, isAdaptiveCard),
                                                                    descriptions: descriptions?.ToList());

            PromptDialog.Choice(context, resume, adPromptOptions, recognizeChoices, recognizeNumbers, recognizeOrdinals, minScore);
        }
        public new static void Choice <T>(IDialogContext context, ResumeAfter <T> resume, PromptOptions <T> promptOptions)
        {
            if (!(context is TranslatingDialogContext))
            {
                context = new TranslatingDialogContext(context);
            }

            var child = new EditablePromptChoice <T>(promptOptions);

            var cp = new ResumeReplacer <T>(resume);

            context.Call <T>(child, cp.ResumeReplace);
        }
        /// <summary> Define a handler that is fired on a regular expression match of a message. </summary>
        /// <typeparam name="U"> Type of input to result handler. </typeparam>
        /// <param name="expression"> Regular expression to match. </param>
        /// <param name="handler"> Handler to call on match. </param>
        /// <param name="resultHandler"> Optional result handler to be called if handler is creating a chaild dialog. </param>
        /// <returns> A commandDialog. </returns>
        #endregion
        public CommandDialog <T> On <U>(Regex expression, ResumeAfter <Connector.IMessageActivity> handler, ResumeAfter <U> resultHandler = null)
        {
            var command = new Command
            {
                CommandId      = ComputeHash(expression.ToString()),
                Expression     = expression,
                CommandHandler = handler,
            };

            commands.Add(command);
            RegisterResultHandler(command, resultHandler);

            return(this);
        }
Exemple #27
0
        void IDialogStack.Call <R>(IDialog <R> child, ResumeAfter <R> resume)
        {
            var callRest = ToRest(child.StartAsync);

            if (resume != null)
            {
                var doneRest = ToRest(resume);
                this.wait = this.fiber.Call <DialogTask, object, R>(callRest, null, doneRest);
            }
            else
            {
                this.wait = this.fiber.Call <DialogTask, object>(callRest, null);
            }
        }
        private async Task GetRepo(IDialogContext context, string token, ResumeAfter <string> resume)
        {
            if (string.IsNullOrEmpty(_lastRepo))
            {
                IReadOnlyList <Repository> list = await GitHubCommands.GetRepoList(token, string.Empty);

                IEnumerable <string> repos = list.Select(i => i.Name);
                PromptDialog.Choice(context, resume, repos, "Which repo are you asking about?", "I didn't catch that...try again?");
            }
            else
            {
                await resume(context, Awaitable.FromItem(_lastRepo));
            }
        }
Exemple #29
0
        async Task IDialogStack.Forward <R, T>(IDialog <R> child, ResumeAfter <R> resume, T item, CancellationToken token)
        {
            // put the child on the stack
            IDialogStack stack = this;

            stack.Call(child, resume);
            // run the loop
            IEventLoop loop = this;
            await loop.PollAsync(token);

            // forward the item
            this.fiber.Post(item);
            // run the loop again
            await loop.PollAsync(token);
        }
Exemple #30
0
        public async Task None(IDialogContext context, LuisResult result)
        {
            await context.PostAsync($"What does {result.Query} mean?");

            ResumeAfter <object> processDontKnow = async(c, s) =>
            {
                await s;
                var   messages = new[] { "I don't think I needed to know that", "I'm not sure that's useful", "Actually, I don't care", "Whatever", "That's not very interesting", "Let's just put that on the back burner" };

                await c.PostAsync(messages[random.Next(messages.Length)]);

                c.Wait(this.MessageReceived);
            };

            context.Wait(processDontKnow);
        }
 /// <summary>
 /// Suspend the current dialog until the user has sent a message to the bot.
 /// </summary>
 /// <param name="stack">The dialog stack.</param>
 /// <param name="resume">The method to resume when the message has been received.</param>
 public static void Wait(this IDialogStack stack, ResumeAfter<Message> resume)
 {
     stack.Wait<Message>(resume);
 }
 /// <summary>
 /// Suspend the current dialog until the user has sent a message to the bot.
 /// </summary>
 /// <param name="stack">The dialog stack.</param>
 /// <param name="resume">The method to resume when the message has been received.</param>
 public static void Wait(this IDialogStack stack, ResumeAfter<IMessageActivity> resume)
 {
     stack.Wait<IMessageActivity>(resume);
 }