(function() { 'use strict'; // Prevent duplicate widgets if (document.getElementById('base44-chatbot-widget')) return; // Embed configuration directly const widgetConfig = {"business_id":"68f0072fb3ff868b42eb02ca","greeting_message":"Hi! Welcome to KC Air Conditioning . How can I help you today?","placeholder_text":"Type your message...","primary_color":"#02caf2","position":"bottom-right","button_style":"circle","button_icon":"live-chat","button_size":"medium","window_size":"medium","animation":"fade","theme":"modern","show_badge":false,"pulse_effect":true,"custom_image_url":null,"fallback_message":"I'm not sure about that. Please contact us at {contact_email} for more help.","id":"68f0072f88172291f5ee30cf","created_date":"2025-10-15T20:42:23.823000","updated_date":"2025-10-15T21:48:07.925000","created_by_id":"68ee89252f34796ff678736e","created_by":"ramo1312@gmail.com","is_sample":false}; const chatbotId = 'chatbot_1760560942814_2lnl93uam'; const apiBaseUrl = 'https://chat-flow-ai-b643e540.base44.app'; const buttonIcon = '📞'; const customImageUrl = null; const companyLogoUrl = null; const enableContactCapture = false; console.log('Chatbot widget loaded. API Base URL:', apiBaseUrl); // Wait for DOM to be ready if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initChatbot); } else { initChatbot(); } function initChatbot() { const primaryColor = widgetConfig.primary_color; let conversationId = null; const visitorId = 'visitor_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9); let messages = [{ role: 'assistant', content: widgetConfig.greeting_message }]; let isOpen = false; let isLoading = false; let showContactForm = enableContactCapture; // Initialize based on config let visitorInfo = { email: '', phone: '' }; // Function to parse markdown links function parseMessageLinks(text) { return text.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '$1'); } // Create widget container with maximum z-index const widgetContainer = document.createElement('div'); widgetContainer.id = 'base44-chatbot-widget'; widgetContainer.style.cssText = 'position: fixed; right: 20px; bottom: 20px; z-index: 2147483647; font-family: system-ui, -apple-system, sans-serif; pointer-events: none;'; // Create toggle button const toggleButton = document.createElement('button'); toggleButton.id = 'base44-chat-toggle'; toggleButton.style.cssText = ` width: 60px; height: 60px; border-radius: 50%; background: ${primaryColor}; border: none; cursor: pointer; box-shadow: 0 4px 12px rgba(0,0,0,0.15); display: flex; align-items: center; justify-content: center; transition: transform 0.2s; color: white; font-size: 24px; pointer-events: auto; `; // Set button content (custom image or icon) if (customImageUrl) { toggleButton.innerHTML = 'Chat'; } else { toggleButton.innerHTML = buttonIcon; } toggleButton.onclick = window.toggleBase44Chat = function() { isOpen = !isOpen; const btn = document.getElementById('base44-chat-toggle'); if (btn) { if (customImageUrl) { // Keep image, just rotate or change opacity to show state btn.style.transform = isOpen ? 'rotate(90deg)' : 'rotate(0deg)'; } else { btn.innerHTML = isOpen ? '✕' : buttonIcon; btn.style.fontSize = isOpen ? '28px' : '24px'; } } renderWidget(); }; // Create chat window const chatWindow = document.createElement('div'); chatWindow.id = 'base44-chat-window'; chatWindow.style.cssText = ` position: absolute; bottom: 80px; right: 0; width: 380px; height: 600px; background: white; border-radius: 16px; box-shadow: 0 10px 40px rgba(0,0,0,0.15); display: none; flex-direction: column; overflow: hidden; pointer-events: auto; `; // Header with logo const header = document.createElement('div'); header.style.cssText = ` background: ${primaryColor}; color: white; padding: 20px; display: flex; justify-content: space-between; align-items: center; `; const logoHtml = companyLogoUrl ? 'Company logo' : '
💬
'; header.innerHTML = `
${logoHtml}
Chat Support
Online
`; // Messages container const messagesContainer = document.createElement('div'); messagesContainer.id = 'base44-messages'; messagesContainer.style.cssText = ` flex: 1; overflow-y: auto; padding: 20px; background: #f9fafb; `; // Input container const inputContainer = document.createElement('div'); inputContainer.style.cssText = ` padding: 16px; border-top: 1px solid #e5e7eb; background: white; `; inputContainer.innerHTML = `
`; // Assemble chat window chatWindow.appendChild(header); chatWindow.appendChild(messagesContainer); chatWindow.appendChild(inputContainer); // Assemble widget widgetContainer.appendChild(chatWindow); widgetContainer.appendChild(toggleButton); document.body.appendChild(widgetContainer); // Handle Enter key document.addEventListener('keypress', function(e) { if (e.key === 'Enter' && document.activeElement.id === 'base44-chat-input') { sendBase44Message(); } }); // Render messages function renderMessages() { const container = document.getElementById('base44-messages'); if (!container) return; let html = ''; // Contact form - only show if enableContactCapture is true if (showContactForm) { html += `

Let's get started! 👋

`; } html += messages.map(msg => `
${parseMessageLinks(msg.content)}
`).join(''); if (isLoading) { html += `
Typing...
`; } container.innerHTML = html; container.scrollTop = container.scrollHeight; // Attach contact form handlers const contactForm = document.getElementById('base44-contact-form'); if (contactForm) { contactForm.onsubmit = async function(e) { e.preventDefault(); visitorInfo.email = document.getElementById('base44-email').value; visitorInfo.phone = document.getElementById('base44-phone').value; if (conversationId) { try { await fetch(apiBaseUrl + '/functions/updateVisitorInfo', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ conversation_id: conversationId, visitor_email: visitorInfo.email, visitor_phone: visitorInfo.phone }) }); } catch (err) { console.error('Error saving contact:', err); } } showContactForm = false; messages.push({ role: 'assistant', content: 'Thank you! How can I help you today?' }); renderMessages(); }; const skipBtn = document.getElementById('base44-skip-contact'); if (skipBtn) { skipBtn.onclick = function() { showContactForm = false; renderMessages(); }; } } } // Render widget state function renderWidget() { const chatWin = document.getElementById('base44-chat-window'); if (!chatWin) return; if (isOpen) { chatWin.style.display = 'flex'; renderMessages(); } else { chatWin.style.display = 'none'; } } // Send message window.sendBase44Message = async function() { const input = document.getElementById('base44-chat-input'); if (!input) return; const message = input.value.trim(); if (!message || isLoading) return; messages.push({ role: 'user', content: message }); input.value = ''; isLoading = true; renderMessages(); try { const fetchUrl = apiBaseUrl + '/functions/chat'; console.log('Sending message to:', fetchUrl); console.log('Payload:', { chatbot_id: chatbotId, message, conversation_id: conversationId, visitor_id: visitorId }); const response = await fetch(fetchUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ chatbot_id: chatbotId, message: message, conversation_id: conversationId, visitor_id: visitorId, visitor_email: visitorInfo.email, visitor_phone: visitorInfo.phone }) }); console.log('Response status:', response.status); if (!response.ok) { throw new Error('Network response was not ok: ' + response.status); } const data = await response.json(); console.log('Response data:', data); conversationId = data.conversation_id; messages.push({ role: 'assistant', content: data.response }); } catch (error) { console.error('Chat error:', error); messages.push({ role: 'assistant', content: 'Sorry, something went wrong. Please try again.' }); } isLoading = false; renderMessages(); }; // Add spin animation const style = document.createElement('style'); style.textContent = '@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }'; document.head.appendChild(style); // Initial render renderMessages(); } })();