This repository has been archived by the owner on Jun 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
gmaps-app.js
204 lines (183 loc) · 7.19 KB
/
gmaps-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
// WARNING: This is a generated file.
// If you edit it you will be sad.
// Edit src/app.js instead.
var go = {};
go;
go.app = function() {
var vumigo = require('vumigo_v02');
var location = require('go-jsbox-location');
var googlemaps = location.providers.googlemaps;
var LocationState = location.LocationState;
var App = vumigo.App;
var EndState = vumigo.states.EndState;
var ChoiceState = vumigo.states.ChoiceState;
var Choice = vumigo.states.Choice;
var FreeText = vumigo.states.FreeText;
var JsonApi = vumigo.http.api.JsonApi;
var GoogleMaps = App.extend(function(self) {
App.call(self, 'states:start_loc');
self.init = function() {
self.http = new JsonApi(self.im);
};
self._extract_geometry = function(result) {
return result.geometry.location;
};
self.states.add('states:start_loc', function(name) {
return new LocationState(name, {
question: "Where are you now?",
next: 'states:end_loc',
map_provider: new googlemaps.GoogleMaps({
extract_address_data: self._extract_geometry,
}),
namespace: 'startlocation'
});
});
self.states.add('states:end_loc', function(name) {
return new LocationState(name, {
question: "Where do you want to go?",
next: 'states:send_dir',
map_provider: new googlemaps.GoogleMaps({
extract_address_data: self._extract_geometry,
}),
namespace: 'endlocation'
});
});
self.states.add('states:send_dir', function(name) {
return new ChoiceState(name, {
question: 'Where should the directions be sent?',
choices: [
new Choice('myself', 'Myself'),
new Choice('other', 'Someone else')
],
next: function(choice) {
return {
myself:'states:end',
other:'states:custom_to_addr'
}[choice.value];
}
});
});
// This function normalizes cellphone number inputs
self.normalize_msisdn = function(number, country_code) {
// Remove invalid characters
number = number.replace(/[^0-9+]/g, '');
// Handle ``00`` case
number = number.replace(/^0{2}/, '+');
if(country_code){
// Handle ``0`` case
number = number.replace(/^0/, ['+',country_code].join(''));
// Add ``+``
if(number.match('^' + country_code)) {
number = ['+',number].join('');
}
}
return (number.match(/^\+/)) ? number : null;
};
self.states.add('states:custom_to_addr', function(name) {
return new FreeText(name, {
question:'Please specify the number to recieve the directions:',
next: function(content){
// normalize if the endpoint is cellphone
if(self.im.config.endpoint === 'sms') {
content = self.normalize_msisdn(content,
self.im.config.country_code);
}
// go to the end state if the input is valid
return content !== null ? {
name: 'states:end',
creator_opts: {reply:content}
} : 'states:custom_to_addr';
}
});
});
self.directions_lookup = function() {
var start, end, param;
return self.im.contacts.for_user()
.then(function(contact) {
start = {
lng:contact.extra
['startlocation:lng'],
lat:contact.extra
['startlocation:lat']
};
end = {
lng:contact.extra
['endlocation:lng'],
lat:contact.extra
['endlocation:lat']
};
param = {
origin:[start.lat,start.lng].join(','),
destination:[end.lat,end.lng].join(','),
sensor:'false'
};
return self.http.get(
'http://maps.googleapis.com/maps/api/directions/json', {
params:param})
.then(function(resp){
return resp.data;
});
});
};
self.process_directions = function(resp) {
if(!resp.routes || !resp.routes[0] || !resp.routes[0].legs ||
!resp.routes[0].legs[0] || !resp.routes[0].legs[0].steps
|| !resp.routes[0].legs[0].steps[0])
return null;
return resp.routes[0].legs[0].steps.map(function(step, index) {
return [
(index+1), '. ',
step.html_instructions.replace(/<(?:.|\n)*?>/gm, '')
].join('');
}).join('\n');
};
self.send_message = function(addr, message) {
if(addr) {
return self.im.outbound.send({
to: addr,
endpoint: self.im.config.endpoint,
content: message
});
} else {
return self.im.outbound.send_to_user({
endpoint: self.im.config.endpoint,
content: message
});
}
};
self.states.add('states:end', function(name, opts) {
return self.directions_lookup()
.then(function(resp) {
if(resp.status !== "OK") {
return null;
}
var directions = self.process_directions(resp);
return directions === null ?
null : self.send_message(opts.reply, directions);
})
.then(function(resp) {
return resp === null ?
new EndState(name, {
text: ["Error, cannot find directions for the",
"given locations."].join(' '),
next: 'states:start'
})
: new EndState(name, {
text: 'Directions sent!',
next: 'states:start'
});
});
});
});
return {
GoogleMaps: GoogleMaps
};
}();
go.init = function() {
var vumigo = require('vumigo_v02');
var InteractionMachine = vumigo.InteractionMachine;
var GoogleMaps = go.app.GoogleMaps;
return {
im: new InteractionMachine(api, new GoogleMaps())
};
}();