2.14.0 • Published 8 days ago

push-webchat v2.14.0

Weekly downloads
30
License
MIT
Repository
github
Last release
8 days ago

Weni Webchat npm

A simple webchat widget to connect with Weni 💬platform. Originally forked from rasa-webchat and react-chat-widget and optimized for Weni.

Features

  • Text Messages
  • Quick Replies
  • Images and Videos
  • Markdown support
  • Easy to import in a script tag or as a React Component
  • Persistent sessions
  • Typing indications
  • Smart delay between messages

Usage

In a <script> tag

In your <body/>:

<div id="webchat"/>
<script src="https://storage.googleapis.com/push-webchat/wwc-latest.js"></script>
// Or you can replace latest with a specific version
<script>
  WebChat.default.init({
    selector: "#webchat",
    initPayload: "Hello",
    channelUuid: '05221ba1-e9c5-4ead-82fe-9e1086e54258', // External Channel UUID from Weni
    host: 'https://flows.weni.ai', // Host URL from Weni
    socketUrl: "https://websocket.weni.ai",
    title: "Title",
    subtitle: "Subtitle",
  })
</script>

About images: width and height define the size in pixels that images in messages are crop-scaled to. If not present, the image will scale to the maximum width of the container and the image.

It is recommended to use a particular version (i.e. "widget-.js") however the file "widget-latest.js" is also available and is updated continuously with the latest version.

As a React component

Install the package from GitHub by running:

npm install push-webchat

Then once it is installed it can be implemented as follows.

import { Widget } from 'push-webchat';

function CustomWidget = () => {
  return (
    <Widget
      initPayload={"/get_started"}
      socketUrl={"https://websocket.weni.ai"}
      title={"Title"}
    />
  )
}
  • Make sure to have the prop embedded set to true if you don't want to see the launcher.

Parameters

Prop / ParamDefault valueDescription
initPayloadnullPayload sent to Push when conversation starts
socketUrlnullSocket URL
sessionIdnullIf provided, this will be the socket session Id, if not, the socket connection will provide a random one.
customDatanullArbitrary object sent with the socket. It's not supported by Push at the moment.
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.
customizeWidgetSee belowSpecifies a set of custom css parameters for the widget style
handleNewUserMessagenullCall a custom function when the user send a new message
profileAvatarnullImage to be displayed on chat Header and with a bot message
openLauncherImagenullImage to be displayed on the Launcher while the chat is closed
closeImagenullImage to be displayed on the Launcher while the chat is open
connectingText'Waiting for server...'Message displayed while the connection between the widget and socket server is established
showCloseButtontrueBoolean to define if the close button will be shown
startFullScreenfalseBoolean to define if the widget is started on fullscreen mode
tooltipMessagenullMessage that will be displayed as tooltip
tooltipDelay500Delay for the tooltipMessage
disableTooltipsfalseBoolean to define if tooltips should be displayed
showHeaderAvatartrueBoolean to define if the image provided in profileAvatar attribute must be displayed in Header
headerImagenullIf provided, the image will be displayed in place of profileImage, title and subtitle
suggestionsConfigSee belowProp to define the configuration for the source of input suggestions data
customAutoCompleteSee belowThis prop is a function, the function take the current input string as an argument. The defined function will be called everytime the user modify the input within a delay of 500ms, the function return must be an array of strings that will be used as input suggestions.

Additional Examples

customMessageDelay
(message) => {
    let delay = message.length * 30;
    if (delay > 2 * 1000) delay = 3 * 1000;
    if (delay < 400) delay = 1000;
    return delay;
}
customAutoComplete
(currentInput) => {
    // you can gather strings in any manner to use as input suggestions
    return ['first message', 'second message', 'third message', 'fourth message', 'fifth message'];
}
onSocketEvent
onSocketEvent={{
  '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
          }
        }
      }}
suggestionsConfig

The suggestionsConfig props contains the configuration for the suggestions request made to the given url:

  • url: The API Url for the requests
  • datasets: The datasets IDs for the suggestions
  • language: The language that the suggestions should be returned from the API call
  • excludeIntents: The intents that should be excluded from the suggestions
  • automaticSend: If set to true, when the user clicks the suggestion, it will send it automaticaly instead of changing only the input
suggestionsConfig = {
    url: 'https://api.bothub.it/v2/repository/examples/search_repositories_examples/',
    datasets: [
      "xxxxxxxx-yyyy-zzzz-wwww-yyyyyyyyyyyy",
      "yyyyyyyy-xxxx-wwww-zzzz-wwwwwwwwwwww",
      "aaaaaaaa-cccc-dddd-bbbb-aaaaaaaaaaaa"
    ],
    language: "en",
    excludeIntents: ['bias'],
    automaticSend: false,
  }
