1
votes

I want to create a lottery skill that takes 6 numbers from the user. I'm currently learning by going through the samples and developer guides, and I can go through the guides and get a working skill that will take one input and then end the session. But I believe I need to create a dialog somehow, which is where I get stuck.

Design-wise, I'd like the dialog to go like this: Alexa: Please provide the first number User: 1 Alexa: and now the second... User: 2 etc etc

But I think it would be OK if it went like this: Alexa: Please call out 6 numbers User: 1, 2, 3, 4, 5, 6.

Is this even possible? Will I have to create a custom slot type called "Numbers" and then put in the numbers, eg 1-50 or whatever the limit is?

At best, I can currently get it to ask for one number, so its really the dialog interaction that I'm stuck on. Has anyone ever even done anything like this?

Thanks.

1

1 Answers

2
votes

Yes to both questions. You could string together a response with 6 different custom slots. "User: My numbers are {num1}, {num2}, {num3}, {num4}, {num5}, {num6} " and make them all required using the skills beta developer. However, it will be a rather bad user experience if the user does not phrase their answer appropriately and Alexa has to ask follow up questions to obtain each number. The last problem you'll run into is that while a custom slot could be defined to contain the numbers 1-50 alexa will generally recognize similar values to those provided in a custom slot, such as numbers from 50-99. It would then be up to you to check that the values you receive are between 1 and 50. If not you'd want to ask the user to provide a different number in the appropriate range.

Conclusion: You'll want to have individual interactions where a user provides a single number at a time.

Alexa:"you will be prompted for 6 numbers between 1 and 50 please state them one at a time. Choose your first number."
User:"50"
Alexa:"Your First number is 50, Next number."...

You can implement this using a single intent. let's name that intent GetNumberIntent. GetNumberIntent will have sample uterances along the line of

{number}
pick {number}
choose {number}

where {number} is a custom slot type or simply AMAZON.NUMBER. It will then be up to you to check that the number is between 1 and 50.

I program in Node.js using the SDK. Your implementation may vary depending upon your language choice.

What I would do is define 6 different state handlers. Each handler should have the GetNumberIntent. When a GetNumberIntent is returned if the slot value is apropriate store the value to the session data and or dynamodb and move forward to the next state. If the slot value is invalid stay for example at state "NumberInputFiveStateHandlers" until a good value is received then change state to the next "NumberInputSixStateHandlers"

var NumberInputFiveStateHandlers = Alexa.CreateStateHandler(states.NUMFIVEMODE, {
    'NewSession': function () {
        this.emit('NewSession'); // Uses the handler in newSessionHandlers
    },
    //Primary Intents
    'GetNumberIntent': function () {
      let message = ` `;
      let reprompt = ` `;
      let slotValue = this.event.request.intent.slots.number.value;
      if(parseInt(slotValue) >= 1 && parseInt(slotValue) <= 50){
          this.handler.state = states.NUMSIXMODE;
          this.attributes['NUMBERFIVE'] = this.event.request.intent.slots.number.value;
          message = ` Your fifth number is `+slotValue+`. please select your sixth value. `;
          reprompt = ` please select your sixth value. `;
      }else{
          message = ` The number `+slotValue)+` is not in the desired range between 1 and 50. please select a valid fifth number. `;
          reprompt = ` please select your fifth value. `;
      }
      this.emit(':ask',message,reprompt);
    },
    //Help Intents
    "InformationIntent": function() {
      console.log("INFORMATION");
      var message = ` You've been asked to choose a lottery number between 1 and 50. Please say your selection.`;
      this.emit(':ask', message, message);
    },
    "AMAZON.StopIntent": function() {
      console.log("STOPINTENT");
      this.emit(':tell', "Goodbye!");
    },
    "AMAZON.CancelIntent": function() {
      console.log("CANCELINTENT");
      this.emit(':tell', "Goodbye!");
    },
    'AMAZON.HelpIntent': function() {
        var message = `You're playing lottery. you'll be picking six numbers to play the game.  For help with your current situation say Information.  otherwise you may exit the game by saying quit.`;
        this.emit(':ask', message, message);
    },
    //Unhandled
    'Unhandled': function() {
        console.log("UNHANDLED");
        var reprompt = ' That was not an appropriate response. Please say a number between 1 and 50.';
        this.emit(':ask', reprompt, reprompt);
    }
});

This is an example of the fifth request. You'll have 6 identical states like this one that string back to back. Eventually you'll end up with 6 session values.

this.attributes['NUMBERONE']
this.attributes['NUMBERTWO']
this.attributes['NUMBERTHREE']
this.attributes['NUMBERFOUR']
this.attributes['NUMBERFIVE']
this.attributes['NUMBERSIX']

You can then use these values for your game.


If you have not used the alexa-sdk before you must remember to register your state handlers and add your modes to the states variable.

alexa.registerHandlers(newSessionHandlers, NumberInputOneStateHandlers, ... NumberInputSixStateHandlers); 

var states = {
    NUMONEMODE:    '_NUMONEMODE',
    ...
    ...
    NUMSIXMODE:    '_NUMSIXMODE',
}

This answer is not intended to cover the basics of coding using Alexas-SDK. There are other resourced for more specific questions on that topic.


Alternatively, because your intent is identical [GetNumberIntent], you may be able to get by with a single StateHandler that pushes new valid numbers onto an array until the array is the desired length. That would simply require more logic inside the Intent Handler and a conditional to break out of the state once the array is of length 6. Try the code above first because it's easier to see the different states.