Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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

...

:

Code Block
{
    "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.

Code Block
<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 (above) in the HTML Directive (below).

Code Block
if(!msg.payload.orbita) {
msg.payload.orbita = {};
}
if(!msg.payload.orbita.directive) {
msg.payload.orbita.directive = [];
}

msg.payload.orbita.directive = 
  [{
    type    : "html5",
    options : 
    {
        

...

template : "300x300",
        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>`
        id : "newId",
        renderBubble : true
    }
   }]
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: ``

Place the html content here.

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

Image Added

renderBubble: false

Image Added

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.

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

Code Block
var myFunction = function(e) { 
     var fname = $('#'+$('iframe').attr('id')).contents().find('#fname').val();
     var lname = $('#'+$('iframe').attr('id')).contents().find('#lname').val();
     self.submitQuery('derive', 'form submitted','', { "fname": fname, "lname": lname });
    e.preventDefault();
 }

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

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.

View file
nameHTML_Directive.json

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.

Code Block
msg.payload.orbita.directive = 
  [{
    type    : "html5",
    options : 
    {
        // template : "300x300",
        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.submitQuery('Your data submited', 'User data', null, {fname: document.getElementById('fname').value, lname: document.getElementById('lname').value});">Submit</button> </form>`

...


    }
   }]
return msg;

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

Filter by label (Content by label)
showLabelsfalse
max5
spacescom.atlassian.confluence.content.render.xhtml.model.resource.identifiers.SpaceResourceIdentifier@130ff
sortmodified
showSpacefalse
reversetrue
typepage
cqllabel in ( "how-to" , "directive" , "experience-designer" ) and type = "page" and space = "OCS"
labelsdirective experience-designer how-to