Triggering messages from URL parameters

This tutorial explains how to configure your chat widget to automatically send a predefined message based on URL query parameters. When the chat loads, it injects the message that is extracted from the URL into the conversation.

How automatic triggering works

When the chat widget finishes loading, the script runs the following actions:

  • Waits a short period to help ensure that the chat widget finishes its initialization.

  • Reads query parameters from the current page URL, for example, https://yoursite.com?data=hello.

  • Automatically sends the extracted value as a chat message.

This technique is useful for pre‑populating conversations, deep‑linking to specific chat flows, or automating context-aware interactions.

Step 1: Query the data to send to the chat instance

Create the onChatLoad function to run after the chat widget fully loads. It does the following actions:

  • Uses a pageload flag to help ensure that automatic logic runs only once.

  • Waits 5 seconds to help ensure the chat widget UI is initialized.

  • Extracts the data query parameter from the URL by using the URLSearchParams API.

  • Sends the extracted value into the chat by using instance.send().

  • Stores the chat instance globally if you need it later.

function onChatLoad(instance) {
    
    let pageload = false;
    if (!pageload){
        setTimeout(() => {
            const params = new URLSearchParams(window.location.search);
            const data = params.get('data');
            if (data) {
                instance.send(data);
            }
        }, 5000)
        pageload = true;
    }
    chatInstance = instance;
}

Step 2: Test your script

Add the script to your webpage and include a data query parameter in the URL. For example:

https://mywebsite.com/deep-link?data=generate%20a%20sample%20email

After the chat widget loads, it automatically sends the following message through the instance.send() function.

generate a sample email

Full script

Use the following script to apply the code from this tutorial to your chat widget:

<script>
function onChatLoad(instance) {

    let pageload = false;

    if (!pageload) {
        setTimeout(() => {
            const params = new URLSearchParams(window.location.search);
            const data = params.get('data');
            if (data) {
                instance.send(data);
            }
        }, 10000)
        pageload = true;
    }
    chatInstance = instance;
}

window.wxOConfiguration = {
    orchestrationID: "your-orgID_orchestrationID",
    hostURL: "https://dl.watson-orchestrate.ibm.com",
    rootElementID: "root",
    showLauncher: false,
    deploymentPlatform: "ibmcloud", // Required for IBM Cloud 
    crn: "your-org-crn", // Required for IBM Cloud. Learn how to get the CRN in https://cloud.ibm.com/docs/key-protect?topic=key-protect-retrieve-instance-ID&interface=ui
    chatOptions: {
        agentId: "your-agent-id",
        agentEnvironmentId: "your-agent-env-id",
        onLoad: onChatLoad // Callback function when the chat widget is loaded
    },
};
    setTimeout(function () {
        const script = document.createElement('script');
        script.src = `${window.wxOConfiguration.hostURL}/wxochat/wxoLoader.js?embed=true`;
        script.addEventListener('load', function () {
            wxoLoader.init();
        });
        document.head.appendChild(script);
    });
</script>