How to disable certain intents in custom Alexa skill?

424 Views Asked by At
...
CountryIntent{ ... };
CityIntent{ ... };
YesIntent{ ... };
FallBackIntent{ ... };
...

I'm building custom Alexa skill. User calls CityIntent from CountryIntent. But, if user accidentally says YES, he'll call YesIntent from CountryIntent; that will be incorrect logic. How to disable YesIntent so that user calls FallBackIntent when he accidentally says YES?

3

There are 3 best solutions below

0
On BEST ANSWER

You can't disable intents. By building intents you teach your skill to understand specific sentences/words spoken by human - and once taught, it's hard to forget.

What you can actually do is build a state machine and keep the current state of your conversation in SessionAttribute. Then in each intent you have to check in which state the conversation is and act according to your logic - so in your case when you expect city and someone says "Yes", your skill should ask once again about the city and ignore the "yes" answer.

0
On

You can enable ANY handler you've coded to handle the YesIntent by adding it as a true condition in the canHandle for the handler and you can match it with session values to make it possible for different handlers to be used depending on the context.

Here's an example.

let attributes = await handlerInput.attributesManager.getSessionAttributes();

    return ((Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
      && Alexa.getIntentName(handlerInput.requestEnvelope) === 'CityIntent')
      || (Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && 
         Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.YesIntent' &&
         attributes.last === "City"));

So if they explicitly hit the CityIntent, or they hit it before (and you stored that in attributes.last) but answered "Yes," it gets triggered.

Using the FallbackIntent when you know they might answer "yes" is an anti-pattern. It's intended to be used when they say something you don't expect. You expect "yes," so proactively handle it.

0
On

Agree with @slawciu. The best way to handle yes/no responses is to keep track of the last intent in the session attributes

In your case, you can enable validation on the city intent to accept only valid city names thereby eliminating need to handle an incorrect response.