Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 10 Next »

In this document, you will learn to use an HTML directive and get user input.

If you call a 3rd party site for javascript or CSS, you will need to white list their domain.

For illustration, we will use a simple HTML form in the HTML directive to get the data from the user and post it to an intent.

Template node

<html>
  <head>
      <style>
       </style>
    <script type="text/javascript">
    </script>
  </head>
  <body>
    <form> 
        <p><label for="fname">First name:</label> <input type="text" id="fname" name="fname"></p> 

        <p><label for="lname">Last name:</label> <input type="text" id="lname" name="lname"></p> 
        <p style="text-align: center;"><button onclick="window.parent.htmlFormSendData(event)">Submit</button></p>
  
    </form>
  </body>
</html>

HTML Directive 

Use the HTML form (above) in the HTML Directive (below).

msg.payload.orbita = msg.payload.orbita || {};
    msg.payload.orbita.directive = msg.payload.orbita.directive || [];
    //node.warn(msg.payload.multiagent.chat.chatText );
    msg.payload.orbita.directive.push({
        type: "html5",
        options: {
            template: "400x150",
            url: `/oeapi/userdata`
        }
    });
return msg;

Property

Description

type : “html5“

The type should be set to html5 to invoke the HTML Directive.

options: {}

The options object contains the following properties.

  • template

  • html

  • id

  • renderBubble

template : "300x300"

(OPTIONAL) The template property defines the size of HTML content.

html: ``

URL to the HTML page

id:"html-content-1"

(OPTIONAL) This property helps to set the id value of the iframe.

renderBubble : true

(OPTIONAL) This property defines how to render the html5 directive in the chatbot. If it is true, it will render inside the chat bubble and if it is false it will render outside of the chat bubble.
By default, the renderBubble is set to true

renderBubble: true

renderBubble: false

Bot View Template

To handle the Submit button from the HTML form, you have to create a function in the Custom JS tab of the Bot View Template node and use the self.submitQuery property.

If using an intent utterance to trigger an intent:

self.submitQuery('intent utterance','display text', 'Event', Custom Object}

if using an event to trigger the intent,

self.submitQuery('FlowMessageFreeTextEvent','display text', 'FlowMessageFreeTextEvent', Custom Object}

For event:

For utterance:

var htmlFormSendData = function(e) { 
     var fname = $('#'+$('iframe').attr('id')).contents().find('#fname').val();
     var lname = $('#'+$('iframe').attr('id')).contents().find('#lname').val();
     // self.submitQuery('FlowMessageFreeTextEvent', 'Thanks','FlowMessageFreeTextEvent', { "fname": fname, "lname": lname });
     self.submitQuery('utterance intent formResult', 'Thanks','', { "fname": fname, "lname": lname });
    e.preventDefault();
 };

When the user fills the form and clicks the submit button, the intent utterance mentioned in the Bot view template code will be sent to cause the formResult intent to fire.

Your details Say node:

Your details:

First Name is :  {{msg.payload.originalDetectIntentRequest.payload.extPayload.fname}}

Last Name is :  {{msg.payload.originalDetectIntentRequest.payload.extPayload.lname}}  

The example flow, everything except the javascript for the Bot View Template.

