DEV Community

loading...

Echo Bot .NET Core 3

Elena Salcedo
.NET Developer Me divierto programando bots, trabajo montando Legos. Algún día mis bots montarán Legos.
・2 min read

Interesting news has come with ASP.NET Core 3.x. This project has been built to take advantage all of them; it allows to start at the typical Echo Bot, this time running already on .NET Core 3.

It's important to keep Visual Studio updated, due to .NET Core 3.x it's only supported starting at Visual Studio 2019 version 16.3

Clone this repo.

As a result, we´ll have a solution like this one:

Solution explorer pointing at 4 classes we are going to explain next

Let's see every class on it:

1. Program

HostBuilder is created, as always, using startup class; built and run.

public static void Main(string[] args)
{
            CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>
{
            webBuilder.UseStartup<Startup>();
});

2. Startup

A Bot it's an API, thus, the Startup class configures services and the app's request pipeline.

At ConfigureServices method, EchoBot (an IBot implementation) is registered as transient; AdapterWithErrorHandler (an IBotFrameworkAdapter implementation) is registered as singleton.

// Create the Bot Framework Adapter with error handling enabled.
services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();

// Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
services.AddTransient<IBot,Bot>();

We will detailed both implementations later.
Configure method has changed while migrating to ASP.NET Core 3.x, now we use a routing middleware, it is on charge of analysing every route that comes in a http request.
Currently there is no need to use the whole MVC middleware; we can map each endpoint with the action it should run if the http request matches that route.

app.UseRouting();
app.UseEndpoints(endpoints =>
{
    endpoints.Map("api/messages", async context =>
    {
        var bot = context.RequestServices
                    .GetRequiredService<IBot>();
        var adapter = context.RequestServices
                    .GetRequiredService<IBotFrameworkHttpAdapter>();

        await adapter.ProcessAsync(context.Request, context.Response, bot);
    });
});

It is needed to set the endpoint as "api/messages" if we want to use the Bot Service.
When the action runs, the adapter will process the request.

3. Adapter With Error Handler

BotFrameworkHttpAdapter connects Azure Bot Service with our Bot. Azure Bot Service would be the one that communicates with the channels on which the Bot is published.

The implementation AdapterWithErrorHandler of the adapter, extends its functionality to inform the user if an exception has been thrown.

4. Bot

EchoBot inherits ActivityHandler (an IBot implementation).
ActivityHandler allows to override several methods that correspond with different situations on with the bot can interact with the user.

In this sample, we are managing two of these situations:
When someone joins the conversation, if it is not the bot itself (recipient), the bot greets:

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

When a message is received, on this version of Echo Bot, it "Echoes" exactly what the user has said:

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

Discussion (1)

Collapse
cmendibl3 profile image
Carlos Mendible

Wow! take a look at this: .NET Core and Microsoft Bot Framework you'll see how much things have changed since 2016

Can I update my sample with your code or better yet would you like to collaborate and send me a PR?