2.9.4 • Published 1 month ago

dialogshift-webchat-sdk v2.9.4

Weekly downloads
1,067
License
MIT
Repository
github
Last release
1 month ago

Dialogshift Webchat SDK

npm version

A client library for embed Dialogshift webchat to webpages. Written in TypeScript and published in UMD and ES2015.

SDK Demo

Table of Contents

About

Dialogshift is a Conversational AI platform that helps businesses to improve communication with their customers, enhance the customer experience and ultimately grow revenue through customer retention and automation.

Messaging is an intuitive communication tool and has the ability to bring brands much closer to their customers, increasing the engagement through more frequent 1:1 touchpoints.

This SDK allows to embedding Dialogshift webchat to webpages, customize and control chat widgets, change conversational flow as you need, communicate with Conversational AI platform. Webchat widgets are fully responsive for desktop, tablet or mobile pages. SDK has native JavaScript and TypeScript versions.

How it works

SDK and chat workflow phases.

Loading scripts

Starts when js and css scripts are injected into a webpage. After scripts are loaded SDK is waiting for initialization.

SDK initialization

Starts when user calls Dialogshift.instance(...options) for the first time.

  1. SDK loads Webconfig with settings, custom css, custom options.
  2. Renders toggle button and other widgets on webpage.
  3. Fires event init. User can read chat config or work with widgets.

Chat loading

Starts when chat window is manually or programmatically opened for the first time.

  1. SDK loads chat app inside iframe.
  2. Chat establishes a connection with a message layer.
  3. Chat loads message history and trigger an initial message if needs.
  4. Fires event ready.

Chat destroying

Starts when Dialogshift.destroy() is called.

  1. SDK unbinds all events, removes chat iframe and widgtes from a webpage.
  2. Fires event destroy.

Getting started

You can install SDK using npm or use CDN link directly. To obtain app id signup and copy id from Member area.

Replace %id% in snippets below with your app id and initialize Dialogshift chat instance. Your app will interact with the chat client through the instance Dialogshift.instance(), which will available in your scope.

Install from npm

Install package

npm i dialogshift-webchat-sdk --save

Import and initialize chat instance

import * as Dialogshift from 'dialogshift-webchat-sdk'
import 'dialogshift-webchat-sdk/bundles/dialogshift-webchat-sdk.min.css'

const chat = Dialogshift.instance({
  id: '%id%',
})

Include from CDN

Add the following code towards the end of <head> section of your page.

<script
  type="text/javascript"
  src="https://cdn.dialogshift.com/sdk/latest/dialogshift-webchat-sdk.umd.js"></script>

<link
  rel="stylesheet"
  type="text/css"
  href="https://cdn.dialogshift.com/sdk/latest/dialogshift-webchat-sdk.min.css"/>

Add the following code towards the end of <body> section of your page.

<script type="text/javascript">
  var chat = Dialogshift.instance({
    id: '%id%',
  })
</script>

API methods

instance(chatConfig config): ChatInstance

Creates a new one chat instance or returns previously created instance. Returns singleton instance.

chatConfig
PropertyTypeDescription
idstringClient id obtained from the application dashboard.
locale?stringChat locale. Defaults to en.
position?'left' | 'right'Chat position on webpage. Defaults to right.
isChatboxVisible?booleanShow chat window expanded if true. Defaults to false.
isButtonVisible?booleanShow toggle button if true. Defaults to true.
renderButton?booleanRender toggle button if true. If button is not rendered show or hide it later is impossible. Defaults to true.
initialElement?stringTrigers initial message.
unreadCounter?numberAmount of unread messages.
theme?'round' | 'tile'UI theme. Defaults to round.
context?objectContext variables for visitor.

First time initialization.

const client = Dialogshift.instance({
  id: '%id%',
  locale: 'de',
  position: 'left',
  initialElement: 'welcome-message',
  unreadCounter: 2,
  context: {
    channel: 'pwa-ibe',  // pwa-ibe (Booking Engine), pwa-guestapp (Generic guest-app), for additional codes contact support@dialogshift.com
    email: 'foo@bar.baz',
    name: 'John Doe'
  }
})

// Returns the same instance

console.log(client === Dialogshift.instance()) // true

