# Release0 JS library

Frontend library to embed chat agents from [Release0](https://release0.com/).

## Installation

### Using npm

To install, simply run:

```bash
npm install @release0/js
```

### Directly in your HTML

```
<script type="module">
  import Agent from 'https://cdn.jsdelivr.net/npm/@release0/js@1.2.0/dist/web.js'

  Agent.initStandard({
    agent: 'my-agent-id',
  })
</script>

<agent-standard style="width: 100%; height: 600px; "></agent-standard>
```

## Standard

You can get the standard HTML and Javascript code by clicking on the "HTML & Javascript" button in the "Integrations" tab of your agent.

There, you can change the container dimensions. Here is a code example:

```html
<script type="module">
  import Agent from "https://cdn.jsdelivr.net/npm/@release0/js@1.2.0/dist/web.js";

  Agent.initStandard({
    agent: "my-agent-id",
  });
</script>

<agent-standard style="width: 100%; height: 600px; "></agent-standard>
```

This code is creating a container with a 100% width (will match parent width) and 600px height.

## Popup

You can get the popup HTML and Javascript code by clicking on the "HTML & Javascript" button in the "Integrations" tab of your agent.

Here is an example:

```html
<script type="module">
  import Agent from "https://cdn.jsdelivr.net/npm/@release0/js@1.2.0/dist/web.js";

  Agent.initPopup({
    agent: "ag_herexisxyourxagentxid",
    apiHost: "https://release0.com",
    autoShowDelay: 2000,
  });
</script>
```

This code will automatically trigger the popup window after 2 seconds.

### Open or Close a popup

You can use these commands:

```js
Agent.open();
```

```js
Agent.close();
```

```js
Agent.toggle();
```

You can bind these commands on a button element, for example:

```html
<button onclick="Agent.open()">Contact us</button>
```

## Bubble

You can get the bubble HTML and Javascript code by clicking on the "HTML & Javascript" button in the "Integrations" tab of your agent.

Here is an example:

```html
<script type="module">
  import Agent from "https://cdn.jsdelivr.net/npm/@release0/js@1.2.0/dist/web.js";

  Agent.initBubble({
    agent: "ag_herexisxyourxagentxid",
    previewMessage: {
      message: "I have a question for you!",
      autoShowDelay: 5000,
      avatarUrl: "https://release0.com/images/logo512.png",
    },
    theme: {
      button: { backgroundColor: "#0042DA", iconColor: "#FFFFFF" },
      previewMessage: { backgroundColor: "#ffffff", textColor: "black" },
      chatWindow: { backgroundColor: "#ffffff" },
    },
  });
</script>
```

This code will show the bubble and let a preview message appear after 5 seconds.

### Open or close the preview message

You can use these commands:

```js
Agent.showPreviewMessage();
```

```js
Agent.hidePreviewMessage();
```

### Open or close the agent

You can use these commands:

```js
Agent.open();
```

```js
Agent.close();
```

```js
Agent.toggle();
```

You can bind these commands on a button element, for example:

```html
<button onclick="Agent.open()">Contact us</button>
```

## Additional configuration

You can prefill the agent variable values in your embed code by adding the `initialContext` option. Here is an example:

```js
Agent.initStandard({
  agent: "ag_herexisxyourxagentxid",
  initialContext: {
    "Account URL": "https://my-site.com/account",
    "User name": "My username",
  },
});
```

It will prefill the `Account URL` variable with "https://my-site.com/account" and the `User name` variable with "My username".

Note that if your site URL contains query params (i.e. https://my-site.com?User%20name=My%20username), the variables will automatically be injected to the agent. So you don't need to manually transfer query params to the agent embed configuration.
