首页 > 解决方案 > Alexa:响应来自服务的响应(节点 v2 SDK)

问题描述

我正在使用 Alexa API。我想让 Alexa 响应从服务接收到的内容。

不确定在哪里添加承诺。我试过了,但 Alexa 说“请求的技能响应有问题”

const HelloWorldIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent';
    },
    handle(handlerInput) {

        MyService.facts().then(function(data) {

            const speechText = 'No facts';

            if (data) {
                speechText = 'random facts: '
                data.forEach(function (fact) {
                    speechText += fact;
                })
            }

            return handlerInput.responseBuilder
                .speak(speechText)
                .reprompt(speechText)
                .getResponse();

        }, function(err) {
            console.log(err);
        });

    }
};

标签: node.jsalexa-skills-kit

解决方案


不使用 Promise 的响应。这个例子怎么样:

async handle(handlerInput) {
    return await MyService.facts().then(function(data) {
        // other stuff
    });
}

推荐阅读