Get startedGet started for free

Asking questions & queuing answers

1. Asking questions & queuing answers

Now we're going to explore some other tricks you can use to build up conversation flows. As your bot becomes more sophisticated, your state machine will gain more and more states, and ever more rules for transitioning between them. You should always try to reduce this complexity, and in these exercises you will learn some tricks for doing so.

2. Reusable patterns

Let's say also start selling coffee filters through your bot-powered coffee business. The filters come in two colors, white and brown. Let's say your supplier is a little unreliable, and sometimes your run out of stock. These two very similar conversations are then possible. One way to handle these cases is to add an additional state which says that an alternative has been offered to the user, and adding some policy rules to handle the "yes" and "no" answers. But asking your user for confirmation before proceeding with something is a common pattern, and adding more states increases the complexity of your code. You can avoid this complexity by saving the follow-up as a "pending action"

3. Pending actions

To implement this, create a policy which returns two values the selected next `action` and a `pending_action`. The pending action will in many cases be `None`. Save the pending action in the outer scope, where the params dictionary and the state variable were also defined. - if we get a "yes" intent and there is a pending action, we execute it - if we get a "no" intent, we wipe any pending actions

4. Pending state transitions

In addition to pending actions, saving pending state transitions is another trick for keeping your state space small & complexity low. In this example, need to request some authentication info from the user First the users tells us they'd like to order coffee. We are in the init state, the selection action is to request authorization info, and we have a pending transition to the authed state. We request the user's phone number, and they supply it. Now, we immediately transition to the authed state, and we're ready for the user to place an order.

5. Let's practice!

Now it's your turn to try these techniques in some realistic bot scenarios.