Search code examples
node.jsbotframeworkadaptive-cards

can't reach to section in json file


I'm trying to retrieve the selection in an adaptive card of a json file. So when the allergic option 'peanut' is selected, I want to assign it to a variable. But the code keeps throwing an error.

Adaptive card code:

{
    "type": "message",
    "speak": "...",
    "attachments": [
        {
            "contentType": "application/vnd.microsoft.card.adaptive",
            "content": {
                "type": "AdaptiveCard",
                "version": "1.0",
                "body": [
                    {
                        "type": "TextBlock",
                        "text": "Almost there...",
                        "size": "large",
                        "weight": "bolder"
                    }
                ],
                "actions": [
                    {
                        "type": "Action.ShowCard",
                        "title": "Beef",
                        "card": {
                            "type": "AdaptiveCard",
                            "body": [
                                {
                                    "type": "TextBlock",
                                    "text": "What are you allergic to?",
                                    "size": "medium",
                                    "wrap": true
                                },
                                {
                                    "type": "Input.ChoiceSet",
                                    "value": "BeefAllergy",
                                    "id": "BeefAllergy",
                                    "style": "expanded",
                                    "isMultiSelect": false,
                                    "isCompact": false,
                                    "choices": [
                                        {
                                            "title": "Peanut",
                                            "value": "peanut"
                                        },
                                        {
                                            "title": "Seafood",
                                            "value": "seafood"
                                        }
                                    ]
                                }
                            ],
                            "actions": [
                                {
                                    "type": "Action.Submit",
                                    "title": "Next",
                                    "data": {
                                        "mealOptions": "beef"
                                    }
                                }
                            ]
                        }
                    }
                ]
            }
        }
    ]
}

Here is how I'm trying to store it in a variable:

lunchAllergy= session.message.attachments.content.actions.card.body[1].choices[1].value
session.send(lunchallergy)

And here's the error:

ERROR: Cannot read property 'actions' of undefined


Solution

  • In my test with your adaptive card content, actually, when your user click the Action.Submit button, your bot can get users selection in the session.message.value, and you can store what you want in variables.

    enter image description here

    You can refer to the sample at https://github.com/Microsoft/BotBuilder-Samples/blob/master/Node/cards-AdaptiveCards/app.js for details.