2016-06-08 8 views
4

사용자가 요청을 만들 수있는 봇을 작성하려고합니다 (예 : 티셔츠를 사고 싶거나 컴퓨터를 수리하고 싶습니다). 그들이 선택한 요청에 따라 요청과 관련된 일련의 질문을 받게됩니다. 질문은 그들이 선택한 요청과 직접 관련이 있습니다. 현재이 질문 목록을 반복하고 PromptDialog.Text 메서드를 사용하여 답변을 얻으려고합니다. 그러나 이것은 목록의 마지막 질문 만 묻고 답이 주어지면 이 필요하지 않습니다 : 예상 됨 기다림, 완료했습니다 메시지로 보냅니다.불확실한 질문에 대한 프롬프트 대화

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); 
    } 
} 

내가 동적으로 사용자가 런타임에 메시지가 표시됩니다 질문을 결정하려는 아니면 좀 더 정적, 스크립트 접근 방식을 취해야합니다입니다 이런 식으로 뭔가를 할 수 있습니까?

답변

2

각 질문마다 사용자 답장을 기다려야하므로 PromptDialog.Text을 여러 번 호출 할 수 없습니다. 다음과 같이 해보십시오 :

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); 
    } 
}