Getting Started With Microsoft Bot Using Visual Studio 2019

Introduction

 
Visual Studio 2019 streamlines your experience so you can get right down to focused work. Microsoft announced Visual Studio 2019 will launch on April 2, 2019, with many attractive features. Initially the preview version didn't support the Bot template, but after a long discussion with the MS team, Microsoft released Bot Template with VS 2019 support. In this article, we will learn how to create a bot by using Visual Studio 2019 with Microsoft Bot Framework template, and will be testing it with the Bot Emulator version 4.2.1.
 
Getting Started With Microsoft Bot Using Visual Studio 2019 

The Bot Framework enables you to build bots that support different types of interactions with users. You can design conversations in your bot to be free. Your bot can also have more guided interactions where it provides the user's choices or actions. The conversation can use simple text strings or more complex rich cards that contain text, images, and action buttons. And, you can add natural language interactions, which let your users interact with your bots in a natural and expressive way.

Prerequisites

  1. Download Visual Studio 2019 Preview ++
  2. Download the Bot Framework V4 Emulator for your platform from the GitHub releases

Create a new project

 
Let's start with creating a new Bot application using Visual Studio 2019. Go to Windows >> Visual Studio 2019.
 
Getting Started With Microsoft Bot Using Visual Studio 2019 

Before creating a new project, install the Bot Builder template from Tools >Extensions and Updates and search Bot Build V4 SDK > click on "Install".

Getting Started With Microsoft Bot Using Visual Studio 2019 

Create a new bot application project or open the recent project.

Getting Started With Microsoft Bot Using Visual Studio 2019 

Provide a project name and location in the following screen and click "Create".

Getting Started With Microsoft Bot Using Visual Studio 2019 

After clicking the "Create" button, the solutions will be created with SDK 3, SDK 4 and echo bot. You can select the required project version and select another project and delete.

Getting Started With Microsoft Bot Using Visual Studio 2019
 

Run the application

 
Run the app by clicking on the IIS Express button in Visual Studio (with the green play icon).

Check the port that your web application is running on. If it is not running on port 3978, you'll need to update the Bot configuration. In order to update this setting, go to Visual Studio project and open the EchoBot.bot file. Update the endpoint setting to match the port that your app is using.

Getting Started With Microsoft Bot Using Visual Studio 2019 

Edit OnTurnAsync method

 
Open EchoBotBot.cs and add the required namespace and modify the OnTurnAsync method, replace the content of the else statement with the following code snippet.
  1. public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))  
  2.      {  
  3.          // Handle Message activity type, which is the main activity type for shown within a conversational interface  
  4.          // Message activities may contain text, speech, interactive cards, and binary or unknown attachments.  
  5.          // see https://aka.ms/about-bot-activity-message to learn more about the message and other activity types  
  6.          if (turnContext.Activity.Type == ActivityTypes.Message)  
  7.          {  
  8.              // Get the conversation state from the turn context.  
  9.              var state = await _accessors.CounterState.GetAsync(turnContext, () => new CounterState());  
  10.   
  11.              // Bump the turn count for this conversation.  
  12.              state.TurnCount++;  
  13.   
  14.              // Set the property using the accessor.  
  15.              await _accessors.CounterState.SetAsync(turnContext, state);  
  16.   
  17.              // Save the new turn count into the conversation state.  
  18.              await _accessors.ConversationState.SaveChangesAsync(turnContext);  
  19.   
  20.              // Echo back to the user whatever they typed.  
  21.              var responseMessage = $"Turn {state.TurnCount}: You sent '{turnContext.Activity.Text}'\n";  
  22.              await turnContext.SendActivityAsync(responseMessage);  
  23.          }  
  24.          else  
  25.          {  
  26.              await turnContext.SendActivityAsync($"{turnContext.Activity.Type} event detected");  
  27.          }  
  28.      }  

Test Bot Applications

 
Open the Bot Framework Emulator from the Start menu and click "Open Bot" and select the file EchoBot.bot from the project. Previously, we had to provide the bot endpoint to the emulator but now it can read all the configurations from a .bot file.
 
Getting Started With Microsoft Bot Using Visual Studio 2019
 

Summary

 
In this article, your learned how to create a Bot application using Visual Studio 2019. If you have any questions/ feedback/ issues, please write in the comment box.


Similar Articles