← Level 5 – Inventor

Lesson 7 of 8 ⏱ 40 minutes Premium

Your own chatbot

Build a chat window on your website and connect it to the server from the last lesson.

Show me: the chat window

A chat is three things: a list of messages, an input field, and a Send button.

<div class="chat">
  <div id="messages" class="chat-messages"></div>
  <form id="chat-form" class="chat-form">
    <input id="chat-input" type="text" placeholder="Type your question…" autocomplete="off" />
    <button type="submit">Send</button>
  </form>
</div>
.chat { border: 1px solid #ddd; border-radius: 12px; max-width: 420px; }
.chat-messages { height: 300px; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.message { padding: 8px 12px; border-radius: 12px; max-width: 80%; }
.message.me { background: #4f46e5; color: white; align-self: flex-end; }
.message.bot { background: #f0f0f0; align-self: flex-start; }
.chat-form { display: flex; gap: 8px; padding: 12px; border-top: 1px solid #ddd; }
.chat-form input { flex: 1; }

Connecting to the server

const messages = document.querySelector('#messages');
const form = document.querySelector('#chat-form');
const input = document.querySelector('#chat-input');

function addMessage(text, who) {
  const el = document.createElement('div');
  el.className = `message ${who}`;
  el.textContent = text;
  messages.appendChild(el);
  messages.scrollTop = messages.scrollHeight; // scroll down to the new message
}

form.addEventListener('submit', async (e) => {
  e.preventDefault();
  const question = input.value.trim();
  if (!question) return;

  addMessage(question, 'me');
  input.value = '';
  addMessage('Thinking…', 'bot');

  const r = await fetch('/ask', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ question }),
  });
  const data = await r.json();

  messages.lastChild.textContent = data.text; // replace "Thinking…" with the answer
});

Everything in that code you already know: addEventListener, createElement, fetch with POST. The only new part is the idea of putting it all together into a chat.

💡

“Thinking…” is a small but important trick. AI takes a few seconds to answer, without that message, a visitor might think the chat is broken. Good programmers always show that something is happening.

Your turn 💪

  1. Build the chat following the example and match the colors to your website.
  2. In the system prompt on the server (lesson 6), use the prompt you wrote for your project in lesson 5: that turns a generic AI into your own helper.
  3. Test it: ask 5 questions a visitor might have. Does the bot answer correctly? Refine the prompt.
  4. Bonus: add a greeting message from the bot that appears right after the page loads (“Hi! Ask me anything about our school.”).
⚠️

Chat security: add a sentence to the system prompt like “Only answer questions about [your website]. Politely decline to answer anything else.”, otherwise the bot will happily write anything to visitors, even things unrelated to your website.

✅

Check: A chat runs on your website: you type a question, your bubble appears, then “Thinking…”, then the bot’s answer. The bot talks like a helper for your project.

What to take away from this lesson