DEV Community

Cover image for BUILDING BMI BOT USING MICROSOFT BOT FRAMEWORK COMPOSER AND DEPLOYING ON AZURE
Neha Dubey
Neha Dubey

Posted on

BUILDING BMI BOT USING MICROSOFT BOT FRAMEWORK COMPOSER AND DEPLOYING ON AZURE

While we all like indulging in our cravings and eating junk food, we do not realize the damage it does to our bodies until later.

Take me for example, I am a chocoholic and samosa lover. Sometimes it is good to know that you’re not alone. Almost all the ladies out there have food cravings. Isn’t it? Now this tends to indulge in unhealthy eating which leads to various negative health consequences.

There are various harmful effects like excess of body fat, increase disease risk, may cause excessive gas or bloating, etc.

Now here there is a recommended method for diagnosing if you are overweight or underweight or obese by knowing your BMI (Body Mass Index) which is derived using weight and height of a person. It is widely used to determine if you are in a healthy weight range.

So today we going to build a friendly Bot which evaluates your BMI based on your body height and weight.

Lets get started…..

All you need is
• Bot Framework Composer: Follow this link to install
• Azure Account to host it

Intro

Create a new Bot.

  1. Start composer.
  2. In the Home page, select the New icon.

Open Bot Composer

  1. In the displayed Create from template or scratch? wizard, you'll be presented with different options to create your bot. For this tutorial, select Create from Scratch. Then click on Next

Create New

  1. In the displayed Create a bot project form, enter the following information: a. Name. Enter the name BMIBot. Notice that spaces and special characters are not allowed in the bot's name. b. Description. Enter “A friendly bot who can talk about BMI”. c. Location. Select the location on your computer where to save the project. d. Click OK.

Name and Location

After creating your bot, Composer will load the new bot's main dialog in the editor. It should look like this:

Main Page

As you can see in the above picture, you have the Navigation pane (BMIBot) on the left, the Authoring canvas in the middle, and the Properties panel (Adaptive dialog) on the right.

  1. Select the Greeting trigger in the Navigation pane on the left. In the Authoring canvas, the related conversation flow is displayed.

Note: A dialog contains one or more triggers that define the actions available to the bot while the dialog is active. When you create a new bot, an Activities trigger of type Greeting (Conversation Update activity) is automatically provisioned. Triggers help your dialog capture events of interest and respond to them using actions.

  1. Now, it is time to make the bot do something. Let us instruct it to send a simple greeting to the user. In the Authoring canvas, select the existing Send a response action. Its properties will appear on the right in the Properties panel. This action has only one property: the text of the message to send.

Basic Actions

  1. Type a welcome message into this field such as:

“Welcome to the BMI Bot”

It is always a good idea to have your bot introduce itself and explain its main features.

Change Welcome Text

8. Start you Bot
Select Start bot in the upper right-hand corner of the screen. This tells Composer to launch the bot's runtime, which is powered by the Bot Framework SDK.

Start Bot

  1. Then click on Open Web Chat
  2. Web Chat Window will appear, and you can see welcome message.

Web Chat

In the next step you will learn how to create a new dialog and connect the new dialog using Dialog Management menu.

Create a new dialog

  1. In the Navigation pane select the three vertical dots associated with the BMIBot dialog.
  2. In the drop-down menu, select Add new dialog.

Add New Dialog

  1. In the displayed Create a dialog, enter the following information, then select OK: Name. CheckBMI Description. Get your BMI

Create a Dialog

This will result in a new dialog with a pre-configured BeginDialog trigger. For now, you can just add a simple message to get started.

  1. In the Navigation pane on the left, select the BeginDialog trigger.
  2. In the Authoring canvas, under the BeginDialog trigger, select the plus (+) icon.
  3. In the drop-down menu, select the Send a response action.

Send Response

  1. In the Properties panel, enter the text Let us calculate your BMI into the Bot Responses section.

Text in response

Connect the new dialog

  1. In the Navigation pane, select BMIBot
  2. In the Authoring canvas, select the plus (+) icon beneath the Send a Response action (Welcome to BMI Bot).
  3. From the drop-down menu, select Dialog management, then Begin a new dialog.

Dialog Connect

  1. With the Begin a new dialog action active, select CheckBMI from the Dialog name drop down.

Dialog Connect

Now when a user runs your bot, your bot will respond by activating the CheckBMI dialog after printing welcome message.

In the next step you will learn how to prompt the user for additional information, then calculate BMI and return the results to the user, but first you need to validate that the functionality developed so far works correctly, you will do this using the Emulator.

Select Start bot in the upper right-hand corner of the screen.

If the bot is still running from the previous tutorial, you can select Restart bot, this will update the bot runtime app with all the new content and settings.

Check BMI

Before you can get the BMI you need to know the weight and height of the user. You can create a Text Input action to prompt the user for a weight and height to calculate BMI.

  1. In the Navigation pane, select the the CheckBMI dialogs BeginDialog trigger.
  2. In the Authoring canvas, select the plus (+) icon beneath the Send a response action, then select Number from the Ask a question menu.

Ask Question

After selecting Number from the Ask a question menu, you will notice that two new nodes appear in the flow. Each node corresponds to a tab in the Properties panel (See image below).
Bot Asks refers to the bots prompt to the user for information.
User Input enables you to assign the user input to a property that is saved in memory and can be used by the bot for further processing.
Other enables you to validate the user input and respond with a message if invalid input is entered.

  1. In the properties panel, select the Bot Asks tab and enter Please enter your weight in kilograms into the Prompt for text field.

Bot Asks

This is the question that the bot will ask the user.
Select the User Input tab in the Properties panel. This section contains properties related to the user's response, including where to store the value and how to process it. Enter the following values:

  1. Property. user.weight. The property used to store the user's answer (weight).
  2. Select the Other tab in the Properties panel. This is where you specify validation rules for the prompt, as well as error messages displayed to the user if they enter an invalid value based on the Validation Rules you create. • Expand the Recognizers section and enter the following in the Unrecognized Prompt field: Invalid Input. Please enter your weight in kilograms.

• Enter the following in the Invalid prompt field: Invalid Input. Please enter your weight in kilograms.

User Response

  1. Repeat above steps to get users height in meters and store it in user.height property. • Property. user.height. The property used to store the user's answer (height). • In the Unrecognized Prompt field enter: Invalid Input. Please enter your height in meters. Calculating BMI and Storing in a property
  2. In the Authoring canvas, select the plus (+) icon then select Set a Property from the Manage properties menu.

Set Property

  1. In the Properties panel on the right, enter user.bmi into the Property field.
  2. Next, enter =round(div(user.weight, mul(user.height, user.height)) , 2) into the Value field.

Property Value

Now in the In the Authoring canvas, select the plus (+) icon then select “Send a Response.”
In bot responses property click on “Add new Variation” and give text as “Your BMI is ${user.bmi}.“

Now in the In the Authoring canvas, select the plus (+) icon then select “Branch: if/else” from Create a Condition menu.
Follow below image to create a nested if else conditions to check if the user’s BMI is underweight, normal, overweight, or obese.

If Else

Our BMI Bot is ready ,now let us learn how to publish & test our bot on Azure.

Follow the below steps:

Create a new Azure Resource

Publish your bot

Test your Bot on Azure

Top comments (0)