-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchatbot UI.js
164 lines (132 loc) · 4.11 KB
/
chatbot UI.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<html lang="en" data-locale="en-US">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>
Weather Chatbot
</title>
<style style="text/css">
body {
background-image: url("https://ybtraveler.files.wordpress.com/2014/05/clouds-and-blue-sky-background-hd-wallpapers-2560-x-1600-2.jpg");
background-position: 50% 50%;
background-repeat: repeat;
text-align: center;
}
{
margin:0;
padding: 0;
font-family: tahoma, sans-serif;
box-sizing: border-box;
}
.chatbox {
width: 700px;
height: 700px;
background-image: url("http://c8.alamy.com/comp/EYR24G/the-thermometer-icon-high-and-low-temperature-EYR24G.jpg");
padding: 20px;
margin: 15px auto;
box-shadow: 0 3px #ccc;
}
.chatlogs {
padding: 10px;
width: 100%;
height: 450px;
}
.chat .chat-message {
width: 60px;
height: 60px;
background: #ccc;
border-radius: 50%;
}
</style>
<link rel="stylesheet"
type="text/css"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
<div id="no-script"class="bg-info">
This application needs JavaScript enabled in your browser!
</div>
<div id="id_contextdump"><h3></h3></div>
<center><h1><h1>Know Real Time Weather Data</h1></h1></center>
<div class="chatbox">
<div class="chatlogs">
<div class="chat">
<div id=id_botchathistory>
</div>
<form>
<label for="id_chattext"><i><h3>Your Input: </h3></i></label>
<input type="text" name="chattext" id="id_chattext">
<br/>
</form>
<button type="button" value="submit" onclick="javascript:onChatClick()" id="buttonpress">Send</button>
</div>
</div>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript">
$("#id_chattext").keyup(function(event){
if(event.keyCode == 13){
$("#buttonpress").click();
}
});
$(document).ready(function() {
javascriptCheck();
$('#id_contextdump').hide();
});
// if javascript is enabled on the browser then can
// remove the warning message
function javascriptCheck() {
$('#no-script').remove();
}
function createNewDiv(who, message) {
console.log('002-001');
var txt = who + ' : ' + message;
return $('<div></div>').text(txt);
}
function chat(person, txt) {
$('#id_botchathistory').append(createNewDiv(person, txt));
}
function processOK(response) {
console.log('003-001');
console.log(response);
console.log(response.botresponse.messageout);
console.log(response.botresponse.messageout.output.text);
console.log(response.botresponse.messageout.context);
chat('Bot', response.botresponse.messageout.output.text);
$('#id_contextdump').data('convContext', response.botresponse.messageout.context);
}
function processNotOK() {
chat('Error', 'Error whilst attempting to talk to Bot');
}
function invokeAjax(message) {
var contextdata = $('#id_contextdump').data('convContext');
console.log('checking stashed context data');
console.log(contextdata);
//var ajaxData = "msgdata=" + message;
var ajaxData = {};
ajaxData.msgdata = message;
if (contextdata) {
ajaxData.context = contextdata;
}
$.ajax({
type: 'POST',
url: 'botchat',
data: ajaxData,
success: processOK,
error: processNotOK
});
}
// User has entered some text.
function onChatClick() {
console.log('001-001');
var txt = $('#id_chattext').val();
chat('You', txt);
invokeAjax(txt);
console.log('001-002');
}
</script>
</body>
</html>