Mark
Mark

Reputation: 4537

How to prompt for a single slot value in an intent containing multiple slots in Alexa using Python

I'm using the ask_sdk_core in an AWS Lambda written in Python to handle the logic for an Alexa app. The intent in question is a way for a researcher to capture a weight associated with a sample number. The intent is structured like Record [weight] grams for sample [sample_number]. The sample number must be a 6 digit integer and the weight is a decimal number. Originally I developed a Lex skill and there you can prompt for specific slot values, so if the intent arrived with an ill-formed sample number (e.g. a five digit number) I could prompt for just the sample number but leave the weight captured as is.

The lambda function uses classes to handle intents, so my structure is below. add_weight extracts values from slots, passes them through a validator which returns values for speech and reprompt which informs the user if they provided ill-formed sample numbers or ill-formed weights. Right now though, the user has to redo the whole prompt.

Is there a way to preserve the well-formed values and prompt the user for just the missing value? Right now it seems like I would have to create an intent for each slot to handle that kind of user input but that feels bad.

class AddWeightHandler(AbstractRequestHandler):
    """Handler for AddWeight Intent."""

    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
        return ask_utils.is_intent_name("AddWeight")(handler_input)

    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        speech, reprompt = add_weight(handler_input)

        return (
            handler_input.response_builder
                         .speak(speech)
                         .ask(reprompt)
                         .response
        )

Upvotes: 0

Views: 465

Answers (1)

Timothy Aaron
Timothy Aaron

Reputation: 3078

You need a state machine. If I'm understanding your setup correctly…

  1. an Intent like "NumberOnly" that accepts just a AMAZON.NUMBER slot
  2. a handler for each of the different numbers you want to accept (e.g., AddSampleNumberHandler)
  3. that handler's can_handle needs to say something like is_intent_name("NumberOnly") and session_attributes['state'] == 'sample_number'
  4. the intent that is requesting a "NumberOnly" response needs to set the state (AddWeightHandler would need to set session_attributes['state'] = 'sample_number')

Upvotes: 1

Related Questions