Amazon Alexa Skill Lambda 代码无法执行
Amazon Alexa Skill Lambda code won't execute
我正在尝试为 NodeJS 中的 Amazon Alexa 技能编写 Lambda 函数。 Alexa 是可以响应您的声音的圆柱形扬声器,"skill" 基本上是它的语音应用程序。此函数从 Alexa 设备获取 JSON 输入并创建响应,然后将新的 JSON 发送回设备以供输出。
此代码应该从 BTC-e JSON 中提取比特币对美元的汇率,提取 'avg' 值,并将其输出到 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)"
]
}
我的密码是here。我感觉问题出在第 84-106 行的某处,因为那是我完成大部分工作的地方。
感谢您的帮助!
下面的代码应该可以工作,只需取消注释我添加的待办事项即可。您缺少几个大括号,我切换到默认包含在 AWS lambda 中的不同 http 请求库。我也改变了流程,让我更容易测试,所以现在它不会重新提示它只是告诉你最新的比特币价格。
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
}
}
我正在尝试为 NodeJS 中的 Amazon Alexa 技能编写 Lambda 函数。 Alexa 是可以响应您的声音的圆柱形扬声器,"skill" 基本上是它的语音应用程序。此函数从 Alexa 设备获取 JSON 输入并创建响应,然后将新的 JSON 发送回设备以供输出。
此代码应该从 BTC-e JSON 中提取比特币对美元的汇率,提取 'avg' 值,并将其输出到 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)"
]
}
我的密码是here。我感觉问题出在第 84-106 行的某处,因为那是我完成大部分工作的地方。
感谢您的帮助!
下面的代码应该可以工作,只需取消注释我添加的待办事项即可。您缺少几个大括号,我切换到默认包含在 AWS lambda 中的不同 http 请求库。我也改变了流程,让我更容易测试,所以现在它不会重新提示它只是告诉你最新的比特币价格。
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
}
}