6/16/2019 Webmaster
Creating a Hello World! Bot (MS Bot Framework V4)
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
- Visual Studio 2019 (or higher) with the following workloads:
- ASP.NET and web development
- Azure development
- .NET Core cross-platform development
- Bot Builder V4 SDK Template for Visual Studio
- Bot Framework Emulator
- A Microsoft Azure Subscription
Create The Project
Open Visual Studio and select Create a new project.
Enter Echo Bot in the search box, select the Echo Bot template and click the Next button.
Name the project HelloWorldBot and click Create.
The project will be created.
Hit F5 to run the project.
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
Open the Bot Framework Emulator.
Select Open Bot.
Enter the web address you noted from before and click Connect.
You can now chat with the Bot.
Update The Code
Return to Visual Studio and press Shift+F5 to stop debugging.
Open the EchoBot.cs file.
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); }
Hit F5 to run the web application, return to the emulator and click the Restart conversation button.
We now see that the Bot will respond differently when we send the message “My name is..”
Deploy The Bot
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.
Click Create a resource.
Search for Bot then select Web App Bot.
Click Create.
Configure the Bot and click the Create button.
The Bot will be created and deployment will begin...
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.
Select the App Service.
Select Configuration and copy the MicrosoftAppId and MicrosoftAppPassword.
Return to Visual Studio.
Open the appsettings.json file and paste in the copied values.
Right-Click on the project node and select Publish.
On the Publish dialog, click the Advanced button.
Select Remove additional files at destination and click Save.
Select App Service, Existing, and click Publish.
Sign into your Azure account, select your Subscription and your App Service and click OK.
The code will be published to the App Service.
Return to the Web App Bot
You can now click the Test in Web Chat button and communicate with the Bot.
You can click on the Channels button to set up more channels for the Bot.
For example, you can click the Skype button…
Then click Publish.
Fill in the required information and click the Save button.
Agree to Microsoft’s Terms of Service.
Click the Cancel button.
This takes you back to the Connect to channels page.
Click on the Skype link.
This will take you to page that allows you to add the bot to your Skype contacts.
Click the Add to Contacts button.
Click the button to Open Skype (if you have it installed on your computer).
Otherwise click the button to Download Skype.
Skype will open.
Click Send message.
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
Download
You can download the code from the Download page