Search code examples
c#botframework

Creating a loop in a C# botframework


I'm designing a bot that queries a database, however I've hit a wall.

[LuisIntent("ProjectInfo")]
        public async Task projectInfo(IDialogContext context, LuisResult result)
        {
            PromptDialog.Text(context,AfterPromptMethod,"Please enter your project name", attempts: 100);
        }
        async Task AfterPromptMethod(IDialogContext context, IAwaitable<string> userInput)
        {
            var InputText = await userInput;
            string projectName = InputText.ToString();
            if (projectName!= null)
            {
                TestInfo MI = new TestInfo();
                if (MI.FindProject(projectName) == 0)
                {
                    await context.PostAsync($"Project Found. What do you want to know ?");
                }
                else
                {
//PromptDialog.Text(context,AfterPromptMethod,"Pleaase check your product name and try again", attempts: 100);
                    await context.PostAsync($"Project Not Found. Check your project name and try again.");

                }
            }
            context.Wait(MessageReceived);
        }

This is where I'd like to put the loop, projectInfo is called alright, but when it reaches the if statement where project isn't found, it does nothing. I've tried to insert "context.Wait(projectInfo)" but it didn't help. I also tried to use the prompt dialog, if yes it loops back if no it goes to main menu. However I haven't been able to wrap my head around that approach.

Does anyone have any suggestions or better ways of doing that?


Solution

  • If what you want to is to go again to the projectInfo method, then just call it :)

    I updated your code

    [LuisIntent("ProjectInfo")]
    public async Task projectInfo(IDialogContext context, LuisResult result)
    {
        PromptDialog.Text(context,AfterPromptMethod,"Please enter your project name", attempts: 100);
    }
    
    async Task AfterPromptMethod(IDialogContext context, IAwaitable<string> userInput)
    {
        var InputText = await userInput;
        string projectName = InputText.ToString();
        if (projectName!= null)
        {
            TestInfo MI = new TestInfo();
            if (MI.FindProject(projectName) == 0)
            {
                await context.PostAsync($"Project Found. What do you want to know ?");
                context.Wait(MessageReceived);
            }
            else
            {
                await context.PostAsync($"Project Not Found. Check your project name and try again.");
                await this.projectInfo(context, null);
            }
        }
    }