我正在使用Apache Tomcat上托管的Java Alexa Skills Kit SDK实现Alexa Skills逻辑(speechlets)。但是,我需要将项目移动到基于Apache Sling的服务器。它基于OSGi容器(Apache Felix)。我发现Sling DI机制非常有用。然而,看起来Java Alexa Skills Kit SDK完全没有为此类用途做好准备。主要问题是SDK servlet是一个普通的Java servlet,而Sling不支持它。此外,SDK甚至不是OSGi包。在Sling风格中使用它会很好,但我不想从头开始复制SDK。
有没有人在OSGi容器中创建Skills作为Sling服务?我必须自己创建一个SlingServlet吗? Java Alexa Skills Kit SDK可以与Sling服务一起使用吗?
答案 0 :(得分:1)
你是对的,Java Alexa Skills Kit SDK不支持OSGi,servlet也不能与Sling一起使用。但是,API的其余部分(除了servlet之外)由普通的Java对象组成,因此可以将它与Sling一起使用。这就是我创建alexa-skills-sling库的原因,它将Java Alexa Skills Kit SDK包装成Sling功能,以便您可以使用服务和DI机制。
要使用它,您需要添加依赖项:
<dependency>
<groupId>eu.zacheusz.sling.alexa</groupId>
<artifactId>alexa-skills-sling</artifactId>
<version>1.2.1</version>
</dependency>
并将其安装为OSGi包。例如:
<plugins>
<plugin>
<groupId>org.apache.sling</groupId>
<artifactId>maven-sling-plugin</artifactId>
<executions>
<execution>
<id>install-dependency</id>
<goals>
<goal>install-file</goal>
</goals>
<phase>install</phase>
<configuration>
<!-- install dependency to test AEM Server -->
<slingUrl>http://${vm.host}:${vm.port}/apps/alexa/install</slingUrl>
<deploymentMethod>WebDAV</deploymentMethod>
<user>${vm.username}</user>
<password>${vm.password}</password>
<groupId>eu.zacheusz.sling.alexa</groupId>
<artifactId>alexa-skills-sling</artifactId>
<version>${alexa-skills-sling.version}</version>
<packaging>jar</packaging>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
然后,为了实现单个意图逻辑,只需将吊索注释添加到实现中,它就会被库拾取。
@Component
@Service(IntentHandler.class)
Here is a very basic example的Intent逻辑实现和you can find more examples in this project:
@Component
@Service(IntentHandler.class)
public class ExampleSimpleIntentHandlerService implements IntentHandler {
private static final String SLOT_NAME = "mySlot";
private static final String INTENT_NAME = "myIntent";
@Override
public boolean supportsIntent(String intentName) {
return INTENT_NAME.equals(intentName);
}
@Override
public SpeechletResponse handleIntent(final SpeechletRequestEnvelope<IntentRequest> requestEnvelope) {
final IntentRequest request = requestEnvelope.getRequest();
final Intent intent = request.getIntent();
final Slot slot = intent.getSlot(SLOT_NAME);
final String responseMessage;
if (slot == null) {
responseMessage = format(
"I got your request, but there is no slot %",
SLOT_NAME);
} else {
responseMessage = format(
"I got your request. Slot value is %s. Thanks!",
slot.getValue());
}
return newTellResponse(responseMessage);
}
private SpeechletResponse newTellResponse(final String text) {
return SpeechletResponse.newTellResponse(newPlainTextOutputSpeech(text));
}
private PlainTextOutputSpeech newPlainTextOutputSpeech(final String text) {
final PlainTextOutputSpeech speech = new PlainTextOutputSpeech();
speech.setText(text);
return speech;
}
}