4

I am attempting to write a bot which would allow users to create a request (Example. I want to buy a t-shirt or I want to fix my computer ). Depending on the request they pick, they will be asked a series of questions associated with that request; the questions are related directly to the request they choose. Currently I am attempt to iterate through this list of questions and use the PromptDialog.Text method to get the answers. However, this only asks the last question in the List and when an answer is given I get an error with invalid need: expected Wait, have Done as the message.

private async Task ListOfferings(IDialogContext context, IAwaitable<string>  result)
{
   string name = await result;
   var offerings = Offerings.Where((x) =>          
      CultureInfo.CurrentCulture.CompareInfo.IndexOf(x.Name, name, 
      CompareOptions.IgnoreCase) >= 0
   );
   if (offerings.Count() != 0)
   {
      PromptDialog.Choice(context, CreateOffering, offerings, "Which of these would you like?");
   }
   else
   {
      await context.PostAsync(string.Format("Could not find an offering with name {0}", name));
      context.Wait(MessageReceived);
   }
}


private async Task CreateOffering(IDialogContext context, IAwaitable<Offering> result)
{
   var offering = result.GetAwaiter().GetResult();
   await context.PostAsync(string.Format("***CREATING {0}***", offering.Name.ToUpper()));

   foreach (var question in offering.Questions)
   {
      PromptDialog.Text(context, null, question.Question);
   }
}

Is it possible to do something like this, where I want to dynamically decide the questions the user will be asked at runtime or am I required to take a more static, scripted approach?

Lars
  • 9,976
  • 4
  • 34
  • 40
Stephen Newell
  • 198
  • 1
  • 10

1 Answers1

4

You cannot just do multiple calls of PromptDialog.Text in a loop, because you have to wait for a user reply after each question. Try to do it this way:

private int questionNumber;
private Offering offering;

private async Task CreateOffering(IDialogContext context, IAwaitable<Offering> result)
{
    offering = await result;
    await context.PostAsync($"***CREATING {offering.Name.ToUpper()}***");
    questionNumber = 0;
    // fix access to a particular question in case Questions is not an IList
    PromptDialog.Text(context, OnQuestionReply, offering.Questions[questionNumber].Question);
}

private async Task OnQuestionReply(IDialogContext context, IAwaitable<string> result)
{
    var answer = await result;
    // handle the answer for questionNumber as you need

    questionNumber++;
    // use Count instead of Length in case it is not an array
    if (questionNumber < offering.Questions.Length)
    {
        PromptDialog.Text(context, OnQuestionReply, offering.Questions[questionNumber].Question);
    }
    else
    {
        // do what you need when all the questions are answered
        await context.PostAsync("I have no more questions for this offering.");
        context.Wait(MessageReceived);
    }
}
Eugene Berdnikov
  • 2,150
  • 2
  • 23
  • 30