Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AdaptiveCard: how to use in Node.js

I'm trying to use AdaptiveCards npm package on NodeJS to generate programmatically the card and I don't see how to generate the JSON to pass to the message. So far, my code is fairly simple:

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;
}

I've tried to call to render method but it wasn't work. I also tried to call JSON.stringify(card) but I get TypeError: Converting circular structure to JSON message error. Any idea? If I pass the JSON to the content attachment, all work fine.

like image 866
sGambolati Avatar asked May 16 '17 21:05

sGambolati


People also ask

How to create an adaptive card in JavaScript?

You can use any tooling that you want to create the adaptive card json. The adaptivecards npm package defines a library for working with adaptive cards in javascript The following API shows how to construct an Adaptive Card using the object model and serialize it to JSON.

How to send adaptive cards using bot Framework SDK for nodejs?

Any idea? If I pass the JSON to the content attachment, all work fine. To send an Adaptive Card using Bot Framework SDK for Node.js, use the JSON format described at Adaptivecards.io, then add the adaptive card object as an attachment to a botbuilder.Message object, then send the message as usual.

What does the adaptivecard method do?

The adaptiveCard method creates an Attachment object with the correct content type and the card JSON itself. For those of you familiar with these cards might have seen the content type before:

Where does the input from an adaptive card go?

When a user fills out an input on an Adaptive Card, it ends up in context.activity.value, which is an object where the key names are the id in your menuJson and the values are the field values in the adaptive card.


1 Answers

To send an Adaptive Card using Bot Framework SDK for Node.js, use the JSON format described at Adaptivecards.io, then add the adaptive card object as an attachment to a botbuilder.Message object, then send the message as usual.

Example:

// 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();
});
like image 185
nwxdev Avatar answered Sep 25 '22 00:09

nwxdev