Creating a bot with the Bot Framework SDK
Tags: botframework, teams, dotnet
Published: 2021-09-23 19:41:54

---
title: Creating a bot with the Bot Framework SDK
published: true
date: 2021-09-18 15:51:11 UTC
tags: botframework,teams,dotnet
canonical_url: https://thewindev.net/creating-a-bot-with-the-bot-framework-sdk
---

In the past few months I worked on creating a Microsoft Teams bot using the Bot Framework SDK so I thing I have some useful knowledge to share about this topic. In this series of articles I want to show you how you can create your own Teams bot starting from scratch, and then we'll do more complex scenarios like SSO authentication with Microsoft Graph/Microsoft Dynamics 365, using adaptive cards and pushing them to their limits, etc.

In this article I'll show you how you can create your bot from scratch in C# and talk to it from the Bot Emulator. In the next article we'll connect to it from Teams as well.

## Content

🔹1. Creating a bot using C#

🔹2. Connect to the bot using the Bot Emulator

* * *

### 🔹 1. Creating a bot using C

**Prerequisites**

- [.NET Core 3.1](https://dotnet.microsoft.com/download/dotnet/thank-you/sdk-3.1.413-windows-x64-installer) 
- [Bot Framework Emulator](https://github.com/microsoft/BotFramework-Emulator/blob/master/README.md) 
- [Bot Framework Visual Studio Templates](https://marketplace.visualstudio.com/items?itemName=BotBuilder.botbuilderv4) 

Once you installed everything, the first step is to open Visual Studio and create a new "Echo Bot" project like in the screenshot below (make sure you're using the 3.1 version).

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1631977663780/hOh6MaMeI.png)

This template will automatically install the [Microsoft.Bot.Builder.Integration.AspNet.Core](https://www.nuget.org/packages/Microsoft.Bot.Builder.Integration.AspNet.Core/4.14.0/) Nuget package and create a basic bot that will reply with any text you send it. After this, pick a name for your bot and click on Create.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1631977850997/eNMwEL9wM.png)

### 2. Connect to the bot using the Bot Emulator

Once the solution is ready, you just have to run your bot (hit F5) and then we'll connect using the Bot Framework Emulator. If everything went fine, you should see this page:

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1631977966197/7_7jMIsAS.png)From this page we'll need the bot URL, in my case it's http://localhost:3978/api/messages

I will take this URL and put it in the Bot Framework Emulator, and then hit Connect.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1631978029192/ASXoNIuns.png)

Now we can send the first message:

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1631978100157/krofz5y6h.png)

As you can see, everything I type is sent back to me by the bot.

### How it works

Now, I want to explain you how everything works, so let's head back to Visual Studio and look at the solution.

The "magic" starts in the file "BotController.cs". This is a basic ASP.NET controller with only one endpoint, POST/GET on /api/messages. In this endpoint you can see that there's a simple call:

```
await Adapter.ProcessAsync(Request, Response, Bot);

```

Once this line is executed, the method "OnMessageActivityAsync" from EchoBot.cs is invoked. But how exactly does it do this? Well, if you look in the constructor, you can see that the BotController receives two parameters, an IBotFrameworkHttpAdapter and an IBot.

```
public BotController(IBotFrameworkHttpAdapter adapter, IBot bot)
{
       Adapter = adapter;
       Bot = bot;
}

```

If you now look in Startup.cs, we do the following service registrations:

```
services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();
services.AddTransient<IBot, EchoBot>();

```

So every GET/POST on /api/messages will be processed by our EchoBot and the OnMessageActivityAsync is just one of the ways it can interpret it.

Here, you can see that we do two things:

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

```

First, we take the text send by the user, this can be found in **turnContext.Activity.Text**. Then, we send this text back using the method **SendActivityAsync()** from turnContext.

There's also a call to **MessageFactory.Text()** here. This is because we are actually sending an **Activity** , but an activity can be of multiple times and we just send text, hence the call to MessageFactory.Text. In a future blog post from this series we'll send other types of activities as well.

That's it for this article, I don't want to make it too long. In the next one we'll connect to the bot from Microsoft Teams, so if you want to learn more you can follow me on [Twitter](https://twitter.com/thewindev).

The post [Creating a bot with the Bot Framework SDK](https://thewindev.net/series/bot-framework) appeared first on [thewindev.net](https://thewindev.net).