我正在使用Apache上的Java Alexa技能工具包SDK实现Alexa技能逻辑(演讲稿)。但是,我需要将项目转移到基于阿帕奇吊索的服务器。它基于OSGi容器(阿帕奇费利克斯)。我发现Sling DI机制非常有用。然而,看起来完全没有做好这样的准备。主要问题是servlet是一个普通的,Sling不支持它。而且,SDK甚至不是一个OSGi包。在Sling风格中使用它会很好,但是我不想从头开始复制SDK。
有没有人在OSGi容器中创建作为吊索服务的技能?我必须自己创建一个SlingServlet吗?Java技能工具包SDK能与Sling服务一起工作吗?
发布于 2017-11-29 15:59:17
您是对的,没有启用OSGi,而且servlet不适用于Sling。但是,API的其余部分(除了servlet)由普通的Java对象组成,因此可以与Sling一起使用它。这就是为什么我创建了阿列克萨-技巧-吊索库,它将封装到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)意图逻辑实现的下面是一个非常基本的例子与您可以在这个项目中找到更多的示例。
@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;
}
}https://stackoverflow.com/questions/47488871
复制相似问题