2
votes

How can I do this flow in amazon lex with lambda validation? I have tried to do it but the question is cycled and does not advance Thanks!!

here is the image of the flow

This is the code that I currently have, from slot1 to slot2 if it happens, but when I try to go from slot2 to slot3, slot2 cycles and does not advance, thanks!!

exports.handler = (event, context, callback) => {
    const sessionAttributes = event.sessionAttributes;
    const slots = event.currentIntent.slots;
    
    const slot1 = slots.Slot1;
    const slot2 = slots.Slot2;
    const slot3 = slots.Slot3;
    
    const optSlot1 = ['1','2'];
    const optSlot2 = ['1','2'];
    const optSlot3 = ['1','2'];
    
        if(slot1 === optSlot1[0]) {
    
        let response = { 
            
          dialogAction: {
            type: "ElicitSlot",
             intentName: event.currentIntent.name,
             slots: slots,
             slotToElicit : "slot2",
            
          }
        }
        callback(null, response);
    }
    
    
        if(slot2 === optSlot2[0] || slot2 === optSlot2[1]) {
    
        let response = { 
            
          dialogAction: {
            type: "ElicitSlot",
             intentName: event.currentIntent.name,
             slots: slots,
             slotToElicit : "slot3",
            
          }
        }
        callback(null, response);
    }

    };
1
Welcome to StackOverflow. In order to help, you'll have to provide more information. (1) What code are you currently using in Lambda to attempt this? (2) Can you give an example conversation to explain what you mean by "question is cycled and does not advance"?Jay A. Little
I have already corrected the question, thanks for the advice!Carlos Calderon

1 Answers

1
votes

Your slot2 is continuously getting elicited because your first IF statement for slot1 continues to resolve as true, so it returns the elicit slot2 again to Lex, and the rest of your code, never gets reached.

You should consider including checks for if a slot is empty before checking what the value is. For example, I chose to write my bot's Lambda code as something like this:

                             --((pseudo code))--
if slot1 is empty then 
    elicit slot1
else                                           --slot1 is filled
    if slot1 is Yes then 
        if slot2 is empty then 
            elicit slot2
        else                                   --slot2 is filled
            if slot3 is empty
                elicit slot3
            else                               --slot3 is filled
               ...and so on
        end
    else                                       --slot1 is No
        elicit slotDates
end

You can simplify it depending on your conversation flow, but that's the basic idea. The deeper into the conversation also brings you deeper into the nested if statements. And when you read the code, you can follow it down it's branches.


Side Tips (feel free to ignore this advice):

const optSlot1 = ['1','2'];
    ...
if(slot1 === optSlot1[0]) {...

This will be hard to understand as your bot becomes more complex because you will have to constantly scroll back out of your conversation logic to review the options for that slot. You can make it much easier on yourself, more natural for the user, and use more of Lex's built-in slot value recognition.

For example when you have a boolean question of Yes/No, then you can create a custom SlotType in Lex, provide 2 values "Yes" and "No", and then provide many synonyms for each such as "yea, yeah, yep, right, for sure, of course". This way, the bot can ask a natural open-ended question, and the user can respond naturally.

You should avoid using Lex like an old style automatic phone operator that spoon-feeds the user how to answer each question like this: "For Yes, please type 1. For No, please type 2" Lex is much more powerful than that.

So if you use Lex to determine what the user said and equate it to either "Yes" or "No" and fill the slot with one of those values, then your code would read like this:

const slot1 = event.slots.slot1
if (slot1 == "Yes") { ...

Much easier to read.