Skip to content

Instantly share code, notes, and snippets.

@leoaiassistant
Created August 2, 2020 09:51
Show Gist options
  • Save leoaiassistant/7b570368347594910d0d06f2376da0ea to your computer and use it in GitHub Desktop.
Save leoaiassistant/7b570368347594910d0d06f2376da0ea to your computer and use it in GitHub Desktop.
const Alexa = require('ask-sdk-core');
const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
handle(handlerInput) {
const speakOutput = 'Welcome to Voice Travel';
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
},
};
const DemoHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'love';
},
handle(handlerInput) {
const speakOutput = 'I dont think so';
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
},
};
const HelpHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const speakOutput = 'You can say hello to me!';
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
},
};
const CancelAndStopHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent'
|| handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
const speakOutput = 'Goodbye!';
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
},
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);
return handlerInput.responseBuilder.getResponse();
},
};
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`Error handled: ${error.message}`);
console.log(error.trace);
return handlerInput.responseBuilder
.speak('Sorry, I can\'t understand the command. Please say again.')
.getResponse();
},
};
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(
LaunchRequestHandler,
DemoHandler,
HelpHandler,
CancelAndStopHandler,
SessionEndedRequestHandler,
)
.addErrorHandlers(ErrorHandler)
.lambda();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment