你说得对,Java Alexa Skills Kit SDK不支持OSGi,而且Servlet不能与Sling配合使用。但是,其余的API(除了servlet)由普通的Java对象组成,所以可以在Sling中使用它。这就是为什么我创建alexa-skills-sling库将Java Alexa技能套件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>
然后为了实现一个意向逻辑,只需将Sling注释添加到实现中,它将被库拾取。
@Component
@Service(IntentHandler.class)
意图逻辑实现和you can find more examples in this project的Here is a very basic example:
@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;
}
}
感谢 - 这正是我需要的,我一直在寻找。奇怪的是之前没有人做过。 – John