【问题标题】:Alexa NodeJS failing on speechoutput without a specific errorAlexa NodeJS 在没有特定错误的情况下在语音输出上失败
【发布时间】:2017-04-11 23:13:54
【问题描述】:

我正在尝试创建一个 Alexa 技能,它可以根据 Alexa 设备的位置在网络服务上查找位置。

我的所有代码都可以正常工作,因为我可以在日志中看到正确的输出,但我正在努力解决的一件事是让语音输出正常工作。

任何帮助将不胜感激。我对 NodeJS 很陌生。我觉得这与在 https.request 中调用 tell 有关,但我现在不知道如何解决。

代码:

deviceAddressRequest.then((addressResponse) => {
    switch(addressResponse.statusCode) {
        case 200:
            console.log("Address successfully retrieved, now responding to user.");
            const address = addressResponse.address;
            const postcode = `${address['postalCode']}`;

            // Get JSON response

            console.info("test begin");

            var post_data =
            {
                "postcode":
                "`${address['postalCode']}`"
            };
            console.info ('Users postcode is ' + postcode);
            var post_options = {
                host:  '[redacted]',
                port: '443',
                path: '/alexa/address.php?postcode=' + encodeURI(postcode),
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Content-Length': Buffer.byteLength(JSON.stringify(post_data))
                }
            };
            console.info ('Post options set');
            var post_req = https.request(post_options, function(res) {
                res.setEncoding('utf8');
                var returnData = "";
                res.on('data', function (chunk) {
                    returnData += chunk;
                });
                res.on('end', function () {
                    console.info("Data returned!")
                    console.info('returnData: ' + returnData);
                    var nearBoutique = JSON.parse(returnData).location1;
                    console.info("Nearest location: " + nearLocation);
                    const ADDRESS_MESSAGE = "Your nearest location is: " + nearLocation;
                });
            });
            post_req.write(JSON.stringify(post_data));
            post_req.end();
            console.info(ADDRESS_MESSAGE);
            // I tried it here, and also under the "const ADDRESS_MESSAGE"
            this.emit(":tell", ADDRESS_MESSAGE);
            break;
        case 204:
            console.log("Successfully requested from the device address API, but no address was returned.");
            this.emit(":tell", Messages.NO_ADDRESS);
            break;
        case 403:
            console.log("The consent token we had wasn't authorized to access the user's address.");
            this.emit(":tellWithPermissionCard", Messages.NOTIFY_MISSING_PERMISSIONS, PERMISSIONS);
            break;
        default:
            this.emit(":ask", Messages.LOCATION_FAILURE, Messages.LOCATION_FAILURE);
    }

    console.info("Ending getAddressHandler()");
});

还有 Lambda CloudWatch 日志:

START RequestId: *** Version: $LATEST
Beginning execution for skill with APP_ID=amzn1.ask.skill.***
Starting newSessionRequestHandler()
Starting getAddressHandler()
Creating AlexaAddressClient instance.
Ending newSessionRequestHandler()
Ending execution for skill with APP_ID=amzn1.ask.skill.***
Device Address API responded with a status code of : 200
Address successfully retrieved, now responding to user.
test begin
Users postcode is *** ***
Post options set
Data returned!
returnData: {"location1":"123 Street Name, London, is currently open.","location2":"123 Street Name, London, is currently open.","store3":"123 Street Name, London, is currently open.","postcode":*** ***}
Nearest location: 123 Street Name, London, is currently open.
END RequestId: ***
REPORT RequestId: Duration: 1869.51 ms Billed Duration: 1900 ms Memory Size: 128 MB Max Memory Used: 31 MB
START RequestId:  Version: $LATEST
Beginning execution for skill with APP_ID=amzn1.ask.skill.***
Starting sessionEndedRequestHandler()
Ending sessionEndedRequestHandler()
Ending execution for skill with APP_ID=amzn1.ask.skill.***
END RequestId: 
REPORT RequestId:  Duration: 15.49 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 31 MB

我不是在寻找直接的解决方案,但请指出正确方向的指针。

谢谢

【问题讨论】:

    标签: javascript jquery node.js aws-lambda alexa


    【解决方案1】:

    this 关键字在 JavaScript 中的工作方式似乎存在问题,您需要查看this link from mdn 以了解更多关于它的工作原理以及它在箭头函数方面的工作原理。

    【讨论】:

    • 一个不完美的解决方案是在原始 Alexa 调用的范围内定义一个变量,然后稍后引用它。 “让 thiz = this; ... thiz.emit ...”
    猜你喜欢
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    相关资源
    最近更新 更多