If we see the current technology trends, BOT is quite popular. Most of the modern solutions come with BOT to automate tasks and respond to the conversation with human interaction.

In simple terms, a bot is a software application that automates the tasks which are repetitive in nature and respond without human interaction. A bot is a short form of robot.

In this article, we will learn how to develop a bot using Microsoft bot framework SDK using Visual Studio. Overview of this article.

  • What is bot
  • What is the Bot Framework SDK
  • Azure bot Service
  • Bot Framework Emulator
  • Bot Framework Templates for Visual Studio
  • Create a bot solution
  • Test the bot with Bot Framework Emulator

What is a bot?

As explained above, a bot is a software application that automates the tasks which are repetitive in nature and respond without human interaction. A bot can respond to queries quickly and provides sophisticated experiences without any human interaction.

(Microsoft)

It can be implemented with messaging solution like Facebook, Discord, Slack or Microsoft Teams. And users can interact through a channel of those messaging applications.

We can use various technology stacks and tools to develop a bot, depend upon the requirement and type or nature of bot.

Bot Framework SDK

Bot framework SDK provides extensible SDK for building bots, tools, templates, libraries, and AI services. This Bot framework works with Azure Bot Service and provide a complete platform to develop, test, deploy and manage bots effectively. Using this bot framework SDK, developers can create bots for several scenarios like conversational bots, voice enable bots, and likewise.

Azure Bot Service

An Azure bot service is a complete development platform for developing enterprise-grade bot solutions. This environment enables the development of high-quality conversational AI experiences bots for all possible scenarios. This service has the capability to build multimodal and multilingual bots.

To know more details and how to create Azure Bot Service, follow this link. https://rijsat.com/2021/11/03/create-azure-bot-service-from-azure-portal/

Bot Framework Emulator

It is a desktop-based emulator for bots. Bot Framework emulator is a free desktop application that can be used for testing and debugging specially for bots which are developed using Bot Framework SDK. It is available for windows, OS X and Linux platforms.

https://github.com/microsoft/BotFramework-Emulator/blob/master/README.md

Download Link: https://github.com/Microsoft/BotFramework-Emulator/releases/tag/v4.14.1

Bot Framework v4 SDK Templates for Visual Studio

Visual Studio templates for building bots are available in marketplace. The Microsoft Bot builder v4 are ready-made templates that are available for .net to download and add in Visual Studio.

Download: https://marketplace.visualstudio.com/items?itemName=BotBuilder.botbuilderv4

Creating a bot using Bot framework SDK

Now we will commence with bot development. We will create a bot using the Bot Framework SDK.

Prerequisites

Visual Studio 2019 or later (I am using Visual Studio 2022 for this tutorial)

Bot Framework Emulator

ASP.NET Core

Bot Framework v4 SDK Templates for Visual Studio

Note: Before starting, we need to install Bot Framework v4 SDK Templates for Visual Studio.

We will open Visual Studio and create a new project.

We will search for AI bots; all the bot templates will appear as project template. These templates are appearing because we have installed Bot Framework v4 SDK Templates for Visual Studio. We will select Echo Bot (Bot framework v4 – .Net core 3.1) project template and proceed by clicking Next as shown.

The next screen will provide you options to enter project name, location, and solution name as given below.

Enter the values as per your project and location and click Create.

We have successfully created the bot project and we will project files and folder as portrayed below.

Thanks to the Bot Framework v4 SDK Templates, we will have all the necessary libraries and codes included in the project. We will build and run the solution.

We will see a default page in our browser after running the bot solution.

Now our bot is ready to test with endpoint URL http://localhost:3978/. However, we will explore a bit more on the code of the solution.

Let’s see startup class.

I will some the configuration services for the bot solution.

