Communicating with your bot from Microsoft Teams
Tags: c, dotnet, teams, azure
Published: 2021-09-24 12:57:53

---
title: Communicating with your bot from Microsoft Teams
published: true
date: 2021-09-19 18:54:37 UTC
tags: c#, dotnet, teams, azure
canonical_url: https://thewindev.net/communicating-with-your-bot-from-microsoft-teams
---

In the previous article I showed you how you can create your own bot in C# and talk to it by using the Bot Emulator. Now, I'm going to show you how to communicate with it from Microsoft Teams. There are two approaches, one for production purposes and one that allows you to debug it locally from Visual Studio. We'll go with the second approach, and in the next post I'll show you how to deploy it in Azure.

### Content

1. [Create an Azure Bot](#1-create-an-azure-bot) 
2. [Connect the bot to the MS Teams channel](#2-connect-the-bot-to-the-ms-teams-channel) 

* * *

### 1. Create an Azure Bot

Prerequisites:

- Azure subscription
- MS Teams account
- [ngrok](https://ngrok.com/) 

You'll first need to login into the Azure Portal and create a new Azure Bot.

![Azure bot](https://cdn.hashnode.com/res/hashnode/image/upload/v1632043394287/ZXm6_fu1E.png)

Choose your region or the pricing plan you need, it doesn't have to be the same as in the screenshot below.

![Create bot in Azure](https://cdn.hashnode.com/res/hashnode/image/upload/v1632043474785/PA2nIbl3b.png)

That's it for the first step, our bot is created but we still have to add it to MS Teams

* * *

### 2. Connect the bot to the MS Teams channel

Connecting the bot to MS Teams is not that complicated. We first need to add a Teams channel by going to the "Channels" option and clicking on the Teams icon where it says "Add a featured channel":

![Add Teams channel](https://cdn.hashnode.com/res/hashnode/image/upload/v1632069561385/-odLyWFoh.png)

Now you'll have to click on Save and agree to the terms and conditions:

![Save channel](https://cdn.hashnode.com/res/hashnode/image/upload/v1632069595349/tlxVRZYff.png)

For the next step we need to head over to the "Configuration" tab.

![Configuration tab](https://cdn.hashnode.com/res/hashnode/image/upload/v1632077099918/ACQ6k0dnD.png)

Here we can configure the messaging endpoint where it can send the messages. Usually, this should be another app deployed in Azure, but in order to make things simple we're going to connect the bot to our local machine by using ngrok.

With ngrok you can expose a port from your machine to the internet, and that's what we're going to do by using this command:

```
ngrok http 3978 --host-header=localhost:3978

```

If you remember from the past article, our web app uses the port 3978 for communication, so we need to expose this port to the internet. The command above will generate the following output:

![ngrok](https://cdn.hashnode.com/res/hashnode/image/upload/v1632072621808/4S1OaFOIm.png)

We need to grab the https endpoint and use it in the bot configuration like this, by appending /api/messages to it:

![Bot configuration](https://cdn.hashnode.com/res/hashnode/image/upload/v1632070461233/mvh9enKjE.png)

Now you can go back to channels and click on the Teams icon. This should redirect you to a conversation with the bot inside MS Teams (web or desktop, depends on your preferences), and you can now try sending a message. The only thing left to do is to run the project from Visual Studio that we created on the previous post.

![401 Unauthorized bot](https://cdn.hashnode.com/res/hashnode/image/upload/v1632070564174/vu3ia-sQa.png)

As you can see, the bot isn't replying back and if we look at the ngrok console we see a 401 status code returned by our bot. This happens because we need an app id and secret in order to connect to our Azure Bot. Follow the steps below to:

- create a new Secret
- get the Microsoft App Id of the bot
- paste them inside appsettings.json

![add bot id secret.gif](https://cdn.hashnode.com/res/hashnode/image/upload/v1632072872738/Lr4NDpPS1.gif)

Once you do this you need to run the bot again in Visual Studio and send a message from Teams, now everything should work ok and the bot will echo back whatever you send it.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1632076646670/aCX-tc8ge.png)

The advantage of this approach is that you can debug every message you send by setting breakpoints in Visual Studio, but each time you start ngrok you have to change the URL in Azure as well. In the next post we'll deploy our bot to Azure, as you should do when you send it to production.

If you don't understand something or you're stuck somewhere then please let me know in the comments and I'll try to cover this in a YouTube video.

PS: You can follow me on [Twitter](https://twitter.com/thewindev), so you can get notified instantly when I post something new.


The post [Communicating with your bot from Microsoft Teams](https://thewindev.net/communicating-with-your-bot-from-microsoft-teams) appeared first on [thewindev.net](https://thewindev.net).