[{"id":"9ab6af65.44a53","type":"function","z":"90a4d408.2e36e8","name":"html5 directive","func":"msg.payload.orbita = msg.payload.orbita || {};\n    msg.payload.orbita.directive = msg.payload.orbita.directive || [];\n    //node.warn(msg.payload.multiagent.chat.chatText );\n    msg.payload.orbita.directive.push({\n        type: \"html5\",\n        options: {\n            template: \"400x150\",\n            url: `/oeapi/userdata`\n        }\n    });\nreturn msg;\n\n","outputs":1,"noerr":0,"x":380,"y":500,"wires":[["922bf62f.856878"]]},{"id":"c9e83795.c44548","type":"alexa-http intent","z":"90a4d408.2e36e8","name":"","skillConfig":"91aa18ea.44c798","url":"/alexa_test","intent":"HTMLform","lastIntent":"anystate","x":150,"y":500,"wires":[["9ab6af65.44a53"]]},{"id":"8a4a97b2.9e0318","type":"alexa-http response","z":"90a4d408.2e36e8","name":"","x":820,"y":500,"wires":[]},{"id":"53341621.d9fbc8","type":"alexa-say","z":"90a4d408.2e36e8","name":"","saytext":"<p>Your details:</p><p>First Name is :  {{msg.payload.originalDetectIntentRequest.payload.extPayload.fname}}</p><p>Last Name is :  {{msg.payload.originalDetectIntentRequest.payload.extPayload.lname}}  </p>","reprompttext":"","chatText":"","chatRePrompt":"","screenShortTitle":"","screenLongTitle":"","screenBody":"","screenSmallImage":"","screenLargeImage":"","ablabel":"","endsession":false,"lastIntent":"","requireLastIntent":false,"buttons":"{\"type\":\"\",\"name\":\"\",\"choices\":[]}","exporttext":"{\n   \"voice\": {\n      \"sayText\": \"<p>Your details:</p><p>First Name is :  {{msg.payload.originalDetectIntentRequest.payload.extPayload.fname}}</p><p>Last Name is :  {{msg.payload.originalDetectIntentRequest.payload.extPayload.lname}}  </p>\",\n      \"rePrompt\": \"\"\n   },\n   \"chat\": {\n      \"chatText\": \"\",\n      \"rePrompt\": \"\"\n   },\n   \"screen\": {\n      \"shortTitle\": \"renderedShortTitle\",\n      \"longTitle\": \"renderedLongTitle\",\n      \"body\": \"\",\n      \"smallImage\": \"renderedSmallImage\",\n      \"largeImage\": \"renderedLargeImage\"\n   },\n   \"buttons\": {\n      \"type\": \"buttonObj.type\",\n      \"name\": \"buttonObj.name\",\n      \"choices\": \"buttonObj.choices\"\n   }\n}","disableAnyState":false,"x":610,"y":560,"wires":[["8a4a97b2.9e0318"]]},{"id":"a56d50f8.820e3","type":"alexa-http intent","z":"90a4d408.2e36e8","name":"","skillConfig":"91aa18ea.44c798","url":"/alexa_test","intent":"formResults","lastIntent":"anystate","x":150,"y":560,"wires":[["53341621.d9fbc8","55ea56c8.f63e08","2d86a0a8.e408e"]]},{"id":"55ea56c8.f63e08","type":"debug","z":"90a4d408.2e36e8","name":"User Details","active":true,"console":"false","complete":"payload.originalDetectIntentRequest.payload.extPayload","x":430,"y":540,"wires":[]},{"id":"922bf62f.856878","type":"alexa-say","z":"90a4d408.2e36e8","name":"","saytext":"<p>Enter details</p>","reprompttext":"","chatText":"","chatRePrompt":"","screenShortTitle":"","screenLongTitle":"","screenBody":"","screenSmallImage":"","screenLargeImage":"","ablabel":"","endsession":false,"lastIntent":"","requireLastIntent":false,"buttons":"{\"type\":\"\",\"name\":\"\",\"choices\":[]}","exporttext":"{\n   \"voice\": {\n      \"sayText\": \"<p>info</p>\",\n      \"rePrompt\": \"\"\n   },\n   \"chat\": {\n      \"chatText\": \"\",\n      \"rePrompt\": \"\"\n   },\n   \"screen\": {\n      \"shortTitle\": \"renderedShortTitle\",\n      \"longTitle\": \"renderedLongTitle\",\n      \"body\": \"\",\n      \"smallImage\": \"renderedSmallImage\",\n      \"largeImage\": \"renderedLargeImage\"\n   },\n   \"buttons\": {\n      \"type\": \"buttonObj.type\",\n      \"name\": \"buttonObj.name\",\n      \"choices\": \"buttonObj.choices\"\n   }\n}","disableAnyState":false,"x":610,"y":500,"wires":[["8a4a97b2.9e0318"]]},{"id":"dd528269.035d1","type":"http in","z":"90a4d408.2e36e8","name":"","url":"/userdata","method":"get","upload":false,"swaggerDoc":"","x":130,"y":620,"wires":[["dac0bd77.8c942"]]},{"id":"2c803a75.bee8c6","type":"http response","z":"90a4d408.2e36e8","name":"","statusCode":"","headers":{},"x":580,"y":620,"wires":[]},{"id":"dac0bd77.8c942","type":"template","z":"90a4d408.2e36e8","name":"HTML 5 FORM","field":"payload","fieldType":"msg","format":"html","syntax":"mustache","template":"<html>\n  <head>\n      <style>\n       </style>\n    <script type=\"text/javascript\">\n    </script>\n  </head>\n  <body>\n    <form> \n        <p><label for=\"fname\">First name:</label> <input type=\"text\" id=\"fname\" name=\"fname\"></p> \n\n        <p><label for=\"lname\">Last name:</label> <input type=\"text\" id=\"lname\" name=\"lname\"></p> \n        <p style=\"text-align: center;\"><button onclick=\"window.parent.htmlFormSendData(event)\">Submit</button></p>\n  \n    </form>\n  </body>\n</html>\n","output":"str","x":380,"y":620,"wires":[["2c803a75.bee8c6"]]},{"id":"2d86a0a8.e408e","type":"debug","z":"90a4d408.2e36e8","name":"","active":true,"console":"false","complete":"false","x":430,"y":580,"wires":[]},{"id":"91aa18ea.44c798","type":"alexa-skill-config","skillname":"Main","projectId":"5f9565f13e6777007d1ebfd1","intents":"[{\"name\":\"HTMLform\",\"options\":{\"slots\":[],\"utterances\":[\"Html form\"],\"prompts\":{\"confirm\":[]},\"events\":[],\"nlp\":[{\"_id\":\"5ff07ebb9b1ec40080b38371\",\"provider\":\"ALEXA\",\"value\":true},{\"_id\":\"5ff07ebb9b1ec40080b38370\",\"provider\":\"GOOGLE\",\"value\":true}]}},{\"name\":\"formResults\",\"options\":{\"slots\":[],\"utterances\":[\"utterance intent formResult\"],\"prompts\":{\"confirm\":[]},\"events\":[],\"nlp\":[{\"_id\":\"5ff0a9569b1ec40080b383af\",\"provider\":\"ALEXA\",\"value\":true},{\"_id\":\"5ff0a9569b1ec40080b383ae\",\"provider\":\"GOOGLE\",\"value\":true}]}}]","skillstate":"fromsession, testState,help,emergency,FlowStudio,flow, test,flow, test,flow, test,flow, test,fromsession,flow, test,flow, test,flow, test,flow, test,flow, test,flow, test,flow, test,flow, test,flow, test,flow, test,flow, test,flow, test,fromsession,flow, test,flow, test,flow, test,flow, test,flow, test,flow, test,flow, test,flow, test,fromsession, testState,fromsession, Reminder, sanity, flow_stud,fromsession, Reminder, sanity, flow_stud"},{"id":"slots","type":"slots","z":"","slots":[]}]

