question

newuser-5796327d-4ca3-4ef6-a477-5d0a971f0df6 avatar image

Make a dynamic key for dynamodb

Hey! I`m looking for a way to make my key "id" dynamic so that if I say "ten" I get the answer for my dynamodb message for the id ten! Anyone has a fix I tried a lot but everything resulted in errors!

const AWSregion = 'eu-west-1';  // us-east-1

const Alexa = require('alexa-sdk');
const AWS = require('aws-sdk');

AWS.config.update({
    region: AWSregion
});
exports.handler = function(event, context, callback) {
    var alexa = Alexa.handler(event, context);
 alexa.registerHandlers(handlers);
    alexa.execute();
};

const handlers = {
    'LaunchRequest': function () {
        this.response.speak('Welcome! Ask yes or no question').listen('versuch es nochmal');
        this.emit(':responseReady');
    },

    'MyIntent': function () {
        
        var MyQuestion = this.event.request.intent.slots.MyQuestion.value;
       
       var params = {
    TableName: 'yesno',
    Key:{ "id": "one" }
};
        console.log('MyQuestion : ' + MyQuestion);

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

            say = myResult;

            say = 'Deine Frage war:, ' + MyQuestion + '. Die Antwort darauf ist: ' + myResult;
            this.response.speak(say).listen('try again');
            this.emit(':responseReady');

        });
function readDynamoItem(params, callback) {

    var AWS = require('aws-sdk');
    AWS.config.update({region: AWSregion});

    var docClient = new AWS.DynamoDB.DocumentClient();

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

    docClient.get(params, (err, data) => {
        if (err) {
            console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
        } else {
            console.log("GetItem succeeded:", JSON.stringify(data, null, 2));

            callback(data.Item.message);  // this particular row has an attribute called message

        }
    });

}


alexa skills kitnodejsdynamodb
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

KAIYIN@AMAZON.COM avatar image
KAIYIN@AMAZON.COM answered

Are you able to get the key, for example: "10" from the intent?

10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.