Customize the Widget interface
Prop / ParamDefault valueTypeDescription
titleColor#fffcolorSet the widget Title color
subtitleColor#fffcolorSet the widget Subtitle color
headerBackgroundColor#003a9bcolorSet the widget Header background-color
launcherColor#003a9bcolorSet the widget Launcher color
chatBackgroundColor#fffcolorSet the widget Chat background-color
inputBackgroundColor#f4f7f9colorSet the widget Input background-color
inputFontColor#000colorSet the widget Input font color
inputPlaceholderColor#b5b5b5colorSet the widget Input placeholder color
userMessageBubbleColor#003a9bcolorSet the user message bubble color
userMessageTextColor#fffcolorSet the user message text color
botMessageBubbleColor#f4f7f9colorSet the bot message bubble color
botMessageTextColor#000colorSet the bot message text color
widgetHeight65vhdimensionSet the widget Height
widgetWidth370pxdimensionSet the widget Width
launcherHeight60pxdimensionSet the Launcher Height
launcherWidth60pxdimensionSet the Launcher Width
quickRepliesFontColor#0084ffcolorSet the Quick-Replies font color
quickRepliesBackgroundColornonecolorSet the Quick-Replies background-color
quickRepliesBorderColor#0084ffcolorSet the Quick-Replies border color
quickRepliesBorderWidth1pxdimensionSet the Quick-Replies border width
fullScreenBotMessageBubbleColorrgba(0,0,0,0)colorSet the bot message bubble color on fullscreen mode
suggestionsBackgroundColor#edf1f3colorSet the suggestions container background color
suggestionsSeparatorColor#e6e6e6colorSet suggestions line separator color
suggestionsFontColorgreycolorSet the suggestions font color
suggestionsHoverFontColor#03348acolorSet the suggestions font color

Other features

Tooltip

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 (/hello to Push. If text is specified, it will be displayed as a user message. If not specified, no user message will be displayed
WebChat.reload()Reload the chat widget
WebChat.clear()Clear the chat conversation and remove it from storage

Backends

The widget can be used with Push socket server. Contact contato@ilhasoft.com.br if you want to use it.

Push

The Push Webchat was forked from an open source project rasa-webchat team and it works with Push.

Sending messages from the backend to the chat widget
sending plain text
emit(session_id, {"text": "hello"})
sending quick replies
message = {
  "text": "Happy?",
  "quick_replies":[
    {"title":"Yes", "payload":"/affirm"},
    {"title":"No", "payload":"/deny"}
  ]}
emit(session_id, message)
sending a link Snippet

Admittedly a bit far fetched, thinking that Snippets would evolve to carousels of generic templates :)

message = {
  "attachment":{
    "type":"template",
    "payload":{
      "template_type":"generic",
      "elements":[
        {
          "title":"Title",
          "buttons":[ {
            "title":"Link name",
            "url": "http://link.url"
          }
        ]
      }
    ]
  }
}
}
emit(session_id, message)
sending a Video Message
message = {
  "attachment":{
    "type":"video",
    "payload":{
      "title":"Link name",
      "src": "https://www.youtube.com/watch?v=f3EbDbm8XqY"
    }
  }
}
emit(session_id, message)
sending an Image Message
message = {
      "attachment":{
        "type":"image",
        "payload":{
          "title":"Link name",
          "src": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT_IX5FSDQLrwm9qvuXu_g7R9t_-3yBSycZ8OxpRXvMDaTAeBEW"
        }
      }
    }
emit(session_id, message)
sending a message with custom data
message = {
      "data":{
        "customField1": 'anything you want',
        "customField2": 'other custom data, 
      }
    }
emit(session_id, message)
sending a message to be displayed as a tooltip

The prop tooltipMessage will define what message should be displayed as a tooltip.

The prop tooltipDelay lets you set a delay before calling the payload. It default to 500ms.

message = {
  "text": "Hi!",
  "metadata":{
    "tooltip": true
   }
 }
emit('bot_uttered', message, room=socket_id)

Styles

hierarchy:

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

Distributing

If you want to fork this repository to create your own distribution, you can reuse some build files and enable Google Cloudbuild to do so. Just follow these steps:

  1. Create a project in Google Cloud Console and follow these instructions;
  2. Once you've connected and selected the right project in your terminal for Google Cloud project, just setup your npm user by running npm adduser;
  3. Update package.json file and cloudbuild.yaml with proper name of your app;
  4. Once everything is ready, you can just run gcloud builds submit --config=cloudbuild.yaml --substitutions=TAG_NAME="replace_with_tag_name"

Contributors

@Ilhasoft @johncordeiro @PHLF @znat @TheoTomalty @Hub4IT @dliuproduction @MatthieuJnon @mofortin

2.14.0

8 days ago

2.13.0

11 months ago

2.12.0

1 year ago

2.11.0

2 years ago

2.10.1

2 years ago

2.10.2

2 years ago

2.10.0

2 years ago

2.9.0

2 years ago

2.8.1

2 years ago

2.8.0

2 years ago

2.8.2

2 years ago

2.6.0

2 years ago

2.5.0

2 years ago

2.5.1

2 years ago

2.3.1

3 years ago

2.0.0

3 years ago

1.8.0

3 years ago

1.7.2

4 years ago

1.7.1

4 years ago

1.7.0

4 years ago

1.6.0

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.7.37

4 years ago

0.7.36

4 years ago

0.7.35

4 years ago

0.7.34

4 years ago

0.7.33

4 years ago

0.7.32

4 years ago

0.7.31

4 years ago

0.7.30

4 years ago

0.7.29

4 years ago

0.7.28

4 years ago

0.7.27

4 years ago

0.7.26

4 years ago

0.7.25

4 years ago

0.7.24

4 years ago

0.7.23

4 years ago