content: /**
      Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  
      Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
  
          http://aws.amazon.com/apache2.0/
  
      or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
   */
  package com.amazon.asksdk.minecrafthelper;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import com.amazon.speech.slu.Intent;
  import com.amazon.speech.slu.Slot;
  import com.amazon.speech.speechlet.IntentRequest;
  import com.amazon.speech.speechlet.LaunchRequest;
  import com.amazon.speech.speechlet.SpeechletV2;
  import com.amazon.speech.speechlet.SessionEndedRequest;
  import com.amazon.speech.speechlet.SessionStartedRequest;
  import com.amazon.speech.speechlet.SpeechletResponse;
  import com.amazon.speech.json.SpeechletRequestEnvelope;
  import com.amazon.speech.ui.PlainTextOutputSpeech;
  import com.amazon.speech.ui.Reprompt;
  import com.amazon.speech.ui.SimpleCard;
  
  /**
   * This sample shows how to create a Lambda function for handling Alexa Skill requests that:
   *
   * <ul>
   * <li><b>Custom slot type</b>: demonstrates using custom slot types to handle a finite set of known values</li>
   * </ul>
   * <p>
   * <h2>Examples</h2>
   * <p>
   * <b>One-shot model</b>
   * <p>
   * User: "Alexa, ask Minecraft Helper how to make paper."
   * <p>
   * Alexa:"(reads back recipe for paper)."
   */
  public class MinecraftSpeechlet implements SpeechletV2 {
      private static final Logger log = LoggerFactory.getLogger(MinecraftSpeechlet.class);
  
      /**
       * The key to get the item from the intent.
       */
      private static final String ITEM_SLOT = "Item";
  
      @Override
      public void onSessionStarted(SpeechletRequestEnvelope<SessionStartedRequest> requestEnvelope) {
          log.info("onSessionStarted requestId={}, sessionId={}", requestEnvelope.getRequest().getRequestId(),
                  requestEnvelope.getSession().getSessionId());
  
          // any initialization logic goes here
      }
  
      @Override
      public SpeechletResponse onLaunch(SpeechletRequestEnvelope<LaunchRequest> requestEnvelope) {
          log.info("onLaunch requestId={}, sessionId={}", requestEnvelope.getRequest().getRequestId(),
                  requestEnvelope.getSession().getSessionId());
  
          String speechOutput =
                  "Welcome to the Minecraft Helper. You can ask a question like, "
                          + "what's the recipe for a chest? ... Now, what can I help you with?";
          // If the user either does not reply to the welcome message or says
          // something that is not understood, they will be prompted again with this text.
          String repromptText = "For instructions on what you can say, please say help me.";
  
          // Here we are prompting the user for input
          return newAskResponse(speechOutput, repromptText);
      }
  
      @Override
      public SpeechletResponse onIntent(SpeechletRequestEnvelope<IntentRequest> requestEnvelope) {
          IntentRequest request = requestEnvelope.getRequest();
          log.info("onIntent requestId={}, sessionId={}", request.getRequestId(),
                  requestEnvelope.getSession().getSessionId());
  
          Intent intent = request.getIntent();
          String intentName = (intent != null) ? intent.getName() : null;
  
          if ("RecipeIntent".equals(intentName)) {
              return getRecipe(intent);
          } else if ("AMAZON.HelpIntent".equals(intentName)) {
              return getHelp();
          } else if ("AMAZON.StopIntent".equals(intentName)) {
              PlainTextOutputSpeech outputSpeech = new PlainTextOutputSpeech();
              outputSpeech.setText("Goodbye");
  
              return SpeechletResponse.newTellResponse(outputSpeech);
          } else if ("AMAZON.CancelIntent".equals(intentName)) {
              PlainTextOutputSpeech outputSpeech = new PlainTextOutputSpeech();
              outputSpeech.setText("Goodbye");
  
              return SpeechletResponse.newTellResponse(outputSpeech);
          } else {
              String errorSpeech = "This is unsupported.  Please try something else.";
              return newAskResponse(errorSpeech, errorSpeech);
          }
      }
  
      @Override
      public void onSessionEnded(SpeechletRequestEnvelope<SessionEndedRequest> requestEnvelope) {
          log.info("onSessionEnded requestId={}, sessionId={}", requestEnvelope.getRequest().getRequestId(),
                  requestEnvelope.getSession().getSessionId());
  
          // any cleanup logic goes here
      }
  
      /**
       * Creates a {@code SpeechletResponse} for the RecipeIntent.
       *
       * @param intent
       *            intent for the request
       * @return SpeechletResponse spoken and visual response for the given intent
       */
      private SpeechletResponse getRecipe(Intent intent) {
          Slot itemSlot = intent.getSlot(ITEM_SLOT);
          if (itemSlot != null && itemSlot.getValue() != null) {
              String itemName = itemSlot.getValue();
  
              // Get the recipe for the item
              String recipe = Recipes.get(itemName);
  
              if (recipe != null) {
                  // If we have the recipe, return it to the user.
                  PlainTextOutputSpeech outputSpeech = new PlainTextOutputSpeech();
                  outputSpeech.setText(recipe);
  
                  SimpleCard card = new SimpleCard();
                  card.setTitle("Recipe for " + itemName);
                  card.setContent(recipe);
  
                  return SpeechletResponse.newTellResponse(outputSpeech, card);
              } else {
                  // We don't have a recipe, so keep the session open and ask the user for another
                  // item.
                  String speechOutput =
                          "I'm sorry, I currently do not know the recipe for " + itemName
                                  + ". What else can I help with?";
                  String repromptSpeech = "What else can I help with?";
                  return newAskResponse(speechOutput, repromptSpeech);
              }
          } else {
              // There was no item in the intent so return the help prompt.
              return getHelp();
          }
      }
  
      /**
       * Creates a {@code SpeechletResponse} for the HelpIntent.
       *
       * @return SpeechletResponse spoken and visual response for the given intent
       */
      private SpeechletResponse getHelp() {
          String speechOutput =
                  "You can ask questions about minecraft such as, what's "
                          + "the recipe for a chest, or, you can say exit... "
                          + "Now, what can I help you with?";
          String repromptText =
                  "You can say things like, what's the recipe for a"
                          + " chest, or you can say exit... Now, what can I help you with?";
          return newAskResponse(speechOutput, repromptText);
      }
  
      /**
       * Wrapper for creating the Ask response. The OutputSpeech and {@link Reprompt} objects are
       * created from the input strings.
       *
       * @param stringOutput
       *            the output to be spoken
       * @param repromptText
       *            the reprompt for if the user doesn't reply or is misunderstood.
       * @return SpeechletResponse the speechlet response
       */
      private SpeechletResponse newAskResponse(String stringOutput, String repromptText) {
          PlainTextOutputSpeech outputSpeech = new PlainTextOutputSpeech();
          outputSpeech.setText(stringOutput);
  
          PlainTextOutputSpeech repromptOutputSpeech = new PlainTextOutputSpeech();
          repromptOutputSpeech.setText(repromptText);
          Reprompt reprompt = new Reprompt();
          reprompt.setOutputSpeech(repromptOutputSpeech);
  
          return SpeechletResponse.newAskResponse(outputSpeech, reprompt);
      }
  }
  