-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat.js
105 lines (79 loc) · 2.65 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
// Collapsible
var coll = document.getElementsByClassName("collapsible");
for (let i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function () {
this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.maxHeight) {
content.style.maxHeight = null;
} else {
content.style.maxHeight = content.scrollHeight + "px";
}
});
}
function getTime() {
let today = new Date();
hours = today.getHours();
minutes = today.getMinutes();
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
let time = hours + ":" + minutes;
return time;
}
// Gets the first message
function firstBotMessage() {
let firstMessage = "How's it going?"
document.getElementById("botStarterMessage").innerHTML = '<p class="botText"><span>' + firstMessage + '</span></p>';
let time = getTime();
$("#chat-timestamp").append(time);
document.getElementById("userInput").scrollIntoView(false);
}
firstBotMessage();
// Retrieves the response
function getHardResponse(userText) {
let botResponse = getBotResponse(userText);
let botHtml = '<p class="botText"><span>' + botResponse + '</span></p>';
$("#chatbox").append(botHtml);
document.getElementById("chat-bar-bottom").scrollIntoView(true);
}
//Gets the text text from the input box and processes it
function getResponse() {
let userText = $("#textInput").val();
if (userText == "") {
userText = "I love Code Palace!";
}
let userHtml = '<p class="userText"><span>' + userText + '</span></p>';
$("#textInput").val("");
$("#chatbox").append(userHtml);
document.getElementById("chat-bar-bottom").scrollIntoView(true);
setTimeout(() => {
getHardResponse(userText);
}, 1000)
}
// Handles sending text via button clicks
function buttonSendText(sampleText) {
let userHtml = '<p class="userText"><span>' + sampleText + '</span></p>';
$("#textInput").val("");
$("#chatbox").append(userHtml);
document.getElementById("chat-bar-bottom").scrollIntoView(true);
//Uncomment this if you want the bot to respond to this buttonSendText event
// setTimeout(() => {
// getHardResponse(sampleText);
// }, 1000)
}
function sendButton() {
getResponse();
}
function thumbsUpButton() {
buttonSendText("Okay, it was helpful...!!!")
}
// Press enter to send a message
$("#textInput").keypress(function (e) {
if (e.which == 13) {
getResponse();
}
});