My First Bot

If you want to get in on the bot action, here’s a simple way to do it. Check out Burger Bot, which is all about your fast food experience. However, for us this is a really simple bot just to show you how things work.

There are three main parts to it:

  • The front-end, which runs in your browser, and allows you to enter messages.
  • The back-end which exposes an API for the front-end to call, and which communicates with…
  • The Watson Conversation service, which runs in Bluemix (IBM’s cloud development platform).

The front-end and back-end are included in the github project, as well as the training data for the Conversation service itself.

How does it all fit together?

The basic flow of the bot is as follows:

  • A user enters some text in the front-end.
  • The front-end calls back-end via an API, sending the user’s text.
  • The back-end adds in the necessary credentials and then sends it to the Watson Conversation service.
  • Watson Conversation determines the intent of the text and responds appropriately to the back-end, which then sends it back to the front-end so it can display Watson’s response.

How do we train Watson?

In order to train Watson Conversation for your particular use-case, you need to create an instance of the Conversation service on Bluemix. Have a look at the README in the github project for more details on how to do this.

Once you have an instance of the service, you need to create a workspace, which is a container for all the parts of your bot’s brain. This consists of three main parts:

  • Intents
  • Entities
  • Dialog

Intents and entities are a way to teach Watson how to understand the user’s input. The intent defines the user’s overall meaning, and entities are particular attributes mentioned by the user. The dialog enables you define the behavior of the bot in response to the intents and entities. If you want to know more about intents, entities and dialog, take a look at the three short videos referenced at the bottom of this article. For now, let’s move on and make use of these concepts …

The Burger Bot project includes a pre-built workspace that you can import into Watson in order to train Watson. However, in this article we’ll build a workspace from scratch to help you understand what’s going on.

Build it

  • Go to the Conversation tooling, and click “Create” on the Workspaces page to create a new workspace.
  • Once create, you will be automatically navigated inside the workspace to the Intents page.
  • Click “Create new” and enter a name for the intent, e.g. “order”, and then provide some examples of ordering, e.g. “can I order a burger ?”, “I would like a hotdog”, “pizza please”.
  • Click “Done” to save the new intent.
  • Now we need to add an entity for the food that can be ordered, so navigate to the Entities page.
  • Click “Create new”, enter a name for the entity, e.g. “food”, and add a value for each type of food, e.g. “burger”, “hotdog”.
  • OK, now Watson can understand some input, so let’s define how Watson should respond to that. Navigate to the Dialog page.
  • Click “Create”. This will build you a very basic dialog tree. The dialog tree consists of nodes where each node has a condition and some output. When the user sends a message, the node conditions are checked in order (top to bottom) until one matches. The “Fallback” node catches any input that didn’t match another previous node. The “Welcome” node is also a special node in that it provides a response which only happens at the beginning of a conversation.
  • Let’s add a node to handle our #order intent. Select the 3-dot menu on the “Welcome” node and you will see some options to add nodes. Adding a node above or below allows you to add an alternative conversation path, i.e. a node that will be checked before/after the particular selected node has been checked and failed to trigger. Adding a child node allows you to add to an existing conversation path, i.e. a follow on node to the selected node, which will only be activated on a subsequent user input after the selected node has been triggered. We want to start a new conversation path so add a node below the “Welcome” node.
  • You will see the details of your new node in the node editor on the right. Enter a name for the node, e.g. “Order”.
  • Now we have to decide when this node should trigger so we need to enter a condition to describe this. In this case we want it to trigger if the user’s input matches the #order intent, so enter “#order” as the condition.
  • We also only want this node to trigger if they mention a food, so click the + button beside your #order condition, and enter @food.
  • Now of course, we need to ask if they want fries with it, so in the response section, add a response of “Would you like fries with that?”.
  • Now we need to be able to handle their response to the fries question, so go back to the Intents page and add a new intent #yes which has some examples of saying yes, e.g. “yes”, “yep”, “ok”, etc. It’s also good to create a #no intent as well.
  • Now go back to your dialog, and select the “Order” node and add a child node. This will add a follow on node to handle the user’s response to the fries question; let’s call the node “Fries”.
  • Add a condition to the follow on node, i.e. #yes.
  • Add a response, e.g. “OK, I’ve added fries to your order”.
  • Now a node below the “Fries” node. We will use this to handle if the user says anything other than yes. Add a condition of “anything_else” — this is a special condition that will always trigger if it reaches this node.
  • Add a response, e.g. “OK, no fries with that”.
  • Now click the speech bubble button in the top right of the page. This opens the Try It Out panel, which allows you to test the conversation you’ve just written.
  • Try it out!

What did you order?

So we now have a simple working bot, but it doesn’t actually remember what food you ordered. We can remember things by storing them in context. Select the “Order” node, and click the 3 dots button beside the response, and select “JSON”. Now add a “context” object and store the food like this:

Now we can print out the food they ordered in our final responses. Select the #yes node, and change the response to show the food: “Ok, so that’s a $food and fries”, and the “anything_else” node’s response to “Ok, just a $food”.

How do I make this bot available?

Now you can point the app to use this workspace, by using the workspace ID of this workspace — see the instructions in the github project.

Additional material


If you want to know more about building with IBM Watson Conversation, have a look at my previous articles: