Subscribe rss
Blog categories
All | Backendless features
Developing a Custom Skill for an Alexa Game
October 23, 2018 by Stanislaw Grin

In one of my previous articles, I showed how to develop a custom Alexa skill using an example of a Trip Planner app  (How To Build A Dialogue Custom Alexa Skill Using JavaScript (Without Lambda). In this article I will show you a more complex example of the interaction between Alexa and the user. Today will build a  “Guess My Number” game where Alexa (or technically the skill) thinks of a number and the user tries to guess it while the skills suggests whether it is lower or higher. Here’s a sample dialogue a user may have with Alexa once you implement the skill:
guess-my-number-example1 guess-my-number-example2

In this article, I will omit some details about the user interface of the Amazon Developer Account and assume that you are already familiar with this from the previous article.


As before, you will need the following to get started:

  • An Amazon Developer Account.
  • A Backendless Developer Account and an App created in Backendless.
  • the SDK for Amazon Alexa installed from the Backendless Marketplace.

Skill Setup

The first step is to set up the skill in the Amazon Developer Account. To do this, go to the Amazon Developer console and create a new skill from the empty template (see the previous article for details).  I called the new Alexa Skill “Guess My Number”, but you are welcome to name it any way you want to.
Next step is to create  a new intent named NumberGuessIntent and an Intent Slot called number with the type Amazon.NUMBER. Make sure to add the Sample Utterances as shown below:

20181009 image 1

Finally, it is important to add all the Built-in Intents listed in the screenshot above as you will need them later, but for now,  save the model by clicking the Save Model button.

Developing and Configuring an Alexa Skill as a service in Backendless

If you have already installed the SDK for Amazon Alexa, you can skip steps 2 and 3.

  1. Login to the Backendless Dashboard and select an app where the custom skill implementation will reside.
  2. Click the Marketplace icon in the vertical toolbar on the left.
  3. Click the All Services section and install the SDK for Amazon AlexaThis will install all the APIs necessary to work with requests from Alexa.
  4. Switch to the Business Logic section of the Backendless Console and click the CODING tab.
  5. Select JS from the from the language drop down list and click the Download project icon as shown in the screenshot below:
    20181009 image 2
  6. Extract the files from the downloaded ZIP file and open the downloaded project an IDE of your choice (we recommend IntelliJ IDEA).
  7. Run the following command from the root directory of the project:
    npm i 
  8. Create four files in the /services  directory named as follows:
  9. Open alexa-base.js  for editing and copy-paste the following JavaScript code in it:

For the explanation of each method see the previous article.

Open the file response-builder.js for editing, and enter the following code:

Now open the file skill-helper.js for editing, and enter the following code:

And finally, open the file guess-my-number.js for editing. In this file you need to declare the  guess-my-number  service and register it with Backendless:

In the code above you have created a Backendless API service method named guessMyNumber. The method will be accepting Alexa requests and respond with answers.

Amazon requires that Alexa requests must be verified – meaning your code must ensure that requests are indeed sent by Alexa. To achieve this you will use the   this.verifyRequest(req)  method, which is inherited from the AlexaBase class.

To begin implementing the logic for the guessMyNumber method, define requestType , intentName  and skillHelper as shown below:

Next, you need to declare Intent handlers for each of the Intents.  Schematically an intent handler should look like this:

The first handler that you need to declare is LaunchRequest handler. In this handler you will set the initial attributes, greet the user, report how many times they have already played and offer to start the new game. If the user replies “Yes”, the built-in AMAZON.YesIntent  will be invoked, and if they reply “No” – the AMAZON.NoIntent  will run. Handling of these intents will be done later.

The input argument has the following properties and methods (just in case if you decide to explore further):

  • request –  Alexa request object
  • getSessionAttributes  – returns the attributes set in the session in the format {[key: string]: any}. By default, an empty object is returned.
  • setSessionAttributes – sets in the session attributes in the format {[key: string]: any}. These attributes will exist throughout the session until it ends.
  • getPersistentAttributes  – returns the attributes set in the persistent storage in the format {[key: string]: any}. As a repository, a pre-prepared table is used in the Data service.
  • setPersistentAttributes  –  sets the attributes in the {[key: string]: any} format to permanent storage. This data is stored independently of the session and will be available at any time. Also sets the attributes in the session.
  • responseBuilder – a set of helper-functions that help to design the response of Alexa. responseBuilder can consist of several elements, so these helpers will remove the need to manually initialize each one.

Now your the first Intent Handler is ready. In case the user decides to end the session, we can use one of the built-in intents such as  AMAZON.CancelIntent  or AMAZON.StopIntent: and implement the ExitHandler and SessionEndedRequest:

The next Intent that you need to declare for a good user experience is AMAZON.HelpIntent. It is needed in case when the user does not understand how to work with your skill and asks for help.  This is a great place to add  instructions on your skill:

Now you need to process the user’s answer to the question whether they want to play or not. Possible answers “yes” or “no” will be processed by the built-in intents AMAZON.YesIntent and AMAZON.NoIntent. Below is the handler for the “yes” answer:

Note that in canHandle, you should add a check to see if the game has already started. This is necessary for those cases when the user for some reason says “Yes” or “No” during the game. You don’t want the game to start over again because of this and the handler is invoked only if the game has not yet started.

Notice that the  gameState variable is used in the session attributes. It identifies that the game is already in process. Also the guessNumber variable contains the actual number which the users is trying to guess. For a more advanced version of this skill, you can have Alexa ask the user at beginning of the game what maximum number to use – for example, the user may want to make the game harder so that Alexa guesses a number between 0 and 1 million.

Another idea to explore is making an Intent for a hint. The user asks for a hint and Alex reports a slightly more minimal search circle for the desired number. 

Let’s move on with the implementation. The next step is for you to handle the case where the user responds “no”. In that case, you need to handle the AMAZON.NoIntent intent. Here you also need to make sure that the game is not in the process, and in the permanent store we record information – for example, that we had a session, but the game was not continued for some reason (and as a result the gamesPlayed counter was not updated).

Then, if you receive the “no” response from the user, the  shouldEndSession should be invoked, signaling that the session should end:

The next Intent is  NumberGuessIntent, it is the one responsible for the logic and progress of the game itself.  In  canHandle you will check that the game is in process, and also that the Intent is GUESS_MY_NUMBER_INTENT.

In the body of the handler (the handle function), you get from the number the user spoke (available in the input.req.request.intent.slots.number.value variable), and compare it with the number to be guessed. Depending on the result, you will determine the response to be sent back. If the number was guessed correctly, the attributes are stored in the permanent storage and prompt the user to play again.

The basic logic of the skill is almost finished, it only remains to handle errors and any unhandled intents. To do this, add the following   ErrorHandler:

And for all other cases when the request could not be processed by all previous handlers add UnhandledIntent :

Now you need to register all handlers and build skills with skillHelper:

Please note that the order of these handlers is very important, because they will be called exactly in the order in which you register them. The addErrorHandlers method, like addRequestHandlers , takes an unlimited number of handlers. Each handler will be called in the order in which you list them.

The final method in the sequence is called start. It is called at the very end and takes the required req parameter.

In order to work with the persistent storage, you must specify the name of the table where the data will be stored. Otherwise, you can not use the get/setPersistentAttributes methods. Make sure to create the GuessNumberGame  table in the Backendless database. The table schema must be configured as shown below:

20181009 image 3


The final version of the service should look like this:

The new service is now ready to use! You will need to deploy the code to Backendless with the npm run deploy command,  and install the Endpoint URL in Amazon Developer Console. For more details, see the steps 16-18 from the previous article.

When you are back in the Amazon Developer Console, make sure to click the Build Model button. Once the model is built, you can start testing your skill!  Simply ask Alexa: “Play guess my number”, and the game should start.

In the future, we plan to expand and add new features to our Backendless SDK for Alexa. If there are features that you would like us to add, please do not hesitate to tell us about it, we will be happy to improve the capabilities of the SDK. 

Happy Coding!

If you have any questions about this procedure, please post your questions in in our support forum ( or on Slack (

Share this post
Tweet about this on TwitterShare on FacebookGoogle+