Skip to content

Instantly share code, notes, and snippets.

@KayLerch
Created October 30, 2017 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KayLerch/60cbdf7aeca4c65a34022299ab2bec91 to your computer and use it in GitHub Desktop.
Save KayLerch/60cbdf7aeca4c65a34022299ab2bec91 to your computer and use it in GitHub Desktop.
Wraps RequestStreamHandler and logs request payload in Alexa Java SDK
import com.amazon.speech.speechlet.SpeechletException;
import com.amazon.speech.speechlet.SpeechletRequestHandlerException;
import com.amazon.speech.speechlet.SpeechletV2;
import com.amazon.speech.speechlet.lambda.LambdaSpeechletRequestHandler;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
public class MyRequestStreamHandler implements RequestStreamHandler {
private static final Logger log = LoggerFactory.getLogger(WiseGuySpeechlet.class);
private static final Set<String> supportedApplicationIds;
// 1) instantiate your speechlet handler
private static final SpeechletV2 mySpeechlet = null;
// 2) add your skill id(s) here
static {
supportedApplicationIds = new HashSet<String>();
supportedApplicationIds.add("amzn1.xxxx.xxxx.xxxx.xxxx.xxxx");
}
@Override
public void handleRequest(final InputStream input, final OutputStream output, final Context context) throws IOException {
byte[] serializedSpeechletRequest = IOUtils.toByteArray(input);
log.info(new String(serializedSpeechletRequest));
final LambdaSpeechletRequestHandler handler = new LambdaSpeechletRequestHandler(supportedApplicationIds);
try {
byte[] outputBytes = handler.handleSpeechletCall(mySpeechlet, serializedSpeechletRequest);
output.write(outputBytes);
} catch (SpeechletRequestHandlerException | SpeechletException e) {
// wrap actual exception in expected IOException
throw new IOException(e);
}
}
}
@KayLerch
Copy link
Author

Use this as your new entry point for your Java Alexa skill instead of creating a class that derives from SpeechletRequestStreamHandler as described in the tutorials. Reference your speechlet handler in line 22 and your skill id(s) in line 27.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment