|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | +<title>Chatbot</title> |
| 5 | +<style> |
| 6 | + body { |
| 7 | + font-family: sans-serif; |
| 8 | + display: flex; |
| 9 | + justify-content: center; |
| 10 | + align-items: center; |
| 11 | + min-height: 100vh; |
| 12 | + background-color: #f0f0f0; |
| 13 | + } |
| 14 | + |
| 15 | + #chat-container { |
| 16 | + background-color: #fff; |
| 17 | + border-radius: 10px; |
| 18 | + padding: 20px; |
| 19 | + box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.1); |
| 20 | + width: 400px; |
| 21 | + } |
| 22 | + |
| 23 | + h1 { |
| 24 | + text-align: center; |
| 25 | + margin-bottom: 20px; |
| 26 | + } |
| 27 | + |
| 28 | + #chat-history { |
| 29 | + height: 300px; |
| 30 | + overflow-y: scroll; |
| 31 | + } |
| 32 | + |
| 33 | + .user-message { |
| 34 | + text-align: right; |
| 35 | + padding: 10px; |
| 36 | + background-color: #f0f0f0; |
| 37 | + border-radius: 10px; |
| 38 | + margin-bottom: 5px; |
| 39 | + } |
| 40 | + |
| 41 | + .bot-message { |
| 42 | + text-align: left; |
| 43 | + padding: 10px; |
| 44 | + background-color: #e0f0e0; |
| 45 | + border-radius: 10px; |
| 46 | + margin-bottom: 5px; |
| 47 | + } |
| 48 | + |
| 49 | + form { |
| 50 | + display: flex; |
| 51 | + } |
| 52 | + |
| 53 | + input { |
| 54 | + flex-grow: 1; |
| 55 | + margin-right: 10px; |
| 56 | + padding: 10px; |
| 57 | + border: 1px solid #ccc; |
| 58 | + border-radius: 5px; |
| 59 | + } |
| 60 | + |
| 61 | + button { |
| 62 | + background-color: #4CAF50; |
| 63 | + color: white; |
| 64 | + border: none; |
| 65 | + padding: 10px 15px; |
| 66 | + border-radius: 5px; |
| 67 | + cursor: pointer; |
| 68 | + } |
| 69 | + #loader { |
| 70 | + display: none; /* Hide by default */ |
| 71 | + position: absolute; |
| 72 | + top: 50%; |
| 73 | + left: 50%; |
| 74 | + transform: translate(-50%, -50%); |
| 75 | +} |
| 76 | + </style> |
| 77 | +</head> |
| 78 | +<body> |
| 79 | + <div id="chat-container"> |
| 80 | + <h1>Coding Money Chatbot</h1> |
| 81 | + <div id="chat-history"></div> |
| 82 | + <form id="chat-form"> |
| 83 | + <input type="text" id="user-input" placeholder="Enter your message"> |
| 84 | + <button type="submit">Send</button> |
| 85 | + </form> |
| 86 | +</div> |
| 87 | +<div id="loader"> |
| 88 | + <img src="loader.gif" width="150px" alt="Loading..."> |
| 89 | +</div> |
| 90 | + <script> |
| 91 | + const chatHistory = document.getElementById('chat-history'); |
| 92 | + const userInput = document.getElementById('user-input'); |
| 93 | + const form = document.getElementById('chat-form'); |
| 94 | + |
| 95 | + async function sendMessage() { |
| 96 | + const userMessage = userInput.value; |
| 97 | + userInput.value = ''; // Clear input field |
| 98 | + console.log(userMessage) |
| 99 | + try { |
| 100 | + const response = await fetch('/chat', { |
| 101 | + method: 'POST', |
| 102 | + headers: { |
| 103 | + 'Content-Type': 'application/json', |
| 104 | + }, |
| 105 | + body: JSON.stringify({ userInput: userMessage }), |
| 106 | + }); |
| 107 | + |
| 108 | + const data = await response.json(); |
| 109 | + console.log(data) |
| 110 | + const botMessage = data.response; |
| 111 | + console.log(botMessage) |
| 112 | + // Add chat message to the chat history |
| 113 | + chatHistory.innerHTML += `<div class="user-message">${userMessage}</div>`; |
| 114 | + chatHistory.innerHTML += `<div class="bot-message">${botMessage}</div>`; |
| 115 | + |
| 116 | + // Scroll to the bottom of the chat history |
| 117 | + chatHistory.scrollTop = chatHistory.scrollHeight; |
| 118 | + } catch (error) { |
| 119 | + console.error('Error:', error); |
| 120 | + // Handle errors gracefully, e.g., display an error message to the user |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + form.addEventListener('submit', (event) => { |
| 125 | + event.preventDefault(); // Prevent form submission |
| 126 | + const loader = document.getElementById('loader'); |
| 127 | + loader.style.display = 'block'; // Show the loader |
| 128 | + sendMessage().finally(() => { |
| 129 | + loader.style.display = 'none'; // Hide the loader after the message is sent |
| 130 | + });; |
| 131 | + }); |
| 132 | + </script> |
| 133 | +</body> |
| 134 | +</html> |
0 commit comments