-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
116 lines (93 loc) · 3.32 KB
/
Copy pathscript.js
File metadata and controls
116 lines (93 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// script.js
const userInput = document.getElementById('user-input');
const chatMessages = document.getElementById('chat-messages');
const chatOutPut = document.getElementById('chat-output')
function appendMessage(message, isUser) {
const messageContainer = document.createElement('div');
messageContainer.className = isUser ? 'user-message' : 'bot-message';
const userName = isUser ? 'You' : 'ChatBot'; // Determine the username
messageContainer.textContent = `${userName}: ${message}`; // Include the username with the message
chatMessages.appendChild(messageContainer);
// Automatic Scroll for newer messages
chatOutPut.scrollTop = chatOutPut.scrollHeight;
}
async function callAPI(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.text();
return data
} catch (error) {
console.error('Error:', error);
return 'An error occurred while fetching data';
}
}
async function processUserInput() {
const userMessage = userInput.value;
userInput.value = '';
appendMessage(userMessage, true);
// Generate a random response from the bot
const responseData = await callAPI("http://142.58.89.218:8000/ask/?message=hello");
const words = responseData;
// for(const word of words){
// if (responseData[word]) {
// appendMessage(responseData[word], false); // Display the message associated with the keyword
// return; // Exit the loop if a match is found
// }
// }
appendMessage(words, false);
}
const clearChatButton = document.getElementById('clear-chat-button');
clearChatButton.addEventListener('click', function () {
clearChat();
});
function clearChat() {
chatMessages.innerHTML = ''; // This clears all the content inside the chat-messages container.
}
userInput.addEventListener('keyup', function (event) {
if (event.key === 'Enter') {
processUserInput();
}
});
// Pop-up menu buttons and functions
const menuButton = document.getElementById('menu-button');
const menuPopup = document.getElementById('menu-popup');
const resetButton = document.getElementById('reset-button');
const saveButton = document.getElementById('save-button');
const loadButton = document.getElementById('load-button');
const exitButton = document.getElementById('exit-button');
menuButton.addEventListener('click', function () {
toggleMenu();
});
function toggleMenu() {
if (menuPopup.style.display === 'block') {
menuPopup.style.display = 'none';
} else {
menuPopup.style.display = 'block';
}
}
// Handle menu options
resetButton.addEventListener('click', function () {
// Implement your reset logic here
alert('Chat Reseted.');
resetChat();
menuPopup.style.display = 'none';
});
saveButton.addEventListener('click', function () {
// Implement your save logic here
alert('Save button clicked');
});
loadButton.addEventListener('click', function () {
// Implement your load logic here
alert('Load button clicked');
});
exitButton.addEventListener('click', function () {
// Implement your exit logic here
menuPopup.style.display = 'none';
});
function resetChat(){
chatMessages.innerHTML = '';
userInput.value = '';
}