destroy()

Destroys current instance.

Dialogshift.instance().destroy()

isDestroyed(): boolean

Returns true if chat is destroyed.

Dialogshift.instance().isDestroyed()

on(string eventName, function handler)

Listen on a new event by type and handler. The handler will not be listened if it is a duplicate.

Dialogshift.instance().on('ready', () => {
  console.log('Chat is ready to send messages')
})

once(string eventName, function handler)

Listen on a once event by type and handler. The handler will not be listened if it is a duplicate.

Dialogshift.instance().once('chatbox.show', () => {
  console.log('Chat is opened for the first time')
})

off(string eventName?, function handler?)

Listen off an event by type and handler. Or listen off events by type, when if only type argument is passed. Or listen off all events, when if no arguments are passed.

Dialogshift.instance().off('chatbox.show')

//OR

Dialogshift.instance().off()

offAll()

Listen off all events.

Dialogshift.instance().offAll()

showChatbox()

Show chatbox.

Dialogshift.instance().showChatbox()

hideChatbox()

Hide chatbox window.

Dialogshift.instance().hideChatbox()

showButton()

Show toggle button.

Dialogshift.instance().showButton()

hideButton()

Hide toggle button.

Dialogshift.instance().hideButton()

setButtonText(string text)

Change toggle button text. text could be an empty string.

Dialogshift.instance().setButtonText('Help')

// OR to remove button text

Dialogshift.instance().setButtonText('')

showTeaser()

Show teaser.

Dialogshift.instance().showTeaser()

hideTeaser()

Hide teaser.

Dialogshift.instance().hideTeaser()

setTeaserText(string text)

Change teaser text.

Dialogshift.instance().setTeaserText('👋🏻 Hi, can I help you?')

setPosition('left' | 'right' position)

Change chat container position regarding left or right of the window.

Dialogshift.instance().setPosition('left')

isChatboxVisible(): boolean

Returns true if chatbox window is opened.

Dialogshift.instance().isChatboxVisible()

setContext(string key, string | object value): Promise

Send additional context information to the chat instance.

Dialogshift.instance()
  .setContext('currentUser', 'John Doe')
  .then(() => {
    console.log('Context is written')
  })

// OR rewrite previous variable

Dialogshift.instance()
  .setContext('currentUser', 'Jane Doe')
  .then(() => {
    console.log('Context is overwritten')
  })

// OR write object

Dialogshift.instance()
  .setContext('currentUser', {
    firstName: 'John',
    lastName: 'Doe',
  })
  .then(() => {
    console.log('Context is written')
  })

getContext(string key): Promise

Returns context variable.

Dialogshift.instance().setContext('visitor', {
  firstName: 'John',
  lastName: 'Doe',
})

// ...

Dialogshift.instance()
  .getContext('visitor')
  .then(visitor => {
    console.log(visitor.firstName) // John
    console.log(visitor.lastName) // Doe
  })

// OR returns null if context is not setted

Dialogshift.instance()
  .getContext('user')
  .then(user => {
    console.log(user === null) // true
  })

setUnreadCounter(number amout)

Set value to unread counter widget. If amount = 0 widget will be hidden.

Dialogshift.instance().setUnreadCounter(2)

// OR to hide counter

Dialogshift.instance().setUnreadCounter(0)

increaseUnreadCounter()

Increase unread counter widget value.

Dialogshift.instance().increaseUnreadCounter()

getConfig(): Config

Returns chat config.

const config = Dialogshift.instance().getConfig()

triggerElement(options: TriggerElementOptions)

Triggers answer from chatbot content.

TriggerElementOptions

NameTypeDescription
successorstringConversational element to trigger.
showChatbox?booleanIf true chat window will be opened. Default to true
suppressInitialElement?booleanIf true initial element will be suppressed. Default to false
Dialogshift.instance().triggerElement({
  successor: 'welcome-message',
  suppressInitialElement: true,
})

setInitialElement(element: InitialElement)

Set a initial element.

Dialogshift.instance().setInitialElement({
  successor: 'welcome-1',
  suppress: false,
})

InitialElement

NameTypeDescription
successorstringInitial message to trigger
suppressbooleanIf true initial element will be suppressed. Default to false

