How to set up a launch message on my website bot?

You can launch the Bot with a task or message on behalf of the user. Haptik provides the following methods for doing so.

launchTask(taskId, callback)

Launches the SDK and automatically sends specified taskId as a user message (The same message that would be sent if the user tapped on that task)

Params:

  • taskId <Number>The task id is specified in the tasks section of the bot builder (business manager)
  • callback <Function>(optional)The callback executes once the call completes. The callback function is executed with success the boolean flag as a parameter indicating whether the call was completed or failed.

launchMessage(message, callback, hidden)

Launches the SDK and automatically sends a specific message as a user message. It can optionally be sent as a hidden user message

launchMessage() that opens the bot automatically on page load.

Params:

  • message <String>The message that is to be sent
  • callback <Function>(optional)The callback executes once the call completes. The callback function is executed with success boolean flag as a parameter indicating whether the call was completed or failed.
  • hidden <Boolean>Whether to send the message as a hidden user message. A hidden message is not visible to the user but can be seen by the agent or bot

Sample snippet

The following snippet is an example of how the launch message is sent to the bot. There needs to be a start step created on the bot which will get triggered once the launch message is sent.

The example is for an eCommerce bot, here "Track my order" is the default message that is sent to the bot. "Track my order" should trigger a start step on the bot to start a new conversation.

<html>
    <head>
    <title>Haptik JS SDK Integration Demo</title>
    <meta charset="UTF-8" />
    <meta content="width=device-width; initial-scale=1.0;" name="viewport" />
    </head>
    <body>
    ...Some HTML Code Snippet...
    <script
    type="text/javascript"
    charset="UTF-8"
    src="https://toolassets.haptikapi.com/platform/javascript-xdk/production/loader.js"
    ></script>
<script>
    window.haptikInitSettings = {
    "business-id": <View Credentials>, ##Click on "View Credentials" and get the required values
    "client-id": <View Credentials>, ##Make sure to have the client-id in double quotes as its a string
    "base-url": "https://staging.hellohaptik.com/",
    };
    document.addEventListener('haptik_sdk', function () {
    HaptikSDK.launchMessage("Track My Order",callbackFunction(),false);
    function callbackFunction(){
    }});
    </script>
    </body>
    </html>

When the launch message is sent every time when the user clicks on the Lets chat button -

Declare a boolean global variable "isMessageSent" and add an if condition that only if the variable value is false, the launch message method will be called and then set the variable value as true. This way for a particular session, we can send the launch message just once.

If we don't have the button click, and the user is a logged-in user, the best way is to call the launch message inside the custom sign-up method only when the custom sign-up is successful.

If we don't have a button click and the user is a guest user, then call the launch message directly below the init settings.

Can’t find what you’re looking for? Email us at support@haptik.ai.