响应无效,Alexa使用DynamoDB

时间:2018-02-12 15:18:09

标签: node.js aws-lambda alexa alexa-skills-kit

您好我正在尝试创建一种技能,可以扫描我的DynamoDB表,然后回来显示电影的时间或电影在特定时间显示的内容。

这是我目前的Lambda函数



const AWSregion = 'us-east-1'; // us-east-1
const Alexa = require('alexa-sdk');
const AWS = require('aws-sdk');
//params for searching table

var docClient = new AWS.DynamoDB.DocumentClient();
console.log("Querying for movies from 1985.");
var say = '';
var params = {
// Return a movie, by primary key
TableName: "Cinema",
KeyConditionExpression: "filmname = :f and time = :t",
ExpressionAttributeValues: {
    ":a": "Titanic",
    ":t": "13:00"
}
};

function queryDynamoDate_single(params) {
const message = this.attributes['message'];
readDynamoItem(params, myResult => {
    say = myResult;
    say = 'you asked,. The answer is: ' + myResult;
    this.response.speak(say)
        .listen('try again');
    this.emit(':responseReady');
});
}


// 2. Skill Code =======================================================================================================


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 my skill.  ask me a 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 + '. The answer is: ' + 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 {} ========================================================================================
// 3. 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.scan(params, function(err, data) {
    if (err)
        console.log(err, err.stack); // an error occurred
    else {
        console.log(data); // successful response
        callback(JSON.stringify(data));
    }
});
  
var docClient = new AWS.DynamoDB.DocumentClient();
//Get item by key
docClient.query(params, function(err, data) {
    if (err) {
        console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
    } else {
        console.log("Query succeeded.");
        data.Items.forEach(function(item) {
            console.log(" -", item.year + ": " + item.title);
        });
        callback(data.Item.message); // this particular row has an attribute called message

    }
});

console.log('Loading function');

exports.lambda_handler = function(event, context, callback) {
    console.log(JSON.stringify(event, null, 2));
    event.Records.forEach(function(record) {
        console.log(record.eventID);
        console.log(record.eventName);
        console.log('DynamoDB Record: %j', record.dynamodb);
    });
    callback(null, "message");
};

}

docClient.query(params, function(err, data) {
if (err) {
    console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
} else {
    console.log("Query succeeded.");
    data.Items.forEach(function(item) {
        console.log(" -", item.year + ": " + item.title);
    });
}
});




当我在我的Alexa Skill Kit上运行时,我得到以下回复:

The response is invalid.

谁能告诉我哪里出错了?我对此很新。

0 个答案:

没有答案