HTML Directive

In this document, you will learn to use an HTML directive and get user input and store them into the dynamic data.

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

Dynamic Schema

You need a dynamic schema to save the content from the user.

Sample schema:

{ "fields": [ { "type": "String", "fieldType": "text", "ref": "", "options": [], "multiple": false, "validation": [ { "required": false } ], "isDefault": false, "label": "First Name", "key": "firstname" }, { "type": "String", "fieldType": "text", "ref": "", "options": [], "multiple": false, "validation": [ { "required": false } ], "isDefault": false, "label": "Last name", "key": "lastname" } ] }

HTML form

This Sample HTML form contains two input fields and a submit button.

<form> <label for="fname">First name:</label><br> <input type="text" id="fname" name="fname" value="John"><br> <label for="lname">Last name:</label><br> <input type="text" id="lname" name="lname" value="Doe"><br><br> <button onclick="window.parent.myFunction(event)">Submit</button> </form>

HTML Directive 

Use the HTML form in the HTML Directive as shown below.

if(!msg.payload.orbita) { msg.payload.orbita = {}; } if(!msg.payload.orbita.directive) { msg.payload.orbita.directive = []; } msg.payload.orbita.directive = [{ type : "html5", options : { template : "300x240", html: `<form> <label for="fname">First name:</label><br> <input type="text" id="fname" name="fname" value="John"><br> <label for="lname">Last name:</label><br> <input type="text" id="lname" name="lname" value="Doe"><br><br> <button onclick="window.parent.myFunction(event)">Submit</button> </form>` } }]; return msg;

Properties and their Description

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

  • url

  • id

  • renderBubble

template : "300x300"

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

html: ``

Place the HTML content here. Optional if url field is present.

url: ""

Place the url of the HTML page here. Optional if html field is present.

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

If both url and html fields are present, the html field will take precedence.

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}

In this example, place the below code in the Custom JS tab of Bot View Template node.

When the user fills the form and clicks the submit button, the intent mentioned in the Bot view template code will get triggered and the flow will continue.

Saving the user input from HTML directive

You can make an interactive HTML form and get input from the chatbot users and store that data.

In the below example, you can collect the Firstname and Lastname of the chatbot users when they click on the Submit button.

The example flow is in the attachment. After importing this example flow, you must make changes to the Bot view template as suggested above to get the flow working.

  File Modified

File HTML_Directive.json

Jan 08, 2021 by Rajkiran Alla (Deactivated)

The First name and the Last name will be available in the property  msg.payload.originalDetectIntentRequest.payload.extPayload

Dynamic Data Manager

You should use the Dynamic Data Manager node to store the data.

You can use a function node before the Dynamic Data Manager node to clean the data before storing it.

In this example, we have used a function node to arrange the data before storing it.

And the Dynamic Data Manager node should be set to Create with an empty payload.

Related Articles