Digital Assistant and Mobile

Get Involved. Join the Conversation.

Topic

    Pawan Gupta
    user.<variable> can it store object or collectionsAnswered
    Topic posted November 4, 2019 by Pawan GuptaBlue Ribbon: 750+ Points, tagged Conversational AI, Virtual Assistant, VPA 
    16 Views, 4 Comments
    Title:
    user.<variable> can it store object or collections
    Summary:
    user.<variable> can it store object or collections
    Content:

    I am trying to make it work, not sure if my code issue or if it is not supported, appreciate if someone can confirm that we can store object or collection in the user.<variable> to persist?

    Best Comment

    Frank Nimphius

    Turns out you can always use Grant's approach if you know what you want to access. So my sample

      text: "The color of the oracle key is: ${(user.myObject?string?eval).oracle.color}"

    could be written as

      text: "The color of the oracle key is: ${user.myObject.value.oracle.color}"

    So eval (though it works) is only needed if you wanted to generically access keys in an object. 

    Frank

    Comment

     

    • Frank Nimphius

      Hi,

      user scope variables can store anything that dialog flow variables can store too. However, the content is serialized and saved as a JSON string. So you need to use the Apache FreeMarker ?eval expression to obtain the object back. Here is a sample:

      context:
        variables:
          myObject: "string"        

      states:

        setVariable:
          component: "System.SetVariable"
          properties:      
            variable: "myObject" 
            value: 
              apple:
                color: "green"
                type: "fruit"
              banana:
                color: "yellow"
                type: "fruit"
              oracle:
                color: "red"
                type: "company"
          transitions:
            next: "saveToUserScopeVar"            

        saveToUserScopeVar:
          component: "System.SetVariable"
          properties:
            variable: "user.myObject"
            value: "${myObject.value}"
          transitions:
            next: "readObjectFromUserScopeVariable"

        readObjectFromUserScopeVariable:
          component: "System.Output"
          properties:
            text: "The user scope object has the following keys: 
                   <#list user.myObject?string?eval?keys as k>${k} </#list>"      
            keepTurn: true
          transitions:
            next: "readOracleColorFromUserScopeVariable"

        readOracleColorFromUserScopeVariable:
          component: "System.Output"
          properties:
            text: "The color of the oracle key is: ${(user.myObject?string?eval).oracle.color}"
          transitions:
            return: "done"

      Frank

    • Grant Ronald

      Ha Frank beat me to it!

      states:
        fruitArray:
          component: "System.SetVariable"
          properties:
            variable: "user.fruits"
            value:
            - name: "Apple"
              color: "green"
            - name: "Banana"
              color: "yellow"


        output:
          component: "System.Output"
          properties:
            text: "Here is the fruit ${user.fruits.value[0].name}"
            keepTurn: false
            translate:
          transitions: 
            return : "done"

      You can find examples of this by going to bit.ly/ODAEnablement and look for Apache Freemarker

       

    • Frank Nimphius

      Turns out you can always use Grant's approach if you know what you want to access. So my sample

        text: "The color of the oracle key is: ${(user.myObject?string?eval).oracle.color}"

      could be written as

        text: "The color of the oracle key is: ${user.myObject.value.oracle.color}"

      So eval (though it works) is only needed if you wanted to generically access keys in an object. 

      Frank

    • Pawan Gupta

      Dear Grant and Frank thank you so much, I see you release blog on this topic as well. It is very helpful. This solves my big problem i was dealing with.