gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:41:05 25 4
gpt4 key购买 nike

我正在尝试在不使用 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
}


我的 Controller 使用 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 类注册了一个自定义 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 {

}
}

奇迹般有效!

关于spring - 是否可以使用 Spring Boot https 端点创建自定义 Alexa 技能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40556117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com