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

时间:2019-09-03 15:59:27

标签: c# botframework qnamaker

我已经使用c#在Bot Framework v4中创建了一个qna maker bot,现在在qna知识库中找不到答案时,我必须调用一个瀑布对话框向用户提出一些问题。

我该怎么做?

 Container(
                  child: StreamBuilder<List<Asset>>(
                      stream: _bloc.uploadImageStream,
                      builder: (context, snapshot) {
                        if (snapshot.hasData) {
                          return Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: <Widget>[
                              images == null
                                  ? new Container(
                                      height: 300.0,
                                      width: 300.0,
                                      child: new Icon(
                                        Icons.image,
                                        size: 250.0,
                                        color: Theme.of(context).primaryColor,
                                      ),
                                    )
                                  : new SizedBox(
                                      height: 200.0,
                                      width: double.infinity,
                                      child: new ListView.builder(
                                        scrollDirection: Axis.horizontal,
                                        itemCount: snapshot.data.length,
                                        itemBuilder: (BuildContext context,
                                                int index) =>
                                            new Padding(
                                                padding:
                                                    const EdgeInsets.all(5.0),
                                                child: AssetThumb(
                                                  height: 200,
                                                  width: 200,
                                                  asset: snapshot.data[index],
                                                )),
                                      ),
                                    ),
                              IconButton(
                                  icon: Center(
                                    child: Icon(
                                      Icons.camera,
                                      size: 30.0,
                                    ),
                                  ),
                                  onPressed: () {
                                    _bloc.getImage();
                                  })
                            ],
                          );
                        } else {
                          return IconButton(
                              icon: Center(
                                child: Icon(
                                  Icons.camera,
                                  size: 30.0,
                                ),
                              ),
                              onPressed: () {
                                _bloc.getImage();
                              });
                        }
                      }))

1 个答案:

答案 0 :(得分:0)

您可以利用多转提示,该提示使用瀑布对话框,一些提示和组件对话框来创建简单的交互,向用户询问一系列问题。机器人通过UserProfileDialog与用户进行交互。当创建机器人的DialogBot类时,我们会将UserProfileDialog设置为其主要对话框。然后,该漫游器使用“运行帮助程序”方法来访问对话框。

要使用提示,请在对话框的某个步骤中调用它,并在接下来的步骤中使用stepContext.Result检索提示结果。您应该始终从瀑布步骤返回非null的DialogTurnResult。

向用户询问其姓名的示例如下:

 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文档将帮助您更好地理解多提示对话框。

希望这会有所帮助。