Added SoftwareDevChatbot
This commit is contained in:
40
SoftwareDevChatbot/public/main.js
Normal file
40
SoftwareDevChatbot/public/main.js
Normal 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));
|
||||
}
|
||||
Reference in New Issue
Block a user