首页 > 解决方案 > 亚马逊 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 为空”错误。

标签: alexaalexa-skills-kit

解决方案


两件事情...

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

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

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

所以:

async handle(handlerInput)
const res = await axios.get(...)

解包.thenandfinishReq代码,使其全部在处理程序范围内。


推荐阅读