node.jsaws-lambdaalexa-skills-kitalexa-slot

How to get custom intent slot values using handlerInput in ASK-SDK v2


I'm creating a basic calculator skill using ASK-SDK v2. I'm not sure how to get the slot values provided by the user into the Lambda code with the new version. I was able to make it work with the older version.

Conversation User: Open calculate Alexa: You can ask me to add, subtract, multiply and divide User: Add two and three Alexa: Sum of 2 and 3 is 5

Below is my IntentSchema

{
    "interactionModel": {
        "languageModel": {
            "invocationName": "calculate",
            "intents": [
                {
                    "name": "AMAZON.CancelIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.HelpIntent",
                    "samples": []
                },
                {
                    "name": "AMAZON.StopIntent",
                    "samples": []
                },
                {
                    "name": "AddIntent",
                    "slots": [
                        {
                            "name": "numA",
                            "type": "AMAZON.NUMBER"
                        },
                        {
                            "name": "numB",
                            "type": "AMAZON.NUMBER"
                        }
                    ],
                    "samples": [
                        "Sum of {numA} and {numB}",
                        "add {numA} and {numB}"
                    ]
                },
                {
                    "name": "SubIntent",
                    "slots": [
                        {
                            "name": "numA",
                            "type": "AMAZON.NUMBER"
                        },
                        {
                            "name": "numB",
                            "type": "AMAZON.NUMBER"
                        }
                    ],
                    "samples": [
                        "difference between {numA} and {numB}",
                        "subtract {numA} from {numB}"
                    ]
                },
                {
                    "name": "ProductIntent",
                    "slots": [
                        {
                            "name": "numA",
                            "type": "AMAZON.NUMBER"
                        },
                        {
                            "name": "numB",
                            "type": "AMAZON.NUMBER"
                        }
                    ],
                    "samples": [
                        "multiply {numA} and {numB}",
                        "product of {numA} and {numB}"
                    ]
                },
                {
                    "name": "DivideIntent",
                    "slots": [
                        {
                            "name": "numA",
                            "type": "AMAZON.NUMBER"
                        },
                        {
                            "name": "numB",
                            "type": "AMAZON.NUMBER"
                        }
                    ],
                    "samples": [
                        "divide {numB} by {numA}",
                        "divide {numA} by {numB}"
                    ]
                },
                {
                    "name": "ExponentialIntent",
                    "slots": [
                        {
                            "name": "numA",
                            "type": "AMAZON.NUMBER"
                        },
                        {
                            "name": "numB",
                            "type": "AMAZON.NUMBER"
                        },
                        {
                            "name": "numC",
                            "type": "AMAZON.NUMBER"
                        }
                    ],
                    "samples": [
                        "{numA} raised to the power of {numB} by {numC}",
                        "{numA} raised to the power {numB}"
                    ]
                },
                {
                    "name": "AMAZON.NavigateHomeIntent",
                    "samples": []
                }
            ],
            "types": []
        }
    }
}

I'm adding the addintenthandler here. Please tell me if the approach I'm using to get the slot values from the intent is correct or if I should use sessionattributes

const AddIntentHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
            && handlerInput.requestEnvelope.request.intent.name === 'AddIntent';
    },
    handle(handlerInput) {
        var output1 = "";
        var num1 = handlerInput.resuestEnvelope.request.intent.slots.numA.value;
        var num2 = handlerInput.resuestEnvelope.request.intent.slots.numB.value;
        if((num1)&&(num2)){
            output1 = 'The sum of ' +num1+ ' and ' +num2+ ' is ' + (num1+num2);
        }
        else {
            output1 = 'Enter valid number';
        }
        const speechText = output1;
        return handlerInput.responseBuilder
            .speak(speechText)
            .reprompt(speechText)
            .getResponse();
    }
};

Alexa responds with "Unable to process requested skill response" Any help is welcome


Solution

  • Update: there are now built-in functions in the SDK for this: Alexa.getSlotValue() (returns the string value) and getSlot() (returns Slot object) Alexa.getSlotValue(handlerInput.requestEnvelope, "someSlotName")

    Old answer: You have a typo, resuestEnvelope should be requestEnvelope. In any case I have created exactly the same skill, a calculator (in Spanish but it's basically the same thing) and I use a helper function called getSlotValues() which I encourage you to reuse. It will also work great when you have to capture custom slots (which are processed differently because the entity resolution structure is different):

    https://github.com/germanviscuso/skill-sample-nodejs-mycalculator