To make my learning more interesting and to avoid writing another “Hello Word” skill I decided to create a skill for something that has caught my eye last week. Typical Twitter surfing evening has finished with searching deeper and deeper into how the Great Britain government works. This marathon began with seeing #PMQ’s hashtag. It stands for Prime Minister’s Questions., a weekly session of questions to the Prime Minister from the parliament members. My first skill using the new ASK-SDK will be a skill that allows users to get info about the last Prime Minister’s Questions session.

Because this story is about the skill code and not about custom skills developing generally I’ll skip the part about creating a custom skill in Amazon Developer Console, building interaction model, and adding the ARN endpoint to the skill. If you know nothing about it I recommend you start with the video below.

The first version of skill will have only minimum necessary intents:

- LastSessionDetails

- AMAZON.CancelIntent

- AMAZON.HelpIntent

- AMAZON.StopIntent

- AMAZON.YesIntent

- AMAZON.NoIntent

Also LaunchRequest Intent handler must be implemented in code because every custom skill has this intent by default.

At first, the new SDK looks a little bit more complex and intricate than the previous version. But from the other side it looks more unified and I hope that they will add Alexa Smart Home support in future releases.

First thing I noticed was that in this version, handlers must be defined not as one object with a lot of methods but as separate objects with two methods handle and canHandle . Unlike previous SDK, now if you need to use a few states in the skill the state-ckeck needs to be implemented in the canHandle method of handlers the execution of which depends on the state. This also means that different handlers can be set for the same intent name. The result of the value check in the canHandle method of each handler determines which handler will be executed.

For example, two handlers that return from their canHandle methods handlerInput.requestEnvelope.request.type === ‘LaunchRequest’ && isNewUser and handlerInput.requestEnvelope.request.type === ‘LaunchRequest’ && !IsNewUser :

And when the user launches the skill for the first time (let’s imagine that somewhere in the code there is a function that checks it and sets a corresponding value of isNewUser ) the firstTimeLaunchIntentHandler will be executed.

Let’s create the LaunchRequest handler for PMQ’s skill. Previous SDK has two ways of creating the response: using “:ask”, “:tell”, “:tellWithCard”, etc. directives and using response builder. The new SDK uses the way closer to the second.

In the code above, the items was taken from session attributes but how can the session attributes be not empty in the intent that was executed first time? The answer is: “ Interceptor was used”. Interceptor it is the middle-ware that can be executed before handler (Request interceptor) or after the handler (Response Interceptor). I created the request interceptor that adds the array of items to the session attributes if they are not present there.

I used Promise in the interceptor above because getItems method is asynchronous. To make the development more convenient I implemented the response Interceptor that logs stringified response object to the console so that I could see its body in CloudWatch.

To make the skill read the questions I created the LastSessionDetails handler that makes the request that scraps the needed data from the news web site. Also In LaunchRequest handler, skill just responds with the last PMQ’s session date and asks the user whether they want some details. The user can answer “Yes” or “No”. So the built-in Alexa Intents that will be triggered on this words need to be done too.

The doings of “Yes” and “No” intents depend on what question was asked before (previous intent), lastAsked variable reflects last executed intent. To make this skill work properly the other default intent handlers had to be implemented. All of the handlers must be registered in the skill builder:

For now, the skill is live and is just responding with the list of questions from last PMQ’s session. You can check it out by link below.

I am planning to add some features to it so please don't be shy and offer some good ideas about this skill. All the code you can see in the repository below: