无法访问Alexa Skill DynamoDB端点

时间:2016-10-24 23:51:53

标签: node.js amazon-dynamodb aws-lambda alexa

我正在努力使用简单的Alexa技能改变我的DynamoDB中的一个值。我的代码基于亚马逊提供的ScoreKeeper示例。当我使用服务模拟器甚至使用我的Echo进行测试时,我得到“无法调用远程端点,或者它返回的响应无效。”

这是我的内联代码。我确保我的Lambda Role可以访问DynamoDB。不确定我错过了什么。任何帮助表示赞赏!

var https = require('https');
var AWS = require("aws-sdk");


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 to an Alexa Skill, this is running on a deployed lambda function", true),
        {}
      )
    )
    break;

  case "IntentRequest":
    // Intent Request
    console.log(`INTENT REQUEST`);
    var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
    switch(event.request.intent.name) {
      case "IncreaseVolume":

        dynamodb.putItem({
            TableName: 'VolumeTable',
            Item: {
                DeviceID: {
                    S: this._session.user.userId
                },
                Volume: {
                    S: "5"
                }
            }
        }, function (err, data) {
            if (err) {
                console.log(err, err.stack);
            }
        });
        context.succeed(
            generateResponse(
            buildSpeechletResponse("Volume has been increased", true),
            {}
            )
        )
        break;


      default:
        throw "Invalid intent"
    }

    break;

  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
}

}

1 个答案:

答案 0 :(得分:1)

您应该以这种方式重构代码:

dynamodb.putItem({
        TableName: 'VolumeTable',
        Item: {
            DeviceID: {
                S: this._session.user.userId
            },
            Volume: {
                S: "5"
            }
        }
    }, function (err, data) {
        if (err) {
            console.log(err, err.stack);
        }

        context.succeed(
            generateResponse(
            buildSpeechletResponse("Volume has been increased", true),
            {}
            );
        );
    });