Skip to content

Latest commit

 

History

History
672 lines (496 loc) · 14.5 KB

api-reference.md

File metadata and controls

672 lines (496 loc) · 14.5 KB

API Reference

Welcome to the tawk.to JavaScript API documentation.

The API provides a flexible set of methods that can be used in your web projects. To invoke one of the methods below, please be sure to call a method after the embed code on your page.

Use the JavaScript API to manipulate the chat widget displayed on your website.

Table of contents


onLoad

Callback function invoked right after the widget is rendered. This callback is not supported in pop out chat window.

this.TawkMessengerAngular.onLoad(() => {
	// place your code here
});

onStatusChange

Callback function invoked when the page status changes. The function will receive the changed status which will be either online, away or offline. This callback is not supported in pop out chat window.

this.TawkMessengerAngular.onStatusChange((status?: string) => {
	// place your code here
});

onBeforeLoad

Callback function invoked right when Tawk_API is ready to be used and before the widget is rendered. This callback is not supported in pop out chat window.

this.TawkMessengerAngular.onBeforeLoad(() => {
	// place your code here
});

onChatMaximized

Callback function invoked when the widget is maximized. This callback is not supported in pop out chat window.

this.TawkMessengerAngular.onChatMaximized(() => {
	// place your code here
});

onChatMinimized

Callback function invoked when the widget is minimized. This callback is not supported in pop out chat window.

this.TawkMessengerAngular.onChatMinimized(() => {
	// place your code here
});

onChatHidden

Callback function invoked when the widget is hidden. This callback is not supported in pop out chat window.

this.TawkMessengerAngular.onChatHidden(() => {
	// place your code here
});

## onChatStarted Callback function invoked when the widget is started.
this.TawkMessengerAngular.onChatStarted(() => {
	// place your code here
});

## onChatEnded Callback function invoked when the widget is ended. This callback is not supported in pop out chat window.
this.TawkMessengerAngular.onChatEnded(() => {
	// place your code here
});

onPrechatSubmit

Callback function invoked when the Pre-Chat Form is submitted. The submitted form data is passed to the function. This callback is not supported in pop out chat window.

this.TawkMessengerAngular.onPrechatSubmit((data?: any) => {
	// place your code here
});

onOfflineSubmit

Callback function invoked when the Offline form is submitted. The submitted form data is passed to the function. Form data will contain {name : ”, email : ”, message : ”, questions : []}. This callback is not supported in pop out chat window.

this.TawkMessengerAngular.onOfflineSubmit((data?: any) => {
	// place your code here
});

onChatMessageVisitor

Callback function invoked when message is sent by the visitor. The message is passed to the function. This callback is not supported in pop out chat window.

this.TawkMessengerAngular.onChatMessageVisitor((message?: any) => {
	// place your code here
});

onChatMessageAgent

Callback function invoked when message is sent by the agent. The message is passed to the function. This callback is not supported in pop out chat window.

this.TawkMessengerAngular.onChatMessageAgent((message?: any) => {
	// place your code here
});

onChatMessageSystem

Callback function invoked when message is sent by the system. The message is passed to the function. This callback is not supported in pop out chat window.

this.TawkMessengerAngular.onChatMessageSystem((message?: any) => {
	// place your code here
});

onAgentJoinChat

Callback function invoked when an agent joins the chat. The data is passed to the function. Will contain {name : ”, position : ”, image : ”, id : ”}. This callback is not supported in pop out chat window.

this.TawkMessengerAngular.onAgentJoinChat((data?: any) => {
	// place your code here
});

onAgentLeaveChat

Callback function invoked when an agent leaves the chat. The data is passed to the function. Will contain {name : ”, id : ”}. This callback is not supported in pop out chat window.

this.TawkMessengerAngular.onAgentLeaveChat((data?: any) => {
	// place your code here
});

onChatSatisfaction

