1
votes

I am currently developing an Amazon Alexa Skill, which asks the user for a color. The user enters the color by voice and Alexa checks, if the color is in a defined array of values. If that is the case, it returns the color name with an ID. Now this works as intended, but now I would like to put this value in AWS DynamoDB. I read some tutorials on how to connect and to write into DynamoDB using AWS Lambda (Runtime Node.js 8.10) So, in the following code, you can see my AnswerIntentHandler of my Alexa Skill. I included the exports. handle function to write the value of the ID of the color and the name of the color in the table called "alexa_farbe". But when I simulate the Skill with the Alexa Skill developer, the code only gives out the "speechText" and doesn't seem to run the code to write into the DynamoDB. Perhaps it is not possible to run a exports.handle in the AnswerIntentHanlder or is it? I am very new to this topic so I am not really sure where the mistake in this code is. I provide the code of my AnswerIntentHandler, but I can also provide the whole code of the Alexa Skill. I hope somebody can give me a hint what to to.

const AnswerIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name ===   'AnswerIntent';
  },
  handle(handlerInput) {
    const slots = handlerInput.requestEnvelope.request.intent.slots;
const number = slots['FarbAuswahl'].value;
var numberid = slots['FarbAuswahl'].resolutions.resolutionsPerAuthority[0].values[0].value.id; /* ID der Farbe auslesen */

var speechText = 0;

 speechText = `Prima, ich stelle die Farbe ${number} mit der ID ${numberid} ein!`;


/* Ab hier Versuch in die Datenbank DynamoDB zu schreiben */

exports.handle = function(e, etx, callback) {

    var params = {
        Item: {
            ID: '${numberid}',
            Farbname: '${number}'
        },

        TableName: 'alexa_farbe'
    };

    docClient.put(params, function(err, data) {
        if(err) {
            callback(err, null);
        } else {
            callback(null, data);
        }


    });
};    

  return handlerInput.responseBuilder
  .speak(speechText)
  .withSimpleCard('Ausgewählte Farbe:', speechText)
  .getResponse();

},


};
2

2 Answers

0
votes

You should not put the code in the exports.handler. Please take a look at this example (the DynamoDB support code is in the helpers.js file). As a coincidence it's also working with colors.

If the data you're storing is associated to each skill user (eg. a user attribute) a much easier way to do DynamoDB persistence is to use ASK persistent attributes (already supported in the ASK SDK).

0
votes

I tried to edit the code and put the whole DynamoDB posting to another file called dynamodb.js. This is the content:

const AWS         = require('aws-sdk');
const docClient   = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});

exports.handle = function(e, ctx, callback) {

var params = {
    Item: { 
    date: Date.now(),
    message: "This hopefully works"

    },

    TableName: 'alexa_farbe'

};

docClient.put(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
 }
 });
};

I just want to write the date and an example phrase into my DynamoDB.

To trigger this function, I tried to implement some kind of "runScript" into my AnswerIntentHandler of my Alexa Skill. But it doesn't seem to trigger the file. Can you give me some advice whats wrong with this code and how to call another node.js file from the AnswerIntent. Here is my edited AnswerIntent:

const AnswerIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
  && handlerInput.requestEnvelope.request.intent.name ===   'AnswerIntent';
},
handle(handlerInput) {
const slots = handlerInput.requestEnvelope.request.intent.slots;
let number = slots['FarbAuswahl'].value;
let numberid =       slots['FarbAuswahl'].resolutions.resolutionsPerAuthority[0].values[0].value.id; /* ID   der Farbe auslesen */

var speechText = 0;

speechText = `Prima, ich stelle die Farbe ${number} mit der ID ${numberid} ein!`;

 module.runScript('./some-script.js', function (err) {
  if (err) throw err;
  console.log('finished running some-script.js');
 });



 return handlerInput.responseBuilder
 .speak(speechText)
 .withSimpleCard('Ausgewählte Farbe:', speechText)
 .getResponse();

},

};