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

How to use QnA service within the dialog?

在我想使用 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);

感谢您的帮助。

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

这里给大家加个示例代码

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