如何在对话框中使用QnA服务?

时间:2018-11-20 10:10:01

标签: botframework bots qnamaker

在我想使用QnA获得答案之前,我使用了一些瀑布步骤。

 WaterfallStep[] steps = new WaterfallStep[]
    {
        MenuStepAsync,
        QnAAsync,
     };

然后,当我想调用QnA服务时,它需要一个Turncontext对象,但是在Waterfallstep对话框中,我无法访问TurnContext。

  private static async Task<DialogTurnResult> QnAAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
           var response = _services.QnAServices[QnAMakerKey].GetAnswersAsync(turnContext);
            return await stepContext.PromptAsync("name", new PromptOptions { Prompt = MessageFactory.Text("Please enter your name.") }, cancellationToken);
        }
    await

我正在使用C#。我在nodejs中做到了,但是C#有点棘手。以下给出了一个错误,指出stepContext无法转换为Iturncontext。我了解这一点,但不确定如何使它可用于“ GetAnswersAsync”:

_services.QnAServices[QnAMakerKey].GetAnswersAsync(turnContext);

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

stepContext.Context就是Turn上下文。它解决了我的问题。

答案 1 :(得分:0)

在这里为所有人添加示例代码。

    private async Task<DialogTurnResult> InitialStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        var results = await _botServices.QnAMaker.GetAnswersAsync(stepContext.Context);
        if (results.Any())
        {
            await stepContext.Context.SendActivityAsync(MessageFactory.Text(results.First().Answer), cancellationToken);
        }
        else
        {
            await stepContext.Context.SendActivityAsync(MessageFactory.Text("Sorry, could not find an answer in the Q and A system."), cancellationToken);
        }
        return await stepContext.NextAsync(null, cancellationToken);
    }
相关问题