2017-11-25 209 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