nanorep.sdk.ConversationTool

Options

storageKey

Type: String

Default: 'nr-conversation-tool'

Specify the key that will be used by the component to store its state inside the localStorage

Example usage:

var convTool = new nanorep.sdk.ConversationTool({
  storageKey: 'nr-custom-storage-key'
}, document.getElementById('example-options-storagekey'));

Methods

connectTo

connectTo(params) : void

Set connection parameters like host, account, kb, apiKey, context and initializedEntities

Parameters

params: Object Plain JavaScript object containing following fields:
host*, account*, kb*, apiKey, context, initializedEntities, clientSideEntities.

(Items marked with * are mandatory)

Example usage:

var convTool = new nanorep.sdk.ConversationTool(null, document.getElementById('example-methods-connectto'));

convTool.connectTo({
  host: 'AccountName.nanorep.com',
  account: 'AccountName',
  kb: 'English',
  apiKey: 'a4474788-28d3-4be9-9b6d-a3bcd9b8ac4b',
  context: { endpoint: 'web', another: 'demo' },
  initializedEntities: { brand: 'Apple', UserName: 'John' },
  clientSideEntities: ['USER_BALANCE', 'VISIT_LOCATION', 'PRIVATE_ENTITY']
});
        

reset

reset() : void

Resets the component state

Example usage:

var convTool = new nanorep.sdk.ConversationTool(null, document.getElementById('example-methods-connectto'));

convTool.reset();
        

sendStatement

sendStatement(params) : void

Resets the component state

Parameters

params: Object (required) JavaScript object containing response of the /api/conversation/v1/statement API call. (Note: /api/conversation/v2/statement API endpoint is in introduction phase.)
options: Object (optional) JavaScript object containing display options like showUserMessage (default: false) and showBotResponse (default: true)

Example usage:

var convTool = new nanorep.sdk.ConversationTool(null, document.getElementById('demo-container'));

// connectTo ...

convTool.sendStatement({ statement: 'hey there!' });
convTool.sendStatement({ statement: 'bye-bye!' }, { showUserMessage: true, showBotResponse: true });
        

setEntityValue

setEntityValue(entityName, entityValue) : void

Used to define missing client side entity

Parameters

entityName: String Name of the client side entity that is missing
entityValue: Object JSON Object that represents entity value

Example usage:

var convTool = new nanorep.sdk.ConversationTool(null, document.getElementById('demo-container'));

convTool.connectTo({
  ...
  clientSideEntities: ['USER_BALANCE', 'VISIT_LOCATION', 'PRIVATE_ENTITY']
});

convTool.on('missingEntity', function(response) {
  convTool.setEntityValue(entityName, { kind: entityName, type: 'text', value: 'MOCKED VALUE FOR ' + entityName });
});
        

Events

botResponse

convTool.on('botResponse', function(query) {})

Called after user asked something inside the widget (search and conversational)

Arguments

query: String Search query

Example usage:

var convTool = new nanorep.sdk.ConversationTool(null, document.getElementById('example-methods-connectto'));

// connectTo ...

convTool.sendStatement({ statement: 'hey there!' });
convTool.sendStatement({ postback: 'ARTICLE:904673272' });
convTool.sendStatement({ any: 'other', field: 'supported' });
        

missingEntity

convTool.on('missingEntity', function(entityName) {})

Called each time when client side entity value is requested by the backend

Arguments

entityName: String Name of the client side entity that is missing

Example usage:

var convTool = new nanorep.sdk.ConversationTool(null, document.getElementById('example-methods-connectto'));

convTool.connectTo({
  ...
  clientSideEntities: ['USER_BALANCE', 'VISIT_LOCATION', 'PRIVATE_ENTITY']
});

convTool.on('missingEntity', function(entityName) {
  console.log('Client side entity that is missing:', entityName);
});
        

Example usage

1. Add script to your page:

<script src="https://<AccountName>.nanorep.co/web/include.js?sdk=conversation-tool"></script>

2. Create empty <div>:

<div id="demo-container"></div>

3. Initialize component:

var convTool = new nanorep.sdk.ConversationTool({
  storageKey: 'nr-conversation-tool'
}, document.getElementById('demo-container'));

convTool.reset();

convTool.on({
  botResponse: function(response) {
    console.log('Bot Response:', response);
  },
  missingEntity: function(entityName) {
    convTool.setEntityValue(entityName, { kind: entityName, type: 'text', value: 'Mocked value for ' + entityName });
  }
});

convTool.connectTo({
  host: 'product_demo.nanorep.co',
  account: 'product_demo',
  kb: 'Demo_Bot',
  apiKey: 'a4474788-28d3-4be9-9b6d-a3bcd9b8ac4b',
  context: { endpoint: 'web', another: 'demo' },
  initializedEntities: { brand: 'Apple', UserName: 'John' },
  clientSideEntities: ['PRIVATE_ENTITY', 'ANOTHER_DEMO_ENTITY']
});

Live demo: