Created
October 9, 2018 16:02
-
-
Save appwiz/791bcf58ab80199811a2f418b786b8be to your computer and use it in GitHub Desktop.
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; | |
Amplify.Logger.LOG_LEVEL = "DEBUG" | |
const myAppConfig = { | |
'aws_appsync_graphqlEndpoint': '...', | |
'aws_appsync_region': '...', | |
'aws_appsync_authenticationType': 'API_KEY', | |
'aws_appsync_apiKey': '...' | |
} | |
API.configure(myAppConfig); | |
const CreateCommand = `mutation createCommands($createcommands2input: CreateCommands2Input!) { | |
createCommands(input: $createcommands2input) { | |
command | |
id | |
timestamp | |
action | |
} | |
}`; | |
const details = { | |
"createcommands2input": { | |
"command": "Voice", | |
"id": "id", | |
"timestamp": "ts", | |
"action": "abc" | |
} | |
}; | |
async function sendCommand(d) { | |
const newEvent = await API.graphql(graphqlOperation(CreateCommand, d)); | |
console.log(newEvent); | |
return newEvent; | |
} | |
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(details); | |
return handlerInput.responseBuilder | |
.speak(speechText) | |
.withSimpleCard('Hello World', speechText) | |
.getResponse(); | |
}, | |
}; | |
const AskActionIntentHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'IntentRequest' | |
&& handlerInput.requestEnvelope.request.intent.name === 'AskActionIntent'; | |
}, | |
handle(handlerInput) { | |
const speechText = 'Ok. Tell me your action.' | |
return handlerInput.responseBuilder | |
.speak(speechText) | |
.reprompt(speechText) | |
.withSimpleCard('Asking for action.', speechText) | |
.getResponse(); | |
}, | |
}; | |
const ActionIntentHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'IntentRequest' | |
&& handlerInput.requestEnvelope.request.intent.name === 'ActionIntent'; | |
}, | |
handle(handlerInput) { | |
const slots = handlerInput.requestEnvelope.request.intent.slots; | |
const color = slots['actionCode'].value; | |
const speechText = `Your actionCode is: ${color}.` | |
sendCommand({ | |
"createcommands2input": { | |
"command": "Voice", | |
"id": "22", | |
"timestamp": "022", | |
"action": color | |
} | |
}); | |
return handlerInput.responseBuilder | |
.speak(speechText) | |
.withSimpleCard('action', 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, | |
AskActionIntentHandler, | |
ActionIntentHandler, | |
HelpIntentHandler, | |
CancelAndStopIntentHandler, | |
SessionEndedRequestHandler | |
) | |
.addErrorHandlers(ErrorHandler) | |
.lambda(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment