0
votes

I am trying to build a simple Alexa Skill. Now, I want to access some of my slot values. This is my code. When I uncomment even the line that I define Alexa in, my skill will not work. Also, if I only uncomment the line defining var text, I still get "there was a problem with the skills response". Const gives the same output. I am using custom slots called recipe. How can I access the slots in my lambda function? Thanks.

const breakfast = {  
    "bacon and eggs":["bacon","egg"],
    "buttered toast":["bread", "butter"]
};
const lunch = {  
    "ham sandwich":["ham","cheese"]
};
const dinner = { "Steak and eggs": ['steak','eggs']};
//const Alexa = require('ask-sdk-core');

exports.handler = (event, context, callback) => {
    try {
        if (event.request.type === 'LaunchRequest') {
            callback(null, buildResponse('Hello from Lambda'));
        } else if (event.request.type === 'IntentRequest') {
            const intentName = event.request.intent.name;


            if (intentName === 'breakfast') {
                callback(null, buildResponse(Object.keys(breakfast)+"") );
            }
            else if (intentName === 'lunch') {
                callback(null, buildResponse(Object.keys(lunch)+"") );
            }
            else if (intentName === 'dinner') {
                callback(null, buildResponse(Object.keys(dinner)+"") );
            }
            
            else if (intentName ==='requestRecipe'){
                //var text = this.event.request.intent.slots.recipe.value;
                //const meal = Alexa.getSlotValue(intentName, "meal")
                
                callback(null, buildResponse("Recipe requested") );

            }
            else {
                callback(null, buildResponse("Sorry, i don't understand"));
            }
        } else if (event.request.type === 'SessionEndedRequest') {
            callback(null, buildResponse('Session Ended'));
        }
    } catch (e) {
        context.fail(`Exception: ${e}`);
    }
};

function buildResponse(response) {
    return {
        version: '1.0',
        response: {
            outputSpeech: {
                type: 'PlainText',
                text: response,
            },
            shouldEndSession: false,
        },
        sessionAttributes: {},
    };
    
}
1

1 Answers

0
votes

for a bit of context: my lambda has the endpoint of what my alexa hosted skill was, and the alexa skill has the endpoint of the lambda. when I say const gives the same output, i mean instead of using var, when I use const, it does the same thing. The JSON file that i get as a reply is empty brackets.

I found the issue behind my problem. Instead of using
//var text = this.event.request.intent.slots.recipe.value; i simply did var text = event.request.intent.slots.recipe.value; I am now able to use text in building a response or something like that.