You can observe that a singleton scope is added for authentication with the bot Adopter. In other words, the Bot framework authentication is created that is to be used with the Bot Adopter. Similarly, a service AdapterWithErrorHandler injected for error handling. And the last service for bot implementation as a transient. To summarize, we are doing the following three in service collections.

  • Create the Bot Framework Authentication to be used with the Bot Adapter.
  • Create the Bot Adapter with error handling enabled.
  • Create the bot as a transient. In this case, the ASP Controller is expecting an IBot.

By default, bot adapter error handler class is created to handle the error related with bot authentication processes and send/receive activities from the bot connector service.

Code:
 public class AdapterWithErrorHandler : CloudAdapter
    {
        public AdapterWithErrorHandler(BotFrameworkAuthentication auth, ILogger<IBotFrameworkHttpAdapter> logger)
            : base(auth, logger)
        {
            OnTurnError = async (turnContext, exception) =>
            {
                // Log any leaked exception from the application.
                logger.LogError(exception, $"[OnTurnError] unhandled error : {exception.Message}");

                // Send a message to the user
                await turnContext.SendActivityAsync("The bot encountered an error or bug.");
                await turnContext.SendActivityAsync("To continue to run this bot, please fix the bot source code.");

                // Send a trace activity, which will be displayed in the Bot Framework Emulator
                await turnContext.TraceActivityAsync("OnTurnError Trace", exception.Message, "https://www.botframework.com/schemas/error", "TurnError");
            };
        }
    }

Additionally, you will see EchoBot.cs which is the main class for Bot activities, called as Activity Handler.

 public class EchoBot : ActivityHandler
    {
        protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            var replyText = $"Echo: {turnContext.Activity.Text}";
            await turnContext.SendActivityAsync(MessageFactory.Text(replyText, replyText), cancellationToken);
        }

        protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
        {
            var welcomeText = "Hello and welcome!";
            foreach (var member in membersAdded)
            {
                if (member.Id != turnContext.Activity.Recipient.Id)
                {
                    await turnContext.SendActivityAsync(MessageFactory.Text(welcomeText, welcomeText), cancellationToken);
                }
            }
        }
    }

In above code, OnMessageActivityAsync is the method for message activities, and OnMembersAddedAsync is for member addition.

As per above class, the bot will echo the message you send. We will proceed with testing.

Test the bot with Bot Framework Emulator

As elucidated above, Bot Framework Emulator is an emulator through which we will connect our bot and do testing. We will open the emulator.

Once the Bot Emulator opens, it will be as shown above. To test our bot, we click create a new bot configuration as portrayed above, which will pop up another window to enter bot name, endpoint URL and other information. However, for our bot, we need to give a bot name and endpoint URL to our bot solution. Our endpoint for bot is http://localhost:3978/ (local  host and port number) plus /api/messages is added to get complete path.

Click save and connect.

After that, we will see the below screen to test the bot.

We will send message, and bot will response with Echo and message.

Cheers!! We have successfully tested our bot with Emulator by sending message and Bot response.

Since, this article has elaborated with getting started with bot using bot framework SDK, however, we can build complex bots using complex business cases, and integrating with AI as well.

Conclusion

In this article, I have described what a bot is, how it works, and tools required for building bots. Furthermore, I have elaborated Bot Framework SDK and develop a bot using it. We can build bots using this Bot framework SDK for any kind of scenarios from simple to complex nature and implementing additional services like AI as so on. Bot developed using Bot Framework works with in combination of Azure Bot Service. In addition to that, we have Bot Emulator to test the bot and ready-made project templates for Visual Studio which is available in Visual Studio marketplace as Bot Framework v4 SDK Templates.

References:

Microsoft

By Rijwan Ansari

MVP, CSM, CSPO, MCP, MCTS. Sr. Software Developer. Expertise in ASP.NET MVC, Angular, AngularJs, .Net Core, C#.NET, Azure, Cognitive Services, Machine Learning, SharePoint, Azure Services, and tools, Requirement Analysis, Team Management, Pre-Sales and more. Ability to work independently and Team, handle pressure and meet commitment. Excellence Communication and adjust in any environment

Leave a Reply

Your email address will not be published. Required fields are marked *