Step 5: Storing and Retrieving Multiple Episodes

by Kaan Kilic on Feb 13, 2019

In this step, we will build a system to store and retrieve the episodes of our podcast on Amazon Alexa and Google Assistant.

Storing the Episodes

Until now we've simply hard coded the episodes URLs. Obviously, that's wrong, but it saved us some time while we were figuring out both audio interfaces for Alexa and Google Assistant.

Instead, we will now store everything inside a JSON file, called episodes.json inside our src folder, with the following structure (thanks again to Bret Kinsella for providing us with the Voicebot Podcast episodes):

This file is an array containing multiple objects, where the very last object, i.e. the index array.length - 1 (in this case index 4 for five episodes) will contain the very first episode and the very first object, i.e index 0, will store the most recent episode.

Each object will contain the title and URL of the episode.

Each episode's index can also be used as the unique value needed for the token we send out with every Alexa play directive. So from now on we will replace every token with the index as well as use the episode's title with every Google Action play directive.

Retrieving the Episodes

To retrieve and work with the JSON file, we will create an interface called player.js inside our app folder.

The interface will allow us to access the latest, first, next and previous episode, which we all need for the finished podcast player. The latest one is needed for people who want to directly start with that or have already listened to every other episode. The first one is for people who want to start from the beginning. The next and previous one, are needed if the user manually changes the episode, as well as when they finished one and it's time to enqueue the next.

For the first two, we simply import the episodes.json file and return the content at the correct index:

The other two will take in an index as a parameter and return the episode at the next/previous index.

Our player.js file should look like this now:

Now we can use our interface to replace the hard-coded episode URLs.

Updating our app.js

First of all, import the player.js file:

Now, the first thing to replace is the logic inside the LAUNCH intent. Instead of simply saving the episode URL inside the database, we will now save the index. For that, we have to add another simple function to our player.js file.

The getEpisodeIndex function will return us the correct index at which the episode is stored in our episodes.json array.

If it's a new user, let's just play the first episode for now. We will improve the user experience later on.

Otherwise, we previously resumed playing the episode the user last listened to. Since we're now saving the index instead of the episode URL, we have to make some changes here as well as in the player.js file as well.

We have to add a function to the player.js interface, that takes the index as a parameter and returns us the episode object.

Now we use the function to play the episode the user last listened to:

There is one more thing to fix before we can test it out. Since the episode variable is now an object instead of a string containing the url, we have to fix the reference from .play(episode, token) to .play(episode.url, token):

Before we test out our new implementation, we have to delete the content of the ../db/db.json file, since we made changes to the database structure. We simply delete everything and save the blank file.

Alright, now we can test it out. The audio file should start playing without any errors.


There's still more to update besides the LAUNCH intent. It's time to fix the enqueue logic as well.

Enqueue on Alexa

Let's start with the Alexa part. In the AlexaSkill.PlaybackNearlyFinished intent we simply get the current index from the database and use the getNextEpisode() method from our player.js file. We also check if episode has a value since the player will reach the last episode at some point:

The AlexaSkill.PlaybackFinished intent will be used to decrease the index as long as it's bigger than 0:

Enqueue on Google Assistant

For Google we do the same with the small difference that it will be all done inside a single intent:


We also have to fix the AMAZON.ResumeIntent:

Next Step

In the next step we will use the new system to allow our user to manually switch between episodes.

Step 6: Manually Switch between Episodes

Kaan Kilic

Technical Content Marketing Associate at Jovo

Comments and Questions

Any specific questions? Just drop them below or join the Jovo Community Forum.

Join Our Newsletter

Be the first to get our free tutorials, courses, and other resources for voice app developers.