如何使用Microsoft Bot Framework中的“自适应对话框”方法从自适应卡中获得用户响应?

时间:2020-07-01 14:03:47

标签: c# botframework

这里的想法很简单。我已经编写了一个代码来显示自适应卡(使用命令-new SendActivity(“ $ {AdaptiveCard()}”)),询问用户名和电子邮件。用户提交这些输入后,我只想检索这些输入以用于将来的实现,但我无法这样做。

我发现了使用Waterfall对话框的几种实现,但是没有使用自适应对话框的实现。

在这里,可以找到堆栈溢出帖子How to retrieve user entered input in adaptive card to the c# code and how to call next intent on submit button click-关于如何将用户在自适应卡中输入的输入检索到C#代码以及如何在提交按钮单击上调用下一个意图 。但是,如果您检查了答案,则用户说它是用于瀑布对话框的,则代码可能是相似的,但是我无法将其转换为Adaptive Dialogs实现。

还有另一个参考,可能有用。 https://blog.botframework.com/2019/07/02/using-adaptive-cards-with-the-microsoft-bot-framework/。尽管本教程显示了如何使用瀑布对话框实现自适应卡,但是代码可能看起来相似。更具体地说,本教程中检索用户输入的实现如下所示。至关重要的是要注意,此实现使用turnContext.Activity.Text从自适应卡访问用户响应,但是如何在自适应对话框中访问这样的变量 turnContext

var txt = turnContext.Activity.Text;

dynamic val = turnContext.Activity.Value;

// Check if the activity came from a submit action
if (string.IsNullOrEmpty(txt) && val != null)
{
    // Retrieve the data from the id_number field
    var num = double.Parse(val.id_number);

    // . . .
}

在这里,我们可以从Adaptive Dialog实现中看到我的RootDialog类的代码。如上例所示,在我的代码中哪里可以检索用户信息?

public class RootDialog : ComponentDialog
    {
        private readonly IConfiguration configuration;

        public RootDialog(IConfiguration configuration)
            : base(nameof(RootDialog))
        {
            this.configuration = configuration;

            string[] paths = { ".", "Dialogs", $"{nameof(RootDialog)}.lg" };
            string fullPath = Path.Combine(paths);
            // Create instance of adaptive dialog. 
            var rootDialog = new AdaptiveDialog(nameof(AdaptiveDialog))
            {
                // These steps are executed when this Adaptive Dialog begins
                Triggers = new List<OnCondition>()
                {
                    // Add a rule to welcome user
                    new OnConversationUpdateActivity()
                    {
                        Actions = WelcomeUserSteps()
                    },

                    // Respond to user on message activity
                    new OnUnknownIntent()
                    {
                        Actions = OnBeginDialogSteps()
                    }
                },
                Generator = new TemplateEngineLanguageGenerator(Templates.ParseFile(fullPath))
            };

            // Add named dialogs to the DialogSet. These names are saved in the dialog state.
            AddDialog(rootDialog);

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

        private static List<Dialog> WelcomeUserSteps()
        {
            return new List<Dialog>()
            {
                // Iterate through membersAdded list and greet user added to the conversation.
                new Foreach()
                {
                    ItemsProperty = "turn.activity.membersAdded",
                    Actions = new List<Dialog>()
                    {
                        // Note: Some channels send two conversation update events - one for the Bot added to the conversation and another for user.
                        // Filter cases where the bot itself is the recipient of the message. 
                        new IfCondition()
                        {
                            Condition = "$foreach.value.name != turn.activity.recipient.name",
                            Actions = new List<Dialog>()
                            {
                                new SendActivity("Hi there, I'm here to help you!")
                            }
                        }
                    }
                }
            };

        }

        private static List<Dialog> OnBeginDialogSteps()
        {
            return new List<Dialog>()
            {
                new SendActivity("${AdaptiveCard()}"),
            };
        }
    }

0 个答案:

没有答案