Type: Object
Instance of Widget API helper object
setUserInput({ input: Input DOM element }) | Initialize input field with custom chat UI |
Type: Object
Widget configuration object
constructor(widgetAPI, cnf) : void
Initialize Widget UI here
widgetAPI: Object |
Instance of Widget API helper object. Details here |
cnf: Object |
Widget configuration object |
addUserMessage(message) : void
Called when user's message should be displayed in UI
message: Object |
Object which contains plain text user input in a "text" property |
addBotMessage(message) : void
Display incoming bot message
message: Object |
Object containing bot message in plain "text" and rich "html" properties |
addAgentMessage(message) : void
Display incoming agent message
message: Object |
Object containing agent message in plain "text" and rich "html" properties |
updateMessage(message) : void
This method could be used later to support updating messages by id
message: Object |
Object containing message including messageId and changed fields |
addPersistentOption(optionName, message, activate) : void
Display persistent option (for specific message)
optionName: String |
Button caption as plain text |
message: Object |
Object containing bot message in plain text and html |
activate: Function |
Callback function to be invoked with no arguments when that option was activated |
addQuickOption(optionName, activate) : void
Display quick option (for entire chat session)
optionName: String |
Button caption as plain text |
activate: Function |
Callback function to be invoked with no arguments when that option was activated |
addStatusMessage(message) : void
Display status message bubble
message: Object |
Message object |
removeQuickOptions() : void
Remove all quick options (if any displayed)
addSuggestion(suggestion, activate) : void
This method will be invoked when widget could show some suggestions for the current user query
suggestion: Object |
Suggestion object with .id, .text and .html properties |
activate: Function |
Callback function to be invoked with no arguments when that option was activated |
setActiveSuggestion(suggestion) : void
Highlight suggestion as active / currently selected
suggestion: Object |
Suggestion object with .id, .text and .html properties |
removeSuggestions() : void
This method will be called when widget should remove all visible suggestions
showDialog(node, dismiss) : void
This method is used when widget needs to display a dialog
node: Node |
DOM node that should be displayed as a modal dialog (for example ContactForm or PhoneDialog) |
dismiss: Function |
Function that should be invoked by UI to dismiss dialog |
showAutolink(params, activate) : void
This method will be called when autolink/guidance should be displayed to the user
params: Object |
Object containing autolink/guidance options |
activate: Function |
Callback function to be invoked with no arguments when that option was activated |
displayURL(url) : void
This method will be called when Channeling policy ask widget to display URL inside of it
url: String |
URL that should be displayed inside the widgetUI (probably by using <iframe>) |
hideDialog(node) : void
This method will be called when widget needs to hide dialog
node: Node |
node DOM node that is going to be hidden |
updateAgentInformation(agentInfo) : void
Handle agent information such as name, avatar, status, etc (can be saved for later use)
agentInfo: Object |
Object containing agent information |
updateAgentTypingStatus(isTyping) : void
Update agent typing status in UI
isTyping: Boolean |
Boolean value which indicates if agent is currently typing |
provideChatArea(channel) : Node
This function must return DOM Node that will be used as container by Chat Provider
onShowBotMessage(statement) : Boolean
Hook to activate custom processing of specific bot message. If handler returns truthy value then default methods won't be called
Return true from custom UI implementation in order to confirm interception intent
statement: Object |
Raw statement object as it comes from public API |
onChatStarted() : void
Hook that will be invoked when live chat session is started
onChatEnded() : void
Hook that will be invoked when live chat session is ended
reset() : void
This hook is called when we need to reset widget to initial UI state
You should see custom floating widget at the bottom of the page.
<form id="demoForm" onsubmit="return loadWidget(demoHost.value, demoAccount.value, demoConfig.value), false"> <input id="demoHost" type="text" placeholder="host (eg. qa.nanorep.co)" required><br> <input id="demoAccount" type="text" placeholder="account (eg. qa)" required><br> <input id="demoConfig" type="text" placeholder="configId (optional)"><br> <button type="submit">Load widget</button> </form>
function loadWidget(host, account, configId) { /* widget embed code */ !function(t,e,o,c,n,a){var s=window.nanorep=window.nanorep||{};s=s[e]=s[e]||{},s.apiHost=a,s.host=n,s.path=c,s.account=t,s.protocol="https:"===location.protocol?"https:":"http:",s.on=s.on||function(){s._calls=s._calls||[],s._calls.push([].slice.call(arguments))};var p=s.protocol+"//"+n+c+o+"?account="+t,l=document.createElement("script");l.async=l.defer=!0,l.setAttribute("src",p),document.getElementsByTagName("head")[0].appendChild(l)}(account,"floatingWidget","floating-widget.js","/web/", location.host, host);document.getElementById('demoForm').style.display='none';if(configId){nanorep.floatingWidget.on('init',function(){this.setConfigId(configId)})}; /* widget embed code */ // provide custom UI implementation to support custom skin nanorep.floatingWidget.on('init', function() { this.useCustomUI({ constructor: function(api, cnf) { // save reference to the API object this.api = api; // create widget container this.container = document.createElement('div'); this.container.innerHTML = '<div class="mywidget">' + '<div class="mywidget__content">' + '<div class="mywidget__log"></div>' + '<div class="mywidget__quick-options"></div>' + '</div>' + '<input class="mywidget__input" placeholder="Type your question here">' + '</div>'; document.body.appendChild(this.container); // save reference to core nodes this.content = this.container.querySelector('.mywidget__content'); this.log = this.container.querySelector('.mywidget__log'); this.input = this.container.querySelector('.mywidget__input'); this.quickOptionsContainer = this.container.querySelector('.mywidget__quick-options'); // initialize input field api.setUserInput({ input: this.input }); }, addUserMessage: function(message) { // display user message in conversation log var newNode = document.createElement('div'); newNode.innerHTML = '<div class="mywidget-message mywidget-message--user">' + message.html + '</div>'; this.log.appendChild(newNode); this._scrollToBottom(); }, addBotMessage: function(message) { // display bot message in conversation log var newNode = document.createElement('div'); newNode.innerHTML = '<div class="mywidget-message mywidget-message--bot" id="msg' + message.id + '">' + message.html + '</div>'; this.log.appendChild(newNode); this._scrollToBottom(); }, addAgentMessage: function(message) { // display agent message in conversation log var newNode = document.createElement('div'); newNode.innerHTML = '<div class="mywidget-message mywidget-message--agent">' + message.html + '</div>'; this.log.appendChild(newNode); this._scrollToBottom(); }, addQuickOption: function(optionName, activate) { // this function will be called when quick option button should be added to the list of quick actions var button = document.createElement('button'); button.className = 'mywidget__quick-option'; button.onclick = activate; button.appendChild(document.createTextNode(optionName)); this.quickOptionsContainer.appendChild(button); this._scrollToBottom(); }, removeQuickOptions: function() { // remove all available quick options this.quickOptionsContainer.innerHTML = ''; }, addPersistentOption: function(optionName, message, activate) { // draw persistent option in specific message var messageContainer = document.getElementById('msg' + message.id), button = document.createElement('button'); button.onclick = activate; button.className = 'mywidget__persistent-option'; button.appendChild(document.createTextNode(optionName)); messageContainer.appendChild(button); }, reset: function() { // reset entire widget UI to initial state this.log.innerHTML = ''; this.removeQuickOptions(); }, _scrollToBottom: function() { // helper function to scroll conversation to the bottom when message is added var maxScrollValue = this.content.scrollHeight - this.content.offsetHeight; if (this.content.scrollTop < maxScrollValue) { this.content.scrollTop = maxScrollValue; } } }); }); }
// prepopulate field values if host/account/kb are defined in documentation page URL (not related to the demo) (function(url) { var pairs = url.substr(url.indexOf('?') + 1).split('&'), params = {}; for (var i = 0; i < pairs.length; i++) { var data = pairs[i].split('='); params[data[0]] = decodeURIComponent(data[1]); } document.getElementById('demoHost').value = params.host || ''; document.getElementById('demoAccount').value = params.account || ''; document.getElementById('demoConfig').value = params.configId || ''; }(top.location.href));