The First name and the Last name will be available in this object:

msg.payload.originalDetectIntentRequest.payload.extPayload

Example 2: Calling an HTM5 Control from Flow Studio

{
    "flowData": {
        "class": "GraphLinksModel",
        "copiesKey": false,
        "linkFromPortIdProperty": "fromPort",
        "linkToPortIdProperty": "toPort",
        "nodeDataArray": [
            {
                "key": "855539587347712516",
                "name": "Start",
                "description": "",
                "type": "start",
                "category": "",
                "loc": "107.52284749830794 70.10354461669922",
                "size": "80 80"
            },
            {
                "key": "806394389093900710",
                "name": "googleChart",
                "description": " ",
                "type": "external hook",
                "category": "",
                "loc": "107.52284749830794 261.8334813484055",
                "switches": [
                    {
                        "operant": "eq",
                        "itemType": "string"
                    },
                    {
                        "operant": "eq",
                        "itemType": "string"
                    },
                    {
                        "operant": "eq",
                        "itemType": "string"
                    }
                ],
                "nodeInput": "custom"
            },
            {
                "key": "392524825426112445",
                "name": "Option",
                "description": "Select One\n",
                "type": "text",
                "category": "",
                "loc": "107.52284749830794 460.08626557841984",
                "phrases": [
                    {
                        "type": "string",
                        "value": "",
                        "isEnable": false,
                        "isTerm": false,
                        "src": "/aurora-ui/assets/images/data-dictionary/datadictionary.png"
                    }
                ],
                "validators": [
                    {}
                ],
                "multiScreen": {
                    "description": "",
                    "validation": {
                        "valid": "",
                        "invalid": ""
                    },
                    "voice": {
                        "sayText": "<p>Select One</p>\n",
                        "rePrompt": ""
                    },
                    "chat": {
                        "chatText": ""
                    },
                    "screen": {
                        "shortTitle": "",
                        "longTitle": "",
                        "body": "",
                        "smallImage": "",
                        "largeImage": ""
                    },
                    "buttons": {
                        "type": "dropdown",
                        "name": "buttons",
                        "choices": []
                    }
                },
                "directives": {
                    "micInput": false,
                    "keyboardInput": false,
                    "waitTimeNoAnimation": false,
                    "endSession": false,
                    "inputModel": "hidden",
                    "inputType": "textbox"
                }
            },
            {
                "key": "832041313240806879",
                "name": "End",
                "description": "You picked:\n\n{{msg.payload.request.intent.slots.freeText.value}}\n\n\nHow else can i help you?\n",
                "type": "end",
                "category": "",
                "loc": "107.52284749830794 651.8162023101261",
                "size": "80 80",
                "multiScreen": {
                    "description": "",
                    "validation": {
                        "valid": "",
                        "invalid": ""
                    },
                    "voice": {
                        "sayText": "<p>You picked:</p>\n\n<h1>{{msg.payload.request.intent.slots.freeText.value}}</h1>\n\n<hr />\n<p>How else can i help you?</p>\n",
                        "rePrompt": ""
                    },
                    "chat": {
                        "chatText": "<p>You picked:</p>\n\n<h1>{{msg.payload.request.intent.slots.freeText.value}}</h1>\n\n<hr />\n<p>How else can i help you?</p>\n"
                    },
                    "screen": {
                        "shortTitle": "",
                        "longTitle": "",
                        "body": "",
                        "smallImage": "",
                        "largeImage": ""
                    },
                    "buttons": {
                        "type": "dropdown",
                        "name": "buttons",
                        "choices": []
                    }
                },
                "directives": {
                    "micInput": true,
                    "keyboardInput": true,
                    "waitTimeNoAnimation": false,
                    "endSession": false
                }
            }
        ],
        "linkDataArray": [
            {
                "from": "855539587347712516",
                "to": "806394389093900710",
                "fromPort": "OUT",
                "toPort": "IN",
                "points": [
                    107.52284749830794,
                    110.15531692504884,
                    107.52284749830794,
                    120.15531692504884,
                    107.52284749830794,
                    147.6812030792236,
                    107.52284749830794,
                    147.6812030792236,
                    107.52284749830794,
                    175.20708923339834,
                    107.52284749830794,
                    185.20708923339834
                ]
            },
            {
                "from": "806394389093900710",
                "to": "392524825426112445",
                "fromPort": "OUT",
                "toPort": "IN",
                "points": [
                    107.52284749830794,
                    318.45987346341263,
                    107.52284749830794,
                    328.45987346341263,
                    107.52284749830794,
                    350.9598734634127,
                    107.52284749830794,
                    350.9598734634127,
                    107.52284749830794,
                    373.4598734634127,
                    107.52284749830794,
                    383.4598734634127
                ]
            },
            {
                "from": "392524825426112445",
                "to": "832041313240806879",
                "fromPort": "OUT",
                "toPort": "IN",
                "points": [
                    107.52284749830794,
                    516.712657693427,
                    107.52284749830794,
                    526.712657693427,
                    107.52284749830794,
                    554.2385438476019,
                    107.52284749830794,
                    554.2385438476019,
                    107.52284749830794,
                    581.7644300017769,
                    107.52284749830794,
                    591.7644300017769
                ]
            }
        ]
    },
    "utterances": [
        {
            "value": "Google Chart"
        }
    ]
}

