亚马逊Alexa技能Lambda代码将不会执行

我正在尝试在NodeJS中编写Amazon Alexa技能的Lambda函数。 Alexa就是那种响应你的声音的圆柱形扬声器,而“技巧”基本上就是一个语音应用程序。 该函数从Alexa设备获取JSONinput并创build一个响应,然后将新的JSON发送回设备进行输出。

这个代码应该从BTC-e JSON中将比特币拉到美元汇率,提取“平均值”并将其输出到Alexa设备。

我很久没有做任何编码,所以请原谅任何愚蠢的错误。 我拿了示例代码,并试图修改它为我的目的,但在AWS中执行时出现此错误:

{ "errorMessage": "Unexpected identifier", "errorType": "SyntaxError", "stackTrace": [ "Module._compile (module.js:439:25)", "Object.Module._extensions..js (module.js:474:10)", "Module.load (module.js:356:32)", "Function.Module._load (module.js:312:12)", "Module.require (module.js:364:17)", "require (module.js:380:17)" ] } 

我的代码在这里 。 我有一个感觉,问题是在84-106行,因为这是我的大部分工作。

感谢您的帮助!

       

网上收集的解决方案 "亚马逊Alexa技能Lambda代码将不会执行"

下面的代码应该工作,只是取消了我添加的待办事项的评论。 你有几个缺失的大括号,我切换到一个不同的http请求库,默认包含在AWS lambda中。 我也改变stream程,使我testing更简单,所以现在它不会再提示它只是告诉你最新的硬币价格。

 var https = require('https'); // Route the incoming request based on type (LaunchRequest, IntentRequest, // etc.) The JSON body of the request is provided in the event parameter. exports.handler = function (event, context) { try { console.log("event.session.application.applicationId=" + event.session.application.applicationId); /** * Uncomment this if statement and populate with your skill's application ID to * prevent someone else from configuring a skill that sends requests to this function. */ // TODO add this back in for your app // if (event.session.application.applicationId !== "amzn1.echo-sdk-ams.app.") { // context.fail("Invalid Application ID"); // } if (event.session.new) { onSessionStarted({requestId: event.request.requestId}, event.session); } if (event.request.type === "LaunchRequest") { onLaunch(event.request, event.session, function callback(sessionAttributes, speechletResponse) { context.succeed(buildResponse(sessionAttributes, speechletResponse)); }); } else if (event.request.type === "IntentRequest") { onIntent(event.request, event.session, function callback(sessionAttributes, speechletResponse) { context.succeed(buildResponse(sessionAttributes, speechletResponse)); }); } else if (event.request.type === "SessionEndedRequest") { onSessionEnded(event.request, event.session); context.succeed(); } } catch (e) { context.fail("Exception: " + e); } }; /** * Called when the session starts. */ function onSessionStarted(sessionStartedRequest, session) { console.log("onSessionStarted requestId=" + sessionStartedRequest.requestId + ", sessionId=" + session.sessionId); } /** * Called when the user launches the skill without specifying what they want. */ function onLaunch(launchRequest, session, callback) { console.log("onLaunch requestId=" + launchRequest.requestId + ", sessionId=" + session.sessionId); // Dispatch to your skill's launch. getWelcomeResponse(callback); } /** * Called when the user specifies an intent for this skill. */ function onIntent(intentRequest, session, callback) { console.log("onIntent requestId=" + intentRequest.requestId + ", sessionId=" + session.sessionId); getWelcomeResponse(callback); } /** * Called when the user ends the session. * Is not called when the skill returns shouldEndSession=true. */ function onSessionEnded(sessionEndedRequest, session) { console.log("onSessionEnded requestId=" + sessionEndedRequest.requestId + ", sessionId=" + session.sessionId); // Add cleanup logic here } // --------------- Functions that control the skill's behavior ----------------------- function getWelcomeResponse(callback) { // If we wanted to initialize the session to have some attributes we could add those here. var sessionAttributes = {}; var cardTitle = "Bitcoin Price"; var speechOutput = "Welcome to the Bitcoin Price skill, " var repromptText = '' var shouldEndSession = true; var options = { host: 'btc-e.com', port: 443, path: '/api/2/btc_usd/ticker', method: 'GET' }; var req = https.request(options, function(res) { var body = ''; console.log('Status:', res.statusCode); console.log('Headers:', JSON.stringify(res.headers)); res.setEncoding('utf8'); res.on('data', function(chunk) { body += chunk; }); res.on('end', function() { console.log('Successfully processed HTTPS response'); body = JSON.parse(body); console.log(body); console.log('last price = ' + body.ticker.last); speechOutput += "The last price for bitcoin was : " + body.ticker.last; callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)); }); }); req.end(); } // --------------- Helpers that build all of the responses ----------------------- function buildSpeechletResponse(title, output, repromptText, shouldEndSession) { return { outputSpeech: { type: "PlainText", text: output }, card: { type: "Simple", title: "SessionSpeechlet - " + title, content: "SessionSpeechlet - " + output }, reprompt: { outputSpeech: { type: "PlainText", text: repromptText } }, shouldEndSession: shouldEndSession } } function buildResponse(sessionAttributes, speechletResponse) { return { version: "1.0", sessionAttributes: sessionAttributes, response: speechletResponse } }