Skip to content

Instantly share code, notes, and snippets.

@hao1987
Last active August 6, 2017 20:07
Show Gist options
  • Save hao1987/68cf9fdfcf374cc2275e8677974d19ea to your computer and use it in GitHub Desktop.
Save hao1987/68cf9fdfcf374cc2275e8677974d19ea to your computer and use it in GitHub Desktop.
/* eslint-disable func-names */
/* eslint quote-props: ["error", "consistent"]*/
// alexa-cookbook sample code
// There are three sections, Text Strings, Skill Code, and Helper Function(s).
// You can copy and paste the entire file contents as the code for a new Lambda function,
// or copy & paste section #3, the helper function, to the bottom of your existing Lambda code.
// TODO add URL to this entry in the cookbook
// 1. Text strings =====================================================================================================
// Modify these strings and messages to change the behavior of your Lambda function
var speechOutput;
var reprompt;
var welcomeOutput = "Let's plan a trip. Where would you like to go?";
var welcomeReprompt = "Let me know where you'd like to go or when you'd like to go on your trip";
var tripIntro = [
"This sounds like a cool trip. ",
"This will be fun. ",
"Oh, I like this trip. "
];
// 2. Skill Code =======================================================================================================
'use strict';
var Alexa = require('alexa-sdk');
var APP_ID = 'amzn1.ask.skill.19b9781e-ff0b-4ec0-85ad-abc9dfcd4971'; // TODO replace with your app ID (OPTIONAL).
var handlers = {
'LaunchRequest': function () {
this.emit(':ask', welcomeOutput, welcomeReprompt);
},
'whoismywife': function () {
//delegate to Alexa to collect all the required slot values
// var filledSlots = delegateSlotCollection.call(this);
// //compose speechOutput that simply reads all the collected slot values
// var speechOutput = randomPhrase(tripIntro);
// //activity is optional so we'll add it to the output
// //only when we have a valid activity
// var travelMode = isSlotValid(this.event.request, "travelMode");
// if (travelMode) {
// speechOutput += travelMode;
// } else {
// speechOutput += "You'll go ";
// }
// //Now let's recap the trip
// var fromCity=this.event.request.intent.slots.fromCity.value;
// var toCity=this.event.request.intent.slots.toCity.value;
// var travelDate=this.event.request.intent.slots.travelDate.value;
// speechOutput+= " from "+ fromCity + " to "+ toCity+" on "+travelDate;
// var activity = isSlotValid(this.event.request, "activity");
// if (activity) {
var speechOutput = "Shengnan is your wife.";
// }
//say the results
this.emit(":tell",speechOutput);
},
'AMAZON.HelpIntent': function () {
speechOutput = "";
reprompt = "";
this.emit(':ask', speechOutput, reprompt);
},
'AMAZON.CancelIntent': function () {
speechOutput = "";
this.emit(':tell', speechOutput);
},
'AMAZON.StopIntent': function () {
speechOutput = "";
this.emit(':tell', speechOutput);
},
'SessionEndedRequest': function () {
var speechOutput = "";
this.emit(':tell', speechOutput);
},
};
exports.handler = (event, context) => {
var alexa = Alexa.handler(event, context);
alexa.APP_ID = APP_ID;
// To enable string internationalization (i18n) features, set a resources object.
//alexa.resources = languageStrings;
alexa.registerHandlers(handlers);
alexa.execute();
};
// END of Intent Handlers {} ========================================================================================
// 3. Helper Function =================================================================================================
function delegateSlotCollection(){
console.log("in delegateSlotCollection");
console.log("current dialogState: "+this.event.request.dialogState);
if (this.event.request.dialogState === "STARTED") {
console.log("in Beginning");
var updatedIntent=this.event.request.intent;
//optionally pre-fill slots: update the intent object with slot values for which
//you have defaults, then return Dialog.Delegate with this updated intent
// in the updatedIntent property
this.emit(":delegate", updatedIntent);
} else if (this.event.request.dialogState !== "COMPLETED") {
console.log("in not completed");
// return a Dialog.Delegate directive with no updatedIntent property.
this.emit(":delegate");
} else {
console.log("in completed");
console.log("returning: "+ JSON.stringify(this.event.request.intent));
// Dialog is now complete and all required slots should be filled,
// so call your normal intent handler.
return this.event.request.intent;
}
}
function randomPhrase(array) {
// the argument is an array [] of words or phrases
var i = 0;
i = Math.floor(Math.random() * array.length);
return(array[i]);
}
function isSlotValid(request, slotName){
var slot = request.intent.slots[slotName];
//console.log("request = "+JSON.stringify(request)); //uncomment if you want to see the request
var slotValue;
//if we have a slot, get the text and store it into speechOutput
if (slot && slot.value) {
//we have a value in the slot
slotValue = slot.value.toLowerCase();
return slotValue;
} else {
//we didn't get a value in the slot.
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment