Spaces:
Runtime error
Runtime error
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Chatbot</title> | |
<style> | |
/* Tambahkan gaya CSS Anda di sini */ | |
body { | |
font-family: Arial, sans-serif; | |
} | |
.container { | |
max-width: 800px; | |
margin: 0 auto; | |
padding: 20px; | |
} | |
.user-message { | |
background-color: #007BFF; | |
color: white; | |
padding: 10px; | |
border-radius: 10px; | |
margin-bottom: 10px; | |
text-align: right; | |
} | |
.bot-message { | |
background-color: #28A745; | |
color: white; | |
padding: 10px; | |
border-radius: 10px; | |
margin-bottom: 10px; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<h1>Chatbot</h1> | |
<div id="chat-container"> | |
<!-- Pesan dari chatbot akan ditambahkan di sini --> | |
</div> | |
<form id="chat-form"> | |
<input type="text" id="user-input" placeholder="Tulis pesan Anda..."> | |
<button type="submit">Kirim</button> | |
</form> | |
</div> | |
<script> | |
// Script JavaScript untuk mengirim pesan ke server dan menerima balasan | |
document.addEventListener('DOMContentLoaded', function () { | |
const chatContainer = document.getElementById('chat-container'); | |
const chatForm = document.getElementById('chat-form'); | |
const userInput = document.getElementById('user-input'); | |
chatForm.addEventListener('submit', function (e) { | |
e.preventDefault(); | |
const userMessage = userInput.value.trim(); | |
if (userMessage === '') return; | |
// Tampilkan pesan pengguna | |
chatContainer.innerHTML += '<div class="user-message">' + userMessage + '</div>'; | |
userInput.value = ''; | |
// Kirim pesan ke server (ganti dengan endpoint yang sesuai) | |
fetch('/', { | |
method: 'POST', | |
body: new URLSearchParams({ user_input: userMessage }), // Kirim data sebagai URLSearchParams | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded' // Set header sesuai dengan data yang dikirim | |
} | |
}) | |
.then(response => response.json()) | |
.then(data => { | |
// Tampilkan balasan dari server | |
chatContainer.innerHTML += '<div class="bot-message">' + data.response + '</div>'; | |
}) | |
.catch(error => { | |
console.error('Error:', error); | |
}); | |
}); | |
}); | |
</script> | |
</body> | |
</html> | |