๐Ÿค– AI Webchat Integration Guide

Complete documentation for integrating the AI chat widget into your website

๐Ÿ“‘ Table of Contents

1. Overview

The AI Webchat widget is a powerful, embeddable chat interface that can be integrated into any website. It provides a seamless user experience with customizable themes, positions, and security features.

๐Ÿ’ก What you'll get: A fully functional AI chat widget that can be embedded on your website with just a few lines of code. The widget is responsive, secure, and customizable.

Key Features

2. Prerequisites

Before you begin, ensure you have the following information from your AI Buddy dashboard:

1 Agent ID - A unique identifier for your AI agent (UUID format)
2 Embed Token - A secure JWT token for authenticating the widget
3 Backend URL - The base URL of the AI Buddy backend service
โš ๏ธ Important: Your domain must be added to the "Allowed Domains" list in the AI Buddy dashboard before the widget will work. Contact your administrator if you need to add your domain.

3. Integration Steps

Step 1: Add the Embed Script

Add the following script tag to your HTML page, typically in the <head> section or just before the closing </body> tag:

<script src="YOUR_BACKEND_URL/embed/YOUR_AGENT_ID?token=YOUR_EMBED_TOKEN"></script>
๐Ÿ’ก Replace the placeholders:
  • YOUR_BACKEND_URL - Your backend service URL (e.g., https://app.ai-buddy.com)
  • YOUR_AGENT_ID - Your unique Agent ID (UUID)
  • YOUR_EMBED_TOKEN - Your embed token (JWT)

Step 2: Complete Example

Here's a complete HTML example:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My Website</title> </head> <body> <h1>Welcome to My Website</h1> <p>Your content here...</p> <!-- AI Chat Widget --> <script src="YOUR_BACKEND_URL/embed/YOUR_AGENT_ID?token=YOUR_EMBED_TOKEN"></script> </body> </html>

Step 3: Alternative - Using an iframe

If you prefer to use an iframe directly, you can embed it like this:

<iframe src="YOUR_BACKEND_URL/embed/YOUR_AGENT_ID?token=YOUR_EMBED_TOKEN&theme=light&position=bottom-right" style="position: fixed; bottom: 20px; right: 20px; width: 400px; height: 600px; border: none; border-radius: 12px; box-shadow: 0 4px 20px rgba(0,0,0,0.15);" allow="microphone" title="AI Buddy Chat Widget" ></iframe>
๐Ÿ’ก Widget Features:
  • Resizable: Users can resize the widget by dragging the resize handle in the bottom-right corner
  • Draggable: Users can move the widget by dragging the header
  • Closeable: Users can close the widget using the X button in the header

4. Configuration Options

You can customize the widget behavior using URL parameters:

Parameter Description Values Default
token Embed authentication token (required) JWT string -
theme Widget color theme light, dark light
position Widget position on page bottom-right, bottom-left, top-right, top-left bottom-right

Example with Custom Configuration

<script src="YOUR_BACKEND_URL/embed/YOUR_AGENT_ID?token=YOUR_EMBED_TOKEN&theme=dark&position=bottom-left"></script>

Programmatic API

After the widget is loaded, you can control it programmatically:

// Open the chat widget window.AIBuddyWidget?.open(); // Close the chat widget window.AIBuddyWidget?.close(); // Toggle the chat widget window.AIBuddyWidget?.toggle(); // Send a message programmatically window.AIBuddyWidget?.sendMessage('Hello!');

Listening to Widget Events (iframe only)

If you're using an iframe, you can listen to events from the widget:

// Listen for messages from the embed widget window.addEventListener('message', function(event) {{ // Security: In production, validate event.origin if (event.data && event.data.type) {{ switch(event.data.type) {{ case 'aibuddy:close': // Widget was closed by user console.log('Widget closed'); // Hide iframe if needed document.getElementById('chat-iframe').style.display = 'none'; break; case 'aibuddy:resize': // Widget was resized by user console.log('Widget resized:', event.data.data); // event.data.data contains: {{ width, height }} break; case 'aibuddy:move': // Widget was moved by user console.log('Widget moved:', event.data.data); // event.data.data contains: {{ x, y }} break; case 'aibuddy:ready': // Widget is ready console.log('Widget ready:', event.data.data); break; }} }} }});

5. Testing Your Integration

๐Ÿงช Test Configuration

Token can also be passed as URL parameter: ?token=YOUR_TOKEN

6. Troubleshooting

โŒ Error: "Origin is not allowed"

Problem: Your domain is not in the allowed origins list.

Solution:
  1. Contact your administrator to add your domain to the "Allowed Domains" list in the AI Buddy dashboard
  2. Ensure you're using the exact domain (including protocol: https:// or http://)
  3. For subdomains, you may need to add each subdomain separately or use a wildcard pattern (e.g., *.example.com)

โŒ Error: "Missing required 'token' parameter"

Problem: The embed token is missing or invalid.

Solution:
  1. Verify that the token parameter is included in the script URL
  2. Check that the token hasn't expired (tokens typically expire after a set period)
  3. Generate a new embed token from the AI Buddy dashboard
  4. Ensure the token is URL-encoded if it contains special characters

โŒ Error: CORS policy blocked

Problem: The browser is blocking the request due to CORS (Cross-Origin Resource Sharing) policy.

Solution:
  1. Verify your domain is in the allowed origins list
  2. Check that you're using HTTPS (required for production)
  3. Ensure the backend URL is correct and accessible
  4. Check browser console (F12) for detailed CORS error messages

โŒ Widget not appearing

Problem: The chat widget doesn't show up on your page.

Solution:
  1. Check browser console (F12) for JavaScript errors
  2. Verify the script tag is correctly placed and the URL is valid
  3. Ensure no other scripts are blocking the widget initialization
  4. Check that your page has loaded completely (try placing the script before </body>)
  5. Test with the "Show Chat Widget" button above to verify your configuration

โŒ Mixed Content Error (HTTPS/HTTP)

Problem: Your page is served over HTTPS but trying to load the widget from HTTP (or vice versa).

Solution:
  1. Ensure both your website and the backend URL use the same protocol (preferably HTTPS)
  2. If your site is HTTPS, the backend URL must also be HTTPS
  3. Update the backend URL in your script tag to use HTTPS

โŒ Widget appears but chat doesn't work

Problem: The widget UI loads but messages aren't being sent or received.

Solution:
  1. Check browser console (F12) for network errors
  2. Verify the Agent ID is correct
  3. Ensure the backend API is accessible and responding
  4. Check that WebSocket connections are allowed (if used for real-time chat)
  5. Verify firewall/proxy settings aren't blocking API requests

7. Best Practices

Security

Performance

User Experience

Testing

โœ… Integration Complete! If you've followed all steps and your widget is working, congratulations! If you're experiencing issues, refer to the Troubleshooting section above or contact support.