【发布时间】:2017-07-18 05:28:47
【问题描述】:
大家好,StackOverflow 的乐于助人!我发现自己需要你的一些帮助和专业知识。我正在关注有关创建 Amazon Alexa 技能的教程,当我测试 lambda 函数的代码时,它成功了,但是,当我使用我指定给它的示例话语时,即“获取当前天气”(不要介意话语与实际端点无关,此时我正在使用来自 json 占位符的端点:https://jsonplaceholder.typicode.com/users 因为我只想要一个响应,任何类型的响应)。我绝对会感谢任何形式的帮助!在此先谢谢大家!
这是我的代码:
var https = require("https");
exports.handler = (event, context) => {
try{
if (event.session.new){
// New Session
console.log("new session!");
}
switch (event.request.type){
case "LaunchRequest":
// > Launch Request
console.log("launch request!");
context.succeed(
generateResponse(
buildSpeechletResponse("Welcome!!!!!!!! Let's make this work!", true),
{}
)
)
break;
case "IntentRequest":
// > Intent Request
console.log("intent request!"); // endpoint added here below
switch(event.request.intent.name){
case "getWeatherIntent":
var endpoint = "https://jsonplaceholder.typicode.com/users"; // this works with this "placebo endpoint data" https://jsonplaceholder.typicode.com/posts ***** api.openweathermap.org/data/2.5/weather?zip=10005,us&APPID=08d6215ef934232110949692d5ffb8da
var body = ""
https.get(endpoint, (response) => {
response.on('data', (chunk) => {body += chunk})
response.on('end', () => {
var data = JSON.parse(body);
var weatherCount = data.userId; // might have something to do with this variable
context.succeed(
generateResponse(
buildSpeechletResponse("current is ${weatherCount}", true),
{}
)
)
})
})
}
break; // endpoint added here above
case "SessionEndedRequest":
// > Session Ended Request
console.log("session ended request!");
break;
default:
context.fail("invalid request type!: {event.request.type}");
}
} catch(error) {context.fail("Exception: ${error}")}
}
// Helpers
buildSpeechletResponse = (outputText, shouldEndSession) => {
return{
outputSpeech:{
type: "PlainText",
text: outputText
},
shouldEndSession: shouldEndSession
}
}
generateResponse = (speechletResponse, sessionAttributes) => {
return {
version: "1.0",
sessionAttributes: sessionAttributes,
response: speechletResponse
}
}
这里也是意图模式:
{
"intents":[
{ "intent": "getWeatherIntent"
}
]
}
这里是 lambda 请求:
{
"session": {
"sessionId": "SessionId.51e05faf-df95-420f-9cfc-3736b1839482",
"application": {
"applicationId": "amzn1.ask.skill.482872e5-20a2-4230-bce4-a06b212443e5"
},
"attributes": {},
"user": {
"userId": "amzn1.ask.account.AFOFPAF44SZKQRQDFH3FW7PZCVEZPXLLPPWT7CO76Z62I2DVI5EFTYSFD3YMEA56R4ACYUSPTPVFGCA2BCFJCNBVLBNWAWSIOCXHCDTW5UM5WNIRE6K35XZ67CM3W2DN3NLIPRVEFWBZ3D6ASD37EYJWBQQFOK4FXB5NMGQCLJVGBJKUJMCZXVEHXU74KLSDXOV5MIF3UZPFLRA"
},
"new": true
},
"request": {
"type": "IntentRequest",
"requestId": "EdwRequestId.4b18c102-47ea-4855-a5da-6407379c0384",
"locale": "en-US",
"timestamp": "2017-02-28T02:13:25Z",
"intent": {
"name": "getWeatherIntent",
"slots": {}
}
},
"version": "1.0"
}
这是 lambda 响应:
{
"version": "1.0",
"response": {
"outputSpeech": {
"type": "PlainText",
"text": "current is ${weatherCount}"
},
"shouldEndSession": true
},
"sessionAttributes": {}
}
变量 ${weatherCount} 不应该已经返回从 JSON 解析的内容,而不是按原样返回吗?
这是在将模板文字周围的引号更改为反引号(将 "${weatherCount}" 更改为 ${weatherCount} )之后的新 Lambda 响应,因此我们可能会有所收获!:
{
"version": "1.0",
"response": {
"outputSpeech": {
"type": "PlainText",
"text": "current is [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]"
},
"shouldEndSession": true
},
"sessionAttributes": {}
}
它现在正在工作!关键是将模板文字周围的引号更改为反引号。这是新的 lambda 请求和新的 lambda 响应。
Lamdba 请求:
{
"session": {
"sessionId": "SessionId.9f8cc454-110f-4610-a7fe-cd0918fd804f",
"application": {
"applicationId": "amzn1.ask.skill.482872e5-20a2-4230-bce4-a06b212443e5"
},
"attributes": {},
"user": {
"userId": "amzn1.ask.account.AFOFPAF44SZKQRQDFH3FW7PZCVEZPXLLPPWT7CO76Z62I2DVI5EFTYSFD3YMEA56R4ACYUSPTPVFGCA2BCFJCNBVLBNWAWSIOCXHCDTW5UM5WNIRE6K35XZ67CM3W2DN3NLIPRVEFWBZ3D6ASD37EYJWBQQFOK4FXB5NMGQCLJVGBJKUJMCZXVEHXU74KLSDXOV5MIF3UZPFLRA"
},
"new": true
},
"request": {
"type": "IntentRequest",
"requestId": "EdwRequestId.229831d9-3bd1-4173-a1c4-7664994a1a77",
"locale": "en-US",
"timestamp": "2017-02-28T02:39:30Z",
"intent": {
"name": "getWeatherIntent",
"slots": {}
}
},
"version": "1.0"
}
新的 Lambda 响应:
{
"version": "1.0",
"response": {
"outputSpeech": {
"type": "PlainText",
"text": "current is 1"
},
"shouldEndSession": true
},
"sessionAttributes": {}
}
【问题讨论】:
-
撞,有谁知道或知道问题可能是什么?
-
您可以粘贴您的意图架构吗?
-
嗨@AnthonyNeace。非常感谢您的回复,因为我仍在努力解决这个问题。以下是我的意图架构: { "intents":[ { "intent": "getWeatherIntent" } ] }
-
您可以提供的任何更多信息,例如调试信息或 cloudwatch 日志(console.log 输出到 lambda 中的 cw)也会有所帮助。
-
嘿@AnthonyNeace:我非常感谢您的帮助。我会把 lambda 请求和响应贴在原文上
标签: json node.js amazon-web-services aws-lambda alexa