Created
October 15, 2018 04:55
-
-
Save appwiz/4bd9c2d353326a404086b2dbf527e9c5 to your computer and use it in GitHub Desktop.
Voice Commands - Alexa Skill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable func-names */ | |
/* eslint-disable no-console */ | |
const Alexa = require('ask-sdk-core'); | |
const Amplify = require('aws-amplify'); | |
const { API, graphqlOperation } = Amplify; | |
API.configure({ | |
'aws_appsync_region': 'us-west-2', | |
'aws_appsync_authenticationType': 'API_KEY', | |
'aws_appsync_graphqlEndpoint': '...', | |
'aws_appsync_apiKey': '...' | |
}); | |
const CreateCommand = `mutation createColorCommand($createcolorcommandinput: CreateColorCommandInput!) { | |
createColorCommand(input: $createcolorcommandinput) { | |
id | |
timestamp | |
color | |
} | |
} | |
`; | |
async function sendCommand(d) { | |
const command = await API.graphql(graphqlOperation(CreateCommand, d)); | |
console.log(command); | |
return command; | |
} | |
const LaunchRequestHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'LaunchRequest'; | |
}, | |
handle(handlerInput) { | |
const speechText = 'Welcome to the Alexa Skills Kit, you can say hello!'; | |
return handlerInput.responseBuilder | |
.speak(speechText) | |
.reprompt(speechText) | |
.withSimpleCard('Hello World', speechText) | |
.getResponse(); | |
}, | |
}; | |
const HelloWorldIntentHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'IntentRequest' | |
&& handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent'; | |
}, | |
handle(handlerInput) { | |
const speechText = 'Hello World!'; | |
sendCommand({ | |
"createcolorcommandinput": { | |
"id": "x", | |
"timestamp": "x", | |
"color": "green" | |
} | |
}); | |
return handlerInput.responseBuilder | |
.speak(speechText) | |
.withSimpleCard('Hello World', speechText) | |
.getResponse(); | |
}, | |
}; | |
const HelpIntentHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'IntentRequest' | |
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent'; | |
}, | |
handle(handlerInput) { | |
const speechText = 'You can say hello to me!'; | |
return handlerInput.responseBuilder | |
.speak(speechText) | |
.reprompt(speechText) | |
.withSimpleCard('Hello World', speechText) | |
.getResponse(); | |
}, | |
}; | |
const CancelAndStopIntentHandler = { | |
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 speechText = 'Goodbye!'; | |
return handlerInput.responseBuilder | |
.speak(speechText) | |
.withSimpleCard('Hello World', speechText) | |
.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}`); | |
return handlerInput.responseBuilder | |
.speak('Sorry, I can\'t understand the command. Please say again.') | |
.reprompt('Sorry, I can\'t understand the command. Please say again.') | |
.getResponse(); | |
}, | |
}; | |
const skillBuilder = Alexa.SkillBuilders.custom(); | |
exports.handler = skillBuilder | |
.addRequestHandlers( | |
LaunchRequestHandler, | |
HelloWorldIntentHandler, | |
HelpIntentHandler, | |
CancelAndStopIntentHandler, | |
SessionEndedRequestHandler | |
) | |
.addErrorHandlers(ErrorHandler) | |
.lambda(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment