-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.js
147 lines (109 loc) · 3.6 KB
/
chat.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// JavaScript
var coll = document.getElementsByClassName("collapsible");
let currentFieldIndex = 0;
let recognition = null;
function speakText(text) {
const utterance = new SpeechSynthesisUtterance(text);
window.speechSynthesis.speak(utterance);
}
if (currentFieldIndex == 0) {
speakText("Welcome to the Yoga App");
}
function getTime() {
let today = new Date();
let hours = today.getHours();
let minutes = today.getMinutes();
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
let time = hours + ":" + minutes;
return time;
}
// function startListening() {
// const fieldId = fields[currentFieldIndex];
// const labelElement = document.querySelector(`label[for="${fieldId}"]`);
// const inputElement = document.getElementById(fieldId);
// if (recognition) {
// recognition.stop();
// }
// recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
// recognition.lang = "en-US";
// recognition.onstart = function () {
// labelElement.textContent = `Listening for ${labelElement.textContent}...`;
// };
// recognition.onresult = function (event) {
// const transcript = event.results[0][0].transcript;
// inputElement.value = transcript;
// };
// recognition.onend = function () {
// labelElement.textContent = `Spoken ${labelElement.textContent}`;
// currentFieldIndex++;
// if (currentFieldIndex < fields.length) {
// startListening();
// } else {
// currentFieldIndex = 0;
// }
// };
// speakText(fieldId);
// setTimeout(() => {
// recognition.start();
// }, 2000);
// }
function firstBotMessage() {
speakText("Hello, what is your name");
let firstMessage = "Hello, what is your name ?";
document.getElementById("chatbox").innerHTML =
'<p class="botText"><span>' +
firstMessage +
"</span></p>";
let time = getTime();
let chatTimestamp = document.createElement("div");
chatTimestamp.setAttribute("id", "chat-timestamp");
chatTimestamp.innerText = time;
document.getElementById("chatbox").appendChild(chatTimestamp);
document.getElementById("textInput").scrollIntoView(false);
}
firstBotMessage();
function getHardResponse(userText) {
let botResponse = getBotResponse(userText);
const delimiter = "_";
const parts = botResponse.split(delimiter);
for (let i = 0; i < parts.length; i++) {
let botHtml = '<p class="botText"><span>' + parts[i] + "</span></p>";
document.getElementById("chatbox").insertAdjacentHTML("beforeend", botHtml);
document.getElementById("chat-bar-bottom").scrollIntoView(true);
}
if (userText.toLowerCase().includes("")) {
// Trigger the hand wave animation
playWaveAnimation();
}
}
function getResponse() {
let userText = document.getElementById("textInput").value;
if (userText == "") {
userText = "";
}
let userHtml = '<p class="userText"><span>' + userText + "</span></p>";
document.getElementById("textInput").value = "";
document.getElementById("chatbox").insertAdjacentHTML("beforeend", userHtml);
document.getElementById("chat-bar-bottom").scrollIntoView(true);
setTimeout(() => {
getHardResponse(userText);
}, 1000);
}
function buttonSendText(sampleText) {
}
function sendButton() {
getResponse();
}
function heartButton() {
buttonSendText("Heart clicked!");
}
document.getElementById("textInput").addEventListener("keypress", function (e) {
if (e.which == 13) {
getResponse();
}
});