-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchatbot-script.js
More file actions
122 lines (108 loc) · 3.77 KB
/
Copy pathchatbot-script.js
File metadata and controls
122 lines (108 loc) · 3.77 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
117
118
119
120
121
122
let currentState = "initial";
const chatPanel = document.getElementById('chat-panel');
window.addEventListener('load', () => {
chatPanel.style.transform = "translateX(100%)";
sendChatMessage("hello");
});
document.getElementById('chat-tab').addEventListener('click', () => {
chatPanel.style.transform = chatPanel.style.transform === "translateX(100%)" ? "translateX(0)" : "translateX(100%)";
});
function sendMessage() {
const inputField = document.getElementById('userInput');
const message = inputField.value;
inputField.value = '';
processMessage(message);
}
function processMessage(message) {
displayUserMessage(message);
updateState(message);
sendChatMessage(message);
}
function sendChatMessage(message) {
const chatContainer = document.getElementById('chat-container');
const state = currentState;
fetch('https://ie121.brighton.domains/chat-api/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ message: message, state: state })
})
.then(response => response.json())
.then(data => {
displayBotResponse(data.response);
clearQuickResponseButtons();
switch (currentState) {
case "initial":
addQuickResponseButtons(["Hello", "Help", "More Info"]);
break;
case "asking_use":
addQuickResponseButtons(["Gaming", "Work", "General Use"]);
break;
case "recommend_components":
addQuickResponseButtons(["CPU", "GPU", "RAM"]);
break;
case "follow_up":
addQuickResponseButtons(["Yes, more info", "No, thank you"]);
break;
}
})
.catch(error => console.error('Error:', error));
}
function clearQuickResponseButtons() {
const existingButtons = document.querySelector('#chat-container .d-flex');
if (existingButtons) {
existingButtons.remove();
}
}
function updateState(message) {
switch (currentState) {
case "initial":
currentState = "asking_use";
break;
case "asking_use":
currentState = "recommend_components";
break;
case "recommend_components":
currentState = "follow_up";
break;
default:
currentState = "initial";
break;
}
}
function displayUserMessage(message) {
const chatContainer = document.getElementById('chat-container');
const userDiv = document.createElement('div');
userDiv.className = 'text-end';
userDiv.textContent = message;
chatContainer.appendChild(userDiv);
}
function displayBotResponse(message) {
const chatContainer = document.getElementById('chat-container');
const botDiv = document.createElement('div');
botDiv.className = 'text-start';
botDiv.textContent = message;
chatContainer.appendChild(botDiv);
}
function addQuickResponseButtons(labels) {
const chatContainer = document.getElementById('chat-container');
const buttonContainer = document.createElement('div');
buttonContainer.className = 'd-flex justify-content-start mt-2';
labels.forEach(label => {
const button = createQuickButton(label);
buttonContainer.appendChild(button);
});
chatContainer.appendChild(buttonContainer);
}
function createQuickButton(label) {
const button = document.createElement('button');
button.className = 'btn btn-outline-primary me-2';
button.textContent = label;
button.onclick = function () { sendQuickMessage(label); };
return button;
}
function sendQuickMessage(message) {
displayUserMessage(message);
sendChatMessage(message);
}