Last active
October 9, 2018 18:24
-
-
Save haigopi/85553c12b3aa84b68bb87569add70c81 to your computer and use it in GitHub Desktop.
Sample Lambda for Alexa 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
'use strict'; | |
const Alexa = require('alexa-sdk'); | |
const APP_ID = "amzn1.ask.skill.072d9e35-a053-40c9-ba6c-cf7467cd1825"; | |
const SKILL_NAME = 'Space Facts'; | |
const GET_FACT_MESSAGE = "Here's your fact: "; | |
const HELP_MESSAGE = 'You can say tell me a space fact, or, you can say exit... What can I help you with?'; | |
const HELP_REPROMPT = 'What can I help you with?'; | |
const STOP_MESSAGE = 'Goodbye!'; | |
const data = [ | |
'A year on Mercury is just 88 days long.', | |
'Despite being farther from the Sun, Venus experiences higher temperatures than Mercury.', | |
'Venus rotates counter-clockwise, possibly because of a collision in the past with an asteroid.', | |
'On Mars, the Sun appears about half the size as it does on Earth.', | |
'Earth is the only planet not named after a god.', | |
'Jupiter has the shortest day of all the planets.', | |
'The Milky Way galaxy will collide with the Andromeda Galaxy in about 5 billion years.', | |
'The Sun contains 99.86% of the mass in the Solar System.', | |
'The Sun is an almost perfect sphere.', | |
'A total solar eclipse can happen once every 1 to 2 years. This makes them a rare event.', | |
'Saturn radiates two and a half times more energy into space than it receives from the sun.', | |
'The temperature inside the Sun can reach 15 million degrees Celsius.', | |
'The Moon is moving approximately 3.8 cm away from our planet every year.', | |
]; | |
const handlers = { | |
'LaunchRequest': function () { | |
console.log("launch request recieved"); | |
this.emit(':ask', "which story you like? Say for example read chande mama story", "Say again please?"); | |
//this.emit('ReadStory'); | |
}, | |
'ReadStory': function () { | |
const factArr = data; | |
const factIndex = Math.floor(Math.random() * factArr.length); | |
const randomFact = factArr[factIndex]; | |
const speechOutput = GET_FACT_MESSAGE + randomFact; | |
var activity = isSlotValid(this.event.request, "TypeInput"); | |
console.log("-->", activity); | |
this.response.cardRenderer(SKILL_NAME, randomFact); | |
this.response.speak(speechOutput); | |
this.emit(':responseReady'); | |
}, | |
'AMAZON.HelpIntent': function () { | |
const speechOutput = HELP_MESSAGE; | |
const reprompt = HELP_REPROMPT; | |
this.response.speak(speechOutput).listen(reprompt); | |
this.emit(':responseReady'); | |
}, | |
'AMAZON.CancelIntent': function () { | |
this.response.speak(STOP_MESSAGE); | |
this.emit(':responseReady'); | |
}, | |
'AMAZON.StopIntent': function () { | |
this.response.speak(STOP_MESSAGE); | |
this.emit(':responseReady'); | |
}, | |
'Unhandled': function () { | |
this.response.speak("No Intent Specified"); | |
this.emit(':responseReady'); | |
}, | |
}; | |
exports.handler = function (event, context, callback) { | |
const alexa = Alexa.handler(event, context, callback); | |
alexa.APP_ID = "amzn1.ask.skill.ef61774b-8e54-4b76-a07d-9014eea1bac1"; | |
alexa.registerHandlers(handlers); | |
alexa.execute(); | |
}; | |
function isSlotValid(request, slotName){ | |
var slot = request.intent.slots[slotName]; | |
console.log("request = "+JSON.stringify(request)); //uncomment if you want to see the request | |
var slotValue; | |
//if we have a slot, get the text and store it into speechOutput | |
if (slot && slot.value) { | |
//we have a value in the slot | |
slotValue = slot.value.toLowerCase(); | |
return slotValue; | |
} else { | |
//we didn't get a value in the slot. | |
return "slot not found"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment