1
所以我有这样的对话,我创建:执行马上行动
[Serializable]
public class StepOneDialog : IDialog<object>
{
// Private fields
protected readonly IList<GroupResponseModel> _groups;
protected readonly GroupResponseModel _group;
protected readonly int _currentStep;
protected int _currentQuestion = 0;
/// <summary>
/// Default constructor
/// </summary>
/// <param name="groups">The question groups</param>
public StepOneDialog(IList<GroupResponseModel> groups, int step)
{
_groups = groups;
_group = groups[step];
_currentStep = step;
}
/// <summary>
/// Start our response
/// </summary>
/// <param name="context">The current context</param>
/// <returns></returns>
public async Task StartAsync(IDialogContext context) => context.Wait(AskQuestion);
/// <summary>
/// When our message is recieved we execute this delegate
/// </summary>
/// <param name="context">The current context</param>
/// <param name="result">The result object</param>
/// <returns></returns>
private async Task AskQuestion(IDialogContext context, IAwaitable<IMessageActivity> result)
{
// Get our question and answers
var question = this._group.Questions[_currentQuestion];
var questionText = question.Text;
var answers = question.Answers.Select(m => m.Text).ToList();
var options = new PromptOptions<string>(questionText, options: answers);
// If wer are the first question AND we have more than 1 question, Post the question header
if (_currentQuestion == 0 && _group.Questions.Count > 1)
await context.PostAsync(_group.Text);
// Ask our question
Choice<string>(context, GetAnswer, options);
}
/// <summary>
/// Get our answer and decide what to do next
/// </summary>
/// <param name="context">The current context</param>
/// <param name="result">The answer text</param>
/// <returns></returns>
private async Task GetAnswer(IDialogContext context, IAwaitable<string> result)
{
// Get our quest
var questions = _group.Questions;
var length = questions.Count;
var question = _group.Questions[_currentQuestion];
// Assign our answer to our question
foreach (var answer in question.Answers)
if (answer.Text == await result)
question.Answer = answer;
// Increase our index
_currentQuestion++;
// If our current index is greater or equal than the length of the questions
if (_currentQuestion == length)
{
// If our step is the same as our group length
var groupLength = _groups.Count - 1;
// If we have reached the end of our steps, this dialog is done
if (groupLength == _currentStep)
context.Wait(ResumeAfter);
// Otherwise, got to the next step
await context.Forward(new StepOneDialog(_groups, _currentStep + 1), ResumeAfter, new Activity { }, CancellationToken.None);
// Otherwise, ask our next question
} else
context.Wait(AskQuestion);
}
/// <summary>
/// When the child dialog has complete, mark this as done
/// </summary>
/// <param name="context">The current context</param>
/// <param name="result">The result object</param>
/// <returns></returns>
private async Task ResumeAfter(IDialogContext context, IAwaitable<object> result) => context.Done(this);
}
有3个步骤,所以当第一步开始提出问题。当用户回答问题时(因为第一步中只有1个问题),一个新的对话实例被启动,新一轮问题正在问。 在这种情况下,有多个问题,所以当用户回答第一个问题时,我希望它立即提出第二个问题。 问题是我有这样的:
// Otherwise, ask our next question
} else
context.Wait(AskQuestion);
在的getAnswer方法,它会等待继续之前用户的响应。我不希望它等待用户响应,我希望它只是执行。有谁知道这是怎么做到的吗?
既然你不使用IAwaitable结果在您的AskQuestion方法......是什么阻止你到方法中的逻辑提取到一个新的,不要没有IAwaitable依赖关系,只需在else子句中调用该新方法? –
是的,你是对的。我刚刚做到了。这就像当我开始使用机器人时,我的脑海里停止了对正常逻辑的思考...... – r3plica
好;不会将其作为答案发布;只是看到你做到了。 –