Balkees Test – MEN MAGAZINE .

sendButton.addEventListener(‘click’, async () => {
const userMessage = messageInput.value;
if (!userMessage) return;

messageInput.value =  »;

chatBox.innerHTML += `

You: ${userMessage}

`;
chatBox.scrollTop = chatBox.scrollHeight;

try {
const response = await fetch(‘http://92.112.194.164:5000/chat’, {
method: ‘POST’,
headers: {
‘Content-Type’: ‘application/json’
},
body: JSON.stringify({ message: userMessage })
});

if (!response.ok) throw new Error(‘Network response was not ok’);

const result = await response.json();
chatBox.innerHTML += `

Bot: ${result.response}

`;
chatBox.scrollTop = chatBox.scrollHeight;
} catch (error) {
console.error(‘Error:’, error);
chatBox.innerHTML += `

Bot: Error sending message

`;
chatBox.scrollTop = chatBox.scrollHeight;
}
});