gpt4 book ai didi

node.js - Alexa 的 AWS Lambda 函数中的 HTTP 请求失败

转载 作者:太空宇宙 更新时间:2023-11-03 23:19:17 25 4
gpt4 key购买 nike

我正在尝试从内联 Lambda 函数中的外部 API 返回数据,但当我在 Alexa 开发者控制台中测试此数据时,我收到“请求的技能响应存在问题”,但我无法找出原因。另外,当我从 AWS 控制台执行此操作时,我无法通过 console.log 查看它实际返回的内容。
(为了帖子的目的,我已经删除了默认 intent )

const request = require('request');

const handlers = {
'LaunchRequest': function () {
this.emit(':ask', 'Welcome');
},
'GiveUpdateIntent': function (){
var slot = this.event.request.intent.slots.line.value;

httpGet(slot, (theResult) => {
this.response.speak(theResult);
this.emit(':responseReady');
});

}

};
function httpGet(query, callback) {
var options = {
host: 'api.tfl.gov.uk',
path: '/line/' + encodeURIComponent(query) + '/status',
method: 'GET',
};

var req = http.request(options, res => {
res.setEncoding('utf8');
var responseString = "";

//accept incoming data asynchronously
res.on('data', chunk => {
responseString += chunk;
});

//return the data when streaming is complete
res.on('end', () => {
console.log(responseString[0]);
callback(responseString[0]);
});

});
req.end();
}


exports.handler = function (event, context, callback) {
const alexa = Alexa.handler(event, context, callback);
alexa.APP_ID = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};

最佳答案

“请求的技能响应存在问题”通常意味着您的技能响应未采用预期格式。

您的 API 请求

例如:维多利亚

https://api.tfl.gov.uk/Line/victoria/Status  

返回 JSON,您不能直接将其作为响应传递给 Alexa。在将其发送回 Alexa 之前,请取出您实际希望 Alexa 说话的状态。然后将其放入任何技能用户都能理解的有意义的句子中并将其发回。

例如,您可以返回如下内容:

var speech = "Status severity description for " + 
this.event.request.intent.slots.line.value +
" is "
+ responseBody[0].lineStatuses.statusSeverityDescription;
this.emit(':ask',speech, "your re-prompt here");

这是我得到的示例 JSON

[
{
"$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities",
"id": "victoria",
"name": "Victoria",
"modeName": "tube",
"disruptions": [],
"created": "2018-07-31T12:11:08.477Z",
"modified": "2018-07-31T12:11:08.477Z",
"lineStatuses": [
{
"$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities",
"id": 0,
"statusSeverity": 10,
"statusSeverityDescription": "Good Service",
"created": "0001-01-01T00:00:00",
"validityPeriods": []
}
],
"routeSections": [],
"serviceTypes": [
{
"$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities",
"name": "Regular",
"uri": "/Line/Route?ids=Victoria&serviceTypes=Regular"
},
{
"$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities",
"name": "Night",
"uri": "/Line/Route?ids=Victoria&serviceTypes=Night"
}
],
"crowding": {
"$type": "Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities"
}
}
]

CloudWatch:请始终使用 CloudWatch 查看 Lambda 函数的日志,您将在 Lambda 函数的Monitoring 选项卡下看到一个链接。

配置 Lambda 测试事件:您可以直接从内嵌编辑器测试 Lambda 代码,方法是在您的 Test 菜单下配置 Lambda Test Events。内联编辑器。一个函数最多可以有 10 个测试事件。

关于node.js - Alexa 的 AWS Lambda 函数中的 HTTP 请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51642100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com