Integration
Overview
The ai12z ChatBot Control is a web component designed to provide a conversational AI experience on your website, similar to chatbots. This feature offers users an interactive, AI-powered chatbot that can answer queries, guide users, and provide relevant information seamlessly.
Setup
To integrate the ChatBot Control, follow either of the two methods below:
-
Include the following script tag in the
<head>
section of your HTML.<script
type="module"
src="https://cdn.ai12z.net/pkg/ai12z@latest/dist/esm/library.js"
></script>
<link
rel="stylesheet"
href="https://cdn.ai12z.net/pkg/ai12z@latest/dist/library/library.css"
/> -
Add the following script to the HTML section of the Tag Configuration in Google Tag Manager.
<script>
;(function () {
var script = document.createElement("script")
script.type = "module"
script.src =
"https://cdn.ai12z.net/pkg/ai12z@latest/dist/esm/library.js"
script.async = true
document.head.appendChild(script)
var link = document.createElement("link")
link.rel = "stylesheet"
link.href =
"https://cdn.ai12z.net/pkg/ai12z@latest/dist/library/library.css"
document.head.appendChild(link)
script.onload = function () {
if (window.Bot) {
window.Bot.init()
}
}
})()
</script>
You can then use the <ai12z-bot>
tag anywhere in your HTML to display the ChatBot.</ai12z-bot>
Attributes and Customization
<ai12z-bot data-key="<API_KEY>"></ai12z-bot>
Attributes
Property | Description | Example | Default |
---|---|---|---|
data-key (Required) | API key from your ai12z project settings | data-key="Add your Key here" | |
data-mode (Optional) | Server to use dev or prod or local | data-mode="dev" | prod |
config-id (Optional) | Configure the particular bot otherwise the default bot config will load | config-id="123455" |
Custom bot (Optional)
If bot tag exists the standard bot control will be replaced with the html in the bot tag
<ai12z-bot data-key="your_api_key_here">
<div slot="bot">
<img
style="width: 20px; cursor: pointer"
src="https://cdn.ai12z.net/assets/web/magnifying-glass-solid.svg"
alt="chat"
/>
</div>
</ai12z-bot>
Passing attributes and tags
Attributes
Developer can pass a dictionary of additional data that can be used in prompts if you include the attribute tag in Answer AI.
IncludeTags
Array of tags, only content with one of these tags will be returned.
ExcludeTags
Array of tags,content will be excluded if it has one of these tags.
Example for passing the attributes,excludeTags and includeTags
document.addEventListener("DOMContentLoaded", function () {
const ele = document.querySelector("ai12z-bot")
if (ele) {
ele.excludeTags = ["blog"]
ele.includeTags = []
ele.dataAttributes = {
content: {
newlist: [
{
_uid: "BUY6Drn9e1",
component: "foo",
headline: "Foo",
},
{
_uid: "gJZoSLkfZV",
component: "bar",
title: "Bar",
},
{
_uid: "X1JAfdsZxy",
component: "foo",
headline: "Another headline",
},
],
},
}
}
})
Custom Events
Event Name | Description | How to use it |
---|---|---|
messageSent | This event will trigger when user sent an message to server | const botElement = document.querySelector('ai12z-bot'); botElement.addEventListener('messageSent',async(event) => {console.log(event.detail)}) |
messageReceived | This event will trigger when user received a response from server | botElement.addEventListener('messageReceived',async(event) => {console.log(event.detail)}) |
close | This event will trigger when user close the bot from client side | botElement.addEventListener('close', async(event) => { console.log(event.detail)}) |
open | This event will trigger when user open the bot from client side | botElement.addEventListener('open', async(event) => {console.log(event.detail)}) |
minimize | This event will trigger when user minimize the bot from client side | botElement.addEventListener('minimize', async(event) => {console.log(event.detail)}) |
errored | This event will trigger when any error thrown from server side | botElement.addEventListener('errored', async(event) => {console.log(event.detail)}) |
Custom Methods
Method Name | Description | How to use it |
---|---|---|
action | Action allows us to do open, close and minimize the bot from client side. We have to pass the action type as an arugument to do particular action | async function openBot() {try {awaitcustomElements.whenDefined('ai12z-bot'); const botElement = document.querySelector('ai12z-bot');await botElement.action('open'); } catch (error) {console.error('Error fetching data:', error);}} |
sendMessage | This method allows us to send a message programmatically.It accepts two parameters.Message (Required) – The first parameter must be a string containing the message to be sent.Display Text (Optional) – The second parameter is optional and should be a string that specifies the display text for the message.If the second parameter is not provided, the default behavior will be used. | async function openBot() {try {awaitcustomElements.whenDefined('ai12z-bot'); const botElement = document.querySelector('ai12z-bot');await botElement.sendMessage('what are the activities available?','List all activities'); } catch (error) {console.error('Error fetching data:', error);}} |
sendJSON | This method allows us to send a JSON programmatically.It accepts two parameters.jsonData (Required) – The first parameter must be an object which will sent.DisplayText(Required) – The second parameter should be a string that specifies the display text for the message. | const botElement=document.querySelector("ai12z-bot");botElement.sendJSON("{firstname:"xxxx",lastname:"yyyy",dob:"zzzz"}","Info submitted") |