Advertisement
brandsc8r

Untitled

Jul 19th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. // This sample demonstrates handling intents from an Alexa skill using the Alexa Skills Kit SDK (v2).
  2. // Please visit https://alexa.design/cookbook for additional examples on implementing slots, dialog management,
  3. // session persistence, api calls, and more.
  4. const Alexa = require('ask-sdk-core');
  5.  
  6. const LaunchRequestHandler = {
  7. canHandle(handlerInput) {
  8. return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
  9. },
  10. handle(handlerInput) {
  11. const speechText = 'CatDay by Brandon and Scratchy has loaded. Ask whos day it is.';
  12. return handlerInput.responseBuilder
  13. .speak(speechText)
  14. .reprompt(speechText)
  15. .getResponse();
  16. }
  17. };
  18. const HelloWorldIntentHandler = {
  19. canHandle(handlerInput) {
  20. return handlerInput.requestEnvelope.request.type === 'IntentRequest'
  21. && handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent';
  22. },
  23. handle(handlerInput) {
  24. const speechText = 'Look at the fridge punk!';
  25. return handlerInput.responseBuilder
  26. .speak(speechText)
  27. //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
  28. .getResponse();
  29. }
  30. };
  31. const HelpIntentHandler = {
  32. canHandle(handlerInput) {
  33. return handlerInput.requestEnvelope.request.type === 'IntentRequest'
  34. && handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';
  35. },
  36. handle(handlerInput) {
  37. const speechText = 'You can say hello to me! How can I help?';
  38.  
  39. return handlerInput.responseBuilder
  40. .speak(speechText)
  41. .reprompt(speechText)
  42. .getResponse();
  43. }
  44. };
  45. const CancelAndStopIntentHandler = {
  46. canHandle(handlerInput) {
  47. return handlerInput.requestEnvelope.request.type === 'IntentRequest'
  48. && (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent'
  49. || handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent');
  50. },
  51. handle(handlerInput) {
  52. const speechText = 'Goodbye!';
  53. return handlerInput.responseBuilder
  54. .speak(speechText)
  55. .getResponse();
  56. }
  57. };
  58. const SessionEndedRequestHandler = {
  59. canHandle(handlerInput) {
  60. return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';
  61. },
  62. handle(handlerInput) {
  63. // Any cleanup logic goes here.
  64. return handlerInput.responseBuilder.getResponse();
  65. }
  66. };
  67.  
  68. // The intent reflector is used for interaction model testing and debugging.
  69. // It will simply repeat the intent the user said. You can create custom handlers
  70. // for your intents by defining them above, then also adding them to the request
  71. // handler chain below.
  72. const IntentReflectorHandler = {
  73. canHandle(handlerInput) {
  74. return handlerInput.requestEnvelope.request.type === 'IntentRequest';
  75. },
  76. handle(handlerInput) {
  77. const intentName = handlerInput.requestEnvelope.request.intent.name;
  78. const speechText = `You just triggered ${intentName}`;
  79.  
  80. return handlerInput.responseBuilder
  81. .speak(speechText)
  82. //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
  83. .getResponse();
  84. }
  85. };
  86.  
  87. // Generic error handling to capture any syntax or routing errors. If you receive an error
  88. // stating the request handler chain is not found, you have not implemented a handler for
  89. // the intent being invoked or included it in the skill builder below.
  90. const ErrorHandler = {
  91. canHandle() {
  92. return true;
  93. },
  94. handle(handlerInput, error) {
  95. console.log(`~~~~ Error handled: ${error.message}`);
  96. const speechText = `Sorry, I couldn't understand what you said. Please try again.`;
  97.  
  98. return handlerInput.responseBuilder
  99. .speak(speechText)
  100. .reprompt(speechText)
  101. .getResponse();
  102. }
  103. };
  104. // This handler acts as the entry point for your skill, routing all request and response
  105. // payloads to the handlers above. Make sure any new handlers or interceptors you've
  106. // defined are included below. The order matters - they're processed top to bottom.
  107. exports.handler = Alexa.SkillBuilders.custom()
  108. .addRequestHandlers(
  109. LaunchRequestHandler,
  110. HelloWorldIntentHandler,
  111. HelpIntentHandler,
  112. CancelAndStopIntentHandler,
  113. SessionEndedRequestHandler,
  114. IntentReflectorHandler) // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
  115. .addErrorHandlers(
  116. ErrorHandler)
  117. .lambda();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement