6/16/2019 Webmaster

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


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). The Bot Builder V4 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.


Requirements

 

Create The Project

image

Open Visual Studio and select Create a new project.

image

Enter Echo Bot in the search box, select the Echo Bot template and click the Next button.

image

Name the project HelloWorldBot and click Create.

image

The project will be created.

Hit F5 to run the project.

image

Your web browser will open.

It will display the web address to connect to the Bot.

Note the web address.

Connect Using The Bot Framework Emulator

image

Open the Bot Framework Emulator.

image

Select Open Bot.

image

Enter the web address you noted from before and click Connect.

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 EchoBot.cs file.

image

We see the OnMessageActivityAsync method that is called with each interaction the Bot makes.

Note: OnMembersAddedAsync is only called when a new user is detected.

Change the OnMessageActivityAsync method to the following:

 

        protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> 
            turnContext, CancellationToken cancellationToken)
        {
            string strIncomingText = turnContext.Activity.Text;
            string strOutgoingText = $"You said: {turnContext.Activity.Text}";
            if (strIncomingText.ToLower().StartsWith("my name is"))
            {
                strOutgoingText = $"Your name is: {strIncomingText.Substring(10, (strIncomingText.Length - 10))}";
            }
            await turnContext.SendActivityAsync(MessageFactory.Text(strOutgoingText), cancellationToken);
        }

 

image

Hit F5 to run the web application, return to the emulator and click the Restart conversation button.

 

image

We now see that the Bot will respond differently when we send the message “My name is..”

 

 

Deploy The Bot

image

We can deploy the bot to Microsoft Azure. Go to: https://azure.microsoft.com

Create an account if you don’t have one and log in.

 

image

Click Create a resource.

 

image

Search for Bot then select Web App Bot.

 

image

Click Create.

 

image

Configure the Bot and click the Create button.

 

image

The Bot will be created and deployment will begin...

 

image

When the deployment is complete, you can select all resources and you will see that two resources have been created:

Web App Bot – This is the Azure resource that will communicate with your Bot code contained in the App Service. This allows your Bot to connect to the Microsoft service that allows you to connect to your users via popular channels.

App Service - This is the Azure resource that will contain your Bot code. This is the location you will publish your code to.

 

image

Select the App Service.

 

image

Select Configuration and copy the MicrosoftAppId and MicrosoftAppPassword.

 

image

Return to Visual Studio.

Open the appsettings.json file and paste in the copied values.

 

image

Right-Click on the project node and select Publish.

 

image

On the Publish dialog, click the Advanced button.

 

image

Select Remove additional files at destination and click Save.

 

image

Select App Service, Existing, and click Publish.

 

image

Sign into your Azure account, select your Subscription and your App Service and click OK.

 

image

The code will be published to the App Service.

 

image

Return to the Web App Bot




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.

For example, you can click the Skype button…

 

image

Then click Publish.

 

image

Fill in the required information and click the Save button.

 

image

Agree to Microsoft’s Terms of Service.

 

image

Click the Cancel button.

 

image

This takes you back to the Connect to channels page.

Click on the Skype link.

 

image

This will take you to page that allows you to add the bot to your Skype contacts.

Click the Add to Contacts button.

 

image

Click the button to Open Skype (if you have it installed on your computer).

Otherwise click the button to Download Skype.

 

image

Skype will open.

Click Send message.

 

image

Skype will open.

You can now chat with the Bot in Skype.

Note: It may take 30+ minutes before the channel will be fully hooked up and responding.

 

Links

Azure Bot Service Documentation for the v4 SDK

Azure Bot Service

 

Download

You can download the code from the Download page

An error has occurred. This application may no longer respond until reloaded. Reload 🗙