Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
  9.     },
  10.     handle(handlerInput) {
  11.         const speakOutput = 'Welcome, you can say Hello or Help. Which would you like to try?';
  12.         return handlerInput.responseBuilder
  13.             .speak(speakOutput)
  14.             .reprompt(speakOutput)
  15.             .getResponse();
  16.     }
  17. };
  18. const TalkPointNewsIntentHandler = {
  19.     canHandle(handlerInput) {
  20.         return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
  21.             && Alexa.getIntentName(handlerInput.requestEnvelope) === 'TalkPointNewsIntent';
  22.     },
  23.     handle(handlerInput) {
  24.         const speakOutput = 'Hello Woods!';
  25.         return handlerInput.responseBuilder
  26.             .speak(speakOutput)
  27.             //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
  28.             .getResponse();
  29.     }
  30. };
  31. const HelloWorldIntentHandler = {
  32.     canHandle(handlerInput) {
  33.         return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
  34.             && Alexa.getIntentName(handlerInput.requestEnvelope) === 'HelloWorldIntent';
  35.     },
  36.     handle(handlerInput) {
  37.         const speakOutput = 'Hello Worlds!';
  38.         return handlerInput.responseBuilder
  39.             .speak(speakOutput)
  40.             //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
  41.             .getResponse();
  42.     }
  43. };
  44. const HelpIntentHandler = {
  45.     canHandle(handlerInput) {
  46.         return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
  47.             && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
  48.     },
  49.     handle(handlerInput) {
  50.         const speakOutput = 'You can say hello to me! How can I help?';
  51.  
  52.         return handlerInput.responseBuilder
  53.             .speak(speakOutput)
  54.             .reprompt(speakOutput)
  55.             .getResponse();
  56.     }
  57. };
  58. const CancelAndStopIntentHandler = {
  59.     canHandle(handlerInput) {
  60.         return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
  61.             && (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
  62.                 || Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
  63.     },
  64.     handle(handlerInput) {
  65.         const speakOutput = 'Goodbye!';
  66.         return handlerInput.responseBuilder
  67.             .speak(speakOutput)
  68.             .getResponse();
  69.     }
  70. };
  71. const SessionEndedRequestHandler = {
  72.     canHandle(handlerInput) {
  73.         return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
  74.     },
  75.     handle(handlerInput) {
  76.         // Any cleanup logic goes here.
  77.         return handlerInput.responseBuilder.getResponse();
  78.     }
  79. };
  80.  
  81. // The intent reflector is used for interaction model testing and debugging.
  82. // It will simply repeat the intent the user said. You can create custom handlers
  83. // for your intents by defining them above, then also adding them to the request
  84. // handler chain below.
  85. const IntentReflectorHandler = {
  86.     canHandle(handlerInput) {
  87.         return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
  88.     },
  89.     handle(handlerInput) {
  90.         const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
  91.         const speakOutput = `You just triggered ${intentName}`;
  92.  
  93.         return handlerInput.responseBuilder
  94.             .speak(speakOutput)
  95.             //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
  96.             .getResponse();
  97.     }
  98. };
  99.  
  100. // Generic error handling to capture any syntax or routing errors. If you receive an error
  101. // stating the request handler chain is not found, you have not implemented a handler for
  102. // the intent being invoked or included it in the skill builder below.
  103. const ErrorHandler = {
  104.     canHandle() {
  105.         return true;
  106.     },
  107.     handle(handlerInput, error) {
  108.         console.log(`~~~~ Error handled: ${error.stack}`);
  109.         const speakOutput = `Sorry, I had trouble doing what you asked. Please try again.`;
  110.  
  111.         return handlerInput.responseBuilder
  112.             .speak(speakOutput)
  113.             .reprompt(speakOutput)
  114.             .getResponse();
  115.     }
  116. };
  117.  
  118. // The SkillBuilder acts as the entry point for your skill, routing all request and response
  119. // payloads to the handlers above. Make sure any new handlers or interceptors you've
  120. // defined are included below. The order matters - they're processed top to bottom.
  121. exports.handler = Alexa.SkillBuilders.custom()
  122.     .addRequestHandlers(
  123.         LaunchRequestHandler,
  124.         TalkPointNewsIntentHandler,
  125.         HelloWorldIntentHandler,
  126.         HelpIntentHandler,
  127.         CancelAndStopIntentHandler,
  128.         SessionEndedRequestHandler,
  129.         IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
  130.     )
  131.     .addErrorHandlers(
  132.         ErrorHandler,
  133.     )
  134.     .lambda();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement