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 spacegeek;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import com.amazon.speech.slu.Intent;
  import com.amazon.speech.speechlet.IntentRequest;
  import com.amazon.speech.speechlet.LaunchRequest;
  import com.amazon.speech.speechlet.Session;
  import com.amazon.speech.speechlet.SessionEndedRequest;
  import com.amazon.speech.speechlet.SessionStartedRequest;
  import com.amazon.speech.speechlet.Speechlet;
  import com.amazon.speech.speechlet.SpeechletException;
  import com.amazon.speech.speechlet.SpeechletResponse;
  import com.amazon.speech.ui.PlainTextOutputSpeech;
  import com.amazon.speech.ui.Reprompt;
  import com.amazon.speech.ui.SimpleCard;
  
  /**
   * This simple sample has no external dependencies or session management, and shows the most basic
   * example of how to handle Alexa Skill requests.
   */
  public class SpaceGeekSpeechlet implements Speechlet {
      private static final Logger log = LoggerFactory.getLogger(SpaceGeekSpeechlet.class);
  
      /**
       * Array containing space facts.
       */
      private static final String[] SPACE_FACTS = new String[] {
              "A year on Mercury is just 88 days long.",
              "Despite being farther from the Sun, Venus experiences higher temperatures "
                      + "than Mercury.",
              "Venus rotates counter-clockwise, possibly because of a collision in the "
                      + "past with an asteroid.",
              "On Mars, the Sun appears about half the size as it does on Earth.",
              "Earth is the only planet not named after a god.",
              "Jupiter has the shortest day of all the planets.",
              "The Milky Way galaxy will collide with the Andromeda Galaxy in about 5 "
                      + "billion years.",
              "The Sun contains 99.86% of the mass in the Solar System.",
              "The Sun is an almost perfect sphere.",
              "A total solar eclipse can happen once every 1 to 2 years. This makes them "
                      + "a rare event.",
              "Saturn radiates two and a half times more energy into space than it "
                      + "receives from the sun.",
              "The temperature inside the Sun can reach 15 million degrees Celsius.",
              "The Moon is moving approximately 3.8 cm away from our planet every year."
      };
  
      @Override
      public void onSessionStarted(final SessionStartedRequest request, final Session session)
              throws SpeechletException {
          log.info("onSessionStarted requestId={}, sessionId={}", request.getRequestId(),
                  session.getSessionId());
          // any initialization logic goes here
      }
  
      @Override
      public SpeechletResponse onLaunch(final LaunchRequest request, final Session session)
              throws SpeechletException {
          log.info("onLaunch requestId={}, sessionId={}", request.getRequestId(),
                  session.getSessionId());
          return getNewFactResponse();
      }
  
      @Override
      public SpeechletResponse onIntent(final IntentRequest request, final Session session)
              throws SpeechletException {
          log.info("onIntent requestId={}, sessionId={}", request.getRequestId(),
                  session.getSessionId());
  
          Intent intent = request.getIntent();
          String intentName = (intent != null) ? intent.getName() : null;
  
          if ("GetNewFactIntent".equals(intentName)) {
              return getNewFactResponse();
  
          } else if ("AMAZON.HelpIntent".equals(intentName)) {
              return getHelpResponse();
  
          } 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 {
              throw new SpeechletException("Invalid Intent");
          }
      }
  
      @Override
      public void onSessionEnded(final SessionEndedRequest request, final Session session)
              throws SpeechletException {
          log.info("onSessionEnded requestId={}, sessionId={}", request.getRequestId(),
                  session.getSessionId());
          // any cleanup logic goes here
      }
  
      /**
       * Gets a random new fact from the list and returns to the user.
       */
      private SpeechletResponse getNewFactResponse() {
          // Get a random space fact from the space facts list
          int factIndex = (int) Math.floor(Math.random() * SPACE_FACTS.length);
          String fact = SPACE_FACTS[factIndex];
  
          // Create speech output
          String speechText = "Here's your space fact: " + fact;
  
          // Create the Simple card content.
          SimpleCard card = new SimpleCard();
          card.setTitle("SpaceGeek");
          card.setContent(speechText);
  
          // Create the plain text output.
          PlainTextOutputSpeech speech = new PlainTextOutputSpeech();
          speech.setText(speechText);
  
          return SpeechletResponse.newTellResponse(speech, card);
      }
  
      /**
       * Returns a response for the help intent.
       */
      private SpeechletResponse getHelpResponse() {
          String speechText =
                  "You can ask Space Geek tell me a space fact, or, you can say exit. What can I "
                          + "help you with?";
  
          // Create the plain text output.
          PlainTextOutputSpeech speech = new PlainTextOutputSpeech();
          speech.setText(speechText);
  
          // Create reprompt
          Reprompt reprompt = new Reprompt();
          reprompt.setOutputSpeech(speech);
  
          return SpeechletResponse.newAskResponse(speech, reprompt);
      }
  }
  