Forem

Elena Salcedo
Elena Salcedo

Posted on • Originally published at github.com

1 4

Echo Bot .NET Core 3

ASP.NET Core 3.x ha llegado con novedades muy interesantes, con el fin de aprovecharlas también en Bot Framework, se ha creado este Echo Bot que nos permitirá comenzar desde el típico Echo Bot ya migrado a la nueva versión del framework.
Es importante que hayamos actualizado recientemente VS, ya que es a partir de la versión 16.3 de Visual Studio 2019 cuando .NET Core 3.x está soportado.

Clona este proyecto.

Como resultado tendremos una solución como la que se muestra abajo. Veremos uno a una las clases que nos vienen dadas.
Solution explorer pointing at 4 classes we are going to explain next

1. Program

El HostBuilder se crea instanciando la clase startup; se construye y se levanta.

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

El bot no es más que una API, la aplicación se describe en base a las configuraciones que creamos en la clase Startup.
En el método ConfigureServices se registra como transient el EchoBot como implementación de IBot; y se registra como Singleton el AdapterWithErrorHandler como implementación de IBotFrameworkAdapter.

// 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>();

Detallamos ambas implementaciones más adelante.
En el método Configure al haber migrado a ASP.NET Core 3.x, ahora se usa el middleware de routing, encargado de analizar la ruta de la petición http que nos llegará.
Actualmente no hay necesidad de cargar el middleware completo de MVC; podremos mapear endpoints, y la acción que se ejecutará cuando la petición http ataque esa ruta.

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);
    });
});

En este caso, estamos diciendo que cuando recibamos una petición en el endpoint “..api/messages”, pidamos al adapter que procese esa petición.
Es necesario que el endpoint sea api/messages, si queremos utilizar el Bot Service, el endpoint ha de ser exactamente ese.

3. Adapter With Error Handler

El BotFrameworkHttpAdapter es el encargado de interconectar el Bot Service con nuestro bot. A grandes rasgos, será el nexo entre nuestra aplicación de bot y los canales en los que está publicado.
La implementación AdapterWithErrorHandler del adapter, extiende su funcionalidad para informar al usuario de que ha habido un error cada vez que se produzca una excepción no controlada.

4. Bot

EchoBot hereda de ActivityHandler y ésta implementa la interfaz IBot.
El activity handler nos permite sobreescribir varios métodos que se corresponderán con diferentes situaciones con las que nos encontraremos con el usuario.
En este caso, estamos manejando dos de estas situaciones:
Cuando alguien se une a la conversación, si no es el bot (Recipient), se imprime el saludo:

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);
        }
    }
}

Cuando se recibe un mensaje, en esta versión de echo bot, le devolveremos al usuario exactamente lo mismo que ha dicho:

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

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry 👀

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay