> For the complete documentation index, see [llms.txt](https://docs.aisera.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aisera.com/aiseragpt/embed-your-webchat-channel.md).

# Embed Your Webchat Channel

<figure><img src="/files/bufFVgqXhRFa1DSTF5S2" alt=""><figcaption></figcaption></figure>

This topic describes the installation of the Aisera Webchat bot (AiseraGPT application on the Aisera Webchat channel) on any portal or web landing page. This involves setting up session variables to pass information to the bot before loading it, and also the specific JavaScript code that should be used in the page layout to load the bot on the page.

## JavaScript for Aisera Webchat Bots

The Aisera Webchat Bot is included as a default channel when you create a new application/bot. \
You can use the channel 'as is', or add different channels, for a different 'look and feel'.&#x20;

<div align="left"><figure><img src="/files/ShMUIQWGBp5vqCnkuuaj" alt="" width="471"><figcaption></figcaption></figure></div>

If you're using the default Webchat channel, you can add it to an HTML web page by including the parameters within a JavaScript( JS) tag. on your page or portal. \
\
The following is an example Webchat script.

{% code overflow="wrap" %}

```javascript
(function() {var d = document, s = d.createElement('script'); s.async = true; s.src = 'https://<your_tenant>.chatbot.aisera.<top_level_domain>/awc/js?t=a1b2c3d4-e5f6-7890-1234-567890abcdef' + Date.now(); d.getElementsByTagName('head')[0].appendChild(s)})();
```

{% endcode %}

#### To obtain the Webchat Script:

1. In the Aisera Admin UI navigate to **Settings > AiseraGPT** in the left navigation panel.
2. In the **AiseraGPT Details** window under **Channels** click on the **Webchat** channel.
3. At the bottom of the **Channel Details Page** under the **JavaScript** **Snippet** click **Copy.**

<figure><img src="/files/cH0InslD9aEwNyxVGrMw" alt=""><figcaption></figcaption></figure>

4. This script contains all the relevant parameters for loading your bot on your page or portal. \
   To write JavaScript code inside an HTML page, you must enclose your code within **`<script>`** and **`</script>` tags**. Add your webchat javascript (like thie one in the example above) between two script tags, as shown in the red box in the example below.

<div align="left"><figure><img src="/files/GFfGemSve1viUXCmV27U" alt="" width="468"><figcaption></figcaption></figure></div>

**NOTE:** If you choose the **Test** button below the JavaScript snippet (shown in the diagram as part of Step 3 above, the script will render a Webchat bot, according to the JavaScript instructions.

<div align="left"><figure><img src="/files/FgHVDxgTC9XgnqKLOkJw" alt="" width="220"><figcaption></figcaption></figure></div>

## Additional Configurations

The following sections contain additional options, but you must perform these edits and operations outside the regular commands in the Aisera Admin UI.&#x20;

### Session Variables&#x20;

Session variables can optionally be passed to Webchat in the `window.aisera_bot` global variable (or `window.webchat` for backward compatibility).&#x20;

The following session variables have special meaning to Webchat:&#x20;

* `userEmail:` User email&#x20;
* `userFullName:` User full name&#x20;

In addition to the user variables, other session variables could be specified to be used in custom flows.&#x20;

For example:

```javascript
window.aisera_bot = { 
    userEmail: "jsmith@acme.com", 
    userFullName: "John Smith", 
    accountId: "1234" 
} 
```

{% hint style="info" %}
If you specify `userEmail` in the session variables then you have to set the variable before the snippet that loads Webchat. *It cannot be changed later* using the [update session vars](#update-session-vars) message.
{% endhint %}

Below is an example showing session variables being passed to the webchat.

```html
<html>
    <head>
        <script>
            window.webchat = {
                userEmail: "john.doe@domain.com",
                userFullName: "John Doe",
                locale: "fr",
            };
            (function () {
                var d = document,
                    s = d.createElement("script");
                s.async = true;
                s.src =
                    "https://<your_tenant>.chatbot.aisera.<top_level_domain>/awc/js?t=a1b2c3d4-e5f6-7890-1234-567890abcdef" +
                    Date.now();
                d.getElementsByTagName("head")[0].appendChild(s);
            })();
        </script>
    </head>
    <body>
        <h1>Sample page</h1>
    </body>
</html>

```

### Input Messages&#x20;

You can make a request to Webchat to perform operations using the `window.postMessage` function.&#x20;

The JavaScript for these messages looks like the following:

```javascript
let message = { 
    type: MESSAGE_TYPE, 
    // additional fields depending on the message type 
}; 

window.postMessage(JSON.stringify(message), "*"); 
```

&#x20;The following input message types are supported:&#x20;

#### Open

This message opens the application window if it is not already opened.

<table><thead><tr><th width="171">Field</th><th>Description</th></tr></thead><tbody><tr><td>type</td><td><code>aisera.webchat.open</code></td></tr><tr><td>text</td><td>Optional text to be used as a search utterance</td></tr><tr><td>message</td><td>Optional html to be displayed in Webchat<br>Other fields can be specified here and received in the <a href="#opened">opened</a> output messages described below.</td></tr></tbody></table>

#### Close

This message closes the webchat window if it is not already closed

<table><thead><tr><th width="171">Field</th><th>Description</th></tr></thead><tbody><tr><td>type</td><td><code>aisera.webchat.close</code></td></tr></tbody></table>

#### Reset

This message clears the webchat content and the current conversation contexts.

<table><thead><tr><th width="171">Field</th><th>Description</th></tr></thead><tbody><tr><td>type</td><td><code>aisera.webchat.reset</code></td></tr></tbody></table>

#### Hide

This message hides the Webchat and the opener icon.

<table><thead><tr><th width="171">Field</th><th>Description</th></tr></thead><tbody><tr><td>type</td><td><code>aisera.webchat.reset</code></td></tr></tbody></table>

#### Show

This message shows the Webchat and the opener icon if they are hidden using the `aisera.webchat.hide` message.

<table><thead><tr><th width="171">Field</th><th>Description</th></tr></thead><tbody><tr><td>type</td><td><code>aisera.webchat.show</code></td></tr></tbody></table>

#### Update Session Vars

The hosting page can update the `window.aisera_bot_variable` at any time, but it needs to send this notification to notify the application.

<table><thead><tr><th width="171">Field</th><th>Description</th></tr></thead><tbody><tr><td>type</td><td><code>aisera.webchat.update_session_vars</code></td></tr></tbody></table>

### Output Messages

Webchat sends notification messages to the host window during specific operations.&#x20;

The JavaScript that allows Webchat to receive the messages looks like the following:

```javascript
window.addEventListener("message", event => {
    let message = JSON.parse(event.data);
    // use message.type etc
})
```

The following outgoing message types are supported:

#### Ready

This message is sent when Webchat is initialized and is ready to process messages.&#x20;

<table><thead><tr><th width="171">Field</th><th>Description</th></tr></thead><tbody><tr><td>type</td><td><code>aisera.webchat.ready</code></td></tr></tbody></table>

#### Opened

This message is sent when Webchat is opened. All fields specified in the [open](#open) input message are included in the message.

<table><thead><tr><th width="171">Field</th><th>Description</th></tr></thead><tbody><tr><td>type</td><td><code>aisera.webchat.opened</code></td></tr></tbody></table>

#### Closed

This message is sent when Webchat is closed.&#x20;

<table><thead><tr><th width="171">Field</th><th>Description</th></tr></thead><tbody><tr><td>type</td><td><code>aisera.webchat.closed</code></td></tr></tbody></table>

#### Handoff

This message is sent when a button with the Hand-off action is clicked.&#x20;

<table><thead><tr><th width="171">Field</th><th>Description</th></tr></thead><tbody><tr><td>type</td><td><code>aisera.webchat.handoff</code></td></tr><tr><td>transcript</td><td>The conversation transcript as a <code>string</code></td></tr></tbody></table>

#### Session Timeout

This message is sent when the conversation session times out. The session times out when the user idle timeout configured in the Admin UI elapses.

<table><thead><tr><th width="171">Field</th><th>Description</th></tr></thead><tbody><tr><td>type</td><td><code>aisera.webchat.session_timeout</code></td></tr></tbody></table>

## Webchat Embed with an Iframe&#x20;

To use your Webchat in an `iframe` inside a page containing a webchat snippet, change the URL in the snippet to include a `target_id` parameter with the `iframe tag id`.&#x20;

For example, if the page has the following tag:

```html
<iframe id=”webchat-iframe”</iframe>
```

The URL in the snippet should be changed to:&#x20;

```http
https://{webchat-endpoint-url}&target_id=webchat-iframe
```

In order to embed the webchat in a browser window and take up the full width & height of the window, use `&embed` after the application endpoint URL.

```http
https://{webchat-endpoint-url}&embed
```

In the embed mode, you can use the `no_header` URL parameter so the Webchat title bar is not displayed.&#x20;

## Session Variables passed as a URL Query Parameter&#x20;

You can also pass session variables via query parameter as part of the URL.&#x20;

* **Query parameter key:** `session_vars`
* **Value:** JSON Object

For example:&#x20;

```
https://{webchat-endpoint-url}&embed&session_vars={"userEmail":"jsmith@nik.com","user FullName":"John Smith","accountId":"1234"}
```

## Hide Webchat and Open on Button Click&#x20;

You can hide the Webchat opener using following CSS rule:&#x20;

```css
#awc-webchat.closed {
   display:none 
} 
```

Example of how to open Webchat on button click:

## Open Webchat and Pass a Phrase as a URL Parameter

This is an example of a function that passes a phrase from the URL parameter into Webchat.&#x20;

Webchat will automatically open and send this phrase as a request:

```javascript
(function () {
    window.addEventListener("message", function (event) {
        try {
            // Web Chat events are stringified
            const { type } = JSON.parse(event.data);
            if (type === "aisera.webchat.internal.initialized") {
                // Then it's a Web Chat Initialized event.
                // Check whether an utterance to be triggered was provided.
                const queryString = window.location.search;
                const urlParams = new URLSearchParams(queryString);
                const text = urlParams.get("u");
                if (text) {
                    window.postMessage(
                        JSON.stringify({ type: "aisera.webchat.open", text }),
                        "*"
                    );
                }
            }
        } catch (error) {
            // Then `event.data` is NOT valid JSON which means it is not an Aisera WebChat event.
        }
    });
})();
```

&#x20;&#x20;
