node.jsaws-lambdaamazon-dynamodbalexa-skills-kit

Getting timestamp of individual alexa responses in dynamodb


I am creating an Alexa factskill that asks the user for some information on their health, and users respond with a score from 1-10 depending on the level of pain in different areas. I then input the data into a DynamoDB table, which stores the information(score out of 10) for the four health questions (swelling, feeling, sleeping, breathing) and the user ID. However, the entries are not giving a timestamp for when they were created. I was wondering if there was a way to make a timestamp for preferably every health question response, but also a timestamp for the entire entry would help. Would I have to use any external SDKs, as I was looking up DynamoDB documentations and didn't find out any way to add a timestamp.

Below is my index.js code for my Lambda function that is used for my Alexa skill.

'use strict';
const Alexa = require('alexa-sdk');

const SKILL_NAME = 'Home Assist';
const HELP_MESSAGE = 'You can say I want to input my data';
const HELP_REPROMPT = 'What can I help you with?';
const STOP_MESSAGE = 'Goodbye!';


const handlers = {
    'LaunchRequest': function () {
        this.emit('HomeAssistQuestions');
    },
    'HomeAssistQuestions': function () {
        this.attributes.healthscores = {
            'patientID' : 0,
            'scores': {
                'feeling': {
                    'score': 0
                },
                'sleeping': {
                    'score': 0
                },
                'breathing': {
                    'score': 0
                },
                'swollen': {
                    'score': 0
                }
            }
        };

        if(this.event.request.dialogState !== 'COMPLETED'){
            this.emit(':delegate');
        }
        else{
            const feelingScore = this.event.request.intent.slots.feelingRating.value;
            const sleepingScore = this.event.request.intent.slots.sleepingRating.value;
            const breathingScore = this.event.request.intent.slots.breathingRating.value;
            const swollenScore = this.event.request.intent.slots.SwollenRating.value;
            const id = this.event.request.intent.slots.id.value;


            this.attributes.healthscores.patientID = id;
            this.attributes.healthscores.scores['feeling'].score = feelingScore;
            this.attributes.healthscores.scores['sleeping'].score = sleepingScore;
            this.attributes.healthscores.scores['breathing'].score = breathingScore;
            this.attributes.healthscores.scores['swollen'].score = swollenScore;

            this.response.speak("Health Scores Recorded");
            this.emit(':responseReady');
        }


    },
    'AMAZON.HelpIntent': function () {
        const speechOutput = HELP_MESSAGE;
        const reprompt = HELP_REPROMPT;

        this.response.speak(speechOutput).listen(reprompt);
        this.emit(':responseReady');
    },
    'AMAZON.CancelIntent': function () {
        this.response.speak(STOP_MESSAGE);
        this.emit(':responseReady');
    },
    'AMAZON.StopIntent': function () {
        this.response.speak(STOP_MESSAGE);
        this.emit(':responseReady');
    },
    'SessionEndedRequest': function(){
        this.emit('saveState', true);
    }
};

exports.handler = function (event, context, callback) {
    const alexa = Alexa.handler(event, context, callback);
    alexa.dynamoDBTableName = 'HealthScores';
    alexa.APP_ID = "amzn1.ask.skill.d5b8d597-eb50-41c6-a22d-b0f18c23b544";
    alexa.registerHandlers(handlers);
    alexa.execute();
};

Solution

  • You can try something like this:

    ...  
    this.attributes.healthscores.scores['swollen'].score = swollenScore;
    this.attributes.healthscores.timestamp = Date.now();
    
    this.response.speak("Health Scores Recorded");  
    ...