Experience manager flow:

[{"id":"c96ca56c.50dab8","type":"inject","z":"e134133.8a5f0f","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":"0.5","x":150,"y":420,"wires":[["e64c76cc.90af68"]]},{"id":"e64c76cc.90af68","type":"function","z":"e134133.8a5f0f","name":"googleChart","func":"global.get(\"hooks\").googleChart = msg => {\n    node.send(msg);\n};","outputs":1,"noerr":0,"x":330,"y":420,"wires":[["55edd6f6.443818"]]},{"id":"55edd6f6.443818","type":"function","z":"e134133.8a5f0f","name":"HTML5 Directives","func":"msg.payload.orbita = msg.payload.orbita || {};\n    msg.payload.orbita.directive = msg.payload.orbita.directive || [];\n    msg.payload.orbita.directive.push({\n        type: \"html5\",\n        options: {\n            template: \"400x400\",\n            url: `/oeapi/googlechart`\n        }\n    });\n\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":420,"wires":[["4ecb5b2e.dfb594"]]},{"id":"1a031c97.f14e33","type":"http in","z":"e134133.8a5f0f","name":"","url":"/googlechart","method":"get","upload":false,"swaggerDoc":"","x":160,"y":500,"wires":[["7c5de693.7046d8"]]},{"id":"7c5de693.7046d8","type":"template","z":"e134133.8a5f0f","name":"Chart","field":"payload","fieldType":"msg","format":"html","syntax":"mustache","template":"<html>\n  <head>\n    <script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"></script>\n    <script type=\"text/javascript\">\n      google.charts.load('current', {'packages':['corechart']});\n      google.charts.setOnLoadCallback(drawChart);\n\n      function drawChart() {\n\n        var data = google.visualization.arrayToDataTable([\n          ['Task', 'Hours per Day'],\n          ['Work',     11],\n          ['Eat',      2],\n          ['Commute',  2],\n          ['Watch TV', 2],\n          ['Sleep',    7]\n        ]);\n\n        var options = {\n          title: 'My Daily Activities'\n        };\n\n        var chart = new google.visualization.PieChart(document.getElementById('piechart'));\n        chart.draw(data, options);\n        \n        google.visualization.events.addListener(chart, 'select', selectHandler);\n\n        function selectHandler(e) {\n             var tasks = ['Work','Eat','Commute','Watch TV','Sleep']\n            var selection = chart.getSelection();\n            window.parent.orbitaChatBot.sendQuickReply(tasks[selection[0].row]);  // send to Orbita chatBot \n        }\n      }\n    </script>\n  </head>\n  <body>\n    <div id=\"piechart\" style=\"width: 400px; height: 400px;\"></div>\n  </body>\n</html>\n","output":"str","x":430,"y":500,"wires":[["40ef024e.22a7ec"]]},{"id":"40ef024e.22a7ec","type":"http response","z":"e134133.8a5f0f","name":"","statusCode":"","headers":{},"x":590,"y":500,"wires":[]},{"id":"slots","type":"slots","z":"","slots":[]}]

