Last active
September 24, 2020 20:46
-
-
Save keif/30b946b5ee8106891cf759279d881e76 to your computer and use it in GitHub Desktop.
https://www.udemy.com/course/comprehensive-alexa-skill-development-course/learn/lecture/6680210#questions/7303418 I had to update the JavaScript and the JSON provided in the Udemy course that has not been updated.
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
{ | |
"session":{ | |
"new": true, | |
"sessionId":"SessionId.[unique-value-here]", | |
"application":{ | |
"applicationId":"amzn1.ask.skill.[YOUR_SKILL_ID_HERE]" | |
}, | |
"attributes":{ | |
"key": "string value" | |
}, | |
"user":{ | |
"userId":"amzn1.ask.account.[unique-value-here]" | |
} | |
}, | |
"request": { | |
"intent": { | |
"slots": { | |
"FirstName": { | |
"name": "FirstName", | |
"value": "John" | |
} | |
}, | |
"name": "HelloIntent" | |
}, | |
"type": "IntentRequest", | |
"requestId": "request5678" | |
}, | |
"context":{ | |
"System":{ | |
"application":{ | |
"applicationId":"amzn1.ask.skill.[YOUR_SKILL_ID_HERE]" | |
}, | |
"user":{ | |
"userId":"amzn1.ask.account.[unique-value-here]" | |
}, | |
"device":{ | |
"supportedInterfaces":{ | |
} | |
} | |
} | |
}, | |
"version":"1.0" | |
} |
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
"use strict" | |
const Alexa = require('ask-sdk-core'); | |
const TITLE_CARD = 'Greetings Jeeves' | |
const getWish = function getWish() { | |
const myDate = new Date() | |
let hours = myDate.getUTCHours() - 5 | |
if (hours < 0) { | |
hours = hours + 24 | |
} | |
if (hours < 12) { | |
return "Good morning." | |
} else if (hours < 18) { | |
return "Good afternoon." | |
} else { | |
return "Good evening." | |
} | |
} | |
const LaunchRequestHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'LaunchRequest'; | |
}, | |
handle(handlerInput) { | |
const speechText = "Welcome to Greetings Jeeves. Using our skill you can greet your guests. Whom do you want to greet?"; | |
const repromptText = "You can say, for example, say hello to John." | |
return handlerInput.responseBuilder | |
.speak(speechText) | |
.reprompt(repromptText) | |
.withSimpleCard(TITLE_CARD, 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(TITLE_CARD, speechText) | |
.withShouldEndSession(true) | |
.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 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(TITLE_CARD, speechText) | |
.getResponse(); | |
} | |
} | |
const SessionEndedRequestHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest'; | |
}, | |
handle(handlerInput) { | |
//any cleanup logic goes here | |
return handlerInput.responseBuilder.getResponse(); | |
} | |
}; | |
const HelloIntentHandler = { | |
canHandle(handlerInput) { | |
return handlerInput.requestEnvelope.request.type === 'IntentRequest' | |
&& handlerInput.requestEnvelope.request.intent.name === 'HelloIntent'; | |
}, | |
handle(handlerInput) { | |
const firstNameValue = Alexa.getSlotValue(handlerInput.requestEnvelope, 'FirstName') | |
const greeting = getWish(); | |
const speechText = `Hello ${firstNameValue}. ${greeting}` | |
return handlerInput.responseBuilder | |
.speak(speechText) | |
.reprompt(speechText) | |
.withSimpleCard(TITLE_CARD, speechText) | |
.getResponse(); | |
} | |
}; | |
let skill | |
exports.handler = async function (event, context) { | |
console.log(`REQUEST++++${JSON.stringify(event)}`); | |
if (!skill) { | |
skill = Alexa.SkillBuilders.custom() | |
.addRequestHandlers( | |
LaunchRequestHandler, | |
HelloIntentHandler, | |
HelpIntentHandler, | |
CancelAndStopIntentHandler, | |
SessionEndedRequestHandler, | |
) | |
.addErrorHandlers(ErrorHandler) | |
.create(); | |
} | |
const response = await skill.invoke(event, context); | |
console.log(`RESPONSE++++${JSON.stringify(response)}`); | |
return response; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment