text: const Alexa = require('ask-sdk-core');
  
  const Util = require('util.js');
  
  function supportsAPL(handlerInput) {
      const supportedInterfaces = handlerInput.requestEnvelope.context.System.device.supportedInterfaces;
      const aplInterface = supportedInterfaces['Alexa.Presentation.APL'];
      return aplInterface !== null && aplInterface !== undefined;
  }
  const LaunchRequestHandler = {
      canHandle(handlerInput) {
          return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
      },
      handle(handlerInput) {
          const speakOutput = 'Buen día, estas listo para activar tu día con esta clase de insanity. Para comenzar di Abre Calentamiento';
           if (supportsAPL(handlerInput)) {
               let direccion = Util.getS3PreSignedUrl("Media/Cursos.jpg");
               handlerInput.responseBuilder
              .addDirective({
              type: 'Alexa.Presentation.APL.RenderDocument',
              document: require('./visual.json'),
              datasources: {
                "senias": {
                  "properties": {
                    "image": direccion
                  }
                }
              }
              });
          }
          return handlerInput.responseBuilder
              .speak(speakOutput)
              .reprompt(speakOutput)
              .getResponse();
      }
  };
  const HelloWorldIntentHandler = {
      canHandle(handlerInput) {
          return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
              && Alexa.getIntentName(handlerInput.requestEnvelope) === 'HelloWorldIntent';
      },
      handle(handlerInput) {
          const speakOutput = 'Ese curso no se encuentra disponible!';
          return handlerInput.responseBuilder
              .speak(speakOutput)
              .reprompt('Para comenzar di Inicia Calentamiento')
              .getResponse();
      }
  };
  
  const cursos = {
      canHandle(handlerInput) {
          return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
              && Alexa.getIntentName(handlerInput.requestEnvelope) === 'cursos';
      },
      handle(handlerInput) {
              const cursoQueRecibo = (handlerInput.requestEnvelope.request.intent.slots.curso.resolutions.resolutionsPerAuthority[0].values[0].value.name);
              let elcurso;
              if (cursoQueRecibo === 'Insanity') {
                  elcurso = `Abriendo`;
                  if (supportsAPL(handlerInput)) {
                      let direccion = Util.getS3PreSignedUrl("Media/Test.mp4");
                      console.log(`${direccion}`);
                      handlerInput.responseBuilder
                      .addDirective({
                          type: 'Alexa.Presentation.APL.RenderDocument',
                          document: require('./video.json'),
                          datasources: {
                              "senias": {
                              "properties": {
                              "Video": `${direccion}`
                                  }
                              }
                          }
                      });
                  }
              }
              
              if (cursoQueRecibo === 'Calentamiento') {
                  elcurso = `Abriendo`;
                  if (supportsAPL(handlerInput)) {
                      let direccion = Util.getS3PreSignedUrl("Media/Curso2.mp4");
                      console.log(`${direccion}`);
                      handlerInput.responseBuilder
                      .addDirective({
                          type: 'Alexa.Presentation.APL.RenderDocument',
                          document: require('./video.json'),
                          datasources: {
                              "senias": {
                              "properties": {
                              "Video": `${direccion}`
                                  }
                              }
                          }
                      });
                  }
              }
              
              if (cursoQueRecibo === 'Ejercicio') {
                  elcurso = `Abriendo`;
                  if (supportsAPL(handlerInput)) {
                      let direccion = Util.getS3PreSignedUrl("Media/Ejercicio2.mp4");
                      console.log(`${direccion}`);
                      handlerInput.responseBuilder
                      .addDirective({
                          type: 'Alexa.Presentation.APL.RenderDocument',
                          document: require('./video.json'),
                          datasources: {
                              "senias": {
                              "properties": {
                              "Video": `${direccion}`
                                  }
                              }
                          }
                      });
                  }
              }  
          
          const speakOutput = elcurso;
          return handlerInput.responseBuilder
              .speak(speakOutput)
              
              //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
              .getResponse();
      }
  };
  
  const HelpIntentHandler = {
      canHandle(handlerInput) {
          return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
              && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
      },
      handle(handlerInput) {
          const speakOutput = 'You can say hello to me! How can I help?';
  
          return handlerInput.responseBuilder
              .speak(speakOutput)
              .reprompt(speakOutput)
              .getResponse();
      }
  };
  const CancelAndStopIntentHandler = {
      canHandle(handlerInput) {
          return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
              && (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
                  || Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
      },
      handle(handlerInput) {
          const speakOutput = 'Nos vemos!';
          return handlerInput.responseBuilder
              .speak(speakOutput)
              .getResponse();
      }
  };
  const SessionEndedRequestHandler = {
      canHandle(handlerInput) {
          return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
      },
      handle(handlerInput) {
          // Any cleanup logic goes here.
          return handlerInput.responseBuilder.getResponse();
      }
  };
  
  // The intent reflector is used for interaction model testing and debugging.
  // It will simply repeat the intent the user said. You can create custom handlers
  // for your intents by defining them above, then also adding them to the request
  // handler chain below.
  const IntentReflectorHandler = {
      canHandle(handlerInput) {
          return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
      },
      handle(handlerInput) {
          const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
          const speakOutput = `You just triggered ${intentName}`;
  
          return handlerInput.responseBuilder
              .speak(speakOutput)
              //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
              .getResponse();
      }
  };
  
  // Generic error handling to capture any syntax or routing errors. If you receive an error
  // stating the request handler chain is not found, you have not implemented a handler for
  // the intent being invoked or included it in the skill builder below.
  const ErrorHandler = {
      canHandle() {
          return true;
      },
      handle(handlerInput, error) {
          console.log(`~~~~ Error handled: ${error.stack}`);
          const speakOutput = `Sorry, I had trouble doing what you asked. Please try again.`;
  
          return handlerInput.responseBuilder
              .speak(speakOutput)
              .reprompt(speakOutput)
              .getResponse();
      }
  };
  
  // The SkillBuilder acts as the entry point for your skill, routing all request and response
  // payloads to the handlers above. Make sure any new handlers or interceptors you've
  // defined are included below. The order matters - they're processed top to bottom.
  exports.handler = Alexa.SkillBuilders.custom()
      .addRequestHandlers(
          LaunchRequestHandler,
          HelloWorldIntentHandler,
          cursos,
          HelpIntentHandler,
          CancelAndStopIntentHandler,
          SessionEndedRequestHandler,
          IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
      )
      .addErrorHandlers(
          ErrorHandler,
      )
      .lambda();
  