ChannelsWeb Chat

Control the chat widget with code

Open the chat from your own links and buttons, send a first message or reveal a hidden widget.

You can open the Octocom chat widget from anywhere on your site, not just from its launcher. Use it to connect the chat to your own Contact us links, buttons, menus and page logic: open the chat when a checkout fails, start it with the visitor's question already sent, or keep the widget hidden until your own check decides to show it.

The simplest option needs no JavaScript: a link that opens the chat. For buttons and custom logic, dispatch a browser event.

Example uses:

  • A Chat with us link in your footer or help pages.
  • A search bar that sends the visitor's query to the AI agent.
  • A product finder, sizing quiz or room planner that hands its result to the AI agent as the first message.
  • Opening the chat automatically after an error, with the problem already described.

Add open-octocom to a link as a query parameter or a hash. When the page loads with either, the chat opens.

<a href="/?open-octocom">Chat with us</a>
<a href="/#open-octocom">Chat with us</a>

The hash version doesn't reload the page when the visitor is already on it: the chat opens in place. Some site builders and link fields don't allow hashes; use the query parameter there.

You can leave out the path. The browser then applies the parameter to the current page, which is useful in footers and other shared elements that appear on every page:

<!-- Opens widget with a page reload -->
<a href="?open-octocom">Chat with us</a>

<!-- Opens widget without reloading if already on same page -->
<a href="#open-octocom">Chat with us</a>

Links only work with <a> elements. For a <button> or any other element, use the event below.

Opening the chat from your own code

Dispatch a CustomEvent named openChatEvent on document:

const event = new CustomEvent("openChatEvent", {
  detail: {
    type: "OPEN_OCTOCOM",
    message: {
      content: "some text",
      imageUrl: "some URL",
    },
  },
});

document.dispatchEvent(event);
  • Without message, the chat opens and nothing is sent.
  • With message, the chat opens and sends content as the visitor's first message, so the AI agent answers it straight away.
FieldTypeRequiredDescription
typestringYesAlways "OPEN_OCTOCOM".
messageobjectNoA message to send as the visitor when it opens.
message.contentstringYes, with messageThe text of the visitor's message.
message.imageUrlstringNoURL of an image to include with the message.

Example: open the chat from a button

<button
  onClick={() => {
    const event = new CustomEvent("openChatEvent", {
      detail: {
        type: "OPEN_OCTOCOM",
        message: {
          content: "Hello, I need help with my order.",
        },
      },
    });
    document.dispatchEvent(event);
  }}
>
  Open Assistant
</button>

Clicking the button opens the chat and sends the pre-filled message.

Example: open the chat after an error

You can dispatch the event from any of your own logic: menu actions, timeouts or API responses. For example, open the chat when checkout fails:

if (hasCheckoutError) {
  document.dispatchEvent(
    new CustomEvent("openChatEvent", {
      detail: {
        type: "OPEN_OCTOCOM",
        message: {
          content: "I ran into an issue during checkout.",
        },
      },
    }),
  );
}

Opening the chat on page load

To open the chat on every page where the script loads, add data-open-window="true" to the Octocom script tag. To open it only on some pages, or after a delay, use autoOpenPaths or openChatAfter in the configuration instead.

Showing a hidden widget

Some sites decide in code whether a visitor should see the chat at all, for example only after an eligibility check or for signed-in customers. For this, turn on Hidden by default for the configuration in Settings -> Channels -> Web Chat -> Deployment (hideInitially through MCP). The launcher and chat then stay hidden until your code shows or opens the widget.

Dispatch showWidgetEvent with type SHOW_OCTOCOM to reveal the widget without opening the chat panel:

document.dispatchEvent(
  new CustomEvent("showWidgetEvent", {
    detail: { type: "SHOW_OCTOCOM" },
  }),
);
  • The launcher becomes visible, and the visitor can click it to start chatting.
  • This does not send a message, record a chat open or write anything to the visitor's browser storage.
  • Repeating the event is safe. An already open chat stays open.
  • The event applies to every widget instance on the page. Visibility lasts for this page load: the next page load starts hidden again.
  • Deployment rules and the support-hours schedule still apply.

Run this after the widget has loaded its configuration. Events dispatched before then are not queued. The simplest place is the configuration's custom JavaScript, which runs even while the widget is hidden. For example, reveal the widget after your own asynchronous eligibility check:

octocom.on("widget:ready", async function () {
  try {
    // Your site's endpoint: return { eligible: true } to show the widget.
    const response = await fetch("/api/chat-eligibility");
    if (!response.ok) return;
    const result = await response.json();
    if (result.eligible === true) {
      document.dispatchEvent(
        new CustomEvent("showWidgetEvent", {
          detail: { type: "SHOW_OCTOCOM" },
        }),
      );
    }
  } catch (error) {
    // Leave the widget hidden when eligibility cannot be checked.
    console.error("Could not check chat eligibility", error);
  }
});

The endpoint above is an example to implement on your website, not a built-in Octocom endpoint. Replace it with your own logic. From your own page scripts, listen for the octocom:widget:ready event on document when custom JavaScript is enabled. Inside the custom JavaScript snippet, octocom.on("widget:ready", ...) also works after the widget is already ready.

Hidden by default turns off automatic opening on configured paths, timed automatic opening and restoring an open chat from a previous visit. Explicit opening still works: openChatEvent, open-octocom links, data-open-window="true", and embedded or hosted chat pages open the chat directly. Use showWidgetEvent when you want only the launcher to appear, or openChatEvent when you want to open the panel.

This setting controls visibility. It is not an access restriction: to verify who a visitor is, use customer authentication.

Showing an attention grabber

Dispatch showAttentionGrabberEvent to show an attention grabber whose trigger is manual, by its id or name:

document.dispatchEvent(
  new CustomEvent("showAttentionGrabberEvent", {
    detail: { type: "SHOW_OCTOCOM_ATTENTION_GRABBER", grabber: "Spring sale" },
  }),
);

The grabber's conditions and frequency still apply, and nothing shows over an open chat or another grabber. Inside custom JavaScript, octocom.showAttentionGrabber("Spring sale") does the same.

Go further with custom JavaScript

The events on this page work from any script on your site. To react to what happens in the chat (opened, message sent, handed off) or attach your own data to the conversation, add a custom JavaScript snippet to the configuration. To pass visitor context without a snippet, see Chat custom data.

On this page