Search code examples
jsonbotframeworkchatbottemplatingadaptive-cards

How to use $when in an adaptive card template to find out the length of an incoming data payload and drop an input block


I have an adaptive card in the form of a JSON file, which contains an Input.ChoiceSet. This is provided with a data payload, which is dynamic and so it is not the same amount of data every time. I want to be able to drop this Input.ChoiceSet if it breaks a certain threshold based on the length of the array of data that I'm going to pass to it. Is it possible to write this as an condition inside the Input.ChoiceSet using %when to carry this out?

This is currently what I have, but it is not working as I would've hoped:

{
      "type": "Input.ChoiceSet",
      "id": "CompactSelectVal1",
      "$when": "${$data.length < 400}",
      "placeholder": "Select a value",
      "choices": [
        {
          "$data": "${data}",
          "title": "${name}",
          "value": "${tag}"
        }
      ],
      "label": "Input"
}

Using .length here was just a guess here, not based on any documentation. The documentation I have used to find out about $when is the following https://learn.microsoft.com/en-us/adaptive-cards/templating/language.

Any help on this would be much appreciated.


Solution

  • You can use "count" property instead of "length" and also remove the extra '$' inside the curly bracket "${$data.length < 400}".

    Try this:

    {
      "type": "Input.ChoiceSet",
      "id": "CompactSelectVal1",
      "$when": "${count(data) < 400}",
      "placeholder": "Select a value",
      "choices": [
        {
          "$data": "${data}",
          "title": "${name}",
          "value": "${tag}"
        }
      ],
      "label": "Input"
    }
    

    If the condition is true then the choice button will hide from the adaptive card.