-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwit_handler.js
More file actions
121 lines (103 loc) · 3.42 KB
/
wit_handler.js
File metadata and controls
121 lines (103 loc) · 3.42 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
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
function responseFromWit(data) {
console.log("data from wit:");
console.log(JSON.stringify(data));
const intent = data.intents.length > 0 && data.intents[0] || "__foo__";
switch (intent.name) {
case "distanceBetween":
return handleDistanceBetween(data);
case "timeAtPlace":
return handleTimeAtPlace(data);
case "Purchase":
return handlePurchase(data);
}
return handleGibberish();
}
function handleGibberish() {
return Promise.resolve(
"ask me something like 'what time is it in Menlo Park?' or 'how far from Menlo Park to Seattle?'"
);
}
function handlePurchase(data){
var product = data.entities['product:product'][0].value;
if(product == null) return handleGibberish();
//var pro1 = firstEntityValue(data,'Purchase');
var quantity = 10;
return Promise.resolve(
`${product} hiện có ${quantity} sản phẩm, bạn muốn chọn sản phẩm nào?`
);
}
const firstEntityValue = (entities, entity) => {
const val = entities && entities[entity] &&
Array.isArray(entities[entity]) &&
entities[entity].length > 0 &&
entities[entity][0].value;
if (!val) {
return null;
}
return typeof val === 'object' ? val.value : val;
};
// ----------------------------------------------------------------------------
// handleDistanceBetween
function handleDistanceBetween(data) {
const location = data.entities['wit$location:location'];
if (location == null || location.length != 2) {
return handleGibberish();
}
var loc0 = location[0].resolved.values[0];
var loc1 = location[1].resolved.values[0];
var distance = getDistanceFromLatLonInKm(
loc0.coords.lat,
loc0.coords.long,
loc1.coords.lat,
loc1.coords.long
);
distance = roundTo(distance, 0.01);
return Promise.resolve(
`It's ${distance}km from ${loc0.name} to ${loc1.name}`
);
}
//https://stackoverflow.com/questions/27928/calculate-distance-between-two-latitude-longitude-points-haversine-formula
function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2 - lat1); // deg2rad below
var dLon = deg2rad(lon2 - lon1);
var a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat1)) *
Math.cos(deg2rad(lat2)) *
Math.sin(dLon / 2) *
Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c; // Distance in km
return d;
}
function deg2rad(deg) {
return deg * (Math.PI / 180);
}
function roundTo(val, round) {
return Math.floor(val / round) * round;
}
// ----------------------------------------------------------------------------
// handleTimeAtPlace
function handleTimeAtPlace(data) {
const loc = data.entities['wit$location:location'] && data.entities['wit$location:location'][0];
if (loc == null) {
return handleGibberish();
}
const tz = loc.resolved.values[0].timezone;
const placeName = loc.resolved.values[0].name;
return currentTimeFromTimezone(tz).then(res => {
return `It's currently ${res} in ${placeName}`;
});
}
function currentTimeFromTimezone(loc) {
const url = "http://worldtimeapi.org/api/timezone/" + loc;
return fetch(url, {})
.then(res => res.json())
.then(data => {
//trim off the timezone to avoid date auto-adjusting
const time = data.datetime.substring(0, 19);
return (new Date(time)).toUTCString("en-US").substring(0, 22);
});
}
exports.responseFromWit = responseFromWit;