Search code examples
node.jsalexa-skills-kit

Alexa SDK Redirect to Set Session Attributes


Is there a way to temporarily redirect from one intent to another in order to fill slots or session attributes and then return to the original intent to be responded to or fulfilled?

My use-case is for asking for an account PIN as a secondary authentication after account linking. For instance, if someone asks "What are my account details?", I want that intent to change in the session attributes if it exists and if not, temporarily redirect to an intent that will prompt them for the PIN and set it...then come back and answer their account details request. Similar to the example below:

const IntentHandler = {
  canHandle(input) {
    return (
      input.requestEnvelope.request.type === 'IntentRequest' &&
      input.requestEnvelope.request.intent.name === 'MyIntent'
  },
  handle(input) {
    const { accessToken } = input.requestEnvelope.context.System.user
    // ... do stuff with accessToken

    if (!input.attributesManager.getSessionAttributes().pin) {
      // redirect to other intent to set the pin session attribute
    }

    // ...response to intent request
  }
}

I can get this working with a single intent using slot filling prompts but this is a common task for several intents and would like to separate it out so it doesn't have to be configured in the console for all that require it.

FYI: using the ask-sdk for Node.js

Is this possible with the current version of the ask-sdk?


Solution

  • Yes, Alexa makes it easy now to switch between intents and jump right into eliciting a specific slot for the new intent.

    I would use the ElicitSlot Dialog Directive and include the updatedIntent object for the new intent.

    updatedIntent

    Use this to change intents during the dialog or set slot values and confirmation status. See Change the intent or update slot values during the dialog. If you don't need to change the intent, slot values, or confirmation statuses, you can leave this property out of your response.

    When you switch intents with this parameter, Alexa attempts to elicit the specified slot value on the new intent. The next IntentRequest to your skill will be the new intent, not the original.

    Example:

    1. Intent A is missing access token.
    2. Intent A directs Alexa to elicitSlot "pin" from Intent B. (include info in sessionAttributes such as "previousIntent" or "nextIntent" to use as direction to Intent B)
    3. Alexa responds to user with Intent B's elicit prompt for slot "pin".
    4. User provides pin.
    5. Alexa provides user input to Intent B (not A) with slot "pin" filled and Intent B sets the access token in sessionAttribtues.
    6. Then Intent B checks sessionAttribtues for direction of which intent to return to and which slot to elicit from that intent. So you can either confirmIntent or elicitSlot for Intent A.
    7. Intent A will receive the next user input and now detects access token.

    Notice that you will have to address the user with something between switching intents. If you are using confirmIntent then it can be a simple Yes/No confirmation question such as, "Thank you for your pin, would you like me to retrieve your account details now?". The user says "Yes", and that is sent back to Intent A to complete what it started.

    If you don't want this extra back and forth with the user, then as far as I know, you'll have to build the pin slot into each intent you may need to elicit it. That way, you just stay within Intent A. Less efficient code, but more efficient conversation, so it's a bit of a trade off.