English
Français

Blog of Denis VOITURON

for a better .NET world

Microsoft Teams - How to create a Bot using C#

Posted on 2020-12-29

In this article, we will create a Microsoft Teams Bot developed in C#, using the Microsoft SDK. Then we will host it in Azure and publish it in your company’s Teams. This way you can easily adapt the C# code to improve the discussion with your Bot.

The steps are :

  1. Developing a Bot in C#
  2. Test the Bot locally
  3. Publish the Bot in Azure
  4. Create a Bot Channels Registration
  5. Setting up the Teams channel
  6. Create a deployment package for Teams
  7. Install the package in Teams

Developing a Bot in C#

To create a Bot, there are several templates offered by Microsoft. The simplest is to use the Echo template which contains an API that returns the text written by the user.

  1. Install the SDK .Net Core version 3.1 or more.

  2. Install the C# model of the Framework Echo bot.
    dotnet new -i Microsoft.Bot.Framework.CSharp.EchoBot
    
  3. Create a Bot project using the following command.
    dotnet new echobot --name MyEchoBot
    
  4. Open the project with Visual Studio or VSCode and start it in Release mode. Your browser displays a page Your bot is ready! and indicates that it is possible to test the Bot locally, via the emulator.

In Debug mode, an Object reference not set error may occur the first time the test emulator is connected.

Test the Bot locally

  1. Download and install the Bot Framework Emulator.

  2. Press the [Open Bot] button and use the URL http://localhost:3978/api/messages.

  3. You can chat with the Bot: the message “Hello” is displayed by the Bot, in return.

Open Bot

The welcome message and returns are managed via the Echo/EchoBot.cs class.

Publish the Bot in Azure

Once the local tests are completed, you can deploy this code in an Azure web application.

  1. In Azure, create a new Web App that supports the .NET Core 3.1 stack.

  2. In Visual Studio, publish your project from the Build / Publish / Folder menu.

  3. Via FTP, for example, copy the result of the local publication bin\Release\netcoreapp3.1\publish to the Azure folder site\wwwroot.

Once published, you cannot test this project yet, without creating a Bot Channel (below).

Bot Channels Registration

To be able to use this Bot, you have to create a Azure Bot Channels Registration :

  1. In Azure, add a service Bot Channels Registration.
    • Bot handler: MyEchoBot-Channel
    • Princing: F0 (Gratuit)
    • Messaging endpoint: https://YOUR_URL.azurewebsites.net/api/messages
  2. In the section Settings, copy the Microsoft App ID.

  3. In the section Settings / Manage, add a new Client secrets and copy the generated value (App Password).

  4. Modify the file AppSettings.json hosted in the previous Web App and paste these two keys.
{
  "MicrosoftAppId": "1234abab-8a37-1234-85b2-5903ead01234",
  "MicrosoftAppPassword": "JDUDDZN5qD6LGQFT-.6Ozo3HYGR~hysh~c"
}
  1. Restart the Web App to take these configuration settings into account.

  2. Now you can test your Bot, via the section Test in Web Chat in the Bot Channel.

Configure the Teams channel

In Azure Bot Channel, configure the Teams channel.

  1. Via the Channels section, add a Teams channel.

  2. Click the Teams button and save the default settings (Messaging, Calling, Publish).

Create a deployment package for Teams

From your Teams application, install the App Studio application to configure a package to be deployed in your company’s Teams.

  1. Install the application App Studio, from the Apps icon on the left bar.

    Channel Teams

  2. From the Manifest Editor tab, create a new App.

    Channel Teams

  3. Fill in the details fields of the package :
    • Short name: MyApp
    • Full name: My application
    • App ID: générez un nouvel ID
    • Package name: exemple “com.dvoituron.teams.myapp”
    • Version: 1.0.0
    • Descriptions
    • Developer information
    • App URLs: https://dvoituron.com
  4. In the Capabilities section (left), create a new Bot.

    • Select Existing Bot.
    • Encode the Bot ID (MicrosoftAppId) retrieved from the Bot Channels Registration part above and copied into the AppSettings.json file.

    Channel Teams

  5. Download the package to be deployed in your company’s Teams. This file is a ZIP containing the manifest (json) and the icons.

    • In the section Finish / Test and distribute, click on the Download button.

Install the package in Teams

There are several ways to publish a package. Personally, I prefer the one that uses the Office 365 administration interface.

  1. Login to the Teams administration portal : https://admin.teams.microsoft.com/.

  2. In this Teams Admin Center, select the item Teams App / Manage Apps. and add the previously created package.

  3. (Optional) In the Teams App / App Setup section, for the Global strategy, add this new application and make it accessible to all your users.

  4. Each user can add this application through the Applications section of Teams and add it to chat with this Bot.

Resources

Cet article se base sur ces resources :

Languages

EnglishEnglish
FrenchFrançais

Follow me

Recent posts