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

Jun 16

Written by:
6/16/2019 5:33 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). 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.

Tutorial Series

Other articles in this series:

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

9 comment(s) so far...


Gravatar

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

A huge thanks to you Michael. Internet is quite big but so far, there are just a few sites trying to help newbies with comprehensive and updated (V4) bot framework tutorials. I tried books, online courses, tutorials, microsoft virtual academy and references...your material is the best I have found.

By Vinicivs on   10/21/2018 7:14 AM
Gravatar

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

@Vinicivs - Thanks for the support!

By Administrator on   10/21/2018 9:29 AM
Gravatar

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

Hello! Follow the tutorial but I could not publish my bot. After putting appId and passworId and republishing did not get the same result as described here on the site.

By Thiago Bucalon on   1/25/2019 11:29 AM
Gravatar

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

@Thiago Bucalon - post your exact error on Stack Overflow. Go to: stackoverflow.com/questions/tagged/botframework and click the "Ask Question" button.

By Administrator on   1/25/2019 1:29 PM
Gravatar

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

This is such a great set of blog posts! I have it all setup and running locally fine, it publishes fine too, but when I try the Web Chat I get "There was an error sending this message to your bot: HTTP status code Forbidden". I have connected the emulator and when testing 'Production' I get "Cannot post activity. Unauthorized.". I've double checked the keys are all correct. Any ideas?

By Pcgriffiths on   2/20/2019 3:12 PM
Gravatar

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

@Pcgriffiths - Sorry I have not seen that error. The best place for help is on StackOverflow stackoverflow.com/questions/tagged/botframework

By Administrator on   2/20/2019 4:07 PM
Gravatar

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

hello Pcgriffiths, be sure to write production and not Production, all lowercase

By ren on   3/12/2019 9:39 AM
Gravatar

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

Hello, could you help me understand how it should be in case you want the first message to be sent by the bot proactively?

By ren on   3/15/2019 12:13 PM
Gravatar

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

@ren - While proactive messages are possible, I do not have any material that covers it. Please see this link: bit.ly/2O64Ryq

By Administrator on   3/15/2019 12:30 PM