Callback function invoked when an agent leaves the chat. The satisfaction is passed to the function. -1 = dislike | 0 = neutral | 1 = like. This callback is not supported in pop out chat window.

this.TawkMessengerAngular.onChatSatisfaction((satisfaction?: any) => {
	// place your code here
});

onVisitorNameChanged

Callback function invoked when the visitor manually changes his name. The visitorName is passed to the function. This callback is not supported in pop out chat window.

this.TawkMessengerAngular.onVisitorNameChanged((visitorName?: any) => {
	// place your code here
});

onFileUpload

Callback function invoked when a file is uploaded. The link to the uploaded file is passed to the function. This callback is not supported in pop out chat window.

this.TawkMessengerAngular.onFileUpload((link?: any) => {
	// place your code here
});

onTagsUpdated

Callback function invoked when a tag is updated.

this.TawkMessengerAngular.onTagsUpdated((data?: any) => {
	// place your code here
});

onUnreadCountChanged

Callback function invoked when active conversation unread count changed.

this.TawkMessengerAngular.onUnreadCountChanged();

onLoaded

Returns a value (true or undefined) indicating when the plugin is ready.

this.TawkMessengerAngular.onLoaded();

widgetPosition

Returns a string for current position of the widget.

this.TawkMessengerAngular.widgetPosition();

visitor

Object used to set the visitor name and email. Do not place this object in a function, as the values need to be available before the widget script is downloaded.

Setting or changing the values after the widget script has been downloaded will not send the values to the dashboard.

If the name and email will not be available on load time (eg single page app, ajax login), then use the setAttributes function instead.

this.TawkMessengerAngular.visitor({
	name : 'name',
	email : 'email@email.com'
});

maximize

Maximizes the chat widget.

<button [tawkMaximize]></button>

or

this.TawkMessengerAngular.maximize();

minimize

Minimizes the chat widget.

<button [tawkMinimize]></button>

or

this.TawkMessengerAngular.minimize();

toggle

Minimizes or Maximizes the chat widget based on the current state.

<button [tawkToggle]></button>

or

this.TawkMessengerAngular.toggle();

popup

Opens the chat widget as a pop out.

<button [tawkPopup]></button>

or

this.TawkMessengerAngular.popup();

getWindowType

Returns the current widget type whether it’s inline or embed.

this.TawkMessengerAngular.getWindowType();

showWidget

Shows the chat widget.

<button [tawkShowWidget]></button>

or

this.TawkMessengerAngular.showWidget();

hideWidget

Hide the chat widget.

<button [tawkHideWidget]></button>

or

this.TawkMessengerAngular.hideWidget();

toggleVisibility

Hides or Shows the chat widget based on the current visibility state.

<button [tawkHideWidget]></button>

or

this.TawkMessengerAngular.toggeVisibility();

getStatus

Returns the current page status (online, away or offline).

this.TawkMessengerAngular.getStatus();

isChatMaximized

Returns a boolean value (true or false) indicating whether the chat widget is maximized.

this.TawkMessengerAngular.isChatMaximized();

isChatMinimized

Returns a boolean value (true or false) indicating whether the chat widget is minimized.

this.TawkMessengerAngular.isChatMinimized();

isChatHidden

Returns a boolean value (true or false) indicating whether the chat widget is hidden.

this.TawkMessengerAngular.isChatHidden();

isChatOngoing

Returns a boolean value (true or false) indicating whether currently there is an ongoing chat.

this.TawkMessengerAngular.isChatOngoing();

isVisitorEngaged

Returns a boolean value (true or false) indicating whether the visitor is currently chatting or has requested a chat.

this.TawkMessengerAngular.isVisitorEngaged();

endChat

Ends the current ongoing chat.

this.TawkMessengerAngular.endChat();

setAttributes

Set custom metadata regarding this chat/visitor.

This function takes in two values: attribute and callback.

The attribute value is of the object data type, which is a key value pair.

The key is of the string data type and can contain only alphanumeric characters and ‘-‘ (dash).

