πŸ› οΈClient SDK

Client-side SDK functions for controlling the Wonderchat widget

The Wonderchat Client SDK provides JavaScript functions that allow you to programmatically control the chat widget on your website. These functions are available through the wonderchat object on the window object after the Wonderchat widget script is loaded.

Prerequisites

Before using these SDK functions, ensure that the Wonderchat widget script is properly embedded on your website. The functions will only be available after the widget has fully loaded.

Available Functions

wonderchat.changeChatbotId()

Changes the current chatbot displayed in the widget. This is useful when you want to dynamically switch between different chatbots based on user actions or page context.

Syntax:

wonderchat.changeChatbotId(chatbotId);

Parameters:

  • chatbotId (string, required): The ID of the chatbot you want to switch to

Example:

// Switch to a different chatbot
wonderchat.changeChatbotId("your-new-chatbot-id");

// Example: Switch chatbot based on user selection
function switchToSupportBot() {
  wonderchat.changeChatbotId("support-bot-123");
}

function switchToSalesBot() {
  wonderchat.changeChatbotId("sales-bot-456");
}

wonderchat.toggleChat()

Controls the open/closed state of the chat widget. Can be used to programmatically open or close the chat, or toggle between states.

Syntax:

Parameters:

  • show (boolean, optional):

    • true: Opens the chat widget

    • false: Closes the chat widget

    • If omitted: Toggles the current state

Examples:

wonderchat.identify()

Identifies the user for the current chat session. This helps in tracking conversations and providing personalized support.

Syntax:

Parameters:

  • params (object, required): An object containing user identification data

    • email (string, optional): The user's email address

    • phoneNumber (string, optional): The user's phone number

    • name (string, optional): The user's name

    • custom (object, optional): An object containing any arbitrary custom identifiers

Examples:

wonderchat.clearHistory()

Clears the current chat history and starts a new chat session. This function removes all previous messages from the conversation and initializes a fresh chat session with the chatbot.

Syntax:

Parameters:

  • None

Examples:

wonderchat.prefillQuestion()

Programmatically prefills a question in the chatbot's input field for the user to send. This allows you to guide users by suggesting relevant questions based on the page context or user actions.

Syntax:

Parameters:

  • question (string, required): The text to prefill in the chatbot's input field

Examples:

Note: The prefilled question appears in the input field but is not automatically sent. Users need to click send or press Enter to submit the question.

Best Practices

  1. Wait for Widget Load: Ensure the Wonderchat widget script is loaded and ready before calling SDK functions. The script must be embedded on your page first, then use the chatbotLoaded callback:

Last updated

Was this helpful?