getInitialElement(): InitialElement

Returns current initial element.

Dialogshift.instance().getInitialElement()

setTheme(theme: AppTheme): InitialElement

Set an UI theme.

Dialogshift.instance().setTheme('tile')

AppTheme

NameTypeDescription
roundstringUI theme
tilestringUI theme

isReady(): boolean

Returns true if chat is fully loaded and ready to communicate.

Dialogshift.instance().isReady()

setActionButtons(buttons: ActionButton[])

Remove current action buttons and render new.

Dialogshift.instance().setActionButtons([{
  type: 'quickreply',
  successor: 'welcome-back',
  de: {
    title: 'Welcome back',
  },
  en: {
    title: 'Welcome back',
  },
}])

ActionButton

NameTypeDescription
typestringType of action. Possible values quickreply.
successorstringMessage to trigger
deobjectButton title for DE locale
enobjectButton title for EN locale

showActionButtons()

Show action buttons.

Dialogshift.instance().showActionButtons()

hideActionButtons()

Hide action buttons.

Dialogshift.instance().hideActionButtons()

setLocale(locale: string)

Changes current locale, updates widget content.

Dialogshift.instance().setLocale('de')

Events

You can subscribe to events to receive callbacks when events happen. Bind and unbind methods described in section API Methods.

NamePayloadDescription
initFires once when the chat DOM is ready, widgets are rendered and chat config is loaded. You can call API methods but can't send messages because chat is not connected.
readyFires once when the chat DOM is ready, configuration is loaded and chat connected to conversational channel. You can send messages. Mind that chat connects to conversational channel only after first open.
chatbox.show.beforeFires before the chat window is shown.
destroyFires once when the chat is destroyed.
chatbox.showFires whenever the chat window is shown.
chatbox.hide.beforeFires before the chat window is hidden.
chatbox.hideFires whenever the chat window is hidden.
button.show.beforeFires before the toggle button is shown.
button.showFires whenever the toggle button is shown.
button.hide.beforeFires before the toggle button is hidden.
button.hideFires whenever the toggle button is hidden.
message.sentRequestModelFires whenever a visitor sent message.
message.receiveMessageModelFires whenever a visitor received message.
command.receiveCommandModelFires whenever a visitor received command.

Event init example.

const client = Dialogshift.instance()

client.on('init', () => {
  console.log('Widgets are rendered and webconfig is loaded')

  client.showChatbox()
})

Event ready example.

const client = Dialogshift.instance()

client.on('ready', () => {
  console.log('SDK connected to a channel')

  client.triggerElement({
    successor: 'welcome-message',
  })
})

Event chatbox.show example.

const client = Dialogshift.instance()

client.on('chatbox.show.before', () => {
  console.log('Chat window is going to be shown')
})

client.on('chatbox.show', () => {
  console.log('Chat window shown')
})

Event message.sent example.

const client = Dialogshift.instance()

client.on('message.sent', event => {
  const requestModel = event.data

  if (requestModel.requestType === 'text') {
    console.log('The visitor sent message: ' + requestModel.text)
  }
})

RequestModel

NameDescription
requestTypeType of the sent message. Possible values command, text, button, feedback, trigger.

RequestModel contains different fields correspond to requestType.

Event message.receive example.

const client = Dialogshift.instance()

client.on('message.receive', event => {
  const messageModel = event.data
  console.log(messageModel)
  console.log('The visitor got message')
})

MessageModel

NameTypeDescription
datetimedatetimeTime in a datetime format.
fromHistorybooleanSource of the message.
isLiveModebooleantrue if user got the message from a operator.
elementTypestringType of element inside the message. Possible values feedback, text, button, carousel, list,book, dateRange,image.

MessageModel contains different fields correspond to elementType type.

Event command.receive example.

const client = Dialogshift.instance()

client.on('command.receive', event => {
  const commandModel = event.data

  if (commandModel.commandType === 'setLanguage') {
    console.log(
      'The visitor got command to change locale to ' + commandModel.lg
    )
  }
})

CommandModel

NameTypeDescription
commandTypestringType of command. Possible values url, setLanguage, typingIndicatorOn,typingIndicatorOff, livechat,log, actionBroker, setTeaserText.