You can also use this function to set the visitor name and email. However, you will need to enable the secure mode first and also supply the calculated hash value in this function.

Refer to the secure mode section below on how to do this.

The reason it needs to be in secure mode is to ensure data integrity — to ensure the value sent from the widget to the dashboard is true and has not been tampered with.

The callback, which is a function, will be invoked to notify whether the save failed.

Error messages returned:

  1. INVALID_ATTRIBUTES: No attributes were sent
  2. SESSION_EXPIRED: The visitor’s current session has expired
  3. SERVER_ERROR: Internal server error
  4. ACCESS_ERROR: Error in accessing the page
  5. ATTRIBUTE_LIMIT_EXCEEDED: Total custom attributes (excluding name, email and hash) is 50
  6. CONTAINS_INVALID_KEY: Custom key is not alphanumeric or dash (keys will be lower case)
  7. CONTAINS_INVALID_VALUE: Custom value is empty or the total length is more than 255 characters
this.TawkMessengerAngular.setAttributes(attribute, callback);

// Example

this.TawkMessengerAngular.setAttributes({
	id : '<id>',
	store : '<store>'
}, function(error) {
    // do something if error
});

this.TawkMessengerAngular.setAttributes({
	id : '<id>',
	store : '<store>'
	hash : '<hash value>'
}, function(error) {
    // do something if error
});

addEvent

Set a custom event to chat. This function takes in 3 values: event name, optional metadata and callback.

The event name is of the string data type and can contain only alphanumeric characters and ‘-‘ (dash)

The callback which is a function will be invoked to notify whether the save failed.

INVALID_EVENT_NAME, INVALID_ATTRIBUTES, ATTRIBUTE_LIMIT_EXCEEDED, CONTAINS_INVALID_KEY, CONTAINS_INVALID_VALUE, SESSION_EXPIRED, SERVER_ERROR

this.TawkMessengerAngular.addEvent(eventName, metaData, callback);

// Example

this.TawkMessengerAngular.addEvent(
	'requested-quotation',
	'',
	function(error) {
		// do something if error
	}
);

this.TawkMessengerAngular.addEvent(
	'requested-quotation',
	{
		sku : 'A0012',
		name : 'Jeans',
		price : '50'
	},
	function(error) {
		// do something if error
	}
);

addTags

Add tags to the chat. This function takes in two values; tags and callback. This is of the array data type. The content of the tags should be of the string data type.

The total number of tags is 10. The callback, which is a function, will be invoked to notify whether the save failed.

INVALID_TAGS, TAG_LIMIT_EXCEEDED, VERSION_CONFLICT, SESSION_EXPIRED, SERVER_ERROR

this.TawkMessengerAngular.addTags(tags, callback);

// Example

this.TawkMessengerAngular.addTags(
[
	'hello',
	'world'
], function(error) {
	// do something if error
});

removeTags

Remove tags from the chat. This function takes in two values: tags and callback. This is of the array data type. The content of the tags should be of the string data type.

The callback, which is a function, will be invoked to notify whether the save failed.

INVALID_TAGS, TAG_LIMIT_EXCEEDED, SESSION_EXPIRED, SERVER_ERROR

this.TawkMessengerAngular.removeTags(tags, callback);

// Example

this.TawkMessengerAngular.removeTags(
[
	'hello',
	'world'
],
function(error) {
	// do something if error
});

secureMode

Secure method is to ensure the data you are sending is actually from you.

To enable secure mode, embed following code on your page.

The hash is server side generated HMAC using SHA256, the user’s email and your site’s API key.

You can get your API key from Admin>Property Settings.

this.TawkMessengerAngular.visitor({
    name : 'name',
    email : 'email@email.com'
    hash : '<calculate-hash>'
});

customstyle

Object used to update the widget styling. Currently only supports zIndex style. Do not place this object in a function, as the values need to be available before the widget script is downloaded. Setting or changing the values after the widget script has been downloaded will not update the widget’s style.