Build a Caffeine Chatbot with Watson Assistant

Nora AlNashwan
Voice Tech Podcast
Published in
7 min readOct 12, 2019

TL;DR: In this tutorial, I’ll show you how to build a chatbot that will help you calculate how much caffeine you drink based on what you drink and in what size — to alert you when you drink so much caffeine that will cause negative effects on your health.

Here’s a preview of what we will be building.

We will build the chatbot using Watson Assistant service on IBM Cloud in four main steps: Creating the service, creating assistant and skill, adding entities and designing dialog.

okay then…

Step 1: Create Watson Assistant Service

We need to sign up on IBM Cloud to be able to create and use Watson Assistant service. Once you signed in, go to the Watson Assistant page in the IBM Cloud catalog and click Create.

We’ll get started by launching the tool, and before we go into the next steps, let me give you an overview picture about how chatbots are built using Watson Assistant.

First, we create an assistant. The assistant must have at least one skill. In the skill, we define what are the users intents from their messages, what entities they could mention and we should pay attention to, and finally design a dialog where we specify responses based on user’s intent and entities.

Step 2: Create Assistant and Skill

Let’s create an assistant so we can start building a skill for our chatbot. We do that by clicking on Create assistant from Assistants tab.

I named it Bonnayn, the Arabic word for Caffeine. Once you choose a name for yours, go ahead and create a skill by clicking Add dialog skill.

Click Create skill tab and enter a name for your skill i.e., Calculation, then click on Create dialog skill button to be ready for the next step 🚀

Step 3: What the user can input?

As we explained earlier, what matters for us to calculate caffeine is the type of beverage (Cappuccino, Latte, Espresso and so on) and the size of cup (Small, Medium or Big). This can be defined in Watson Assistant by what’s called Entities.

Entities represent information in the user input that is relevant to the user’s purpose.

So, after you open the skill and from the Entities tab, click on Create entity.

Let’s Create entity named drinks and add the name of drinks — I added the names of hot drinks at Starbucks because I will use their Beverage Nutrition Information for caffeine calculation 😉

Now it is time to Create entity named size with the three size values and their synonyms

One more entity to add for user’s answer when we ask them if they want to get started or add another drink

Step 4: Designing the dialog

In the previous step above, we declared the entities that user will enter, but how the bot will response? this is what we will be building in the following steps.

So first click on Create dialog from Dialog tab, then you’ll find two nodes by default, one called Welcome for welcoming message and Anything else for responses when the bot doesn’t understand what the user is saying 🤷‍♀️

Build better voice apps. Get more articles & interviews from voice technology experts at voicetechpodcast.com

The first thing we need to do is editing the welcome message by clicking on the Welcome node, scroll down to Then respond with, and edit the message as following

Now we’ll add three nodes: one to respond when the user answers yes, and one to calculate caffeine and the last to respond when the user answers no.

From the Dialog tab, click on Add dialog. Then, you’ll see a node that has three parts: name, condition(s) and response(s)

For the first node, I will name it Prompt for drink type, and it will respond with Cool, cool. What size? if assistant recognizes @answer:yes.

hmm but how we’ll maintain the caffeine amount during the conversation?

for this we need to use something called context, a variable that you can change its value during conversation.

In the previous node, click on the three dots on the right of Then respond with then click Open context editor. This will add a new section for context variable names and values.

I named it total_caffeine and initialized it to zero.

We’ll need to add one thing before we move to the second node.

We’ll use slots to check if the user mentioned one of the drinks we added. If yes, we will save the value in $drink context variable. If no, we will ask the user to enter a drink.

You can enable slots by clicking on Customize button on the right of node’s name. Then switch Slots to On and click Apply. and the slot will be as following:

So, overall the node will look like this

Now, we’ll move to the next node which we will name it Calculate, and it will respond if assistant recognizes @ sizes

The response (or $total_caffeine value) will be different based on $drink and the entered size. So we need to enable Multiple conditioned responses by turning it On from Customize button, then click Apply.

I added 8 conditions but will share with you one for simplicity

@sizes:medium && $drink:Mocha

that means if the user drinks medium cup of Mocha, in that case from the configuration, I reinitialize the $drink and add 95 mg to $total_caffeine

I do the same thing with all conditions, except that the value I add to $total_caffeine will be different based on the condition.

Finally, we need to add the last node to response when the assistant recognizes @answer:no, and again we will have multiple responses. So don’t forget to enable Multiple conditioned responses 👌

As you can see, we have three responses:

  1. If $total_caffeine is zero: Thank you for using Bonnayn.
  2. If $total_caffeine is smaller than 400: You are in the safe side. Your caffeine intake is: <?$total_caffeine?> mg which is less than the maximum safe dose for most healthy adults (400 mg).
  3. If $total_caffeine is equal or greater than 400: Be careful. Your caffeine intake is: <?$total_caffeine?> mg which is more than the maximum safe dose for most healthy adults (400 mg).

<?$total_caffeine?> will be replaced by the value which we calculated during the conversation.

You can test your chatbot now by clicking on Try it button on the top right of the page.

And that’s all..

Thank you for reading! You might not find this article the shortest to explain how to use Watson Assistant, but I wanted to make it a complete story. It’s not the easiest chatbot but it covers almost every concept (context, slots, multiple responses) in one tutorial so I hope you find it useful.

At the end, I would like to thank Cheyenne Parsley, Watson Software Engineer at IBM US, who helped me to start and suggested the best way to implement it.

Something just for you

--

--

Nora AlNashwan
Voice Tech Podcast

Consultant at IBM Saudi and Co-founder of CODE FOR GIRLS. Passionate about cutting-edge technologies and motivated to to empower developers.