-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathskill.js
More file actions
449 lines (363 loc) · 14.5 KB
/
Copy pathskill.js
File metadata and controls
449 lines (363 loc) · 14.5 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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
var request = require('request');
var UberSkillHandler = require('./uber-skill-handler.js');
var config = require('config');
/**
* For additional samples, visit the Alexa Skills Kit developer documentation at
* https://developer.amazon.com/appsandservices/solutions/alexa/alexa-skills-kit/getting-started-guide
*/
var myLocation = config.get('Alexa.location');
var myStartLocation = function() {
return {
start_longitude : myLocation.longitude,
start_latitude : myLocation.latitude
};
};
// Route the incoming request based on type (LaunchRequest, IntentRequest,
// etc.) The JSON body of the request is provided in the event parameter.
exports.handler = function (event, context) {
try {
console.log('Event is '+JSON.stringify(event));
if ( event && event.session && event.session.application && event.session.application.applicationId ) {
console.log("event.session.application.applicationId=" + event.session.application.applicationId);
}
if ( event.event_type == 'requests.status_changed' ) { // web hooks from Uber (see Uber dev console)
console.log('Uber signaled an event status change.');
context.succeed({'thank you':true});
return;
}
/**
* Uncomment this if statement and populate with your skill's application ID to
* prevent someone else from configuring a skill that sends requests to this function.
*/
/*
if (event.session.application.applicationId !== "amzn1.echo-sdk-ams.app.[unique-value-here]") {
context.fail("Invalid Application ID");
}
*/
/**
Amazon Echo Commands
*/
if (event.session.new) {
onSessionStarted({requestId: event.request.requestId}, event.session);
}
if (event.request.type === "LaunchRequest") {
onLaunch(event.request,
event.session,
function callback(sessionAttributes, speechletResponse) {
context.succeed(buildResponse(sessionAttributes, speechletResponse));
});
} else if (event.request.type === "IntentRequest") {
onIntent(event.request,
event.session,
function callback(sessionAttributes, speechletResponse) {
context.succeed(buildResponse(sessionAttributes, speechletResponse));
});
} else if (event.request.type === "SessionEndedRequest") {
onSessionEnded(event.request, event.session);
context.succeed();
} else {
console.log('Unrecognized event.');
}
} catch (e) {
context.fail("Exception: " + e);
}
};
/**
* Called when the session starts.
*/
function onSessionStarted(sessionStartedRequest, session) {
console.log("onSessionStarted requestId=" + sessionStartedRequest.requestId + ", sessionId=" + session.sessionId);
}
/**
* Called when the user launches the skill without specifying what they want.
*/
function onLaunch(launchRequest, session, callback) {
console.log("onLaunch requestId=" + launchRequest.requestId + ", sessionId=" + session.sessionId);
// Dispatch to your skill's launch.
getWelcomeResponse(callback);
}
/**
* Called when the user specifies an intent for this skill.
*/
function onIntent(intentRequest, session, callback) {
console.log("onIntent requestId=" + intentRequest.requestId + ", sessionId=" + session.sessionId);
var intent = intentRequest.intent,
intentName = intentRequest.intent.name;
// Dispatch to your skill's intent handlers
if ("UberPickupIntent" === intentName) {
uberPickupIntentStart(intent, session, callback);
} else if ("UberPickupConfirmIntent" === intentName) {
UberPickupConfirmIntent(intent, session, callback);
} else if ("UberTimeEstimateIntent" === intentName) {
UberTimeEstimateIntent(intent, session, callback);
} else if ("UberRequestDetailsIntent" === intentName) {
UberRequestDetailsIntent(intent, session, callback);
} else if ("UberDevInfoIntent" === intentName) {
UberDevInfoIntent(intent, session, callback);
} else if ("WeatherRain" === intentName) {
getWeatherRain(intent, session, callback);
} else if ("HelpIntent" === intentName) {
getWelcomeResponse(callback);
} else {
throw "Invalid intent";
}
}
/**
* Called when the user ends the session.
* Is not called when the skill returns shouldEndSession=true.
*/
function onSessionEnded(sessionEndedRequest, session) {
console.log("onSessionEnded requestId=" + sessionEndedRequest.requestId + ", sessionId=" + session.sessionId);
// Add cleanup logic here
}
// --------------- Functions that control the skill's behavior -----------------------
// TODO: untested
function getWelcomeResponse(intent, session, callback) {
var cardTitle = intent.name;
var repromptText = "";
var sessionAttributes = {};
var shouldEndSession = true;
var speechOutput = "Uber is ready";
callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}
function getWeatherRain(intent, session, callback) {
var cardTitle = intent.name;
var repromptText = "";
var sessionAttributes = {};
var shouldEndSession = true;
var speechOutput = "";
var apikey = config.get('Weather.api-key');
if (!apikey ) {
return callback(new Error ('No weather API key'));
}
var options = {
url: 'https://api.forecast.io/forecast/'+apikey+'/'+myLocation.latitude+','+myLocation.longitude,
qs: {
exclude: 'daily,flags,alerts'
}
};
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
var json = JSON.parse(body);
// console.log(JSON.stringify(json,null,'\t'));
// speechOutput = json.hourly.summary;
// var timeUntilChange = json.minutesUntilChange;
var summary = json.minutely.summary;
if ( json.isPrecipitating ) {
speechOutput = summary + '. Do you have an umbrella?';
} else {
speechOutput = summary;
}
} else {
console.log('err');
speechOutput = 'error sorry dannny';
}
callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
});
}
exports.test = function(a,b,c) {
//getWeatherRain(a,b,c);
uberPickupIntentStart(a,b,c);
// UberTimeEstimateIntent(a,b,c);
};
/**
* Begin Uber request
*/
function uberPickupIntentStart(intent, session, callback) {
var cardTitle = intent.name;
// var pickupDateSlot = intent.slots.MyPickupDate;
var repromptText = "";
var sessionAttributes = {};
var shouldEndSession = false;
var speechOutput = "";
console.log('Received Uber intent. Intent='+JSON.stringify(intent));
UberSkillHandler.findMeARide(myStartLocation(), function(err, ride, estimate) {
if ( err ) {
speechOutput = 'I had issues talking to Uber.';
shouldEndSession = true;
} else {
console.log('Ride='+JSON.stringify(ride)+', Estimate='+JSON.stringify(estimate));
sessionAttributes.ride = ride;
sessionAttributes.estimate = estimate;
sessionAttributes.waiting_user_confirmation = true;
var pickup_estimate = estimate.pickup_estimate;
var surge_multiplier = estimate.price.surge_multiplier;
var surge_confirmation_id = estimate.price.surge_confirmation_id;
speechOutput = 'An '+ride.pronouncable_name+' can be here in ' + pickup_estimate + ' minute';
if (pickup_estimate != 1 ) {
speechOutput += 's';
}
if ( surge_multiplier > 1 ) {
sessionAttributes.surge_confirmation_id = surge_confirmation_id;
speechOutput += '. There is a surge of '+surge_multiplier;
}
speechOutput += '. Shall I call it?';
repromptText = 'Ill ask again. '+speechOutput;
}
callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
});
}
function UberPickupConfirmIntent(intent, session, callback) {
var cardTitle = intent.name;
var repromptText = "";
var sessionAttributes = {};
var shouldEndSession = true;
var speechOutput = "";
var respond = true;
console.log('Received Uber intent. Intent='+JSON.stringify(intent) +', Session='+JSON.stringify(session));
if ( session.attributes.waiting_user_confirmation && session.attributes.ride ) {
var userConfirmation = intent.slots.UserResponse;
if ( userConfirmation ) {
if ( userConfirmation.value == 'yes' ) {
respond = false;
var params = myLocation;
if ( session.attributes.surge_confirmation_id ) {
params.surge_confirmation_id = session.attributes.surge_confirmation_id;
console.log('confirming ride with surge'+params.surge_confirmation_id);
}
UberSkillHandler.confirmRideRequest(session.attributes.ride, params, function(err,riderequest) {
if ( err || !riderequest ) {
console.log('Error confirming ride: '+err);
speechOutput = 'There was an error confirming your ride with Uber.';
} else {
console.log('ride request response: '+JSON.stringify(riderequest));
if ( riderequest.errors ) {
speechOutput = 'There was an error making the request.';
} else {
speechOutput = 'Called an '+session.attributes.ride.pronouncable_name;
if ( riderequest.surge_multiplier > 1 ) {
speechOutput = speechOutput + ' There is a surge of '+riderequest.surge_multiplier+'.';
}
}
}
callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
});
} else {
speechOutput = 'Maybe next time.';
}
} else {
speechOutput = "I dont know how to respond. Say yes or no next time";
}
} else {
speechOutput = "Developer error. Confirm Intent Failed.";
}
if ( respond ) {
callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}
}
function UberRequestDetailsIntent(intent, session, callback) {
var cardTitle = intent.name;
var repromptText = "";
var sessionAttributes = {};
var shouldEndSession = true;
var speechOutput = "";
var respond = true;
console.log('Received Uber request details intent. Intent='+JSON.stringify(intent) +', Session='+JSON.stringify(session));
speechOutput = "This feature is not yet complete. DynamoDB is not setup";
callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
// TODO: need persistent storage to get the latest request_id. Use DynamoDB (used in Scorekeeper Echo example from Amazon)
// clever trick to use session || db depending on whether session has the data we need
// var params = {
// request_id :
// }
// UberSkillHandler.whatIsTheStatusOfMyRide(params, function(err,estimate) {
// ... and so on. copied from another function
// if ( err || !estimate ) {
// console.log('Error w/ time estimate: '+err);
// speechOutput = 'There was an error estimating the time.';
// } else {
// speechOutput = 'An '+estimate.pronouncable_name+' can be here in '+estimate.pronouncable_time;
// }
// callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
// });
}
function UberTimeEstimateIntent(intent, session, callback) {
var cardTitle = intent.name;
var repromptText = "";
var sessionAttributes = {};
var shouldEndSession = true;
var speechOutput = "";
var respond = true;
console.log('Received Uber time estimate intent. Intent='+JSON.stringify(intent) +', Session='+JSON.stringify(session));
UberSkillHandler.howLongForARide(myStartLocation(), function(err,estimate) {
if ( err || !estimate ) {
console.log('Error w/ time estimate: '+err);
speechOutput = 'There was an error estimating the time.';
} else {
speechOutput = 'An '+estimate.pronouncable_name+' can be here in '+estimate.pronouncable_time;
}
callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
});
}
function UberDevInfoIntent(intent, session, callback) {
var cardTitle = intent.name;
var repromptText = "";
var sessionAttributes = {};
var shouldEndSession = true;
var speechOutput = "";
var respond = true;
var sandbox = config.get('Uber.sandbox');
speechOutput = 'Uber skill is ';
if ( sandbox ) {
speechOutput += 'using sandbox';
} else {
speechOutput += 'using production';
}
callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}
function createPickupAttributes(date) {
return {
pickupDate: date
};
}
function getColorFromSession(intent, session, callback) {
var cardTitle = intent.name;
var favoriteColor;
var repromptText = null;
var sessionAttributes = {};
var shouldEndSession = false;
var speechOutput = "";
if(session.attributes) {
favoriteColor = session.attributes.favoriteColor;
}
if(favoriteColor) {
speechOutput = "Your favorite color is " + favoriteColor + ", goodbye";
shouldEndSession = true;
}
else {
speechOutput = "I'm not sure what your favorite color is, you can say, my favorite color " + " is red";
}
// Setting repromptText to null signifies that we do not want to reprompt the user.
// If the user does not respond or says something that is not understood, the session
// will end.
callback(sessionAttributes,
buildSpeechletResponse(intent.name, speechOutput, repromptText, shouldEndSession));
}
// --------------- Helpers that build all of the responses -----------------------
function buildSpeechletResponse(title, output, repromptText, shouldEndSession) {
return {
outputSpeech: {
type: "PlainText",
text: output
},
card: {
type: "Simple",
title: "SessionSpeechlet - " + title,
content: "SessionSpeechlet - " + output
},
reprompt: {
outputSpeech: {
type: "PlainText",
text: repromptText
}
},
shouldEndSession: shouldEndSession
};
}
function buildResponse(sessionAttributes, speechletResponse) {
return {
version: "1.0",
sessionAttributes: sessionAttributes,
response: speechletResponse
};
}