2017-02-27 52 views
3

嘿StackOverflow中有帮助的人!我发现自己需要你的一些帮助和专业知识。当我测试lambda函数的代码时,我正在学习关于创建Amazon Alexa技能的教程,但是,当我使用我指定给它的示例话语时,它是“获取当前天气”(不要介意话语与实际终点无关,此时我正在使用来自json占位者的终端:https://jsonplaceholder.typicode.com/users,因为我只想要一个响应,任何类型的响应)。我一定会很感激任何形式的帮助!在此先感谢球员和女球员!我没有得到适当的来自Amazon Lambda的JSON响应

这里是我的代码:

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" 
    } 
    ] 
} 

这里是拉姆达要求:

{ 
    "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" 
} 

这里是拉姆达响应:

{ 
    "version": "1.0", 
    "response": { 
    "outputSpeech": { 
     "type": "PlainText", 
     "text": "current is ${weatherCount}" 
    }, 
    "shouldEndSession": true 
    }, 
    "sessionAttributes": {} 
} 

不应该变量$ {weatherCount}已经返回从JSON解析的东西,而不是按原样返回?

这是改变模板周围的文字引号反引号(改变“$ {} weatherCount”到${weatherCount}),所以我们可能会到一些!:

{ 
    "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请求和新的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": {} 
} 
+0

bump,有没有人知道或有什么问题的想法? – Marcode777

+0

你可以粘贴你的意图架构吗? –

+0

嗨@AnthonyNeace。非常感谢回复,因为我仍然在试图解决这个问题。以下是我的意图模式:{ “意图”:[{ “意图”:“getWeatherIntent” } ] } – Marcode777

回答

2

这似乎我喜欢与你template literals的问题。模板文字需要用反引号代替单引号或双引号。将引号替换为使用模板文字的所有字符串的反引号。

因此,例如这样的:

"current is ${weatherCount}"

变为:

`current is ${weatherCount}` 

其他故障排除提示:

  • 确保您使用最新版本的可用节点的到AWS Lambda;在撰写本文时,这是v4.3.2。老版本的节点不支持模板文字。

  • 如果无法执行这些步骤,只需将变量替换为模板文字即可将其作为问题排除。

  • 控制台。日志输出写入AWS Lambda中的cloudwatch - 您可以使用它来调试您的lambda函数的每个调用。更多的信息在这里:http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-logging.html

+0

解决!谢谢@AnthonyNeace! – Marcode777