-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
96 lines (81 loc) · 2.9 KB
/
index.js
File metadata and controls
96 lines (81 loc) · 2.9 KB
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
function getLocation(name) {
return new Promise((resolve, reject) => {
// Create the path for the HTTP request to get the weather
let icloud = require("./icloud");
var people = [];
people['john'] = {id: 'johndoe@gmail.com', pass: '123456'};
people['jane'] = {id: 'janedoe@gmail.com', pass: '123456'};
var person = people[name.toLowerCase()];
if (!person) {
reject("I don't know " + name);
}
icloud.apple_id = person.id;
icloud.password = person.pass;
icloud.getDevices(function (error, devices) {
var device = null;
if (error || !devices) {
var err = "I have trouble locating " + name;
if (error !== null) {
err += " " + error;
}
reject(err);
return;
}
//pick a device with location and findMyPhone enabled
devices.forEach(function (d) {
if (device == undefined && d.location && d.lostModeCapable
&& d.modelDisplayName.toLowerCase().indexOf("iphone") >= 0) {
device = d;
}
});
if (device) {
icloud.getLocationOfDevice(device, function (err, location) {
var response = null;
if (location) {
response = location.replace('St','Street').replace('Hwy','Highway');
}
if (!response) {
response = "I don't know where is " + name;
}
resolve(response);
});
}
});
});
}
exports.WhereIs = function WhereIs(req, res) {
let name = '';
if (req.body.result.parameters['name']) {
name = req.body.result.parameters['name'];
console.log('Name: ' + name);
}
if (name != '') {
getLocation(name).then((output) => {
// Return the results of the weather API to API.AI
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({
'speech': output,
'displayText': output
}));
}).catch((error) => {
// If there is an error let the user know
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({
'speech': error,
'displayText': error
}));
});
} else {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({
'speech': 'I don\'t know who to locate',
'displayText': 'I don\'t know who to locate'
}));
}
};
/*getLocation('john').then((output) => {
console.log(output);
getLocation('jane').then((output) => {
console.log(output);
});
});*/