-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmychat.html
150 lines (141 loc) · 5.21 KB
/
mychat.html
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
148
149
150
<!DOCTYPE html>
<html>
<head>
<title>Message</title>
<style>
.box {
position: fixed;
bottom: 10px;
right: 30%;
}
td.bot>span {
font: 15px arial, sans-serif;
}
td.user>span {
font: 15px arial, sans-serif;
}
td.bot {
text-align:left;
}
td.user {
text-align:right;
}
table.chat{
width:70%;
overflow-y: scroll;
border-style: dashed;
max-height:40px;
background-color: pink ;
border-width: 0px;
padding-top: 10px;
padding-bottom: 15px;
padding-right: 20px;
padding-left: 15px;
border-radius: 40px;
overflow-y: scroll;
max-height:40px;
}
</style>
</head>
<body onload = "initial()" bgcolor="grey">
<table class = "chat" id="tbl" align=center max-height= "50px">
</table>
<div class = "box" max-height= "50px">
</div>
<div align="center">
<div align="footer">
<table>
<tr>
<td>
<input type=text id="txt" name="user_input" size="110">
</td>
<td>
<input type="button" id="button_send" value="Ask me" placeholder="Ask me" onclick="chatbot()"size="0">
</td>
</tr>
</table>
</div>
</div>
<script>
function initial()
{
var input_txtbx = document.getElementById("txt");
input_txtbx.addEventListener("keyup", function(event) {
if (event.keyCode === 13)
{
event.preventDefault();
document.getElementById("button_send").click();
input_txtbx.value = "";
}
});
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange=function(){
if( this.readyState == 4 && this.status == 200)
{
if(this.responseText == "")
{
window.location.href = "/";
return false;
}
var reply = JSON.parse(this.responseText);
for(var i=0;i<reply['bot'].length;i++)
{
add_bot_text(reply['bot'][i], "tbl");
add_user_text(reply['user'][i], "tbl");
}
add_bot_text("Hey, " + reply['username'] + "!","tbl");
window.scrollTo(0, document.body.scrollHeight);
}
};
xhttp.open("GET","/get_userrname",true);
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.send();
}
function chatbot()
{
user_input = document.getElementById("txt").value.trim().toLowerCase();
if(user_input == "bye")
{
window.location.href = "/logout";
return false;
}
add_user_text(user_input,"tbl");
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if( this.readyState == 4 && this.status == 200)
{
add_bot_text(this.responseText,"tbl");
document.getElementById("txt").value="";
window.scrollTo(0, document.body.scrollHeight);
}
};
data = { "user": user_input};
xhttp.open("POST","/chatresponse",true);
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.send(JSON.stringify(data));
}
function add_user_text(user_, tbl_id)
{
tbl_row = document.createElement("tr");
tbl_d = document.createElement("td");
tbl_span = document.createElement("span");
tbl_d.setAttribute("class","user");
tbl_span.appendChild( document.createTextNode(user_) );
tbl_d.appendChild(tbl_span);
tbl_row.appendChild(tbl_d);
document.getElementById(tbl_id).appendChild(tbl_row);
}
function add_bot_text(bot_, tbl_id)
{
tbl_row = document.createElement("tr");
tbl_d = document.createElement("td");
tbl_span = document.createElement("span");
tbl_d.setAttribute("class","bot");
tbl_span.appendChild( document.createTextNode(bot_) );
tbl_d.appendChild(tbl_span);
tbl_row.appendChild(tbl_d);
document.getElementById(tbl_id).appendChild(tbl_row);
}
</script>
</body>
</html>