Advertisement
Guest User

Untitled

a guest
May 21st, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. /* eslint-disable func-names */
  2. /* eslint-disable no-console */
  3.  
  4. const Alexa = require('ask-sdk');
  5.  
  6. const GetNewFactHandler = {
  7. canHandle(handlerInput) {
  8. const request = handlerInput.requestEnvelope.request;
  9. return request.type === 'LaunchRequest'
  10. || (request.type === 'IntentRequest'
  11. && request.intent.name === 'GetNewFactIntent');
  12. },
  13. handle(handlerInput) {
  14. const factArr = data;
  15. const factIndex = Math.floor(Math.random() * factArr.length);
  16. const randomFact = factArr[factIndex];
  17. const speechOutput = GET_FACT_MESSAGE + randomFact;
  18.  
  19. return handlerInput.responseBuilder
  20. .speak(speechOutput)
  21. .withSimpleCard(SKILL_NAME, randomFact)
  22. .getResponse();
  23. },
  24. };
  25.  
  26. const HelpHandler = {
  27. canHandle(handlerInput) {
  28. const request = handlerInput.requestEnvelope.request;
  29. return request.type === 'IntentRequest'
  30. && request.intent.name === 'AMAZON.HelpIntent';
  31. },
  32. handle(handlerInput) {
  33. return handlerInput.responseBuilder
  34. .speak(HELP_MESSAGE)
  35. .reprompt(HELP_REPROMPT)
  36. .getResponse();
  37. },
  38. };
  39.  
  40. const ExitHandler = {
  41. canHandle(handlerInput) {
  42. const request = handlerInput.requestEnvelope.request;
  43. return request.type === 'IntentRequest'
  44. && (request.intent.name === 'AMAZON.CancelIntent'
  45. || request.intent.name === 'AMAZON.StopIntent');
  46. },
  47. handle(handlerInput) {
  48. return handlerInput.responseBuilder
  49. .speak(STOP_MESSAGE)
  50. .getResponse();
  51. },
  52. };
  53.  
  54. const SessionEndedRequestHandler = {
  55. canHandle(handlerInput) {
  56. const request = handlerInput.requestEnvelope.request;
  57. return request.type === 'SessionEndedRequest';
  58. },
  59. handle(handlerInput) {
  60. console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);
  61.  
  62. return handlerInput.responseBuilder.getResponse();
  63. },
  64. };
  65.  
  66. const ErrorHandler = {
  67. canHandle() {
  68. return true;
  69. },
  70. handle(handlerInput, error) {
  71. console.log(`Error handled: ${error.message}`);
  72.  
  73. return handlerInput.responseBuilder
  74. .speak('Sorry, an error occurred.')
  75. .reprompt('Sorry, an error occurred.')
  76. .getResponse();
  77. },
  78. };
  79.  
  80. const SKILL_NAME = 'Space Facts';
  81. const GET_FACT_MESSAGE = 'Here\'s your fact: ';
  82. const HELP_MESSAGE = 'You can say tell me a space fact, or, you can say exit... What can I help you with?';
  83. const HELP_REPROMPT = 'What can I help you with?';
  84. const STOP_MESSAGE = 'Goodbye!';
  85.  
  86. const data = [
  87. 'A year on Mercury is just 88 days long.',
  88. 'Despite being farther from the Sun, Venus experiences higher temperatures than Mercury.',
  89. 'Venus rotates counter-clockwise, possibly because of a collision in the past with an asteroid.',
  90. 'On Mars, the Sun appears about half the size as it does on Earth.',
  91. 'Earth is the only planet not named after a god.',
  92. 'Jupiter has the shortest day of all the planets.',
  93. 'The Milky Way galaxy will collide with the Andromeda Galaxy in about 5 billion years.',
  94. 'The Sun contains 99.86% of the mass in the Solar System.',
  95. 'The Sun is an almost perfect sphere.',
  96. 'A total solar eclipse can happen once every 1 to 2 years. This makes them a rare event.',
  97. 'Saturn radiates two and a half times more energy into space than it receives from the sun.',
  98. 'The temperature inside the Sun can reach 15 million degrees Celsius.',
  99. 'The Moon is moving approximately 3.8 cm away from our planet every year.',
  100. ];
  101.  
  102. const skillBuilder = Alexa.SkillBuilders.standard();
  103.  
  104. exports.handler = skillBuilder
  105. .addRequestHandlers(
  106. GetNewFactHandler,
  107. HelpHandler,
  108. ExitHandler,
  109. SessionEndedRequestHandler
  110. )
  111. .addErrorHandlers(ErrorHandler)
  112. .lambda();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement