How to store data in your Alexa skills

Tom Berwick
Alexa Skills Dev
Published in
3 min readNov 22, 2019
Photo by fabio on Unsplash

In my last post I talked about the various ways you can store data in your Alexa skills.

In this post I’m going to show you some code and how to get started.

So as I mentioned previously, there are two main ways to store data for Alexa skills when using the NodeJS SDK. Those are session attributes and persistent attributes. I won’t go over the difference between them here as you can find that in the original article, so let’s get started with an example of session attributes.

Session Attributes

If you’ve used Alexa Hosted skills before then you should be familiar with the format of an intent handler below.

hello world gist

In the code above I’ve created a simple handler to handle an “HelloWorld” intent ( you can imagine someone just needs to say “Hello” to invove this).

On line 7, we use object destructuring assignment to grab the “attributesManager” from the handlerInput — this is a variable that the Alexa Skills Kit provides us with.

On line 8 when then call the getSessionAttributes() method on the attributesManager variable and assign the return object from this function to a local variable called sessionAttributes.

On line 10 we then set the value of the helloCount variable to be the current value of helloCount+ 1if it exists as a property on sessionAttributes otherwise we set it to the value of 1. The code here is known as a ternary operator which is like simple inline if/else statement.

Finally, on line 12 we call attributesManager.setSessionAttributes(sessionAttributes) setting the value of the sessionAttributes property on attributesManager to our new values.

Persistent Attributes

Persistent attributes are accessed in much the same way.

The only difference is to note the use of async/await . This is because getPersistentAttributes() and savePersistentAttributes() both return a promise. Notice that we call setPersistentAttributes() and savePersistentAttributes() in this instance. setPersistentAttributes() will cache the persistent attributes but it won’t store them. This is why you must call savePersistentAttributes().

Final Points

One final point to be aware of with persistentAttributes over sessionAttributes is that you will need to set up an adapter in order to save persistentAttributes. If you’re using an Alexa Hosted Skill this will be in the form of an S3Adapter, but if you’re using a custom Lambda function you will most likely be using a dynamodb table.

When you export your function you will need to create your adapter instance. Something like this:

exports.handler = Alexa.SkillBuilders.custom().withPersistenceAdapter(new DynamoDbPersistenceAdapter({ tableName: "myTable", createTable: true })).withApiClient(new Alexa.DefaultApiClient()).addRequestHandlers(LaunchRequestHandler,HelloWorldIntentHandler,HelpIntentHandler,CancelAndStopIntentHandler,FallbackIntentHandler,SessionEndedRequestHandler) .addErrorHandlers(ErrorHandler).lambda();

making sure you import the correct library in your package.json file

e.g (note version may vary)

"dependencies": {"ask-sdk-core": "^2.0.7","ask-sdk-dynamodb-persistence-adapter": "^2.0.0","ask-sdk-model": "^1.4.1","ask-sdk-s3-persistence-adapter": "^2.0.0",}

As I’m sure you can imagine it might be a bit tedious retrieving and saving persistentAttributes in all of your intent handlers when you want to query information across sessions, so in a future post we will look at requestInterceptors and responseInterceptors and how they can make your life easier.

Hope you’ve enjoyed this post. If so I’ve also started a group on facebook for Alexa skill developers, why not check it out.

--

--

Tom Berwick
Alexa Skills Dev

Mobile App, game and full stack developer. Constantly trying to learn new things and dabble in growth hacking