你說得對,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