Skip to content

Commit

Permalink
adding null check for session attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
arun-gupta committed Sep 29, 2017
1 parent b59a689 commit 6385c64
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ public SpeechletResponse onIntent(final IntentRequest request, final Session ses
Intent intent = request.getIntent();
String intentName = (intent != null) ? intent.getName() : null;

if (StarWarsIntent.MOVIE_INTENT.equals(intentName)) {
return getIntroResponse("Star Wars is cool");
} else if (StarWarsIntent.PLANET_INTENT.equals(intentName)) {
if (StarWarsIntent.PLANET_INTENT.equals(intentName)) {
return getPlanetResponse(intent.getSlot("character").getValue());
} else if (StarWarsIntent.LIGHTSABER_INTENT.equals(intentName)) {
return getLightsaberResponse(intent.getSlot("character").getValue());
Expand All @@ -78,27 +76,17 @@ public SpeechletResponse onIntent(final IntentRequest request, final Session ses
* @return SpeechletResponse spoken and visual response for the given intent
*/
private SpeechletResponse getWelcomeResponse() {
StarWarsResponse response = StarWarsResponse.getWelcomeResponse();
StarWarsResponse response = StarWarsResponse.getHelpResponse();
return getSpeechletResponseWithReprompt(response.getSpeechText(), response.getTitle());
}

/**
* Creates a {@code SpeechletResponse} for the hello intent.
*
* @return SpeechletResponse spoken and visual response for the given intent
*/
private SpeechletResponse getIntroResponse(String intro) {
StarWarsResponse response = StarWarsResponse.getWelcomeResponse();
return getSpeechletResponse(response.getSpeechText(), response.getTitle());
}

/**
* Creates a {@code SpeechletResponse} for the help intent.
*
* @return SpeechletResponse spoken and visual response for the given intent
*/
private SpeechletResponse getHelpResponse() {
StarWarsResponse response = StarWarsResponse.getWelcomeResponse();
StarWarsResponse response = StarWarsResponse.getHelpResponse();
return getSpeechletResponse(response.getSpeechText(), response.getTitle());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.sample.aws.chatbot.starwars.common;

public final class StarWarsIntent {
public static final String MOVIE_INTENT = "StarWarsMovieIntent";
public static final String PLANET_INTENT = "StarWarsPlanetIntent";
public static final String LIGHTSABER_INTENT = "StarWarsLightsaberIntent";
public static final String QUOTES_INTENT = "StarWarsQuotesIntent";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,10 @@ public Map<String, String> getSessionAttributes() {
return sessionAttributes;
}

public static StarWarsResponse getWelcomeResponse() {
return new StarWarsResponse("Welcome to Star Wars Trivia, you can ask quotes", "Star Wars Welcome");
}

/**
* Creates a {@code SpeechletResponse} for the hello intent.
*
* @return SpeechletResponse spoken and visual response for the given intent
*/
public static StarWarsResponse getIntroResponse(String intro) {
return new StarWarsResponse("", "Star Wars Intro");
}

/**
* Creates a {@code SpeechletResponse} for the help intent.
*
* @return SpeechletResponse spoken and visual response for the given intent
*/
public static StarWarsResponse getHelpResponse() {
return new StarWarsResponse("Star Wars", "Star Wars Help");
return new StarWarsResponse("Welcome to Star Wars Chatbot! You can ask quotes, questions about lightsaber, Jedi or Sith questions.", "Star Wars Welcome");
}


public static StarWarsResponse getPlanetResponse(String slotValue) {
StarWarsCharacter character = DBUtil.getCharacter(slotValue);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ public LexResponse handleRequest(LexRequest request, Context context) {

String intent = request.getCurrentIntent().getName();
String character = request.getCurrentIntent().getSlots().get("character");
if (StarWarsIntent.MOVIE_INTENT.equals(intent)) {
return getIntroResponse("Star Wars is cool");
} else if (StarWarsIntent.PLANET_INTENT.equals(intent)) {
if (StarWarsIntent.PLANET_INTENT.equals(intent)) {
return getPlanetResponse(character);
} else if (StarWarsIntent.LIGHTSABER_INTENT.equals(intent)) {
return getLightsaberResponse(character);
Expand All @@ -38,6 +36,9 @@ public LexResponse handleRequest(LexRequest request, Context context) {
} else if (StarWarsIntent.QUESTION_INTENT.equals(intent)) {

String actualCharacter = request.getInputTranscript();

if (request.getSessionAttributes() == null)
throw new RuntimeException("Session attributes are null");
String expectedCharacter = request.getSessionAttributes().get("character");
String question = request.getSessionAttributes().get("question");

Expand All @@ -52,15 +53,13 @@ public LexResponse handleRequest(LexRequest request, Context context) {
} else {
return getDialogueQuestion(request.getSessionAttributes());
}
} else if ("AMAZON.HelpIntent".equals(intent)) {
return getHelpResponse();
} else {
throw new RuntimeException("Invalid Intent: " + request.getCurrentIntent().getName());
}
}

private LexResponse getIntroResponse(String intro) {
StarWarsResponse response = StarWarsResponse.getWelcomeResponse();
private LexResponse getHelpResponse() {
StarWarsResponse response = StarWarsResponse.getHelpResponse();
return getLexResponse(response.getSpeechText(), response.getTitle());
}

Expand Down Expand Up @@ -94,12 +93,16 @@ private LexResponse getDialogueQuestion(Map<String, String> sessionAttributes) {

LexResponse lexResponse = new LexResponse();

String answered = sessionAttributes.get("answered");
// String character = sessionAttributes.get("character");
String question = sessionAttributes.get("question");
String answered = "";
String question = "";

if (sessionAttributes != null) {
answered = sessionAttributes.get("answered");
question = sessionAttributes.get("question");
}

if ((null != answered && answered.equals("yes") ||
(null == question))) {
if ((null == question) ||
(null != answered && answered.equals("yes"))) {
System.out.println("Getting a new question");
StarWarsResponse response = StarWarsResponse.getDialogueQuestion();
String character = response.getSessionAttributes().get("character");
Expand Down Expand Up @@ -133,11 +136,6 @@ private LexResponse getDialogueResponse() {
return lexResponse;
}

private LexResponse getHelpResponse() {
StarWarsResponse response = StarWarsResponse.getWelcomeResponse();
return getLexResponse(response.getSpeechText(), response.getTitle());
}

private LexResponse getLexResponse(String speechText, String title) {
Message message = new Message(Message.CONTENT_TYPE_PLAIN_TEXT, speechText);
DialogAction dialogAction = new DialogAction(DialogAction.CLOSE_TYPE, DialogAction.FULFILLMENT_STATE_FULFILLED, message);
Expand Down

0 comments on commit 6385c64

Please sign in to comment.