Howie Chat Widget Overview 🤖

The Howie chat widget provides an interactive interface for users to converse with an AI-powered assistant and seamlessly transfer to a human agent when necessary. It comprises a simple HTML/CSS frontend (index.php), client-side JavaScript logic (script.js), a CORS helper (headercor.php), and a Google site verification stub. Together, these files render the chat UI, manage session state, handle HTTP/WebSocket communication, and ensure cross-origin compatibility.


howie/index.php 📄

This file defines the chat widget’s HTML structure and includes the necessary stylesheet and script. It presents an intro form, chat messages area, typing indicator, and input controls.

Key Elements

  • #chat-widget: Root container for the widget.
  • #chat-header: Toggles the chat open/close.
  • #intro-form: Collects user's name, email, and phone.
  • #chat-content: Hidden by default; shows messages and input.
  • #chat-messages: Displays conversation bubbles.
  • #typing-indicator: Shows “Howie is typing.”
  • #user-input & Send button: Captures user messages.

HTML Snippet

<div id="chat-widget">
  <div id="chat-header" onclick="toggleChat()">Chat with Tech</div>
  <div id="chat-container">
    <div id="intro-form">
      <p>Please enter your details to start chatting:</p>
      <input type="text" id="intro-name" placeholder="Your Name" required>
      <input type="email" id="intro-email" placeholder="Your Email" required>
      <input type="tel" id="intro-phone" placeholder="Your Phone" required>
      <button onclick="submitIntro()">Start Chat</button>
    </div>
    <div id="chat-content" style="display: none;">
      <div id="chat-messages"></div>
      <div id="typing-indicator" style="display: none;">Howie is typing.</div>
      <div id="chat-input">
        <input type="text" id="user-input" placeholder="Type your message.">
        <button onclick="sendMessage()">Send</button>
      </div>
    </div>
  </div>
</div>
<script src="script.js"></script>

howie/script.js 🚀

This script powers the chat widget’s interactivity, handling:

  • Session state and user details
  • Intro submission via HTTP POST
  • Message sending (bot vs. agent)
  • WebSocket registration, messaging, and status updates
  • Fallback to contact form and session cleanup

State Variables

let sessionId = null;         // Current chat session ID
let awaitingContact = false;  // Waiting for human-agent fallback
let ws = null;                // WebSocket connection
let userInfo = null;          // { name, email, phone }
let isAgentChat = false;      // True when chatting with human

Core Functions

  • toggleChat()
    Opens/closes the widget. Closes session if chat is closing without fallback.
  • submitIntro()
    Validates intro form, posts initial greeting, stores sessionId, toggles UI, and opens WebSocket.
  • sendMessage()
    Displays user message, then either sends via WebSocket (agent) or HTTP POST (bot). Handles typing indicator, human-transfer prompts, and fallback to contact form.
  • showContactForm(messagesDiv)
    Injects a simple form to collect details when no agents are available.
  • submitContact()
    Posts contact info to /submit-contact, shows confirmation, and ends session.
  • connectWebSocket(sessionId)
    Establishes a WebSocket to receive agent status updates and real-time messages. Sets isAgentChat on “Connected to Agent” status.
  • closeSession()
    Posts to /close-session, clears sessionId, closes WebSocket, and resets flags.

API Endpoints

{
  "title": "Chat with Howie",
  "description": "Send and receive bot responses",
  "method": "POST",
  "baseUrl": "http://localhost:3000",
  "endpoint": "/chat",
  "headers": [
    { "key": "Content-Type", "value": "application/json", "required": true }
  ],
  "bodyType": "json",
  "requestBody": "{\n  \"message\": \"Hello\",\n  \"session_id\": \"<sessionId>\",\n  \"user_name\": \"Alice\",\n  \"user_email\": \"[email protected]\",\n  \"user_phone\": \"1234567890\"\n}",
  "responses": {
    "200": {
      "description": "Bot reply",
      "body": "{\n  \"reply\": \"Hi there! How can I help?\",\n  \"session_id\": \"abc123\"\n}"
    },
    "400": {
      "description": "Validation error",
      "body": "{\n  \"error\": \"Please fill all fields\"\n}"
    }
  }
}
{
  "title": "Submit Contact Fallback",
  "description": "Records user contact when no agents available",
  "method": "POST",
  "baseUrl": "http://localhost:3000",
  "endpoint": "/submit-contact",
  "headers": [
    { "key": "Content-Type", "value": "application/json", "required": true }
  ],
  "bodyType": "json",
  "requestBody": "{\n  \"session_id\": \"abc123\",\n  \"name\": \"Alice\",\n  \"email\": \"[email protected]\",\n  \"phone\": \"1234567890\"\n}",
  "responses": {
    "200": {
      "description": "Confirmation reply",
      "body": "{\n  \"reply\": \"Thank you! We'll be in touch soon.\"\n}"
    }
  }
}
{
  "title": "Close Chat Session",
  "description": "Ends the current chat session",
  "method": "POST",
  "baseUrl": "http://localhost:3000",
  "endpoint": "/close-session",
  "headers": [
    { "key": "Content-Type", "value": "application/json", "required": true }
  ],
  "bodyType": "json",
  "requestBody": "{\n  \"session_id\": \"abc123\"\n}",
  "responses": {
    "200": {
      "description": "Session closed"
    }
  }
}

Chat Flow Diagram

flowchart TD
  U[User clicks Chat] --> I{Has session?}
  I -- No --> F[Show Intro Form]
  F --> S1[submitIntro() → POST /chat]
  S1 --> B1[Show Bot Reply & open WS]
  I -- Yes --> WS[connectWebSocket()]
  B1 --> M[sendMessage() → POST /chat]
  M -->|Transfer to Agent| WS2[connectWebSocket()]
  M -->|No Agents| CF[showContactForm()]
  CF --> S2[submitContact() → POST /submit-contact]
  WS2 --> A[Agent messages via WS]
  A --> M2[sendMessage() via WS]
  U2[User closes Chat] --> C[closeSession() → POST /close-session]

headercor.php 🛠️

This helper sets permissive CORS headers and hijacks jQuery’s AJAX to prepend a proxy URL when needed. It ensures all outbound requests include the session ID and respect CORS policies.

<?php header("Access-Control-Allow-Origin: *"); ?>
<script>
  // Force all $.ajax calls through CORS proxy
  $.ajaxSetup({
    beforeSend: function(xhr, options) {
      options.url = <?= $header_view_ignore ? '"https://tech.freightanywhere.com/cors.php?url="' : '' ?>
                    + options.url + '&s_id=<?php echo session_id(); ?>';
    }
  });
  // Override $.post
  var originalPost = $.post;
  $.post123 = function(){
    if (!arguments[0].includes("freightanywhere")) {
      arguments[0] = <?= $header_view_ignore ? '"https://tech.freightanywhere.com/cors.php?url="' : '' ?>
                     + arguments[0] + '&s_id=<?php echo session_id(); ?>';
    }
    return originalPost.apply($, arguments);
  };
</script>

googlec13345a465e3b4fa.html 🔐

A simple HTML file for Google site verification. It maps a fixed URL to a meta tag required by Google Search Console.

google-site-verification: googlec13345a465e3b4fa.html

Enjoy seamless AI and human-assisted chats!