BotProt lobby

Integration for BotProt: a client script tag, with optional skins as separate bundles.

Usage

The minimum is BotProt with the reference skin, which shows it in a dialog:

<script src="./botprot.js"></script>
<script src="./skins/reference.js"></script>
<script>
  // Resolves to window.OpenTicket once it has `member`: right away, or when `event` fires.
  function ready(member, event) {
    return new Promise((resolve) => {
      if (window.OpenTicket?.[member]) {
        resolve(window.OpenTicket);
      } else {
        document.addEventListener(event, (e) => resolve(e.detail), { once: true });
      }
    });
  }

  async function start() {
    const OT = await ready('createBotProt', 'BotProt:Client:Ready');
    await ready('createBotProtReferenceSkin', 'BotProt:Skin:reference:Ready');

    try {
      const token = await OT.createBotProt('my-identifier', OT.createBotProtReferenceSkin());
      // Verified: continue with the token.
      console.log('BotProt token:', token);
    } catch (error) {
      // For developers: error.message and error.code (see botprot.d.ts).
      console.error(error);
    }
  }

  start();
</script>

Or with your own element, and without a skin:

<div id="botprot-container"></div>

<script src="./botprot.js"></script>
<script>
  const identifier = 'my-identifier';
  const options = {
    show() {
      return document.getElementById('botprot-container');
    },
  };

  async function start(OT) {
    try {
      const token = await OT.createBotProt(identifier, options);
      // Verified: continue with the token.
      console.log('BotProt token:', token);
    } catch (error) {
      // For developers: error.message and error.code (see botprot.d.ts).
      console.error(error);
    }
  }

  if (window.OpenTicket?.createBotProt) {
    start(window.OpenTicket);
  } else {
    document.addEventListener('BotProt:Client:Ready', (e) => start(e.detail), { once: true });
  }
</script>

Do not add integrity (Subresource Integrity) to these script tags, or to those of the skins: every release updates the scripts at the same URL, so a pinned hash blocks them after the next release.

Types

Download botprot.d.ts into your project for typings of the window.OpenTicket global. Each skin has its own, skins/{name}.d.ts next to skins/{name}.js; save it as well, next to botprot.d.ts, as it builds on it.

These are your own copies: the scripts update at their URL, the copies do not. Download them again now and then, such as when you work on your integration, to keep them up to date.

The types live in the global OpenTicket namespace, so a script without imports can use them, e.g. in JSDoc: /** @type {OpenTicket.BotProtOptions} */. A TypeScript module can import them as well: import type { BotProtOptions } from './botprot.d.ts';.

Playground

botprot.js

Loads the script on demand, like a customer's page would. Ready once it added its members to window.OpenTicket (BotProt:Client:Ready). Reset all unloads it again.

window.OpenTicket = undefinedservice = undefined

Identifier

identifier = 

Skin

The skin to start from, or none to roll your own with the options below. A skin loads its script the first time it is chosen. A page cannot unload it again, so Reset all leaves it loaded.

Options

options = {}

createBotProt(identifier, options)

token = undefined

Frame

Log

    Storage

    Every BotProt: key in localStorage, updated as it is written, also from other tabs. Reset all leaves them, so the next run continues where this one left off; Clear removes them.