Skip to content

Instantly share code, notes, and snippets.

@esouthren
Created May 7, 2020 12:09
Show Gist options
  • Save esouthren/0df2fd88c2088a703cad15704676670f to your computer and use it in GitHub Desktop.
Save esouthren/0df2fd88c2088a703cad15704676670f to your computer and use it in GitHub Desktop.
Hacking with Amazon Alexa
/*
* Copyright 2018-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
// sets up dependencies
const Alexa = require('ask-sdk-core');
const i18n = require('i18next');
const data = {
SKILL_NAME: 'MLH Facts',
GET_FACT_MESSAGE: 'Here\'s your fact: ',
HELP_MESSAGE: 'What can I help you with?',
HELP_REPROMPT: 'What can I help you with?',
FALLBACK_MESSAGE: 'The Facts skill can\'t help you with that. What can I help you with?',
FALLBACK_REPROMPT: 'What can I help you with?',
ERROR_MESSAGE: 'Sorry, an error occurred.',
STOP_MESSAGE: 'Goodbye!',
// TODO: Replace with facts about yourself!
FACTS:
[
'Major League Hacking is commonly called MLH.',
'Major League Hacking\'s mission is to empower hackers.',
'Hackers created over 12,000 projects at MLH hackathons in 2016.',
'Major League Hacking is headquartered in New York City.',
],
}
// core functionality for fact skill
const GetNewFactHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
// checks request type
return request.type === 'LaunchRequest'
|| (request.type === 'IntentRequest'
&& request.intent.name === 'GetNewFactIntent');
},
handle(handlerInput) {
// gets a random fact by assigning an array to the variable
const factIndex = Math.floor(Math.random() * data.FACTS.length);
const randomFact = data.FACTS[factIndex];
// concatenates a standard message with the random fact
const speakOutput = data.GET_FACT_MESSAGE + randomFact;
return handlerInput.responseBuilder
.speak(speakOutput)
.withSimpleCard(data.SKILL_NAME, randomFact)
.getResponse();
},
};
const HelpHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak(data.HELP_MESSAGE)
.reprompt(data.HELP_REPROMPT)
.getResponse();
},
};
const FallbackHandler = {
// The FallbackIntent can only be sent in those locales which support it,
// so this handler will always be skipped in locales where it is not supported.
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& request.intent.name === 'AMAZON.FallbackIntent';
},
handle(handlerInput) {
// const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
return handlerInput.responseBuilder
.speak(data.FALLBACK_MESSAGE)
.reprompt(data.FALLBACK_REPROMPT)
.getResponse();
},
};
const ExitHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& (request.intent.name === 'AMAZON.CancelIntent'
|| request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
return handlerInput.responseBuilder
.speak(data.STOP_MESSAGE)
.getResponse();
},
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return 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 stack: ${error.stack}`);
return handlerInput.responseBuilder
.speak(data.ERROR_MESSAGE)
.reprompt(data.ERROR_MESSAGE)
.getResponse();
},
};
const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(
GetNewFactHandler,
HelpHandler,
ExitHandler,
FallbackHandler,
SessionEndedRequestHandler,
)
.addErrorHandlers(ErrorHandler)
.withCustomUserAgent('sample/basic-fact/v2')
.lambda();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment