是否可以使用Spring Boot https端点创建自定义Alexa技能

时间:2016-11-11 20:58:46

标签: spring spring-boot alexa-skills-kit

我正在尝试在不使用Lambda的情况下创建自定义Alexa技能。因此,我已将Spring Boot应用程序部署到AWS EC2实例,设置SSL证书,并通过使用Postman调用该服务来测试该服务是否正常运行。

然后我将Alexa技能设置为" https"端点。当我在developer.amazon.com上使用Test表单时,我只是回过头来:

  

无法调用远程端点,或者它返回的响应无效。

当我直接使用Postman调用服务时,我得到:



{
  "version": "1.0",
  "response": {
    "outputSpeech": {
      "type": "PlainText",
      "id": null,
      "text": "Hello, World.  I am a Spring Boot custom skill."
    },
    "card": {
      "type": "Simple",
      "title": "HelloWorld",
      "content": "Hello, World.  I am a Spring Boot custom skill."
    },
    "reprompt": null,
    "shouldEndSession": true
  },
  "sessionAttributes": null
}




我的控制器使用Alexa技能集SDK。这是代码:

@RestController
public class AlexaController  {

    @RequestMapping(value="/alexa", 
        method=RequestMethod.POST,
        produces=MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<SpeechletResponseEnvelope> alexa(Model model) {

        String speechText = "Hello, World.  I am a Spring Boot custom skill.";

        SimpleCard card = new SimpleCard();
        card.setTitle("HelloWorld");
        card.setContent(speechText);

        PlainTextOutputSpeech speech = new PlainTextOutputSpeech();
        speech.setText(speechText);


        SpeechletResponse response = SpeechletResponse.newTellResponse(speech, card);


        SpeechletResponseEnvelope envelope = new SpeechletResponseEnvelope();
        envelope.setResponse(response);
        envelope.setVersion("1.0");
        envelope.setSessionAttributes(null);

        return new ResponseEntity<SpeechletResponseEnvelope>(envelope, HttpStatus.OK);
    }
}   

1 个答案:

答案 0 :(得分:11)

所以,我废弃了上面的内容,而是使用Spring的ServletRegistrationBean类注册了一个自定义servlet。

@Configuration
public class AlexaConfig {

    @Autowired
    private MyCustomSpeechlet mySpeechlet;

    @Bean
    public ServletRegistrationBean registerServlet() {

        SpeechletServlet speechletServlet = new SpeechletServlet();
        speechletServlet.setSpeechlet(mySpeechlet);

        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(speechletServlet, "/alexa");
        return servletRegistrationBean;     
    }
}

我的自定义Servlet扩展了Alexa Skill Kit类Speechlet。

@Service
public class MyCustomSpeechlet implements Speechlet {

    @Override
    public void onSessionStarted(SessionStartedRequest request, Session session) throws SpeechletException {    
    }

    @Override
    public SpeechletResponse onLaunch(LaunchRequest request, Session session) throws SpeechletException {
    }

    @Override
    public SpeechletResponse onIntent(IntentRequest request, Session session) throws SpeechletException {

        Intent intent = request.getIntent();
        if (intent == null)
            throw new SpeechletException("Unrecognized intent");

        String intentName = intent.getName();       

        if ( intentName.equals("TerriblyInterestingIntent") ) {

            String speechText = "Hello, World.  I am a Spring Boot custom skill.";

            SimpleCard card = new SimpleCard();
            card.setTitle("Hello World");
            card.setContent(speechText);

            PlainTextOutputSpeech speech = new PlainTextOutputSpeech();
            speech.setText(speechText);

            SpeechletResponse response = SpeechletResponse.newTellResponse(speech, card);
            return response;
        }
        else {
            throw new SpeechletException("I don't understand that intent.");
        }
    }

    @Override
    public void onSessionEnded(SessionEndedRequest request, Session session) throws SpeechletException {

    }
}

像魅力一样!