I have 7 slots in my amazon lex bot intent.
{
slot1: null,
slot2: null,
slot3: null,
slot4: null,
slot5: null,
slot6: null,
slot7: null
}
slot2 has 2 values for resolution. Yes or No.
slot3 has 3 values for resolution.
What I want to achieve is if Yes is selected then display values in slot3 and fullfill the intent.
If No is selected then skip slot3 and proceed with further slots.
Below is the lambda code used for Initialization and validation code hook, do read the comments:
exports.handler = async(event) => {
if (event.currentIntent.slots.slot2 != null) {
if (event.currentIntent.slots.slot4 == null) {
// handle slot2 "yes" or "no"
switch (event.currentIntent.slots.slot2) {
case 'No':
return {
dialogAction: {
type: "ElicitSlot",
intentName: event.currentIntent.name,
slots: event.currentIntent.slots,
slotToElicit: "slot4"
}
};
}
}
// If slot2 value is yes and any one of the value from slot3 is selected then fulfill the intent. Alo if you can tell me that we can add some call to action on slot3 value clicks like mailto: or tel: it will be really helpful.
if (event.currentIntent.slots.slot2 === "Yes" && event.currentIntent.slots.slot3 != null) {
return {
dialogAction: {
type: "Close",
fulfillmentState: "Fulfilled",
message: {
contentType: "PlainText",
content: "Fullfillment text"
}
}
}
}
// here I am trying to remove slot3 from the intent when slot2 value is "no"
if (event.currentIntent.slots.slot2 === "No" && event.currentIntent.slots.slot4 != null) {
delete event.currentIntent.slots.slot3;
}
}
// default return dialog action
return {
dialogAction: {
type: "Delegate",
slots: event.currentIntent.slots
}
};
};
Looks pretty good.
Firstly, don't delete slot3
. Basically, anytime you don't need to use a slot, just leave it or set it to null
and make sure it is not ticked as required in your Lex console, that's all. If you delete it then your response will no longer match all the slots that Lex expects.
You are using DialogAction
: "Delegate", which lets Lex determine which slot to elicit next. So after Lex elicits slot2
, I believe Lex will simply choose the next slot. However, Lex may choose the next required slot as priority over non-required slots, so again make sure that slot3
is not ticked as required.
To have better control over your conversation, you may not want to use Delegate at all, and instead, build the conversation logic exactly to your liking, using only ElicitSlot to ensure the correct slots are elicited at the correct points of the conversation.
If I simplify your code it would look like this:
if slot2 != null
if slot4 == null
if slot2 == No
elicit slot4
if slot2 == Yes and slot3 != null
fulfill intent
else
delegate
There are some things you can clean up about that and seeing it this way may shed light on the issue for you but perhaps you have plans to expand it and there's a reason to it for example why you are using switch case with only one case (for now?).
Consider a more straight-forward and controlled approach without delegate:
if slot1 == null
elicit slot1
else // slot1 filled
if slot2 == null
elicit slot2
else // slot2 filled
(validate slot2 value)
if slot2 == Yes
if slot3 == null
elicit slot3
else // slot3 filled
(validate slot3 value)
fulfill intent
if slot2 == No
elicit slot4
...