是否可以使用 Spring Boot https 端点创建自定义 Alexa 技能
Is it possible to create a custom Alexa skill with Spring Boot https endpoint
我想在不使用 Lambda 的情况下创建自定义 Alexa 技能。因此,我已将 Spring Boot 应用程序部署到 AWS EC2 实例,设置 SSL 证书,并通过使用 Postman 调用它来测试该服务是否正常运行。
然后我将 Alexa 技能设置为 "https" 端点。当我在 developer.amazon.com 上使用测试表格时,我只是返回:
The remote endpoint could not be called, or the response it returned was invalid.
当我直接使用 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 Skill Set 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);
}
}
所以,我放弃了上面的内容,而是使用 Spring 的 ServletRegistrationBean class.
注册了一个自定义 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 class 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 {
}
}
很有魅力!
我想在不使用 Lambda 的情况下创建自定义 Alexa 技能。因此,我已将 Spring Boot 应用程序部署到 AWS EC2 实例,设置 SSL 证书,并通过使用 Postman 调用它来测试该服务是否正常运行。
然后我将 Alexa 技能设置为 "https" 端点。当我在 developer.amazon.com 上使用测试表格时,我只是返回:
The remote endpoint could not be called, or the response it returned was invalid.
当我直接使用 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 Skill Set 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);
}
}
所以,我放弃了上面的内容,而是使用 Spring 的 ServletRegistrationBean class.
注册了一个自定义 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 class 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 {
}
}
很有魅力!