Using the Dialog Service in IBM Watson—the Fox, Goose, and Bag of Beans Chatbot

by Stas TurloAugust 2, 2016
In this blog post, we demonstrate how to create a sample chatbot on the Watson platform to solve a classic river crossing puzzle.


Dialog is an IBM Watson service that enables you to automate branching conversations. Below, we show how to create dialogs for virtual agent applications with this service. As an example, we design a sample chatbot for solving the Fox, goose, and bag of beans puzzle.



To get started, you need:

  • An IBM Bluemix account
  • The Cloud Foundry CLI


How it works

IBM Watson Dialog manages conversations—dialogs—between virtual agents and users through an application programming interface (API). In addition to the automation of branching conversations between a user and your application, the service can also track and store user profile information.


Designing a dialog

In our example, we will create a dialog to solve a classic river crossing puzzle—a fox, a goose, and a bag of beans:

Once upon a time, a farmer went to a market and purchased a fox, a goose, and a bag of beans. On his way home, the farmer came to the bank of a river and rented a boat. But in crossing the river by boat, the farmer could carry only himself and a single one of his purchases—the fox, the goose, or the bag of beans.

If left together, the fox would eat the goose, or the goose would eat the beans.

The farmer’s challenge was to carry himself and his purchases to the far bank of the river, leaving each purchase intact.

The current state can be viewed as the positions of the players. For example, the puzzle begins in the state when all players are on the same side of the river (let’s say on the left side). So, the initial state can be represented by (mFGB|), where m is the man, F is the fox, G is the goose, and B is the bag of beans. If the man crosses with the fox, then the state becomes (GB|mF) with the goose eating the beans on the left bank and the man and fox on the right bank.

Since we have four players, each with two possible states, there are 16 possible states in total:


Blue means the goose ate the beans, and Red means the fox ate the goose. We can consolidate these states to make a total of 12 states:

(mG|FB)(Fox eats Goose)
(FB|mG)(Goose eats Grain)

In the given state space, there is a distinct process that changes from one state to another: crossing the river on the boat. Either it is the man alone or with one other item. Knowing this, we can arrange the states into something called a state diagram showing the possible transitions:


To represent a state in our dialog file, we will use the output element in conjunction with the folder element to group valid inputs and the goto element for state transition. To store the count of crossings, we will also use variables and actions. Each state looks like this:

 <output id="FB_MG_OUTPUT">
    <prompt selectionType="SEQUENTIAL">
        <item>Fox and bag of beans on left bank, you and goose right.</item>
    <getUserInput id="FB_MG_INPUT">
        <search ref="FB_MG"/>
                <prompt selectionType="RANDOM">
                    <item>I did not quite get that.</item>
                    <item>I didnt grasp what you said.</item>
                    <item>I'm not following you at the moment.</item>

<folder id="FB_MG">
    <action varName="Moves" operator="INCREMENT_BY">1</action>
    <goto ref="MFB_G_OUTPUT" />
      <item>Return with goose</item>
      <item>return with goose</item>
    <goto ref="begin" />

For more details, see the element reference documentation.

The rest of our example is trivial: a simple web application gets user inputs, passes it to a dialog service, and shows responses from the services. Here is how the bot works:



Trying it yourself

The source code for the article is available in our GitHub repository. You can use this link to deploy a demo application to your Bluemix account or follow the steps below to deploy it manually:

  1. Clone the repository: git clone

  2. Go to the repository directory: cd watson-dialog-demo

  3. Create an application: cf push APP_NAME

  4. Create a Personality Insight service instance: cf create-service dialog standard dialog_svc

  5. Bind the service instance to your application: cf bind-service YOUR_APP_NAME dialog_svc

  6. Restage your application: cf restage APP_NAME

Alternatively, you can create and bind the service instance using the Bluemix dashboard UI.


The binding process is simple.


As you can see, with the Dialog service, it is really easy to automate branching conversations between a user and your application.


Further reading

This post was written by Stas Turlo with assistance from Victoria Fedzkovich and Alex Khizhniak.