-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.backup
359 lines (352 loc) · 10.8 KB
/
index.backup
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import _ from "lodash";
import "bootstrap/dist/css/bootstrap.min.css";
import css from "./assets/sass/basic.scss";
require("angular");
var moment = require("moment");
moment.updateLocale('en', {
calendar: {
lastDay: '[yesterday]',
sameDay: '[today]',
nextDay: '[tomorrow]',
lastWeek: '[last] dddd',
nextWeek: '[next] dddd',
sameElse: 'L'
}
});
let app = angular.module("ara", [require("angular-route")]);
var chrono = require("chrono-node");
app.config(function($routeProvider) {
$routeProvider.when("/:user1/:user2/:lang", {
templateUrl: "./views/messages.html?v=" + Math.floor(Math.random() * 1000),
controller: "messages"
}).when("/:user1/:user2", {
templateUrl: "./views/messages.html?v=" + Math.floor(Math.random() * 1000),
controller: "messages"
});
});
app.controller("messages", function($scope, $interval, $http, $routeParams, $timeout) {
$scope.messages = [];
$scope.user1 = $routeParams["user1"];
$scope.user2 = $routeParams["user2"];
$scope.lang = $routeParams["lang"];
$scope.last = null;
$scope.speaker = function(text) {
if ($scope.last != text) {
responsiveVoice.speak(text);
}
$scope.last = text;
}
$http.get("http://198.211.125.182:5000/chat?user1=" + $scope.user1 + "&user2=" + $scope.user2).then(function(response) {
$scope.messages = response.data;
sessionStorage.translatedMessages = JSON.stringify([]);
if ($scope.lang != "en" && $scope.lang != null) {
var t = JSON.parse(sessionStorage.translatedMessages);
for (var i = 0; i < $scope.messages.length; i++) {
$http.get("http://198.211.125.182:5000/chat?user1=anand&user2=ara").then(function() {
// $scope.messages[i].text = "ABC";
// $scope.messages[0].text = "ABC";
console.log($scope.messages[i]);
});
}
}
var currLength = $scope.messages.length;
$interval(function() {
$http.get("http://198.211.125.182:5000/chat?user1=" + $scope.user1 + "&user2=" + $scope.user2).then(function(response) {
if (response.data.length != currLength) {
$scope.messages = response.data;
currLength = $scope.messages.length;
if ($scope.messages[$scope.messages.length - 1].from == "ara") {
$scope.speaker($scope.messages[$scope.messages.length - 1].text);
}
}
});
}, 5000);
$interval(function() {
if (sessionStorage.speaking == 1) {
$http.get("http://198.211.125.182:5000/message-list?to=" + $routeParams["user1"]).then(function(response) {
if (($scope.lastMessage != response.data.text) && $scope.lastAuthor != "ara" && response.data.text.indexOf("You have a new message from ara") == -1) {
if ($scope.lastMessage) {
console.log("yes last message");
$scope.lastMessage = response.data.text;
var text = "You have a new message from " + response.data.from + ": " + response.data.text;
$scope.messages.push({
from: "ara",
to: $scope.user1,
text: text,
datetime: chrono.parseDate("right now")
});
$http.post("http://198.211.125.182:5000/chat", {
from: "ara",
to: $scope.user1,
text: text,
datetime: chrono.parseDate("right now")
});
$scope.speaker(text);
} else {
console.log("no last message");
}
}
});
}
}, 5000);
});
$scope.conversation = null;
$scope.responster = null;
$scope.meeting = function(a, b) {
$timeout(function() {
console.log(b);
var message = "I've sent a message to " + a + " about your meeting " + moment(b).calendar();
$http.post("http://198.211.125.182:5000/chat", {
from: "ara",
to: a.toLowerCase().split(" ")[0],
text: "Hi " + capitalizeFirstLetter(a.toLowerCase().split(" ")[0]) + ", " + capitalizeFirstLetter($scope.user1) + " wants to have a meeting with you " + moment(b).calendar() + ". You're free at " + moment(b).format("LT") + ". Should I finalize it?",
datetime: chrono.parseDate("right now")
}).then(function(response) {
$http.post("http://198.211.125.182:5000/chat", {
from: "ara",
to: $scope.user1,
text: message,
datetime: chrono.parseDate("right now")
}).then(function(response) {
$scope.speaker(message);
$scope.messages.push({
from: "ara",
to: $scope.user1,
text: message,
datetime: chrono.parseDate("right now")
});
});
});
}, 4000);
};
$scope.respond = function(a) {
setTimeout(function() {
$scope.typing = 1;
}, 100);
var message = $scope.responster;
switch(a) {
case "thanks":
var messages = ["Happy to help! 😊", "You're welcome! 😊", "Always happy to help! 😊", "No worries! 😊", "Let me know if you need anything else. 😊"];
message = messages[Math.floor(Math.random() * messages.length)];
break;
case "find_time":
console.log($scope.responster);
if (chrono.parseDate($scope.responster)) {
$scope.conversation = null;
message = "Sounds great, I'm on it";
$scope.meeting($scope.with, chrono.parseDate($scope.responster));
} else {
message += " When and where is it?";
$scope.conversation = "find_time";
}
break;
case "driving_mode":
message = "Okay, I'll read your messages now. Drive safe!";
sessionStorage.speaking = 1;
break;
case "meeting_schedule":
var withText = message.split("with")[1];
withText = withText.split(" ");
$scope.with = withText[1];
message = "Okay, I'll schedule your meeting with " + withText[1] + ".";
if (chrono.parseDate($scope.responster)) {
message = "Sounds great! I'm on it.";
$scope.meeting($scope.with, chrono.parseDate($scope.responster));
} else {
message += " When and where is it?";
$scope.conversation = "find_time";
}
break;
}
$timeout(function() {
console.log("responsing to " + a);
// var message = "I'm sorry, I don't understand.";
$http.post("http://198.211.125.182:5000/chat", {
from: "ara",
to: $scope.user1,
text: message,
datetime: chrono.parseDate("right now")
}).then(function(response) {
$scope.speaker(message);
$scope.messages.push({
from: "ara",
to: $scope.user1,
text: message,
datetime: chrono.parseDate("right now")
});
$scope.typing = 0;
});
}, 1000);
}
$scope.check = function(a, abc) {
return new Promise (
function (resolve, reject) {
if ($scope.conversation) {
$scope.responster = abc;
$scope.respond($scope.conversation);
$http.post("http://198.211.125.182:5000/chat", {
from: $scope.user1,
to: $scope.user2,
text: abc,
datetime: chrono.parseDate("right now")
}).then(function(response) {
resolve({
from: $scope.user1,
to: $scope.user2,
text: abc,
datetime: chrono.parseDate("right now")
});
$scope.messageModel = "";
});
} else {
if (a != "normal_function") {
$scope.responster = abc;
$scope.respond(a);
$http.post("http://198.211.125.182:5000/chat", {
from: $scope.user1,
to: $scope.user2,
text: abc,
datetime: chrono.parseDate("right now")
}).then(function(response) {
resolve({
from: $scope.user1,
to: $scope.user2,
text: abc,
datetime: chrono.parseDate("right now")
});
$scope.messageModel = "";
});
} else {
$http.post("http://198.211.125.182:5000/chat", {
from: $scope.user1,
to: $scope.user2,
text: abc,
datetime: chrono.parseDate("right now")
}).then(function(response) {
resolve({
from: $scope.user1,
to: $scope.user2,
text: abc,
datetime: chrono.parseDate("right now")
});
$scope.messageModel = "";
});
}
}
}
);
};
sessionStorage.speaking = 0;
$scope.sendMessage = function(abc) {
if (abc == null) {
abc = $scope.messageModel;
}
if ($scope.user2 == "ara") {
var text = abc.toLowerCase();
var messageOptions = [
{
callback: "driving_mode",
words: ["handsfree", "driving", "drive", "earphones"]
},
{
callback: "meeting_schedule",
words: ["meeting with", "coffee with", "dinner with", "lunch with"]
},
{
callback: "thanks",
words: ["thanks", "thank you"]
}
];
var answer = "normal_function";
for (var i = 0; i < messageOptions.length; i++) {
var a = 0;
for (var j = 0; j < messageOptions[i].words.length; j++) {
if (text.indexOf(messageOptions[i].words[j]) > -1) {
a++;
answer = messageOptions[i].callback;
}
}
}
$scope.check(answer, abc).then(function (response) {
$scope.messages.push(response);
});
}
// $scope.messages.push({
// from: $scope.user1,
// to: $scope.user2,
// text: $scope.messageModel,
// datetime: chrono.parseDate("right now")
// });
// var a = 0;
// var handsFree = ["handsfree", "driving", "drive"];
// for (var i = 0; i < handsFree.length; i++) {
// if ($scope.messageModel.toLowerCase().indexOf(handsFree[i]) != -1) {
// a++;
// }
// }
// if (a == 0) {
// } else if ($scope.user2 == "ara") {
// $scope.speaker("Okay, I'll read your messages now. Drive safe!");
// $http.get("http://198.211.125.182:5000/message-list?to=" + $routeParams["user1"]).then(function(response) {
// $scope.lastMessage = response.data.text;
// $scope.lastAuthor = response.data.from;
// });
// sessionStorage.speaking = 1;
// $scope.messages.push({
// from: "ara",
// to: $scope.user1,
// text: "Okay, I'll read out your messages. Drive safe!"
// });
// $scope.messageModel = "";
// }
// console.log($scope.messages);
}
$scope.sendLike = function() {
$scope.messageModel = "👍";
$scope.sendMessage();
$scope.messageModel = "";
}
$interval(function() {
if (sessionStorage.responseMe) {
$scope.sendMessage(capitalizeFirstLetter(sessionStorage.responseMe));
sessionStorage.removeItem("responseMe");
}
}, 200);
$scope.speechtoText = function() {
listen().then(function(response) {
sessionStorage.responseMe = response;
});
}
var listen = function() {
return new Promise(
function (resolve, reject) {
var recognition = new webkitSpeechRecognition();
recognition.start();
recognition.interimResults = false;
recognition.onresult = function(event) {
resolve(event.results[0][0].transcript);
}
recognition.onerror = function() {
reject("error");
}
}
);
}
});
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function isEquivalent(a, b) {
var aProps = Object.getOwnPropertyNames(a);
var bProps = Object.getOwnPropertyNames(b);
if (aProps.length != bProps.length) {
return false;
}
for (var i = 0; i < aProps.length; i++) {
var propName = aProps[i];
if (a[propName] !== b[propName]) {
return false;
}
}
return true;
}