Directive

msg.payload.orbita = msg.payload.orbita || {};
    msg.payload.orbita.directive = msg.payload.orbita.directive || [];
    msg.payload.orbita.directive.push({
        type: "html5",
        options: {
            template: "400x400",
            url: `/oeapi/googlechart`
        }
    });

return msg;

Template Node code:

<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {

        var data = google.visualization.arrayToDataTable([
          ['Task', 'Hours per Day'],
          ['Work',     11],
          ['Eat',      2],
          ['Commute',  2],
          ['Watch TV', 2],
          ['Sleep',    7]
        ]);

        var options = {
          title: 'My Daily Activities'
        };

        var chart = new google.visualization.PieChart(document.getElementById('piechart'));
        chart.draw(data, options);
        
        google.visualization.events.addListener(chart, 'select', selectHandler);

        function selectHandler(e) {
             var tasks = ['Work','Eat','Commute','Watch TV','Sleep']
            var selection = chart.getSelection();
            window.parent.orbitaChatBot.sendQuickReply(tasks[selection[0].row]);  // send to Orbita chatBot 
        }
      }
    </script>
  </head>
  <body>
    <div id="piechart" style="width: 400px; height: 400px;"></div>
  </body>
</html>

Note in this example 2, we don’t need any javascript code in the bot view template

window.parent.orbitaChatBot.sendQuickReply(......);

This has to be a number or text, not an object. If your HTML card results are an object using the method shown in the first HTML 5 example.

Passes the value as an utterance. In the Flow manager, the Single Input Control is set to hide inputs the keyboard. Textbox - Hidden, it waiting for an input, which sendQuickReply sends.

  • No labels