-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
277 lines (218 loc) · 8.6 KB
/
index.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Chatbot</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="{{ url_for('static', filename='styles/style.css') }}">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/shoelace-css/1.0.0-beta16/shoelace.css">
<link rel="stylesheet" href="styles.css">
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
</head>
<body>
<!-- partial:index.partial.html -->
<section class="msger">
<select id="lang-selector" onchange="myfunction()">
<option value="0">
English
</option>
<option value="1">
Hindi
</option>
</select>
<header class="msger-header">
<div class="msger-header-title">
<i class="fas fa-bug"></i> Chatbot <i class="fas fa-bug"></i>
</div>
</header>
<main class="msger-chat">
<div class="msg left-msg">
<div class="msg-img"
style="background-image: url(https://cdn.jsdelivr.net/gh/corover/assets@latest/askdisha-bucket/disha.svg)">
</div>
<div class="msg-bubble">
<div class="msg-info">
<div class="msg-info-name">Chatbot</div>
<div class="msg-info-time">12:45</div>
</div>
<div class="msg-text">
Hi, welcome to ChatBot! Go ahead and send me a message. 😄
</div>
</div>
</div>
</main>
<form class="msger-inputarea">
<input type="text" class="msger-input" id="note-textarea" placeholder="Enter your message...">
<button type="submit" class="msger-send-btn" id="SendQuery">Send</button>
<button id="start-record-btn" title="Start Recording">Voice Input</button>
</form>
</section>
<div class="container">
<!-- partial -->
<script src='https://use.fontawesome.com/releases/v5.0.13/js/all.js'></script>
<script>
try {
var SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
var recognition = new SpeechRecognition();
} catch (e) {
console.error(e);
$('.no-browser-support').show();
$('.app').hide();
}
</script>
<script>
const msgerForm = get(".msger-inputarea");
const msgerInput = get(".msger-input");
const msgerChat = get(".msger-chat");
// Icons made by Freepik from www.flaticon.com
const BOT_IMG = "https://cdn.jsdelivr.net/gh/corover/assets@latest/askdisha-bucket/disha.svg";
const PERSON_IMG = "https://static.vecteezy.com/system/resources/previews/010/882/754/non_2x/avatar-man-person-face-icon-illustration-head-character-cartoon-human-portrait-profile-avatar-user-man-isolated-white-adult-silhouette-human-face-clipart-icon-character-headshot-element-vector.jpg";
const BOT_NAME = "ChatBot";
const PERSON_NAME = "You";
msgerForm.addEventListener("submit", event => {
event.preventDefault();
const msgText = msgerInput.value;
if (!msgText) return;
appendMessage(PERSON_NAME, PERSON_IMG, "right", msgText);
msgerInput.value = "";
botResponse(msgText);
});
function appendMessage(name, img, side, text) {
// Simple solution for small apps
const msgHTML = `
<div class="msg ${side}-msg">
<div class="msg-img" style="background-image: url(${img})"></div>
<div class="msg-bubble">
<div class="msg-info">
<div class="msg-info-name">${name}</div>
<div class="msg-info-time">${formatDate(new Date())}</div>
</div>
<div class="msg-text">${text}</div>
</div>
</div>
`;
msgerChat.insertAdjacentHTML("beforeend", msgHTML);
msgerChat.scrollTop += 500;
}
function appendMapLink(name, img, side, text) {
// Simple solution for small apps
const msgHTML = `
<div class="msg ${side}-msg">
<div class="msg-img" style="background-image: url(${img})"></div>
<div class="msg-bubble">
<div class="msg-info">
<div class="msg-info-name">${name}</div>
<div class="msg-info-time">${formatDate(new Date())}</div>
</div>
<a href="/showmap" target = "_blank" class="msg-text">${text}</a>
</div>
</div>
`;
msgerChat.insertAdjacentHTML("beforeend", msgHTML);
msgerChat.scrollTop += 500;
}
function botResponse(rawText) {
// Bot Response
$.get("/get", { msg: rawText }).done(function (data) {
console.log("This is the rawText", rawText);
console.log("This is the data", data);
console.log(typeof (data))
if (typeof (data) === "string") {
msgText = data;
appendMessage(BOT_NAME, BOT_IMG, "left", msgText);
// var utterance = new SpeechSynthesisUtterance(msgText);
// var voices = window.speechSynthesis.getVoices();
// utterance.voice = voices[0];
// window.speechSynthesis.speak(utterance);
}
else if (data.map) {
// Initialize the map
localStorage.setItem("mapType", data.map);
appendMapLink("Bhumija", BOT_IMG, "left", "Click here to see the map")
}
else {
const msgText = data.res;
console.log(msgText)
appendMessage(BOT_NAME, BOT_IMG, "left", msgText);
var utterance = new SpeechSynthesisUtterance(msgText);
var voices = window.speechSynthesis.getVoices();
utterance.voice = voices[1];
window.speechSynthesis.speak(utterance);
if (data.url) {
showYesNoButtons(data.url);
}
}
});
}
// Utils
function get(selector, root = document) {
return root.querySelector(selector);
}
function showYesNoButtons(url) {
const buttonsHTML = `
<div class="msg-buttons" id="yesno">
<button onclick="redirectToUrl('${url}')">Yes</button>
<button onclick="sayHello()">No</button>
</div>
`
msgerChat.insertAdjacentHTML("beforeend", buttonsHTML);
} function showYesNoButtons(url) {
const buttonsHTML = `
<div class="msg-buttons">
<button onclick="redirectToUrl('${url}')">Yes</button>
<button onclick="sayHello()">No</button>
</div>
`;
msgerChat.insertAdjacentHTML("beforeend", buttonsHTML);
}
function redirectToUrl(url) {
window.open(`${url}`,'_blank')
}
function sayHello() {
const msgHTML = `
<div class="msg left-msg">
<div class="msg-img" style="background-image: url(${BOT_IMG})"></div>
<div class="msg-bubble">
<div class="msg-info">
<div class="msg-info-name">${BOT_NAME}</div>
<div class="msg-info-time">${formatDate(new Date())}</div>
</div>
<div class="msg-text">Ok</div>
</div>
</div>
`;
msgerChat.insertAdjacentHTML("beforeend", msgHTML);
msgerChat.scrollTop += 500;
}
function formatDate(date) {
const h = "0" + date.getHours();
const m = "0" + date.getMinutes();
return `${h.slice(-2)}:${m.slice(-2)}`;
}
function myfunction() {
setter = document.getElementById("lang-selector")
// console.log(setter);
myVariable = setter.value
fetch('/your_flask_route', {
method: 'POST', // or 'GET' depending on your needs
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ variable: myVariable })
})
.then(response => response.json())
.then(data => {
console.log('Response from Flask:', data);
})
.catch(error => {
console.error('Error:', error);
});
}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="../static/styles/script.js"></script>
</body>
</html>