简体   繁体   中英

Alexa Developer Skill - Parsing Handler defined Const between Handers using Lambda

I'm a newbie to both Alexa Skill development and Lambda I have created my first Alexa Skill, which is very basic. I have defined a number of constants at the top of the script. Which all work fine.

I was hopeing to be able to set some of these constants within some of the Handlers, and be able to check and use them across all Handlers.

When I try to do this, I am only able to see the data initially set. ie When I initially defined the contants. And not when they have been set from within a Handler.

Below is a snippit of my code

    const TRICK = 'NOTHING';
    const TRICK_MESSAGE = 'NOTHING';
    const TRICK_SIMPLECARD = 'NOTHING';
    const PICKACARD_MESSAGE = 'Don\'t tell me <break time="0.5s"/> you can\'t find it?<break time="1s"/> Was the pack shuffled?';
    const PICKACARD_SIMPLECARD = "Don't tell me you can't find it? Was the pack shuffled?";
    const PICKACARD_REPROMT = 'This a reprompt for Pick a card <break time="1s"/> Who chose the card?';

    const THINKOFACARD_MESSAGE = 'Don\'t tell me <break time="0.5s"/> you can\'t read their mind?<break time="1s"/> Who thought of a card?';
    const THINKOFACARD_SIMPLECARD = "Don't tell me, you can't read their mind? Who thought of a card?";
    const THINKOFACARD_REPROMPT = 'This a reprompt for Think of a card - <break time="1s"/> Who thought of a card?';

    //=========================================================================================================================================

   const PickACardHandler = {
      canHandle(handlerInput) {
      const request = handlerInput.requestEnvelope.request;
      const TRICK = 'PICK A CARD';
      return request.type === 'LaunchRequest'
        || (request.type === 'IntentRequest'
          && request.intent.name === 'PickACardIntent');
     },
     handle(handlerInput) {
        const speechOutput = PICKACARD_MESSAGE;

        return handlerInput.responseBuilder
        .speak(speechOutput)
        .reprompt(PICKACARD_REPROMT)
        .withSimpleCard(SKILL_NAME, PICKACARD_SIMPLECARD)
        .getResponse();
      },
    };

    //=========================================================================================================================================

   const LinPickHandler = {
      canHandle(handlerInput) {
        const request = handlerInput.requestEnvelope.request;
        if (TRICK === 'THINK OF A CARD') {
            const TRICK_MESSAGE = LIN_THOUGHT_MESSAGE;
            const TRICK_SIMPLECARD = LIN_THOUGHT_SIMPLECARD
        } else {
            const TRICK_MESSAGE = LIN_PICK_MESSAGE;
            const TRICK_SIMPLECARD = LIN_PICK_SIMPLECARD
        }
    
        return (request.type === 'IntentRequest'
           && request.intent.name === 'LinPickIntent');
      },
  
     handle(handlerInput) {
        const speechOutput = TRICK_MESSAGE;

       return handlerInput.responseBuilder
       .speak(TRICK_MESSAGE)
       .reprompt(LIN_REPROMPT)
       .withSimpleCard(SKILL_NAME, TRICK_SIMPLECARD)
       .getResponse();
    }


    };

I was hoping that initially I tell alexa that I want to say "PICK A CARD" This will then open the PickACardHandler and then set the constant TRICK = 'PICK A CARD'.

There are then a couple more stages, then a prompt which opens the LinPickHandler

When opening the LinPickHandler hoping to test the const TRICK to see if it has been set to "PICK A CARD" and if it is then set the const TRICK_MESSAGE to the appropriate message.

Unfortunatly I just get the constent of what the constant was origianlly set to. In my case "NOTHING"

I am guessing this is because the constanst are local the to Handlers and not being passed back up. similar to UNIX environment variables.

Unfortunatly my knowledge here is VERY limited, and cannot seem to find the solution. Any help greatly appreciated.

ALSO I'm only intending using this in developer mode, ie local to my account

  1. Const are constants which means, once you have assigned a value you can't change those. If you intend to change the value, you need to use variables, where you can reassign a different value later.
  2. scope of a variable / constant means a those items are only "visible" / known, on the same coding level or on nested once. (Some coding languages have different mechanics, but mostly it is applied that way. Variables / constants on the top level are called global variables / constants and are accessible everywhere. (Like those on your first ten lines). While others like in your handlers are only accessible inside those code blocks (the handler - or even the if statement itself like

const TRICK_MESSAGE = LIN_THOUGHT_MESSAGE;

which is even not accessible after the if code block. (var is creating a changeable variable and visible on method (not just on code block level like 'let')

  1. A can handle block should only be used to elaborate if a specific handler should be used - if an intent is incoming, all can-handle code will be executed, until the first response with turn - than that handler will be executed and not the other registered handlers can-handle. So if you do more than comparison, you even execute those code if the handler is not the right one!

  2. All of the code is executed for one response and state get lost after sending the response. For keeping variables between invocations, you need to store those in the session object of the response. They will be forwarded to you again with the next request object session.

With those hints in mind a some minor optimizations, your code should look like:


    const TRICK_PICK_A_CARD = 'PICK A CARD;

    const PICKACARD_MESSAGE = 'Don\'t tell me <break time="0.5s"/> you can\'t find it?<break time="1s"/> Was the pack shuffled?';
    const PICKACARD_SIMPLECARD = "Don't tell me you can't find it? Was the pack shuffled?";
    const PICKACARD_REPROMT = 'This a reprompt for Pick a card <break time="1s"/> Who chose the card?';

    const THINKOFACARD_MESSAGE = 'Don\'t tell me <break time="0.5s"/> you can\'t read their mind?<break time="1s"/> Who thought of a card?';
    const THINKOFACARD_SIMPLECARD = "Don't tell me, you can't read their mind? Who thought of a card?";
    const THINKOFACARD_REPROMPT = 'This a reprompt for Think of a card - <break time="1s"/> Who thought of a card?';

    //=========================================================================================================================================

   const PickACardHandler = {
      canHandle(handlerInput) {
      const requestType = getRequestType(handlerInput.requestEnvelope);
      return requestType === 'LaunchRequest'
        || (requestType === 'IntentRequest'
          && getIntentName(handlerInput.requestEnvelope) === 'PickACardIntent');
     },
     handle(handlerInput) {
        const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
        sessionAttributes.trick  = TRICK_PICK_A_CARD;

        handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
        return handlerInput.responseBuilder
        .speak(PICKACARD_MESSAGE)
        .reprompt(PICKACARD_REPROMT)
        .withSimpleCard(SKILL_NAME, PICKACARD_SIMPLECARD)
        .getResponse();
      },
    };

    //=========================================================================================================================================

   const LinPickHandler = {
      canHandle(handlerInput) {
        return (getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
           && getIntentName(handlerInput.requestEnvelope) === 'LinPickIntent');
      },

     handle(handlerInput) {
        const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();

         if (sessionAttributes.trick === TRICK_PICK_A_CARD) {
            var TRICK_MESSAGE = LIN_THOUGHT_MESSAGE;
            var TRICK_SIMPLECARD = LIN_THOUGHT_SIMPLECARD
        } else {
            var TRICK_MESSAGE = LIN_PICK_MESSAGE;
            var TRICK_SIMPLECARD = LIN_PICK_SIMPLECARD
        }
  

       handlerInput.attributesManager.setSessionAttributes(sessionAttributes);
       return handlerInput.responseBuilder
       .speak(TRICK_MESSAGE)
       .reprompt(LIN_REPROMPT)
       .withSimpleCard(SKILL_NAME, TRICK_SIMPLECARD)
       .getResponse();
    }


    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM