Added SoftwareDevChatbot

This commit is contained in:
2025-11-11 10:59:18 +01:00
parent 0ffeb914c3
commit 378228b208
11 changed files with 1123 additions and 2 deletions

View File

@@ -0,0 +1,40 @@
const chatLog = document.getElementById('chat-log');
const userInput = document.getElementById('user-input');
function sendMessage() {
const message = userInput.value;
// Display user's message
displayMessage('user', message);
// Call OpenAI API to get chatbot's response
getChatbotResponse(message);
// Clear user input
userInput.value = '';
}
function displayMessage(sender, message) {
const messageElement = document.createElement('div');
messageElement.classList.add('message', sender);
// Wrap the message in a <p> tag
const messageParagraph = document.createElement('p');
messageParagraph.innerText = message;
// Append the <p> tag to the message element
messageElement.appendChild(messageParagraph);
chatLog.appendChild(messageElement);
}
function getChatbotResponse(userMessage) {
// Make a request to your server with the user's message
fetch('/getChatbotResponse', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ userMessage }),
})
.then(response => response.json())
.then(data => {
// Display chatbot's response
displayMessage('chatbot', data.chatbotResponse);
})
.catch(error => console.error('Error:', error));
}