在DynamoDB中搜索非主键并集成到Alexa Skills中

时间:2018-01-25 14:45:49

标签: amazon-web-services aws-lambda amazon-dynamodb alexa alexa-skills-kit

我正在尝试使用AWS Lambda搜索非主键并将其集成到Alexa Skills Kit中。我是使用DynamoDB和Alexa Skills Kit的新手,我很难在网上找到任何解决方案。我正在尝试做的基本前提是查询表yesno有两列,id和message。只查看消息列以查找与我在params中指定的文本匹配。

以下是我正在使用的Lambda代码:

const AWSregion = 'eu-west-1';  
const Alexa = require('alexa-sdk');
const AWS = require('aws-sdk');

//params for searching table
const params = {
            TableName: 'yesno',
            Key:{ "message": 'Ben Davies' }
        };

AWS.config.update({
    region: AWSregion
});

exports.handler = function(event, context, callback) {
    var alexa = Alexa.handler(event, context);

    // alexa.appId = 'amzn1.echo-sdk-ams.app.1234';
    // alexa.dynamoDBTableName = 'YourTableName'; // creates new table for session.attributes

    alexa.registerHandlers(handlers);
    alexa.execute();
};

const handlers = {
    'LaunchRequest': function () {
        this.response.speak('welcome to magic answers.  ask me a yes or no question.').listen('try again');
        this.emit(':responseReady');
    },


    'MyIntent': function () {
        var MyQuestion = this.event.request.intent.slots.MyQuestion.value;
        console.log('MyQuestion : ' + MyQuestion);

        readDynamoItem(params, myResult=>{
            var say = MyQuestion;
            say = myResult;

            say = 'you asked, ' + MyQuestion + '. I found a reckord for: ' + myResult;
            this.response.speak(say).listen('try again');
            this.emit(':responseReady');


        });

    },
    'AMAZON.HelpIntent': function () {
        this.response.speak('ask me a yes or no question.').listen('try again');
        this.emit(':responseReady');
    },
    'AMAZON.CancelIntent': function () {
        this.response.speak('Goodbye!');
        this.emit(':responseReady');
    },
    'AMAZON.StopIntent': function () {
        this.response.speak('Goodbye!');
        this.emit(':responseReady');
    }
};

//  END of Intent Handlers {} ========================================================================================
//  Helper Function  =================================================================================================


function readDynamoItem(params, callback) {
    var AWS = require('aws-sdk');
    AWS.config.update({region: AWSregion});

    var dynamodb = new AWS.DynamoDB();

    console.log('reading item from DynamoDB table');

    dynamodb.query(params, function (err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else{
            console.log(data); // successful response
            callback(data.Item.message);
        }               
    });

}

我知道我可能完全错了,但是没有太多的在线将DynamoDB与Alexa Skill集成,我唯一能找到的就是通过ID搜索。如果不将表中的所有项目都拉到地图或列表中,这对我想做的事情不起作用,并且看到我想要创建一个大数据库,它看起来效率很低。

在Alexa方面,我在测试代码时收到以下服务请求:

    {
  "session": {
    "new": true,
    "sessionId": "SessionId.f9558462-6db8-4bf5-84aa-22ee0920ae95",
    "application": {
      "applicationId": "amzn1.ask.skill.9f280bf7-d506-4d58-95e8-b9e93a66a420"
    },
    "attributes": {},
    "user": {
      "userId": "amzn1.ask.account.AF5IJBMLKNE32GEFQ5VFGVK2P4YQOLVUSA5YPY7RNEMDPKSVCBRCPWC3OBHXEXAHROBTT7FGIYA7HJW2PMEGXWHF6SQHRX3VA372OHPZZJ33K7S4K7D6V3PXYB6I72YFIQBHMJ4QGJW3NS3E2ZFY5YFSBOEFW6V2E75YAZMRQCU7MNYPJUMJSUISSUA2WF2RA3CIIDCSEY35TWI"
    }
  },
  "request": {
    "type": "IntentRequest",
    "requestId": "EdwRequestId.7310073b-981a-41f8-9fa5-03d1b28c5aba",
    "intent": {
      "name": "MyIntent",
      "slots": {
        "MyQuestion": {
          "name": "MyQuestion",
          "value": "erere"
        }
      }
    },
    "locale": "en-US",
    "timestamp": "2018-01-25T14:18:40Z"
  },
  "context": {
    "AudioPlayer": {
      "playerActivity": "IDLE"
    },
    "System": {
      "application": {
        "applicationId": "amzn1.ask.skill.9f280bf7-d506-4d58-95e8-b9e93a66a420"
      },
      "user": {
        "userId": "amzn1.ask.account.AF5IJBMLKNE32GEFQ5VFGVK2P4YQOLVUSA5YPY7RNEMDPKSVCBRCPWC3OBHXEXAHROBTT7FGIYA7HJW2PMEGXWHF6SQHRX3VA372OHPZZJ33K7S4K7D6V3PXYB6I72YFIQBHMJ4QGJW3NS3E2ZFY5YFSBOEFW6V2E75YAZMRQCU7MNYPJUMJSUISSUA2WF2RA3CIIDCSEY35TWI"
      },
      "device": {
        "supportedInterfaces": {}
      }
    }
  },
  "version": "1.0"
}

我收到的服务响应错误只是说“响应无效”

非常感谢任何帮助

1 个答案:

答案 0 :(得分:4)

我想在dynamo db part中帮助你。

要访问dynamodb中的非主键列,您应执行扫描操作。

对于您的表格( yesno ),ID是主键,而消息是附加列。

访问非主键列的小部件[消息]

var dynamodb = new AWS.DynamoDB();
var params = {
    TableName: 'yesno',
    FilterExpression: 'message = :value',
    ExpressionAttributeValues: { 
        ':value': {"S": "Ben Davies"}
    }
};
dynamodb.scan(params, function(err, data) {
    if (err)  // an error occurred
    else console.log(data); // successful response
});

访问主键列的代码段[ID]

var docClient = new AWS.DynamoDB.DocumentClient();
//Get item by key
var params = {
    TableName: 'sis_org_template',
    Key: { "id": "1"}
};
docClient.get(params, function(err, data) {
    if (err)  // an error occurred
    else console.log(data); // successful response
});