CommandModel contains different fields correspond to commandType type.

Getting help

Please use our Github issue tracker for questions, feature requests, suggestions, bug reports or any kind of feedback. Or email us to support@dialogshift.com

Resources

SDK Demo

https://www.dialogshift.com

SDK NPM page

2.9.4

1 month ago

2.9.3-develop.0

5 months ago

2.9.3

5 months ago

2.3.17-develop.0

3 years ago

2.3.15-develop.0

3 years ago

2.3.16-stage.0

3 years ago

2.3.16

3 years ago

2.3.14-stage.0

3 years ago

2.3.14

3 years ago

2.3.14-develop.0

3 years ago

2.3.12-stage.0

3 years ago

2.3.12

3 years ago

2.3.9-develop.0

3 years ago

2.3.11-stage.0

3 years ago

2.3.10-develop.0

3 years ago

2.3.8

3 years ago

2.3.9

3 years ago

2.3.8-develop.0

3 years ago

2.3.11-develop.0

3 years ago

2.3.9-stage.0

3 years ago

2.3.10-stage.0

3 years ago

2.3.8-stage.0

3 years ago

2.3.11

3 years ago

2.3.10

3 years ago

2.3.6

3 years ago

2.3.7-stage.0

3 years ago

2.3.7

3 years ago

2.3.7-develop.0

3 years ago

2.3.6-develop.0

3 years ago

2.3.6-stage.0

3 years ago

2.3.5

3 years ago

2.3.5-stage.0

3 years ago

2.3.5-develop.0

3 years ago

2.3.4-develop.0

3 years ago

2.3.4

3 years ago

2.3.3

3 years ago

2.3.3-develop.0

3 years ago

2.3.4-stage.0

3 years ago

2.3.3-stage.0

3 years ago

2.3.2-stage.0

3 years ago

2.3.1

3 years ago

2.3.1-stage.0

3 years ago

2.3.1-develop.0

3 years ago

2.3.2-develop.0

3 years ago

2.3.0

3 years ago

2.3.0-stage.0

3 years ago

2.3.0-develop.0

3 years ago

2.2.9-develop.0

3 years ago

2.2.9-stage.0

3 years ago

2.2.9

3 years ago

2.2.8

3 years ago

2.2.8-stage.0

3 years ago

2.2.8-develop.0

3 years ago

2.2.7-stage.0

3 years ago

2.2.7

3 years ago

2.2.7-develop.0

3 years ago

2.2.6-stage.0

3 years ago

2.2.6

3 years ago

2.2.6-develop.0

3 years ago

2.2.5

4 years ago

2.2.5-stage.0

4 years ago

2.2.4-stage.0

4 years ago

2.2.4

4 years ago

2.2.5-develop.0

4 years ago

2.2.4-develop.0

4 years ago

2.2.3-develop.0

4 years ago

2.2.3

4 years ago

2.2.3-stage.0

4 years ago

2.2.2

4 years ago

2.2.2-develop.0

4 years ago

2.2.2-stage.0

4 years ago

2.2.1

4 years ago

2.2.1-develop.0

4 years ago

2.2.1-stage.0

4 years ago

2.2.0-stage.0

4 years ago

2.2.0-develop.0

4 years ago

2.1.1-stage.0

4 years ago

2.1.1

4 years ago

2.1.1-develop.0

4 years ago

2.1.0-stage.0

4 years ago

2.1.0-develop.0

4 years ago

2.1.0

4 years ago

2.0.8-stage.0

4 years ago

2.0.7-stage.0

4 years ago

2.0.8-develop.0

4 years ago

2.0.7-develop.0

4 years ago

2.0.6

4 years ago

2.0.6-stage.0

4 years ago

2.0.6-develop.0

4 years ago

2.0.5

4 years ago

2.0.5-stage.0

4 years ago

2.0.5-develop.0

4 years ago

2.0.4-stage.0

4 years ago

2.0.4-develop.0

4 years ago

2.0.3-stage.0

4 years ago

2.0.3-develop.0

4 years ago

2.0.2

4 years ago

2.0.1-develop.0

4 years ago

2.0.1-stage.0

4 years ago

2.0.1

4 years ago

2.0.0-stage.1

