1. Learn
  2. /
  3. Courses
  4. /
  5. Building Chatbots in Python

Exercise

Form filling

You'll often want your bot to guide users through a series of steps, such as when they're placing an order.

In this exercise, you'll begin building a bot that lets users order coffee. They can choose between two types: Colombian and Kenyan. If the user provides unexpected input, your bot will handle this differently depending on where they are in the flow.

Your job here is to identify the appropriate state and next state based on the intents and response messages provided. For example, if the intent is "order", then the state changes from INIT to CHOOSE_COFFEE.

A function send_message(policy, state, message) has already been defined for you. It takes the policy, the current state, and message as arguments, and returns the new state as a result. Additionally, an interpret(message) function, similar to the one Alan described in the video, has been pre-defined for you.

Instructions

100 XP
  • Define three states: INIT with value 0, CHOOSE_COFFEE with value 1, and ORDERED with value 2.
  • Create a dictionary called policy with tuples as keys and values. Each key is a tuple containing a state and an intent, and each value is a tuple containing the next state and the response message. The messages have been filled in for you. Your job is to fill in the states.
  • Instantiate a variable state with the value INIT.
  • For each of the messages, call the send_message() function, passing in the policy, state, and message.