简体   繁体   English

如何在C#[Bot Framework v4]中从QnaBot(qna制造商api)调用瀑布对话框?

[英]How to call waterfall dialog from QnaBot (qna maker api) in C# [Bot Framework v4]?

I have created a qna maker bot in Bot Framework v4 using c#, and now when there are no answers found in qna knowledge base, I have to call a waterfall dialog to ask some questions to the users. 我已经使用c#在Bot Framework v4中创建了一个qna maker bot,现在在qna知识库中找不到答案时,我必须调用一个瀑布对话框向用户提出一些问题。

How do I have to do this? 我该怎么做?

protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
    {
        var httpClient = _httpClientFactory.CreateClient();

        var qnaMaker = new QnAMaker(new QnAMakerEndpoint
        {
            KnowledgeBaseId = _configuration["QnAKnowledgebaseId"],
            EndpointKey = _configuration["QnAAuthKey"],
            Host = GetHostname()
        },
        null,
        httpClient);

        _logger.LogInformation("Calling QnA Maker");

        // The actual call to the QnA Maker service.
        var response = await qnaMaker.GetAnswersAsync(turnContext);
        if (response != null && response.Length > 0)
        {
            string message = GetMessage(response[0].Answer);
            Attachment attachment = GetHeroCard(response[0].Answer);
            await turnContext.SendActivityAsync(MessageFactory.Text(message), cancellationToken);
            if (attachment != null)
                await turnContext.SendActivityAsync(MessageFactory.Attachment(attachment), cancellationToken);
        }
        else
        {  
            //HERE I WANT TO CALL WATERFALL DIALOG
            //await turnContext.SendActivityAsync(MessageFactory.Text("No QnA Maker answers were found."), cancellationToken);
        }
    }

You can make use of a multi-turn prompt, which uses a waterfall dialog, a few prompts, and a component dialog to create a simple interaction that asks the user a series of questions.The bot interacts with the user via UserProfileDialog. 您可以使用多转提示,该提示使用瀑布对话框,一些提示和组件对话框来创建一个简单的交互方式,向用户询问一系列问题。机器人通过UserProfileDialog与用户进行交互。 When we create the bot's DialogBot class, we will set the UserProfileDialog as its main dialog. 当创建机器人的DialogBot类时,我们会将UserProfileDialog设置为其主要对话框。 The bot then uses a Run helper method to access the dialog. 然后,该漫游器使用“运行帮助器”方法来访问对话框。

To use a prompt, call it from a step in your dialog and retrieve the prompt result in the following step using stepContext.Result. 要使用提示,请在对话框的某个步骤中调用它,并在接下来的步骤中使用stepContext.Result检索提示结果。 You should always return a non-null DialogTurnResult from a waterfall step. 您应该始终从瀑布步骤返回非null的DialogTurnResult。

An example of asking the user for their name is as follows: 询问用户名称的示例如下:

 public class UserProfileDialog : ComponentDialog
    {
        private readonly IStatePropertyAccessor<UserProfile> _userProfileAccessor;

        public UserProfileDialog(UserState userState)
            : base(nameof(UserProfileDialog))
        {
            _userProfileAccessor = userState.CreateProperty<UserProfile>("UserProfile");

            // This array defines how the Waterfall will execute.
            var waterfallSteps = new WaterfallStep[]
            {
                NameStepAsync,
                NameConfirmStepAsync,
                SummaryStepAsync,
            };

            // Add named dialogs to the DialogSet. These names are saved in the dialog state.
            AddDialog(new WaterfallDialog(nameof(WaterfallDialog), waterfallSteps));
            AddDialog(new TextPrompt(nameof(TextPrompt)));
            AddDialog(new ConfirmPrompt(nameof(ConfirmPrompt)));

            // The initial child Dialog to run.
            InitialDialogId = nameof(WaterfallDialog);
        }

        private static async Task<DialogTurnResult> NameStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            stepContext.Values["transport"] = ((FoundChoice)stepContext.Result).Value;

            return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text("Please enter your name.") }, cancellationToken);
        }

        private async Task<DialogTurnResult> NameConfirmStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            stepContext.Values["name"] = (string)stepContext.Result;

            // We can send messages to the user at any point in the WaterfallStep.
            await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Thanks {stepContext.Result}."), cancellationToken);

            // WaterfallStep always finishes with the end of the Waterfall or with another dialog; here it is a Prompt Dialog.
            return await stepContext.PromptAsync(nameof(ConfirmPrompt), new PromptOptions { Prompt = MessageFactory.Text("Would you like to give your age?") }, cancellationToken);
        }


        private async Task<DialogTurnResult> SummaryStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            if ((bool)stepContext.Result)
            {
                // Get the current profile object from user state.
                var userProfile = await _userProfileAccessor.GetAsync(stepContext.Context, () => new UserProfile(), cancellationToken);

                userProfile.Name = (string)stepContext.Values["name"];

            }
            else
            {
                await stepContext.Context.SendActivityAsync(MessageFactory.Text("Thanks. Your profile will not be kept."), cancellationToken);
            }

            // WaterfallStep always finishes with the end of the Waterfall or with another dialog, here it is the end.
            return await stepContext.EndDialogAsync(cancellationToken: cancellationToken);
        }      
    }

This documentation will help you get a better understanding of multi prompt dialogs. 文档将帮助您更好地理解多提示对话框。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 C# 在瀑布对话框中调用 QnA Maker? - How to call QnA Maker on a waterfall dialog using C#? 如何自动测试 QnA Maker Bot (Bot Framework V4, C#)? - How to automatically test a QnA Maker Bot (Bot Framework V4, C#)? 如何在瀑布对话框中调用 QNA 机器人? - How to call a QNA bot with in a waterfall dialog? 具有复杂对话流的顺序瀑布模型Bot Framework C#v4 - Sequential Waterfall Models with Complex Dialog flows Bot Framework C# v4 瀑布对话框中的C#Bot V4文本提示 - C# bot V4 Text Prompt inside a waterfall dialog How to call AZURE DEVOPS rest API in the water fall dialog built using C# BOT Framework SDK V4? - How to call AZURE DEVOPS rest API in the water fall dialog built using C# BOT Framework SDK V4? 如何从机器人框架 v4 中的 ActivityHandler.OnMessageActivityAsync 启动瀑布对话框 - How to start a waterfall dialog from ActivityHandler.OnMessageActivityAsync in bot framework v4 如何验证自适应卡片机器人框架 v4(瀑布模型)c# 中的输入字段 - How to validate input fields in adaptive card bot framework v4 (waterfall model) c# 在Botframework V4 C#中调度LUIS和QnA Maker - Dispatch LUIS and QnA Maker in Botframework V4 C# 在瀑布对话框中使用 luis 验证提示(Bot 框架 V4) - Validate prompt with luis in waterfall dialog (Bot framework V4)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM