I’ll be speaking at Visual Studio Live!, August 13-17 at Microsoft HQ in Redmond. 
 I’ll be presenting the following sessions:
  • TH04 - Using The Microsoft Cognitive Custom Vision Service - Thursday, August 16
  • TH14 - Building Business Applications Using Bots - Thursday, August 16 
  As a speaker, I can offer $500 savings on the 5-day package.
  Register here: http://bit.ly/RMSPK30Reg

 

Creating a Hello World! Bot (MS Bot Framework V4 Preview Edition)

Jul 1

Written by:
7/1/2018 10:32 AM  RssIcon

image

The Microsoft Bot Framework V4 allows you to create intelligent bots that interact naturally wherever your users are (text/SMS to Skype, Slack, Office 365 mail and other popular services). In preview now, the Bot Builder V4 Preview SDK offers new features and is extensible with a pluggable middleware model.

In this article we will create a simple Hello World! Bot and deploy it.

Tutorial Series

Other articles in this series:

Requirements

 

Create The Project

image

Open Visual Studio and select File, then New, then Project.

image

In the New Project dialog, select the Bot Builder Template, name the project HelloWorldBot and click OK.

image

The project will be created.

Hit F5 to run the project.

image

Your web browser will open.

Make a note of the connection address to use with the Bot Framework Emulator.

Connect Using The Bot Framework Emulator

image

Open the Bot Framework Emulator.

image

Select create a new bot configuration.

image

Enter Hello World (Local) for Bot name.

Enter the connection address from the web page in the Endpoint URL box.

Click Save and connect.

image

Save the file with the Visual Studio project.

image

You can now chat with the Bot.

Update The Code

image

Return to Visual Studio and press Shift+F5 to stop debugging.

image

Open the EchoState.cs file and change the class so it adds a boolSaidHello property:

 

    public class EchoState
    {
        public int TurnCount { get; set; } = 0;
        // boolSaidHello will allow us to track if we have 
        // said hello to the user
        public bool boolSaidHello { get; set; } = false;
    }

 

image

Next, open the EchoBot.cs file (this is the main file for the bot), and alter the OnTurn method to the following:

 

        /// Every Conversation turn for our EchoBot will call this method. In here
        /// the bot checks the Activty type to verify it's a message, bumps the 
        /// turn conversation 'Turn' count, and then echoes the users typing
        /// back to them. 
        /// </summary>
        /// <param name="context">Turn scoped context containing all the data needed
        /// for processing this conversation turn. </param>        
        public async Task OnTurn(ITurnContext context)
        {
            // This bot is only handling Messages
            if (context.Activity.Type == ActivityTypes.Message)
            {
                // Get the conversation state from the turn context
                var state = context.GetConversationState<EchoState>();
                // Bump the turn count. 
                state.TurnCount++;
                if (!state.boolSaidHello)
                {
                    // Handle the Greeting
                    string strMessage = $"Hello World! {System.Environment.NewLine}";
                    strMessage += "Talk to me and I will repeat it back!";
                    await context.SendActivity(strMessage);
                    // Set boolSaidHello 
                    state.boolSaidHello = true;
                }
                else
                {
                    // Echo back to the user whatever they typed.
                    await context.SendActivity($"You said '{context.Activity.Text}'");
                }
            }
        }

 

This checks the status of the boolSaidHello property, and if it is false, sends a “Hello World!” message.

It then sets the property to true so it wont say it again.

image

Rebuild the project in Visual Studio and press F5 to run it.

image

In the Bot Emulator, click the Start Over button to re-start the conversation.

 

image

The greeting message will now only show once during the conversation.

Deploy The Bot

image

In Visual Studio, right-click on the project and select Publish.

image

  1. Select App Service (this is an Azure website)
  2. Select Create New
  3. Click Create Profile

image

  1. Log into your Azure account
  2. Put in the settings to create the new App Service
  3. Click the Create button

image

It may take several minutes for it to deploy.

At this point it is only creating the resources on Azure.

image

Click the Publish button to publish the application to Azure.

image

The application will publish.

Your web browser will open on the site.

Click the button to copy the web address to clipboard. Save it, you will need it for the later configurations.

However, change the “http” to https” because you must use “https” for the remaining configuration steps.

Connect The Bot To The Azure Bot Service

image

At this point we only have a web application deployed. To enable it to connect to the various channels such as Skype, we need to create an Azure Bot Service and connect it to it.

Open a web browser and navigate to: https://portal.azure.com and log in.

Then select Create a resource.

image

Search for Bot Channels and press the Enter key to perform the search.

When Bot Channels Registration appears, click on it.

image

On the edit screen for Bot Channels Registration, click the Create button.

image

  1. Fill in the details for the registration
  2. Click the Auto create App ID and password button…

image

A new blade will open

Click the Auto create App ID and password button

The blade will then close

image

This returns you to the Bot Channels Registration blade.

Click the Create button.

image

It may take several minutes for it to deploy.

image

Click the Go to resource button when it displays.

image

You can also find the Bot Channels Registration you created by selecting All resources (in the Azure portal) and then searching for it.

image

Select Settings then the Manage link for Microsoft App ID.

image

This will take you to the app registration site at: https://apps.dev.microsoft.com

Click on the Generate New Password button.

image

A password will display.

Copy down the password

Click OK.

image

Return to the portal.azure.com site and copy the Microsoft App ID.

image

Search for and select the Bot (the App Service)

image

Select Application settings

Select Add new setting

image

Add the following keys (and the coordinating values you copied down earlier):

  • MicrosoftAppId (for value use the Microsoft App ID you copied earlier)
  • MicrosoftAppPassword (for value use the password you copied earlier)

Click the Save button.

image

The app service will update.

image

Return to the Bot Channels Registration

image

You can now click the Test in Web Chat button and communicate with the Bot.

image

You can click on the Channels button to set up more channels for the Bot.

Connect In The Emulator

image

You can also open the Bot Framework Emulator

Select create a new bot configuration.

image

Enter the connection information to the bot App Service on Azure

Click Save and connect.

 

Links

Azure Bot Service Documentation for the v4 SDK

Azure Bot Service

Build a Microsoft Bot Framework bot with the Bot Builder SDK v4 (alternate way of creating this same bot from scratch)

Conversational AI: Best Practices for Building Bots

Find Your Azure Bot’s AppID and AppSecret

Download

You can download the code from the Download page