立即执行操作

时间:2016-11-18 12:43:38

标签: c# botframework

所以我创建了这个对话框:

[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个步骤,所以当第一步开始时,会询问一个问题。当用户回答问题时(因为在第一步中只有一个问题),会启动对话框的新实例并询问新一轮的问题。 在这种情况下,有一个以上的问题,所以当用户回答第一个问题时,我希望它立即提出第二个问题。 问题是我有这个:

// Otherwise, ask our next question
} else
    context.Wait(AskQuestion);

GetAnswer 方法中,它会在继续之前等待用户的响应。我不希望它等待用户响应,我希望它只是执行。有谁知道怎么做?

1 个答案:

答案 0 :(得分:1)

我是愚蠢的。我刚刚将最后一行更改为:

// Otherwise, ask our next question
} else
    await AskQuestion(context, null);