All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.klerch.alexa.tellask.util.factory.AlexaSpeechletFactory Maven / Gradle / Ivy

Go to download

This SDK is an extension to the Alexa Skills SDK for Java. It provides a framework for handling speechlet requests with multi-variant utterances organized in YAML files that make it easy to create localized skills. This SDK also lets you build your skill in declarative style and avoids a lot of boilerplate code.

There is a newer version: 0.2.3
Show newest version
/**
 * Created by Kay Lerch (https://twitter.com/KayLerch)
 *
 * Contribute to https://github.com/KayLerch/alexa-skills-kit-tellask-java
 *
 * Attached license applies.
 * This source is licensed under GNU GENERAL PUBLIC LICENSE Version 3 as of 29 June 2007
 */
package io.klerch.alexa.tellask.util.factory;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.klerch.alexa.state.handler.AlexaStateHandler;
import io.klerch.alexa.tellask.model.wrapper.AlexaSpeechlet;
import io.klerch.alexa.tellask.schema.UtteranceReader;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Optional;

/**
 * This factory is for creating an AlexaSpeechlet
 */
public class AlexaSpeechletFactory {
    public static final String DEFAULT_LOCALE = "en-US";

    /**
     * Creates an AlexaSpeechlet from bytes of a speechlet request. It will extract the
     * locale from the request and uses it for creating a new instance of AlexaSpeechlet
     * @param serializedSpeechletRequest bytes of a speechlet request
     * @param speechletClass the class of your AlexaSpeechlet to instantiate
     * @param utteranceReader the reader AlexaSpeechlet should use when reading out utterances
     * @param  must extend AlexaSpeechlet
     * @return new instance of AlexaSpeechlet
     * @throws IOException thrown when something went wrong
     */
    public static  T createSpeechletFromRequest(final byte[] serializedSpeechletRequest, final Class speechletClass, final UtteranceReader utteranceReader) throws IOException {
        final ObjectMapper mapper = new ObjectMapper();

        final JsonNode parser = mapper.readTree(serializedSpeechletRequest);
        final String locale = Optional.of(parser.path("request"))
                .filter(node -> !node.isMissingNode())
                .map(node -> node.path("locale"))
                .filter(node -> !node.isMissingNode())
                .map(JsonNode::textValue)
                .orElse(DEFAULT_LOCALE);

        try {
            return speechletClass.getConstructor(String.class, UtteranceReader.class)
                    .newInstance(locale, utteranceReader);
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new IOException("Could not create Speechlet from speechlet request", e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy