AdaptiveCard:如何在Node.js

时间:2017-05-16 21:18:34

标签: node.js botframework adaptive-cards

我尝试在NodeJS上使用AdaptiveCards npm包以编程方式生成卡片,但我不知道如何生成传递给消息的JSON。到目前为止,我的代码非常简单:

session.send(new builder.Message(session).addAttachment({
    contentType: "application/vnd.microsoft.card.adaptive",
    content: createCard()
})); 

function createCard() {
    let card = new adaptiveCards.AdaptiveCard({ type: "AdaptiveCard" });

    // add a text block
    card.addItem(new adaptiveCards.TextBlock({
        text: 'Issue #1',
        weight: "bolder",
        size: "medium"
    }));

    return card;
}

我试图调用render方法,但它无法正常工作。我也尝试拨打JSON.stringify(card),但收到TypeError: Converting circular structure to JSON邮件错误。 任何的想法?如果我将JSON传递给内容附件,那么一切正常。

1 个答案:

答案 0 :(得分:5)

要使用Bot Framework SDK for Node.js发送自适应卡,请使用Adaptivecards.io中描述的JSON格式,然后将自适应卡对象添加为botbuilder.Message对象的附件,然后发送像往常一样留言。

示例:

// adaptive cards example from:
// https://docs.microsoft.com/en-us/bot-framework/nodejs/bot-builder-nodejs-send-rich-cards
bot.dialog('adaptive_card_demo', function(session) {
    var adaptiveCardMessage = new builder.Message(session)
    .addAttachment({
        contentType: "application/vnd.microsoft.card.adaptive",
        content: {
            type: "AdaptiveCard",
            speak: "<s>Your  meeting about \"Adaptive Card design session\"<break strength='weak'/> is starting at 12:30pm</s><s>Do you want to snooze <break strength='weak'/> or do you want to send a late notification to the attendees?</s>",
               body: [
                    {
                        "type": "TextBlock",
                        "text": "Adaptive Card design session",
                        "size": "large",
                        "weight": "bolder"
                    },
                    {
                        "type": "TextBlock",
                        "text": "Conf Room 112/3377 (10)"
                    },
                    {
                        "type": "TextBlock",
                        "text": "12:30 PM - 1:30 PM"
                    },
                    {
                        "type": "TextBlock",
                        "text": "Snooze for"
                    },
                    {
                        "type": "Input.ChoiceSet",
                        "id": "snooze",
                        "style":"compact",
                        "choices": [
                            {
                                "title": "5 minutes",
                                "value": "5",
                                "isSelected": true
                            },
                            {
                                "title": "15 minutes",
                                "value": "15"
                            },
                            {
                                "title": "30 minutes",
                                "value": "30"
                            }
                        ]
                    }
                ],
                "actions": [
                    {
                        "type": "Action.Http",
                        "method": "POST",
                        "url": "http://foo.com",
                        "title": "Snooze"
                    },
                    {
                        "type": "Action.Http",
                        "method": "POST",
                        "url": "http://foo.com",
                        "title": "I'll be late"
                    },
                    {
                        "type": "Action.Http",
                        "method": "POST",
                        "url": "http://foo.com",
                        "title": "Dismiss"
                    }
                ]
        }
    });

    session.send(adaptiveCardMessage);
    session.endDialog();
});