C# 如何创建具有情景文本值的自适应卡?

C# 如何创建具有情景文本值的自适应卡?,c#,botframework,adaptive-cards,C#,Botframework,Adaptive Cards,我目前正在尝试在瀑布式对话框中为我的一个机器人创建一个自适应卡,在渲染时显示名称和搜索项(两个字符串)。我要使用的两个值都存储在对话框的Context.Activity.Value属性中,因此我需要知道的是如何在创建自适应卡时将这些值插入自适应卡,以便文本块的“文本”值可以包含我的值 我已经研究过在adaptive card模式中使用空JSON对象,我可以在adaptive card的创建过程中以某种方式填充这些对象,但还没有弄清楚如何插入所述值。我是C#和Bot框架的相对初学者,所以我不知道该

我目前正在尝试在瀑布式对话框中为我的一个机器人创建一个自适应卡,在渲染时显示名称和搜索项(两个字符串)。我要使用的两个值都存储在对话框的Context.Activity.Value属性中,因此我需要知道的是如何在创建自适应卡时将这些值插入自适应卡,以便文本块的“文本”值可以包含我的值

我已经研究过在adaptive card模式中使用空JSON对象,我可以在adaptive card的创建过程中以某种方式填充这些对象,但还没有弄清楚如何插入所述值。我是C#和Bot框架的相对初学者,所以我不知道该尝试什么

下面是“我的瀑布”对话框中制作自适应卡的步骤:

private async Task<DialogTurnResult> AdaptiveCardTest(WaterfallStepContext stepContext, 
CancellationToken cancellationToken)
        {
            var introCard = File.ReadAllText("./Content/AdaptiveCardTest.json");

            var card = AdaptiveCard.FromJson(introCard).Card;
            var attachment = new Attachment(AdaptiveCard.ContentType, content: card);

            var response = MessageFactory.Attachment(attachment, ssml: card.Speak, 
            inputHint: InputHints.AcceptingInput);

            await stepContext.Context.SendActivityAsync(response);

            return await stepContext.NextAsync();
        }

任何帮助都将不胜感激,谢谢

这是我过去使用过的一种方法,它是一种很好的方法,可以将自适应卡JSON中的令牌替换为模型中的属性。只需确保自适应卡JSON中的令牌与模型属性匹配即可

查看他们的网站以了解更多详细信息,但这只是执行以下操作的一个例子:

Handlebars.Compile(<Adaptive card template>);
handlebar.Compile();

车把是一个可以添加到项目中的Nuget软件包。

对于您的简单场景,我同意@MikeP的建议。在将来,如果您想做一些模板不够的更复杂的事情,那么您可以在拥有NuGet包后使用.NET SDK动态构建自适应卡

.NET SDK上的文档很简单,但AdaptiveCard对象的属性通常与其JSON对应项对齐

例如:

const string ISO8601Format = "yyyy-MM-dd";
string text = "dynamic-text-here;

DateTime today = DateTime.Today;
string todayAsIso = today.ToString(ISO8601Format);

// Create card
AdaptiveCard adaptiveCard = new AdaptiveCard("1.0")
{
    Body =
    {
        new AdaptiveContainer
        {
            Items =
            {
                new AdaptiveTextBlock
                {
                    Text = question,
                    Wrap = true
                },
                new AdaptiveDateInput
                {
                    // This Id matches the property in DialogValueDto so it will automatically be set
                    Id = "UserInput",
                    Value = todayAsIso,
                    Min = today.AddDays(-7).ToString(ISO8601Format),
                    Max = todayAsIso,
                    Placeholder = todayAsIso
                }
            }
        }
    },
    Actions = new List<AdaptiveAction>
    {
        new AdaptiveSubmitAction
        {
            // Data can be an object but this will require the value provided for the 
            // Content property to be serialised it to a string
            // as per this answer https://stackoverflow.com/a/56297792/5209435
            // See the attachment block below for how this is handled
            Data = "your-submit-data",
            Title = "Confirm",
            Type = "Action.Submit"
        }
    }
};

// Create message attachment
Attachment attachment = new Attachment
{
    ContentType = AdaptiveCard.ContentType,
    // Trick to get Adapative Cards to work with prompts as per https://github.com/Microsoft/botbuilder-dotnet/issues/614#issuecomment-443549810
    Content = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(adaptiveCard))
};

cardActivity.Attachments.Add(attachment);

