将DynamoDB表中的项目转换为Lambda函数intent node.js

时间:2018-05-27 10:33:20

标签: amazon-web-services aws-lambda amazon-dynamodb

我在DynamoDB上创建了一个名为users的表,其中包含2个项userIDuserName。下面是我的简单lambda函数的代码。我创建了一个名为userNameIntent的意图,我希望能够从我的表中读取项userName。所以我希望Alexa回复“你的用户名是”,然后从表中回复userName。很抱歉新手问题,编码很新,我似乎无法找到一个简单的解决方案。我很感激你的帮助。感谢。

const Alexa = require('alexa-sdk'); 
var AWS = require('aws-sdk');
var DOC = require('dynamodb-doc');
var dynamodb = new AWS.DynamoDB.DocumentClient({region: 'eu-west-1'});

var params = {
    TableName: 'users',
    Key: { 
        "userID": "00001"
    }
};

const handlers = {
    'LaunchRequest': function () {
        this.response.speak('Welcome');
        this.emit(':responseReady');
    },

'userNameIntent': function () {
    this.response.speak('Your username is ');
    this.emit(':responseReady');
    }
};

exports.handler = function (event, context, callback) {
    const alexa = Alexa.handler(event, context, callback);
    alexa.APP_ID = APP_ID;
    alexa.registerHandlers(handlers);
    alexa.execute();

    dynamodb.get(params, function(err, data) {
        if (err){
            callback ("error", null);
        } else {
            callback(null, data);
        }
    });
};

1 个答案:

答案 0 :(得分:0)

你很近但只有几件事:

1)意图处理程序必须通过将意图详细信息(即槽值)传递给将获取用户信息的DynamoDB查询来处理响应

2)在DynamoDB查询完成之前,intent处理程序无法返回响应(即您只能设置DynamoDB回调的响应)

查看my answer一个非常相似的问题。