How to randomise your responses

and make your skill feel less robotic and more engaging

Tom Berwick
Alexa Skills Dev
2 min readJul 18, 2022

--

Photo by Rock’n Roll Monkey on Unsplash

When creating Alexa skills, it’s quite easy to fall into the trap of writing a response for a specific intent and moving onto the next thing. But depending on what your building, these responses can become boring or monotonous quite quickly. This could make or break the immersion factor. One quick tip is to store all responses for a particular intent in an array and pick a random one each time.

In javascript this is quite easy to do! For the sake of simplicity will focus on just a single intent here, but you could easily apply this to all your intents. In this contrived example we’ll assume it’s a game, and a user has lost. I’ve included a simple random function that you could import at a top level. One thing to note is Math.random() isn’t actually the most random algorithm, but it should illustrate the point.

const getRandom = (arr) => {
const idx = Math.floor(Math.random() * arr.length);
return arr[idx];
//Or if you prefer in one line: arr[Math.floor(Math.random() * arr.length)]
};

const GameOverIntentHandler ={
canHandle(handlerInput){
//some logic for this handler
}, handle(handlerInput) {
const responses = ["Bad luck, you lost.",
"better luck next time.",
"It's over I'm afraid and you lost.",
"Wha, wha, wha. You lose.",
"Game Over."];
const response = getRandom(responses) + " Would you like to play again?";
return handlerInput.responseBuilder.speak(response).reprompt("would you like to play again").getResponse(); }}

A lot of you are probably already familiar with javascript and may even know how to retrieve random elements from an array. But I thought this would server as a good reminder to try and make your skills more engaging.

If there’s any other little tips people would like me to explore, or a more in depth article about a specific part of Alexa development then let me know.

--

--

Tom Berwick
Alexa Skills Dev

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