Build & manage Conversational Flows easily using the Conditional Branching feature in Amazon Lex V2

Sep 9, 2022 | Blog

Share this post
AWS recently updated Lex V2 using the Conditional Branching feature. This new cool feature helps to manage the conversational flow and create new complex conversations without the need to write a lot of lambda code.

In this blog post, let us see how to build a simple bot that helps customers order pizza using a conditional branching feature.

Plan and detailing of pizza order

We all know that, in general, when a customer tries to order pizza online, he/she will first be asked to choose the pizza size, and then the preferred crust options, toppings, and then the order will be placed. But here in this scenario, I am trying to handle a complex conversation to showcase conditional branching power in dealing with complex conversations.

So let’s say we offer discounts for a few customers who fulfill the following two conditions while ordering pizza.

1. Customers who order a large pizza

2. Customers who choose more than 3 toppings and are ready to pay extra charges for extra toppings.

The customers who satisfy the above two conditions will be offered a discount coupon which can be used on their next order.

Bot and Intent creation:

First, let us try to create a bot named OrderPizza with basic Lex permissions and then create two intents, one named customize, where the customer will have pizza customization options like size, crust type, toppings, and the other intent named feedback, to collect customer feedback.

So let’s consider all these customization options as slots, and create four slots, namely Size, crust, toppings, and extra charge.

And there will be only one slot in the feedback intent named “comments,” which takes customer feedback.

Slot type and custom vocabulary:

Now for the above-created slots, let’s create slot types, namely pizza_size, pizza_crust, pizza_toppings, and confirmation for size, crust, toppings, and extra charge slots, respectively, by allowing users to select only from available customization options.

For slot type pizza_size, we added available size options like small, medium, and large. For slot crust, we added available crust options like cheese, thin, thick, and flatbread. For slot toppings,we added available topping options like corn, onion, and jalapeno. And we have created a slot type called Confirmation, with slot values of “yes” and “no.”

Whenever we are creating a custom slot type, we have a check box to enable a custom vocabulary option. If we enable it, the bot will be well trained and will accept synonyms for the given slot values, making the bot more efficient.

Initial Response:

In the intent, after adding the utterances, we have another option called “initial response,” which is used to display the intent’s opening message, and in the advanced options of the initial response, we have the dialog code hook option and the new conditional branching feature.

In this scenario, as we are only using Lex to build this pizza order bot, we will have to disable the dialog code hook and set the next step as an elicit slot to Size.

And now, after the initial response, we will be elicited to the size slot. From here we will be moved to crust and toppings by default. If there are any conditions, we can apply them at the respective slot and navigate as required.

This image shows how to disable dialog code hook and elicit slot at initial response.

Conditions at Slot 1:

We can select “conditional branching” under advanced options at the size slot level, as we apply this condition based on size and number of toppings. We will add the quantifier condition.

as :

“if {Size} CONTAINS “large””

And also, we will set the session attributes here as [discount]=”true”, which indicates that if a user orders a large pizza, they are nearly eligible for the discount coupon but not quite. Adding more than three toppings will allow the customer to fulfill one more requirement.

After adding the condition, we will set the next step in the conversation to elicit a slot to crust.

This image shows conditional branching at the size slot.

Conditions at slots 2 &3 :

After eliciting the crust slot, we will set the next step in the conversation as an elicit slot to toppings, and now in the toppings slot we will add the following two conditions:

1: fn.COUNT({Toppings})>3 i.e., if the customer selects more than 3 toppings, then we will set the next step in the conversation as an elicit slot to extra charges.

2: fn.COUNT({Toppings})<4 i.e.; if the customer selects less than 4 toppings, then we will set the next step in the conversation as elicit to confirm intent, where we will ask for the customer’s final approval to place the order.

This image shows conditional branching at crust slot.

This image shows the first condition of the toppings slot.

This image shows the second condition of the toppings slot.

Conditions at slot 4 :

After eliciting the slot extra charge, we will prompt the user that he will be charged an extra 5 dollars as he has chosen more than 3 toppings.

Here we will add the following two conditions:

1: Where if the user accepts the extra charge, then we can elicit a slot for fulfillment.


2: And if the user denies, then he will be re-elicited to the previous slot toppings, to select only three toppings and not more than that.


This image shows the first condition of the extracharges slot.

This image shows the second condition of the extracharges slot.

Fulfillment condition:

If the customer accepts the additional charge, we will proceed with the fulfillment and then re-evaluate the following two conditions of size and toppings to determine whether the customer qualifies for the discount coupon or not.

1. {Extracharges}=”yes” AND [discount]=” true” i.e.; if the customer chooses a large pizza and toppings of more than three by accepting extra charges, then he will be qualified for a 10% discount coupon on his next order.

2. {Extracharges}=”no” OR “size”!=”large”, i.e., if the customer chooses a large pizza and toppings of less than 4, then he will not be qualified for the discount coupon.

In other words, if the user orders a large pizza and even adds more than three toppings, this

The condition will be assessed. If the user is qualified, we will set the slot elicitation to the closing response.

At which point we will notify him that he qualifies for the discount and ask for

feedback by switching the intent to feedback.

This image shows the first condition of the intent fulfillment.

This image shows the second condition of the intent fulfillment.

Happy path of the conversation design:

The other flow of the conversation would be:

If the user is not qualified for the discount, he will be asked for an order confirmation before the order is placed, and after the order has been placed, he will be asked for feedback.


In general, using conditionals, we can move from slot to slot, slot to intent, intent to the slot, slot to an intent-specific slot, and many other ways seamlessly. With this conditional branching feature, we can enable or disable dialog code hooks not only at the intent level but also at the slot level.

Isn’t it wonderful that from this point forward, we can use conditional branching to control the conversation flow of the bot and can create complex conversations without needing to write a lot of lambda code

That’s how we build intelligent bots. SmartBots help enterprises enhance customer interactions using conversational AI.

Thank you.

Metta Anusha.

(Conversational AI developer and Cloud practitioner at SmartBots)

Similar blog post