This article is a step-by-step guide on how to create a Bot from scratch using Microsoft Bot Framework and how to configure it to work with Microsoft Teams.

Prerequisites

  1. Office 365 Tenant
  2. Azure Subscription with Azure Bot Service, App Service
  3. Visual Studio

Prepare the Azure resources

Navigate and log in to Azure Portal. Create a new resource group then add a new Web App Bot (You can type “bot” in the search bar to filter your results).

Image for post

After you click on the Create button, you will be redirected to the configuration page of your resource.

Image for post

Since you added the resource directly from the resource group, some properties will be automatically set to the resource group values (like Resource group, Location, and Subscription).

Let’s fill up the remaining properties as follow:

  • Bot handle: Unique identifier for your bot.
  • Pricing tier: F0 (Fee up to 10k messages without premium channels) or S1 ($0.50 per 1,000 messages and the possibility to create premium channels)
  • App name: This will form the bot’s Endpoint Url.
  • Bot template: Currently it is possible to use the SDK for C## and Node.JS to implement two different templates: Echo Bots (a simple bot that echoes back the user’s message) and Basic Bot (bot template that contains Language Understanding and Bot Analytics services).

To complete the configuration and create the resource click Create and wait a few seconds to allow Azure to complete the task in the background. From the Channels tab under Bot Management, click on the Microsoft Teams icon to add the MS Teams channel to the bot.

Image for post

Create an empty bot

Open Visual Studio and create a new empty .NET Core web application project. It’s possible to start from a Skill Template but it comes with many pre-added features (like CosmosDb, Monitoring, Multilanguage, and many more) which might confuse you and for the purpose of this demo I prefer to start with a barebone bot.

Add dependencies

Open the Package Manager Console and execute the following instruction to add the required dependencies:

Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson
Install-Package Microsoft.Bot.Builder
Install-Package Microsoft.Bot.Builder.Integration.AspNet.Core

Configure the application

Now it’s time to add services to our collection and make them available in our application through dependency injection and configure the middleware pipeline:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Pegasus.Bots;
namespace Pegasus
{
    public class Startup
    {
        public IConfiguration Configuration { get; }
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers().AddNewtonsoftJson();
            services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();
            services.AddTransient<IBot, PegasusBot>();
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseDefaultFiles()
                .UseStaticFiles()
                .UseWebSockets()
                .UseRouting()
                .UseAuthorization()
                .UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
        }
    }
}

#microsoft-teams #bots #microsoft #coding #tutorial

How to create and configure your bot to work in Microsoft Teams
2.35 GEEK