다음을 통해 공유


Visual studio 2019: Bot Builder SDK Template

 

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. Microsoft released update Bot Template with VS 2019 support on Feb 13. In this article, we will learn how to create a bot by using Visual Studio 2019 with Microsoft BotFramework template and will be testing it with the Bot Emulator version 4.2.1.

 

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

Create a new project

Let's start with creating a new Bot application using Visual Studio 2019. Go to Windows >> 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".

 https://lh5.googleusercontent.com/Rjrr8sgqBFBEbN-WTO-oZbTi8NRgIWOgFhK3JFaeElmEHWobvFxeXLLzrGcF0aiQb5xjAvSRlmLToNXdbcQ5LshhSh8PdAKEU8u3X0qs4eEhgNmJAb7HlDZsLcne0rPGdjxKjovarhfwbq-nSQ 

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

https://lh4.googleusercontent.com/e15jeqmahmMeAtR98CVLoicwVNGrBi8DD4KZIeDCy66s001qI8yrHu-uNxB1fNJAcHAyp5dSYpuk3C4cLSqyRBMkW6sEDQxQ3pgZMbcYh6-D7LPKvIhExAV1qxYaYIAOvpTNa1bzx5pdJk4xrQ 

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

 https://lh3.googleusercontent.com/LVVNmxsXeynmfgJbVkIDlA5W31IUKr43HkRSmaUHfZcrZZbBBxQBbR1HeVC1qFVlpzToCrpVktn7N3YlD3D2OmafSyhXDoUoX3Rc6-R8tFK5ebdy4IJgwbNRSISgeupyxRkWrubxyvWZa4qcSw 

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. 

https://lh5.googleusercontent.com/irJIeWjVSoJqHkRCXsIf1kA-CWNxfg_H9sSJel4xqbv0KTHISt3Hq8S2Nsmug3qpaJFYm9GlpF8m1ynCccC7uIHdoVOToVS5TTo_6aEWTAKK7bWxqwGi9KiP-HBQx24JfwbRMw-_KwPmrSBzKA  

Empty Project 

A good template if you are familiar with BotFramework v4, and simply want a basic skeleton project. Also, a good option if you want to take sample code from the documentation and paste it into a minimal bot in order to learn.

Core Project 

Our most advanced template, the Core Bot template provides 6 core features every bot is likely to have. This template covers the core features of a Conversational-AI bot using LUIS. See the Core Bot Features table below for more details.

Echo Project

A good template if you want a little more than "Hello World!", but not much more. This template handles the very basics of sending messages to a bot and having the bot process the messages by repeating them back to the user. This template produces a bot that simply "echos" back to the user anything the user says to the bot.

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.

https://lh3.googleusercontent.com/OrCRj9bmCa3cOj3grL_18GxoPFZ1HArxfcD98R7XX-mHa40bXOrttyRrq3uwXzcsMN8F5Je-1wnmI0zpl_WM9cMtVobnd4xFAm_Pq6lfRkDL9pKfZ4LIr6UAF6tRoKks1M2to3-TcH3A_bEaFw

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.

public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
  {
   // Handle Message activity type, which is the main activity type for shown within a conversational interface
   // Message activities may contain text, speech, interactive cards, and binary or unknown attachments.
   // see https://aka.ms/about-bot-activity-message to learn more about the message and other activity types
   if (turnContext.Activity.Type == ActivityTypes.Message)
   {
    // Get the conversation state from the turn context.
    var state = await _accessors.CounterState.GetAsync(turnContext, () => new  CounterState());
 
    // Bump the turn count for this conversation.
    state.TurnCount++;
 
    // Set the property using the accessor.
    await _accessors.CounterState.SetAsync(turnContext, state);
 
    // Save the new turn count into the conversation state.
    await _accessors.ConversationState.SaveChangesAsync(turnContext);
 
    // Echo back to the user whatever they typed.
    var responseMessage = $"Turn {state.TurnCount}: You sent '{turnContext.Activity.Text}'\n";
    await turnContext.SendActivityAsync(responseMessage);
   }
   else
   {
    await turnContext.SendActivityAsync($"{turnContext.Activity.Type} event detected");
   }
  }

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.

https://lh3.googleusercontent.com/qkNXrt2zaA8SzkemwkK8SnV9pndlSZP3F-F0xWikByX85yZ1FCsCT1lTfMHfzKQYMEBbNrGVf6SPD45xNLUr1P08qQjttwU9strr40wQT9xK2vHL9999Y-5YRESzY3FNf2f8XZBv4GlSwNv_hA

Summary

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