亚马逊 Alexa“SpeechletResponse 为空”

时间:2021-03-19 20:01:15

标签: alexa alexa-skills-kit

这不是通过任何 3rd 方代码编辑器完成的。这是通过 Alexa 开发者控制台完成的。

我正在尝试为我的学校创建一项技能,以便在学校关闭、延迟 2 小时等情况下获取当前更新。

这是处理它的代码:

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 为空”错误。

谢谢!

1 个答案:

答案 0 :(得分:1)

两件事...

1:由于 axios.get 上的承诺,函数本身可能会在承诺完成之前终止并且不返回任何内容。

考虑使用 async-await 同步拉取 axios.get 调用的结果。

2:finishReq 将响应构建器返回给调用它的函数,而不是作为处理程序的结果。因此,即使您使用 async-await,将处理程序返回包装在函数中也会重定向它,并且不会将其返回到 SDK 以传输到 Alexa。

所以:

async handle(handlerInput)

const res = axios.get(...)

解开 .then 和 finishReq 代码,使其全部在处理程序范围内。