KustomerCore.addListener()

Adds a callback function that is run after a specific event

πŸ‘

First, initialize chat

You must run KustomerCore.init() before you can execute any additional KustomerCore methods.

Examples

We're provided some examples of how you can call KustomerCore.addListener():

const onMessageReceivedHandler = (response, error) => {
  console.log(response);
}

KustomerCore.addListener('onMessageReceived', onMessageReceivedHandler);

πŸ“˜

Remove extra listeners 🧹

Remove any extra listeners you no longer need with KustomerCore.removeListener() to clear up your memory, just as you would for a native window.addEventListener listener.

Syntax

Kustomer.addListener(event, function(callbackResponse, error))

Parameter

Type

Description

event

String

Required

The name of the event you want to add a listener to, for example, onConversationCreate, onMessageReceived, etc...

function(callbackResponse, error)

Function

Required

The function you want to run after the event is triggered.

callbackResponse is either null or a value, depending on what event you are hooking onto. See the list of events below for the different structures.

error is either undefined or a native JavaScript Error object.

event types

onConversationCreate

Hook for after a conversation is created.

// callbackResponse

{
  conversationId: String,
  createdAt: String,
  ended: Boolean,
  isInAssistantMode: Boolean
}

onConversationEnded

Hook for when a conversation is ended.

// callbackResponse

{
  conversationId: String,
  createdAt: String,
  ended: Boolean
}

onConversationUnended

Hook for when a conversation is changed from ended to not ended anymore. The callbackResponse structure is the same as for onConversationEnded.

onAssistantEnded

Hook for when a conversation has ended assistant mode.

// callbackResponse

{
  conversationId: String,
  isInAssistantMode: Boolean,
}

onSatisfactionReceived

Hook for when a conversation receives a satisfaction form.

// callbackResponse

{
  conversationId: String,
  satisfactionId: String,
  timetoken: String,
  form: {
    description: String,
    introduction: String,
    name: String,
    followupQuestion: String,
    ratingPrompt: String,
    scale: {
      options: String,
      labelLow: String,
      labelHigh: String,
      type: String,
    },
  },
  response: {
    rating: Number,
    followupAnswer: String,
  },
}

onLogin

Hook for after the user is authenticated with Kustomer.login().

// callbackResponse

{
  identified: Boolean,
  email: String | undefined,
  externalId: String | undefined,
  customerId: String,
}

onLogout

Hook for after the user is logged out with Kustomer.logout().

onMessageReceived

Hook for when a message was successfully received

// callbackResponse

{
  messageId: String,
  body: String,
  createdAt: String,
  direction: String,
  conversationId: String,
  attachments: Array,
  meta: {
    template: Object,
    articles: Array
  },
  sentBy: {
    type: String
    id: String,
    displayName: String,
    avatarUrl: String
  },
}

onUnreadCountChange

Hook for when the unread count changes.

// callbackResponse

{
  total: Number,
  change: {
    conversationId: String,
    count: Number,
  },
}

onAgentTypingActivity

Hook for when Agent starts typing

// callbackResponse

{
  conversationId: String,
  user: {
    avatarUrl: String,
    displayName: String,
  },
  typing: Boolean
}

Did this page help you?