0.9.0 • Published 4 years ago

ixo-assistant v0.9.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

Usage

Install the npm package:

npm install ixo-assistant

Then:

import { Widget } from 'ixo-assistant';

function IXOAssistant = () => {
  return (
    <Widget
      initPayload={"/get_started"}
      socketUrl={"http://localhost:5500"}
      socketPath={"/socket.io/"}
      customData={{"language": "en"}} // arbitrary custom data. Stay minimal as this will be added to the socket
      title={"Title"}
    />
  )
}

Parameters

Prop / ParamDefault valueDescription
initPayloadnullPayload sent to Rasa when conversation starts
socketUrlnullSocket URL
socketPathnullClose the chat window
customDatanullArbitrary object sent with the socket.
docViewerfalseIf you add this prop to the component or to the init script, docViewer=true , this will treat links in received messages as links to a document ( .pdf .doc .xlsx etc. ) and will open them in a popup using https://docs.google.com/viewer service
title'Welcome"Title shown in the header of the widget
subtitlenullSubtitle shown under the title in the header of the widget
inputTextFieldHint"Type a message"User message input field placeholder
hideWhenNotConnectedtrueIf true the widget will hide when the connection to the socket is lost
connectOn"mount"This prop lets you choose when the widget will try connecting to the server. By default, it tries connecting as soon as it mounts. If you select connectOn='open' it will only attempt connection when the widget is opened. it can only take the values mount and open.
onSocketEventnullcall custom code on a specific socket event
embeddedfalseSet to true if you want to embed the in a web page. The widget will always be open and the initPayload will be triggered immediately
showFullScreenButtonfalseShow a full screen toggle
displayUnreadCountfalsePath to an image displayed on the launcher when the widget is closed
showMessageDatefalseShow message date. Can be overriden with a function: (timestamp, message) => return 'my custom date'
customMessageDelaySee belowThis prop is a function, the function take a message string as an argument. The defined function will be called everytime a message is received and the returned value will be used as a milliseconds delay before displaying a new message.
paramsSee belowEssentially used to customize the image size.
storage"local"Specifies the storage location of the conversation state in the browser. "session" defines the state to be stored in the session storage. The session storage persists on reload of the page, and is cleared after the browser or tab is closed, or when sessionStorage.clear()is called. "local" defines the state to be stored in the local stoage. The local storage persists after the the browser is closed, and is cleared when the cookies of the browser are cleared, or when localStorage.clear()is called.
customComponentnullCustom component to be used with custom responses. E.g.: customComponent={ (messageData) => (<div>Custom React component</div>) }
onWidgetEvent{}call custom code on a specific widget event ( onChatOpen, onChatClose, onChatHidden, are available for now ), add a function to the desired object property in the props to have it react to the event.

Additional Examples

customMessageDelay
(message) => {
    let delay = message.length * 30;
    if (delay > 2 * 1000) delay = 3 * 1000;
    if (delay < 400) delay = 1000;
    return delay;
};
onSocketEvent
onSocketEvent={{
  'bot_uttered': () => console.log('the bot said something'),
  'connect': () => console.log('connection established'),
  'disconnect': () => doSomeCleanup(),
}}
params

The params props only allows to specify custom image dimensions:

params={{
        images: {
          dims: {
            width: 300,
            height: 200
          }
        }
      }}

Other features

Tooltips

Text messages received when the widget is closed will be shown as a tooltip.

Sending a message on page load

When reconnecting to an existing chat session, the bot will send a message contained in the localStorage key specified by the NEXT_MESSAGE constant. The message should be stringified JSON with a message property describing the message and an expiry property set to a UNIX timestamp in milliseconds after which this message should not be sent. This is useful if you would like your bot to be able to offer your user to navigate around the site.

API

MethodDescription
WebChat.toggle()Toggle the open/close state of the chat window, send initPayload if webchat is not initialized and is toggled open
WebChat.open()Open the chat window, send initPayload if webchat is not initialized
WebChat.close()Close the chat window
WebChat.isOpen()Get the open/closed state of the widget
WebChat.show()Show the chat widget, send initPayload if the chat is in open state and not initialized
WebChat.hide()Hide the chat widget
WebChat.isVisible()Get the shown/hidden state of the widget
WebChat.send(payload, text: optionnal)send a payload (/intent{"entity":"value"} to rasa. If text is specified, it will be displayed as a user message. If not specified, no user message will be displayed

Backends

The widget is designed to be used with Rasa

Rasa

Use the socketio channel: See instructions in the Rasa documentation

If you want to process customData in Rasa you have to create a new channel. Use channel rasa_core.channels.socketio as a template for your new channel. In this channel, customData can be retrieved via data['customData']. Then you can modify sender_id, save customData to the database, fill slots or whatever you need to with your additional data.

Styles

.rw-conversation-container
  |-- .rw-header
        |-- .rw-title
        |-- .rw-close-function
        |-- .rw-loading
  |-- .rw-messages-container
        |-- .rw-message
              |-- .rw-client
              |-- .rw-response
        |-- .rw-replies
              |-- .rw-reply
              |-- .rw-response
        |-- .rw-snippet
              |-- .rw-snippet-title
              |-- .rw-snippet-details
              |-- .rw-link
        |-- .rw-imageFrame
        |-- .rw-videoFrame
  |-- .rw-sender
        |-- .rw-new-message
        |-- .rw-send
ClassDescription
.rw-widget-containerThe div containing the chatbox of the default version
.rw-widget-embeddeddiv of the embedded chatbox (using embedded prop)
.rw-full-screendiv of the fullscreen chatbox (using fullScreenMode prop)
.rw-conversation-containerthe parent div containing the header, message-container and sender
.rw-messages-containerthe central area where the messages appear
.rw-senderdiv of the bottom area which prompts user input
.rw-new-messagethe text input element of sender
.rw-sendthe send icon element of sender
.rw-headerdiv of the top area with the chatbox header
.rw-titlethe title element of the header
.rw-close-buttonthe close icon of the header
.rw-loadingthe loading status element of the header
.rw-messagethe boxes holding the messages of client and response
.rw-repliesthe area that gives quick reply options
.rw-snippeta component for describing links
.rw-imageFramea container for sending images
.rw-videoFramea container for sending video