Last active
December 5, 2018 14:03
-
-
Save muttoni/5d1daeebd49bb9cecd576b96d44145da to your computer and use it in GitHub Desktop.
Swear Jar Skill
This file contains hidden or 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'); | |
| const ISworeIntentHandler = { | |
| canHandle(handlerInput) { | |
| const request = handlerInput.requestEnvelope.request; | |
| return request.type === 'LaunchRequest' | |
| || (request.type === 'IntentRequest' | |
| && request.intent.name === 'ISworeIntent'); | |
| }, | |
| handle(handlerInput) { | |
| const request = handlerInput.requestEnvelope.request; | |
| let swearCount = 1; | |
| if(request.type === 'IntentRequest' && request.intent.slots.swearCount) { | |
| swearCount = request.intent.slots.swearCount.value; | |
| } | |
| const responses = [ | |
| 'Disappointing.', | |
| 'What a shame.', | |
| 'Pity.', | |
| 'Really?', | |
| 'I thought better of you.', | |
| 'I am optimistic that this is the last time.', | |
| 'Try a bit harder.', | |
| '<say-as interpret-as="interjection">Wahoo!</say-as>Great job.' | |
| ]; | |
| let speechOutput = "<audio src='soundbank://soundlibrary/ui/gameshow/amzn_ui_sfx_gameshow_negative_response_02'/> "; | |
| speechOutput += responses[Math.floor(Math.random()*responses.length-1)]; | |
| if(swearCount && parseInt(swearCount) > 1 ) { | |
| speechOutput += ' I can\'t believe you swore ' + swearCount + " times." | |
| } | |
| return handlerInput.responseBuilder | |
| .speak(speechOutput) | |
| .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(HELP_MESSAGE) | |
| .reprompt(HELP_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) { | |
| return handlerInput.responseBuilder | |
| .speak(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}`); | |
| return handlerInput.responseBuilder | |
| .speak('Sorry, an error occurred.') | |
| .reprompt('Sorry, an error occurred.') | |
| .getResponse(); | |
| }, | |
| }; | |
| const HELP_MESSAGE = 'You can say tell that you swore, and I will keep a log of it. You can also say exit... What can I help you with?'; | |
| const HELP_REPROMPT = 'What can I help you with?'; | |
| const STOP_MESSAGE = 'Goodbye!'; | |
| const skillBuilder = Alexa.SkillBuilders.standard(); | |
| exports.handler = skillBuilder | |
| .addRequestHandlers( | |
| ISworeIntentHandler, | |
| HelpHandler, | |
| ExitHandler, | |
| SessionEndedRequestHandler | |
| ) | |
| .addErrorHandlers(ErrorHandler) | |
| .lambda(); |
This file contains hidden or 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
| { | |
| "interactionModel": { | |
| "languageModel": { | |
| "invocationName": "swear jar", | |
| "intents": [ | |
| { | |
| "name": "AMAZON.FallbackIntent", | |
| "samples": [] | |
| }, | |
| { | |
| "name": "AMAZON.CancelIntent", | |
| "samples": [] | |
| }, | |
| { | |
| "name": "AMAZON.HelpIntent", | |
| "samples": [] | |
| }, | |
| { | |
| "name": "AMAZON.StopIntent", | |
| "samples": [] | |
| }, | |
| { | |
| "name": "AMAZON.NavigateHomeIntent", | |
| "samples": [] | |
| }, | |
| { | |
| "name": "ISworeIntent", | |
| "slots": [ | |
| { | |
| "name": "swearCount", | |
| "type": "AMAZON.NUMBER" | |
| } | |
| ], | |
| "samples": [ | |
| "I swore more than {swearCount} times", | |
| "I swore again {swearCount} times", | |
| "I swore {swearCount} times", | |
| "Sorry", | |
| "Ops", | |
| "Damn", | |
| "Swearing again", | |
| "Swore again", | |
| "I did it again", | |
| "I swore again", | |
| "I just swore", | |
| "I swore today" | |
| ] | |
| } | |
| ], | |
| "types": [] | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment