Skip to content
KnockDevelopers
Chat

Chat

Add the Knock chat to your site with the Knock tag, with @knock-ai/sdk, or with your own UI (headless). Pick a look, place it inside your page, and let visitors continue on Telegram or Slack.

Chat with a Developer

knock.chat controls the Knock chat on your page. A visitor chats with your AI agent, and with a rep when one joins from your team's Slack. The visitor can also continue the chat on Telegram, or on Slack in a workspace Knock runs, as the same contact. You choose how it looks: one of Knock's looks, floating or inside an element of your page, or your own UI.

Try it

Before you start

The chat needs three things:

  1. The Knock tag or the Knock Product SDK. Make sure your site already loads your Knock tag, or use the Knock Product SDK (see Knock Product SDK). Only the SDK needs your tag ID: in Knock, go to Settings > Administration > Tracking Codes.
  2. A magic link ID. The chat runs on a magic link. Use the link's ID, not its URL.
  3. Your site's origin allowed. The chat is on for every workspace, on your website's domain and all its subdomains over https (for https://www.acme.com: acme.com, www.acme.com, app.acme.com…). Any other origin your pages load from, Knock adds for you. An origin is exact: scheme, host and port. Send each one, for example https://acme-staging.vercel.app or http://localhost:3000.

To build before the third step is done, use test mode: it needs no magic link and no allowed origin.

Continue on Telegram or Slack needs no code. Knock offers an app when all of these are true:

  • The magic link is turned on for that app: the link's messagingApps lists telegram or slack. To read the list, call Get a magic link. A chat link opens one app, so its chat can offer only that app. To offer both apps, run the chat on a link that lists both. To turn an app on or off for a link, go to Click to Engagement > Chat Links in Knock, or ask your Knock contact.
  • The app is set up for you. For Telegram, that is the bot Knock set up for your company. For Slack, it is the Slack workspace Knock runs.
  • The link does not ask its visitors for a private email first.

When the visitor taps an app, Knock also checks the visitor's country against the countries you block. There is no other switch. Knock offers only Telegram and Slack, in that order.

Set it up

Pick the snippet that matches your site. Each snippet is complete. Replace YOUR_MAGIC_LINK_ID, and YOUR_TAG_ID where it shows.

Your siteSnippet
Loads your Knock tag and has no SDK, such as a marketing siteKnock tag
Installs @knock-ai/sdk, such as your productKnock Product SDK
Draws the chat with its own UIHeadless

Knock tag

Use this snippet on a site that already loads your Knock tag. Paste it after the tag's <script>, on every page. You install nothing else.

Knock tag
<script>
  (function () {
    var logo = 'https://assets.start-chat.com/knock/company-icon/knock.company-icon.png';
    function startChat() {
      Knock.loadChat().then(function (chat) {
        if (!chat) return;
        chat.setSkin({
          preset: 'harbor',
          mode: 'light',
          colors: { brand: '#4F46E5', launcherBg: '#FFFFFF' },
          assets: { bubble: logo, header: logo },
        });
        chat.configure({ magicLinkId: 'YOUR_MAGIC_LINK_ID', brandName: 'Knock' });
      });
    }
    if (window.Knock) startChat();
    else addEventListener('knock:ready', startChat, { once: true });
  })();
</script>

The script waits for your tag. Then it loads the chat from the tag, sets its look and sets your magic link. Replace YOUR_MAGIC_LINK_ID, logo, brandName and brand with your own.

  • setSkin is optional. Call it before configure. The chat shows once it has a magic link, so with this order it never shows in the default look.
  • assets.bubble is the picture on the launcher. assets.header is your logo in the chat's header, in place of the agent's picture. Use a square image of 72 px or more, over https.
  • launcherBg: '#FFFFFF' puts a full-color logo on a white launcher. Without it, the logo sits on your brand color.
  • colors sets the look's own mode: light for most looks, dark for nightshift. For the other mode too, put the same keys in colorsDark, or in colorsLight for nightshift.
  • brandName is the name in the chat's header. Without it, the header says "Chat".
  • The looks are harbor, nightshift, fieldbook, signal, terminal and pixel. mode is light, dark or auto. See Change the look.
  • configure takes the same options as chat in init(), such as starters and container.
  • Use chat inside the then. Knock.chat exists only after loadChat() resolves. chat is empty when the chat's file did not load.

Any element with data-knock-chat opens the chat:

<button data-knock-chat>Chat with us</button>

The knockq queue (chat.configure, chat.open) works only when Knock set up the chat in your tag. With this script, call chat inside the then. If the console says Knock.loadChat is not a function, the page has no Knock tag, or the script runs before it.

Knock Product SDK

Use this snippet in an app that installs @knock-ai/sdk. Call init() once, at app start.

Knock Product SDK
import { knock } from '@knock-ai/sdk';

knock.init({
  tagId: 'YOUR_TAG_ID',
  chat: {
    magicLinkId: 'YOUR_MAGIC_LINK_ID',
    brandName: 'Acme',
    starters: ['How much does it cost?', 'Can I see a demo?'],
  },
});

Open and close the chat from your own buttons:

Your own button
<button onClick={knock.chat.open}>Chat with us</button>
<button onClick={knock.chat.close}>Close</button>
MethodWhat it does
knock.chat.open(options?)Opens the chat. send sends a first message for the visitor.
knock.chat.close()Closes the chat.
knock.chat.setSkin(skin)Replaces the look you set from code.
knock.chat.resetSkin()Drops the look you set from code.
knock.chat.configure(options)Sets up the chat. Each key you pass replaces that key.

To load the SDK from a CDN, paste the snippet from Frameworks > CDN. Pass the same chat object to knockai.init(), and use knockai.chat where this page uses knock.chat.

Headless

Use this snippet when your own UI draws the chat. Knock draws nothing, and downloads only the conversation. This example is a message list and a form that are always on the page:

index.html
<ol id="chat-log"></ol>
<form id="chat-form">
  <input id="chat-input" autocomplete="off" placeholder="Write a message" />
  <button type="submit">Send</button>
</form>
chat.ts
import { knock } from '@knock-ai/sdk';
import type { KnockChatClient, KnockChatConversation } from '@knock-ai/sdk';

const log = document.getElementById('chat-log') as HTMLOListElement;
const form = document.getElementById('chat-form') as HTMLFormElement;
const input = document.getElementById('chat-input') as HTMLInputElement;
let chat: KnockChatClient | null = null;

knock.init({
  tagId: 'YOUR_TAG_ID',
  chat: {
    magicLinkId: 'YOUR_MAGIC_LINK_ID',
    headless(client) {
      chat = client;
      client.subscribe(() => render(client.getState()));
      render(client.getState());
      knock.chat.open(); // The UI is always on screen, so it starts the conversation once.
    },
  },
});

function render(state: KnockChatConversation): void {
  form.hidden = state.status === 'unavailable';
  log.replaceChildren(
    ...state.messages.map((message) => {
      const item = document.createElement('li');
      item.className = `from-${message.author.kind}`;
      // Text parts only. Skip a part type that your UI does not know.
      item.textContent = message.parts.map((part) => (part.type === 'text' ? part.text : '')).join('\n');
      return item;
    }),
  );
}

form.addEventListener('submit', (event) => {
  event.preventDefault();
  if (chat?.send(input.value)) input.value = ''; // send() returns false and sends nothing for a blank text.
});

Set headless in init(), at app start. A UI that shows and hides the chat calls knock.chat.open() when it shows it and knock.chat.close() when it hides it. The client has five methods:

MethodWhat it does
getState()The conversation now: status, messages, typing, starters, unread, continueOn and continuedOn.
subscribe(listener)Calls listener after each change. Returns a function that stops it.
send(text)Sends a visitor message, and starts the conversation when needed.
retry(messageId)Sends a failed visitor message again.
resolveAction(actionId)Runs an action, such as book a meeting, an app of continueOn, or the way back to the app of continuedOn. Call it inside the visitor's click. It returns a Promise of what happened: opened, blocked or failed.

Text from the agent and from reps is Markdown. Render it with a Markdown library and sanitize the result. Never insert it as raw HTML. The types of continueOn, continuedOn and the Promise of resolveAction() need @knock-ai/sdk 0.1.5 or later. The rules, the client's types, and complete examples in TypeScript and React are in Build your own chat UI.

Change the look

The looks are harbor, nightshift, fieldbook, signal, terminal and pixel. Each one has a light and a dark mode. You can change the look at any time, and the conversation stays:

Change the look
knock.chat.setSkin({ preset: 'terminal', mode: 'dark' });

A skin also takes your own colors, typography and shape on top of a preset.

Show it inside your page

By default the chat floats over your page. With container, it shows inside an element of your page. Give the element a height and leave it empty:

An inline chat in React
function SupportChat() {
  const ref = useRef<HTMLDivElement>(null);
  useEffect(() => {
    knock.chat.configure({ container: ref.current });
    return () => knock.chat.configure({ container: null });
  }, []);
  return <div ref={ref} style={{ height: 600 }} />;
}

An inline chat is always shown, with no bubble and no close button. The conversation starts at the visitor's first action. To start it at page load, call knock.chat.open().

Continue on Telegram or Slack

A visitor can move the chat to Telegram, or to Slack in a workspace Knock runs, and go on there as the same contact. Your reps still answer from your team's Slack. You write no code for it. Knock's UI shows the apps that Knock offers for your magic link (see Before you start).

The buttons

  • The apps show under the caption "Continue in your own apps": one button per app, with its icon and its name.
  • They show with the welcome message, and under the latest reply of the agent or a rep. Only one set shows at a time.
  • They hide while the visitor's message waits for its reply, while someone types, and while the chat is unavailable.

A tap

  • A tap opens a new tab that says "Opening Telegram…". The tab then goes to the app's page on start-chat.com.
  • When the browser blocks the tab, the chat shows a "Tap to open Telegram" button. In an in-app browser, such as LinkedIn's, Instagram's or Facebook's, the chat always shows this button. It works for up to 15 minutes. After that, a new tap gets a new link.
  • On Slack, an in-app browser cannot sign the visitor in. The page asks the visitor to open it in their browser. Telegram opens its app from an in-app browser.
  • You do not use knock.wrapLink() for these apps. The chat gets each link from Knock at the tap.

The same contact, and the recap

  • After the visitor's first message, each tap gets a new link. The link works once, for 15 minutes. With it, the visitor continues as the same contact.
  • The first message the visitor gets on the app is a short recap of the website chat. Knock sends it to the visitor only. It is never on your team's Slack card, and never in the website chat.
  • The points of the recap are in the language of the conversation. Its header is in the visitor's language.
  • When Knock cannot summarize the chat, the recap quotes the last exchange: the visitor's last messages and the last reply of your agent or a rep. Each line starts with "You:" or "Us:", in the visitor's language.
  • A tap before the visitor's first message opens the app's plain link. The visitor starts on the app as on any click of your link: with the link's welcome message, no recap, and no tie to this website chat. The chat then shows "Continued on Telegram" and keeps the buttons. This note does not mean that the chat continued: the app's conversation does not show in the website chat.
  • A link that was already used, or that is older than 15 minutes, also opens the plain link.
  • Knock never merges two contacts. If the visitor's Telegram account already belongs to another of your contacts, Knock sends no recap, and the two contacts stay apart. The website chat then does not count as continued: it shows "Waiting for you on Telegram" for up to 15 minutes, and the buttons stay.
  • On Slack, the contact can already have a Slack account in the workspace Knock runs. If Knock does not know this browser as that contact's, the tap works as the plain link. Then there is no recap, and the website chat does not count as continued: it shows "Waiting for you on Slack" for up to 15 minutes.

After the visitor arrives

  • The chat counts as continued when the visitor arrives on the app, not at the tap. On Telegram, that is when they tap Start. On Slack, it is when they sign in, or when they are first active there.
  • Until then, the chat shows "Waiting for you on Telegram" for up to 15 minutes. A page reload keeps it.
  • Once the chat continued, Knock offers no other app. The header shows the app's icon just before the close button, or at its end in an inline chat. The icon's label is "Continued on Telegram".
  • The icon takes the visitor back to the app. On Telegram, it opens your bot. On Slack, it signs the visitor back in to their channel in the Slack workspace Knock runs. This works in any browser that has the chat, not only the one the visitor continued in.
  • On Slack, a click on the icon works as a tap on a button: a new tab says "Opening Slack…", and it gets a new link that works once, for 15 minutes. When the browser blocks the tab, the icon becomes "Tap to open Slack". In an in-app browser, the page asks the visitor to open it in their browser.
  • Knock signs the visitor back in to Slack for 30 days after they continued. After that, the icon opens the visitor's channel without signing them in. Knock also does not sign the browser in when the visitor's Slack account has conversations with another company. In both cases, the visitor may need to sign in to Slack themselves.
  • The website chat stays live. The visitor's messages on the app show in it, and so do the replies of the agent and your reps there.
  • A message that the visitor types on the website stays on the website. A rep's reply from your team's Slack card goes to both places.

To change the words or their language, see Change the words and the language of "Continue on". With your own UI, you draw the apps yourself: see Continue on Telegram or Slack in Build your own chat UI.

If the chat does not show

The console says why. Each warning prints once per page and starts with Vendor chat:.

What you seeWhyWhat to do
No chat and no bubbleThe chat has no magic link, or the value is a URL and not an ID. When your code used the chat, the console says so when the chat would load: once the page is idle, at open(), or at a click on a data-knock-chat element.Pass the link's ID in magicLinkId.
"This chat isn't available on this site."The page's origin is not allowed for your chat. The console names your page's origin.Send Knock the exact origin, with its scheme and port.
"This chat isn't available right now."The chat is switched off, or the magic link ID is unknown. The console says so.Check the ID, then ask your Knock contact.
An inline chat shows nothingThe element has no height, or the selector matches no element. The console says which.Give the element a height. In a single-page app, pass the element itself.
Nothing inside an iframeThe chat does not run inside an iframe. When code in that frame used the chat, the frame's console says so.Put the chat on the top page.
Knock's chat shows in place of your own UIheadless was set after the chat had loaded. The console says so.Set headless in init(), at app start.

The origin and switched-off warnings come from Knock's answer, so they print when the chat first asks Knock, most often when the visitor opens it. Test mode never prints them.

To see the reason in your own code, listen for the error event:

Log chat errors
knock.on('error', ({ message, cause }) => console.warn('Knock chat:', message, cause));

Continue on Telegram or Slack prints no warning. When its buttons do not show as you expect:

What you seeWhyWhat to do
No Telegram or Slack buttonsThe link is not turned on for the app, Telegram has no bot for you, or the link asks for a private email first. Or the chat already continued: the header shows the app. The buttons also hide while a reply is pending, while someone types and while the chat is unavailable.Turn the app on for the link in Click to Engagement > Chat Links, or ask your Knock contact. A chat link offers only its own app. An open chat can take a few minutes to show a change: reload the page to check again.
"Telegram isn't available right now." after a tapThe app was turned off for the link, its setup is gone, or the visitor's country is blocked. The button goes away for this visit.Check the link's apps in Click to Engagement > Chat Links, or ask your Knock contact.
The buttons show with no iconYour Content Security Policy does not allow data: images.Add data: to img-src. See Content Security Policy.
The Slack icon in the header does not sign the visitor inThe visitor continued more than 30 days ago, or their Slack account also has conversations with another company. Then Knock does not sign the browser in.Nothing to change. The visitor signs in to Slack themselves.

Test mode

In test mode the chat runs a scripted conversation, and nothing goes to Knock:

Test mode
knock.init({ tagId: 'YOUR_TAG_ID', chat: { test: true } });

Test mode works with every look, with an inline chat and with headless. The script runs in the page, so a page load starts it over.

Every scenario offers Telegram and Slack, except those where the chat cannot load: offline, disabled, origin-blocked and captcha. A tap opens nothing. Before the visitor's first message, a tap gets the app's plain link. After it, test mode counts the visitor as arrived on the app 2 seconds after the tap. Then the app's icon shows next to the close button, and no app is offered. A click on the icon opens nothing either. On Slack, it first gets a new link, as a tap does.

Chat with a Developer

On this page