4 years ago

2.0.0

4 years ago

1.9.1-develop.1

4 years ago

2.0.0-stage.0

4 years ago

1.9.1

4 years ago

1.9.1-stage.0

4 years ago

1.9.1-develop.0

4 years ago

1.9.0

4 years ago

1.9.0-stage.0

4 years ago

1.9.0-develop.0

4 years ago

1.8.0

4 years ago

1.8.0-stage.0

4 years ago

1.8.0-develop.0

4 years ago

1.7.0-stage.0

4 years ago

1.7.0-develop.0

4 years ago

1.7.0

4 years ago

1.6.0

4 years ago

1.6.0-stage.0

4 years ago

1.6.0-develop.0

4 years ago

1.5.0

4 years ago

1.5.0-stage.0

4 years ago

1.5.0-develop.0

4 years ago

1.4.6

4 years ago

1.4.6-stage.1

4 years ago

1.4.6-develop.0

4 years ago

1.4.5

4 years ago

1.4.2-develop.0

4 years ago

1.4.2-stage.0

4 years ago

1.4.3

4 years ago

1.4.2

4 years ago

1.4.1-develop.0

4 years ago

1.4.1

4 years ago

1.4.1-stage.0

4 years ago

1.4.0-stage.0

4 years ago

1.4.0-develop.0

4 years ago

1.4.0

4 years ago

1.3.0-stage.0

4 years ago

1.3.0

4 years ago

1.3.0-develop.0

4 years ago

1.2.6

4 years ago

1.2.6-stage.0

4 years ago

1.2.6-develop.0

4 years ago

1.2.5

4 years ago

1.2.5-stage.1

4 years ago

1.2.5-develop.1

4 years ago

1.2.4

4 years ago

1.2.4-stage.0

4 years ago

1.2.4-develop.0

4 years ago

1.2.3-develop.0

4 years ago

1.2.3

4 years ago

1.2.3-stage.0

4 years ago

1.2.2

4 years ago

1.2.2-develop.0

4 years ago

1.2.2-stage.0

4 years ago

1.2.1

4 years ago

1.2.1-stage.0

4 years ago

1.2.1-develop.0

4 years ago

1.1.1-develop.1

4 years ago

1.1.1-stage.1

4 years ago

1.1.0-stage.0

4 years ago

1.1.0-develop.0

4 years ago

1.0.0

4 years ago

1.0.0-stage.0

4 years ago

1.0.0-develop.0

4 years ago

0.5.3

5 years ago

0.6.0-develop.0

5 years ago

0.6.0-stage.0

5 years ago

0.6.0

5 years ago

0.5.2

5 years ago

0.5.2-stage.0

5 years ago

0.5.2-develop.0

5 years ago

0.4.2

5 years ago

0.4.2-stage.0

5 years ago

0.4.2-develop.0

5 years ago

0.4.1

5 years ago

0.4.1-stage.0

5 years ago

0.4.1-develop.0

5 years ago

0.4.0

5 years ago

0.4.0-stage.0

5 years ago

0.4.0-develop.0

5 years ago

0.3.0-stage.0

5 years ago

0.3.0-develop.0

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.2.0-stage.0

5 years ago

0.2.0-develop.0

5 years ago

0.1.0

5 years ago

0.1.0-stage.1

5 years ago

0.1.0-develop.1

5 years ago

0.1.0-stage.0

5 years ago

0.1.0-develop.0

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.20-stage.0

5 years ago

0.0.20-develop.0

5 years ago

0.0.19

5 years ago

0.0.18-stage.0

5 years ago

0.0.18-develop.0

5 years ago

0.0.18

5 years ago

0.0.18-stage

5 years ago

0.0.17

5 years ago

0.0.17-stage.0

5 years ago

0.0.17-develop.0

5 years ago

0.0.16-stage.3

5 years ago

0.0.16-stage.2

5 years ago

0.0.16-develop.0

5 years ago

0.0.16-stage.1

5 years ago

0.0.16-stage.0

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.13-stage.0

5 years ago

0.0.13-develop.0

5 years ago

0.0.12-develop.0

5 years ago

0.0.12-stage.0

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10-stage.0

5 years ago

0.0.10-develop.0

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago