BUILDING BMI BOT USING MICROSOFT BOT FRAMEWORK COMPOSER AND DEPLOYING ON AZURE  
Tags: dotnet, botcomposer, botemulator, azure
Published: 2021-03-31 15:08:17

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](https://docs.microsoft.com/en-us/composer/install-composer)
•	Azure Account to host it

![Intro](https://technoskulls.com/bmibot/Intro.jpg)

##Create a new Bot.
1.	Start composer.
2.	In the **Home** page, select the **New** icon.

![Open Bot Composer](https://technoskulls.com/bmibot/1_OpenBotComposer.jpg)

3.	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](https://technoskulls.com/bmibot/2_CreateNewEmpty.jpg)

4.	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](https://technoskulls.com/bmibot/3_NameAndLocation.jpg)

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

![Main Page](https://technoskulls.com/bmibot/4_MainPage.JPG)

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.
5.	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._

6.	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](https://technoskulls.com/bmibot/5_Basic Actions of Greeting Trigger.JPG)

7.	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](https://technoskulls.com/bmibot/6_Change Welcome Text.jpg)

**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](https://technoskulls.com/bmibot/7_StartBot.jpg)

9.	Then click on **Open Web Chat**
10.	Web Chat Window will appear, and you can see welcome message.

![Web Chat](https://technoskulls.com/bmibot/9_WebChatWindow.JPG)

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](https://technoskulls.com/bmibot/10_AddNewDialog.jpg)

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

![Create a Dialog](https://technoskulls.com/bmibot/11_CreateADialog.JPG)

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.

4.	In the Navigation pane on the left, select the **BeginDialog** trigger.
5.	In the **Authoring canvas**, under the **BeginDialog** trigger, select the plus **(+)** icon.
6.	In the drop-down menu, select the **Send a response** action.

![Send Response](https://technoskulls.com/bmibot/12_AddButton_SendResponse.jpg)

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

![Text in response](https://technoskulls.com/bmibot/13_Text_in_Response.jpg)

## 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](https://technoskulls.com/bmibot/13z_Dialog1.jpg)

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

![Dialog Connect](https://technoskulls.com/bmibot/13z_Dialog2.jpg)

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](https://technoskulls.com/bmibot/14_AskQuestion.jpg)

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.
3.	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](https://technoskulls.com/bmibot/15_BotAsks.jpg)

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:
4.	**Property. _user.weight_**. The property used to store the user's answer (weight).
5.	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](https://technoskulls.com/bmibot/16_UserInput_userweight.jpg)

6.	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
1.	In the **Authoring canvas**, select the plus **(+)** icon then select Set a Property from the Manage properties menu.

![Set Property](https://technoskulls.com/bmibot/21_SetProperty.jpg)

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

![Property Value](https://technoskulls.com/bmibot/22_PropertyValue.jpg)

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](https://technoskulls.com/bmibot/23_IfElseb.JPG)

_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](https://docs.microsoft.com/en-us/composer/how-to-publish-bot#sign-in-to-azure)**

**[Publish your bot](https://docs.microsoft.com/en-us/composer/how-to-publish-bot#publish)**

**[Test your Bot on Azure](https://docs.microsoft.com/en-us/composer/how-to-publish-bot#test)**