首页 > 解决方案 > 如何在 WebChat 中自定义自适应卡片?

问题描述

这个问题是我之前问题的延伸。(如何将“AdaptiveActionSet”放入“AdaptiveColumn”?

当时,我对自适应卡片的定制和WebChat.html文件了解不多。

我的卡1

上图是我想要的自适应卡的布局。右侧的 Reserve 按钮是 Action.OpenUrl 按钮。

我的卡2

要放置这样的按钮,我需要在列中放置一个 ActionSet。但是,典型的自适应卡片不会在列中显示操作集,如上图所示。内容类型如下所示。 ContentType = "application/vnd.microsoft.card.adaptive" (在网络聊天中)

为了解决这个问题,我必须自定义自适应卡片,但我不确定如何。

(惭愧,我参考了下面的链接,但我仍然无法自定义卡片。

Bot 连接器服务未使用最新的自适应卡 #87

你能告诉我解决这个问题的方法或展示一个简单的定制卡片示例吗?请。

下面是我写的代码。

我的自适应卡代码:

card.Body.Add(new AdaptiveColumnSet()
{
    Columns = new List<AdaptiveColumn>()
    {
        new AdaptiveColumn()
        {
            //(~Date, No problem)
        },
        new AdaptiveColumn()
        {
            //(~Price, No problem)
        },
        new AdaptiveColumn()
        {
            Items =
            {
                new AdaptiveActionSet()
                {
                    Actions =
                    {
                        new AdaptiveOpenUrlAction()
                        {
                            Url = new Uri("https://www.SomeUrl.com"),
                            Title = "Reserve",
                            Style = "positive",
                        }
                    }
                }
            },
            Width = "auto",
        }
    },
});

var reply = turnContext.Activity.CreateReply();
reply.Attachments = new List<Attachment>
{
    new Attachment()
    {
        ContentType = "application/vnd.microsoft.card.custom",
        Content = card
    }
};

我的 webChat.html :

const attachmentMiddleware = () => next => card => {
  if (card.attachment.contentype === 'application/vnd.microsoft.card.custom'){
    card.attachment.contentType = 'application/vnd.microsoft.card.adaptive'
  }
  return next(card)
};

window.WebChat.renderWebChat({
    directLine: botConnection,
    styleOptions,
    adaptiveCardHostConfig,
    attachmentMiddleware
}, document.getElementById('webchat'));

document.querySelector('#webchat > *').focus();

如上,ContentType = "application/vnd.microsoft.card.custom"

如果我将自定义分配给 contentType,

我的卡3

我收到一个名为 No renderer 的错误。

标签: c#jsonbotframeworkadaptive-cardsweb-chat

解决方案


使用 AdaptiveColumn 的 SelectAction 解决了这个问题。它通过一起使用 AdaptiveTextBlock 和 SelectAction 并指定宽度。

前任)

Items =
    {
        new AdaptiveTextBlock()
        {
            HorizontalAlignment = AdaptiveHorizontalAlignment.Center,
            Color = AdaptiveTextColor.Light,
            Text = "Reserve",
            Weight = AdaptiveTextWeight.Bolder,
         }
    },
    SelectAction = new AdaptiveOpenUrlAction()
    {
        Url = new Uri($"{someurl}"),
        Title = "rsv",
        Id = "bb" + cnt,
    },
    Width = "50px"

推荐阅读