2017-11-25 134 views
2

我使用Apache Tomcat上託管的Java Alexa Skills Kit SDK實現了Alexa技能邏輯(speechlets)。但是,我需要將該項目移動到基於Apache Sling的服務器。它基於OSGi容器(Apache Felix)。我發現Sling DI機制非常有用。然而,它看起來像Java Alexa技能套件SDK完全不準備這樣的使用。主要問題是SDK servlet是普通的Java Servlet,而Sling不支持它。而且SDK甚至不是OSGi包。在Sling風格中使用它會很好,但我不想從頭開始複製SDK。在Sling OSGi容器中實現Alexa技能

有沒有人在OSGi容器中創建技能作爲吊索服務?我是否必須自己創建SlingServlet? Java Alexa技能套件SDK可以使用Sling服務嗎?

回答

1

你說得對,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 projectHere 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; 
    } 
} 
+1

感謝 - 這正是我需要的,我一直在尋找。奇怪的是之前沒有人做過。 – John