-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
300 lines (264 loc) · 7.5 KB
/
app.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
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
require("dotenv").config();
const http = require("http");
const express = require("express");
const ejs = require("ejs");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const accountSid = process.env.CLIENT_SID;
const authToken = process.env.CLIENT_TOKEN;
const client = require("twilio")(accountSid, authToken);
const socketio = require("socket.io");
const dasha = require("@dasha.ai/sdk");
const fs = require("fs");
const session = require("express-session");
const passport = require("passport");
const passportLocalMongoose = require("passport-local-mongoose");
//dashboard --> main.html file
const app = express();
const server = http.createServer(app);
const io = socketio(server);
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine", "ejs");
app.use(
session({
secret: "Our little secret.",
resave: false,
saveUninitialized: false,
})
);
app.use(passport.initialize());
app.use(passport.session());
// mongoose.connect(
// "mongodb+srv://mananjain:[email protected]/myFirstDatabase"
// );
mongoose.connect("mongodb+srv://manan:[email protected]/myDb");
// mongoose.connect("mongodb://localhost:27017/userT", {
// useNewUrlParser: true,
// });
const userSchema = new mongoose.Schema({
username: String,
email: String,
contact1: String,
contact2: String,
contact3: String,
contact: String,
password: String,
});
userSchema.plugin(passportLocalMongoose);
const User = new mongoose.model("User", userSchema);
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
let userName;
let myNum;
app.get("/", function (req, res) {
res.render("homePage");
});
app.get("/login", function (req, res) {
res.render("loginSignup");
});
app.get("/register", function (req, res) {
res.render("loginSignup");
});
// ROUTE TO UPDATE PROFILE//////
app.get("/settings", function (req, res) {
res.render("settings", { userName: userName });
});
app.get("/logout", function (req, res) {
req.logOut();
res.redirect("/");
});
async function main() {
console.log(myNum);
const conv = application.createConversation({
phone: "+917827023367",
name: "Person",
});
conv.audio.tts = "dasha";
if (conv.input.phone === "chat") {
await dasha.chat.createConsoleChat(conv);
} else {
conv.on("transcription", console.log);
}
const logFile = await fs.promises.open("./log.txt", "w");
await logFile.appendFile("#".repeat(100) + "\n");
conv.on("transcription", async (entry) => {
await logFile.appendFile(`${entry.speaker}: ${entry.text}\n`);
});
conv.on("debugLog", async (event) => {
if (event?.msg?.msgId === "RecognizedSpeechMessage") {
const logEntry = event?.msg?.results[0]?.facts;
await logFile.appendFile(JSON.stringify(logEntry, undefined, 2) + "\n");
}
});
const result = await conv.execute({
channel: conv.input.phone === "chat" ? "text" : "audio",
});
console.log(result.output);
await logFile.close();
}
let application;
async function myFunc() {
application = await dasha.deploy("./app");
application.setExternal("console_log", (args, conv) => {
console.log(args);
});
await application.start();
}
// myFunc();
app.post("/fakeCall", function (req, res) {
main();
res.redirect("/dashboard");
});
io.on("connection", (socket) => {
socket.on("sendLoc", (myLatitude, myLongitude) => {
let phone1;
let phone2;
let phone3;
User.find({ username: userName }, function (err, foundUser) {
phone1 = foundUser[0].contact1;
phone2 = foundUser[0].contact2;
phone3 = foundUser[0].contact3;
console.log(foundUser);
console.log(phone1);
client.messages
.create({
body: `I need immediate help at ${myLatitude}, ${myLongitude}. Track my current location at https://stree22.herokuapp.com/maps`,
from: "+18593747178",
to: `${phone1}`,
})
.then((message) => console.log(message.sid));
client.messages
.create({
body: `I need immediate help at ${myLatitude}, ${myLongitude}. Track my current location at https://stree22.herokuapp.com/maps`,
from: "+18593747178",
to: `${phone2}`,
})
.then((message) => console.log(message.sid));
client.messages
.create({
body: `I need immediate help at ${myLatitude}, ${myLongitude}. Track my current location at https://stree22.herokuapp.com/maps`,
from: "+18593747178",
to: `${phone3}`,
})
.then((message) => console.log(message.sid));
});
console.log(myLatitude);
console.log(myLongitude);
});
});
var lati;
var longi;
io.on("connection", (socket) => {
socket.on("liveLoc", (myLatitude, myLongitude) => {
//emit and event with lat and long
lati = myLatitude;
longi = myLongitude;
console.log(lati);
console.log(longi);
});
socket.emit("shareLoc", lati, longi);
});
app.post("/liveLocation", (req, res) => {
res.redirect("/dashboard");
});
app.get("/maps", function (req, res) {
res.sendFile(__dirname + "/map.html");
});
app.post("/settings", function (req, res) {
User.findOne({ username: req.body.username }, function (err, data) {
if (err) {
console.log(err);
} else {
User.findOneAndDelete({ username: req.body.username }, function (err, d) {
if (err) {
console.log(err);
} else {
console.log(d);
}
});
User.register(
{
username: req.body.username,
email: req.body.email,
contact1: req.body.phone1,
contact2: req.body.phone2,
contact3: req.body.phone3,
contact: req.body.mynumber,
},
req.body.password,
function (err, user) {
if (err) {
console.log(err);
} else {
console.log(user);
req.logOut();
myNum = user.contact;
res.redirect("/login");
}
}
);
}
});
});
app.post("/login", function (req, res) {
const user = new User({
username: req.body.username,
password: req.body.password,
});
req.login(user, function (err) {
if (err) {
console.log(err);
res.redirect("/login");
} else {
passport.authenticate("local")(req, res, function () {
userName = user.username;
myNum = user.contact;
res.redirect("/dashboard");
});
}
});
});
app.post("/register", function (req, res) {
User.register(
{
username: req.body.username,
email: req.body.email,
contact1: req.body.phone1,
contact2: req.body.phone2,
contact3: req.body.phone3,
contact: req.body.mynumber,
},
req.body.password,
function (err, user) {
if (err) {
console.log(err);
res.redirect("/register");
} else {
passport.authenticate("local")(req, res, function () {
userName = user.username;
myNum = user.contact;
res.redirect("/dashboard");
});
}
}
);
});
app.post("/text", (req, res) => {
res.redirect("/dashboard");
});
app.get("/dashboard", function (req, res) {
if (req.isAuthenticated()) {
res.sendFile(__dirname + "/dashboard.html");
} else {
res.redirect("/login");
}
});
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
server.listen(port, function () {
console.log(`Server started on http://localhost:3000/`);
});