// Send the message
context.SendActivityAsync(cardActivity);
常量字符串ISO8601Format=“yyyy-MM-dd”; string text=“此处为动态文本; DateTime today=DateTime.today; 字符串todayAsIso=today.ToString(ISO8601格式); //创建卡片 AdaptiveCard AdaptiveCard=新的AdaptiveCard(“1.0”) { 身体= { 新的自适应容器 { 项目= { 新的AdaptiveTextBlock { 文本=问题, Wrap=true }, 新的自适应数据输入 { //此Id与DialogValueDto中的属性匹配,因此将自动设置它 Id=“UserInput”, 值=todayAsIso, Min=今天.AddDays(-7).ToString(ISO8601格式), Max=todayAsIso, 占位符=todayAsIso } } } }, 操作=新列表 { 新的自适应submition { //数据可以是对象,但这需要为 //要将其序列化为字符串的内容属性 //根据这个答案https://stackoverflow.com/a/56297792/5209435 //有关如何处理此问题,请参见下面的附件块 Data=“您的提交数据”, Title=“确认”, Type=“Action.Submit” } } }; //创建邮件附件 附件=新附件 { ContentType=AdaptiveCard.ContentType, //让会员卡按照提示使用的技巧https://github.com/Microsoft/botbuilder-dotnet/issues/614#issuecomment-443549810 Content=JsonConvert.DeserializeObject(JsonConvert.SerializeObject(adaptiveCard)) }; cardActivity.Attachments.Add(附件); //发送消息 SendActivityAsync(cardActivity);
因为
Items
Actions
都是集合,所以您可以在代码中使用条件逻辑在运行时基于某些条件构建这些集合,然后将构建集合传递给
Items
Actions
,这将比使用替换pl的JSON模板更具灵活性aceholder令牌位于已知位置。

这很好,我一定会尝试。谢谢!因为在检索自适应卡提交值方面,您可能会遇到与我相同的问题,我将在这里留下,如果您在瀑布中运行自适应卡,但希望它作为提示,即捕获用户输入,那么您可以我需要参考一下(一些浅显的读物哈哈。)我确实有!我从这里开始使用mdrichardson的修复程序:。我仍然没有得到文本输入以进入HandlerResponseAsync。有什么想法吗?有两个选项:1)不要在
postbackActivity
上设置
text
属性,而是直接在
dc.Context.Activity.text
上设置它,这样您就可以oid有这一行
wait dc.Context.SendActivityAsync(postbackActivity);
2)检查MainDialog.cs中的
OnEventAsync
方法,确保没有任何东西阻止您的代码进入
if(forward)
然后实际执行
dc.ContinueDialogAsync()
call-我遇到了这个问题,因此在这里放置一个调试器并在提交自适应卡时逐步执行是值得的。
const string ISO8601Format = "yyyy-MM-dd";
string text = "dynamic-text-here;

DateTime today = DateTime.Today;
string todayAsIso = today.ToString(ISO8601Format);

// Create card
AdaptiveCard adaptiveCard = new AdaptiveCard("1.0")
{
    Body =
    {
        new AdaptiveContainer
        {
            Items =
            {
                new AdaptiveTextBlock
                {
                    Text = question,
                    Wrap = true
                },
                new AdaptiveDateInput
                {
                    // This Id matches the property in DialogValueDto so it will automatically be set
                    Id = "UserInput",
                    Value = todayAsIso,
                    Min = today.AddDays(-7).ToString(ISO8601Format),
                    Max = todayAsIso,
                    Placeholder = todayAsIso
                }
            }
        }
    },
    Actions = new List<AdaptiveAction>
    {
        new AdaptiveSubmitAction
        {
            // Data can be an object but this will require the value provided for the 
            // Content property to be serialised it to a string
            // as per this answer https://stackoverflow.com/a/56297792/5209435
            // See the attachment block below for how this is handled
            Data = "your-submit-data",
            Title = "Confirm",
            Type = "Action.Submit"
        }
    }
};

// Create message attachment
Attachment attachment = new Attachment
{
    ContentType = AdaptiveCard.ContentType,
    // Trick to get Adapative Cards to work with prompts as per https://github.com/Microsoft/botbuilder-dotnet/issues/614#issuecomment-443549810
    Content = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(adaptiveCard))
};

cardActivity.Attachments.Add(attachment);

// Send the message
context.SendActivityAsync(cardActivity);