alexa - 亚马逊 Alexa“SpeechletResponse 为空”
问题描述
这不是通过任何第三方代码编辑器。这是通过 Alexa 开发人员控制台完成的。
如果学校关闭、延迟两小时等,我正在尝试为我的学校创建一项技能以获取当前更新。
这是处理它的代码:
const GetCurrentUpdateHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'GetCurrentUpdate'
},
handle(handlerInput) {
let speakOutput = ""
axios.get("District Link")
.then(res => {
const dom = new JSDOM(res.data)
const msgs = dom.window.document.getElementById("onscreenalert-ctrl-msglist")
const useless = msgs.querySelectorAll("input")
useless.forEach(function (el) {
el.parentNode.removeChild(el)
})
const message = msgs.innerText
if (message === undefined) {
console.log("no messages")
speakOutput = "There are currently no messages."
finishReq(speakOutput)
} else {
console.log("message")
speakOutput = `Here is a message from District. ${msgs.innerText}`
finishReq(speakOutput)
}
})
function finishReq(output) {
return handlerInput.responseBuilder
.speak(output)
.getResponse();
}
}
}
我收到“SpeechletResponse 为空”错误。
解决方案
两件事情...
由于承诺 on
axios.get
,函数本身可能会在承诺完成之前终止并且不返回任何内容。考虑使用同步
async-await
拉取调用的结果。axios.get
finishReq
将响应构建器返回给调用它的函数,而不是作为处理程序的结果。因此,即使您使用async-await
,将处理程序返回包装在一个函数中也会将其重定向,并且不会将其返回给 SDK 以传输给 Alexa。
所以:
async handle(handlerInput)
const res = await axios.get(...)
解包.then
andfinishReq
代码,使其全部在处理程序范围内。
推荐阅读
- python - GoogleAPI 电子表格重复
- assembly - 如何用汇编语言打印出两个变量?
- artifactory - 带竹子的神器许可证管理器
- flutter - 调用 SetState() 时如何避免具有 UniqueKey 闪烁的 Flutter FutureBuilder?
- python - 删除 Jinja 模板中的缩进
- python-3.x - 通过网络抓取从使用 ajax 制作的图形中获取文本
- java - 有没有办法知道过期的保险库客户端令牌?
- .net - 设备未准备好。(新-PSSession)
- reactjs - 无法获取数字以填写井字游戏示例
- java - Android:tflite.runForMultipleInputsOutputs 返回 NaN