Skip to content

Latest commit

 

History

History
 
 

core-Middleware

Middleware Bot Sample

A sample bot showing how to intercept messages and log them.

Deploy to Azure

Prerequisites

The minimum prerequisites to run this sample are:

  • The latest update of Visual Studio 2015. You can download the community version here for free.
  • The Bot Framework Emulator. To install the Bot Framework Emulator, download it from here. Please refer to this documentation article to know more about the Bot Framework Emulator.

Code Highlights

One of the most common operations when working with conversational history is to intercept and log message activities between bots and users. The Microsoft.Bot.Builder.History namespace provides interfaces and classes for doing this. In particular, the IActivityLogger interface contains the definition of the functionality that a class needs to implement to log message activities.

Check out the DebugActivityLogger implementation of the IActivityLogger interface that writes message activities to the trace listeners only when running in debug.

public class DebugActivityLogger : IActivityLogger
{
    public async Task LogAsync(IActivity activity)
    {
        Debug.WriteLine($"From:{activity.From.Id} - To:{activity.Recipient.Id} - Message:{activity.AsMessageActivity()?.Text}");
    }
}

By default, the BotBuilder library registers a NullActivityLogger with the conversation container, which is a no-op activity logger. Check out the registration in the Autofac container of the DebugActivityLogger of the sample in the Global.asax.cs.

var builder = new ContainerBuilder();
builder.RegisterType<DebugActivityLogger>().AsImplementedInterfaces().InstancePerDependency();
builder.Update(Conversation.Container);

Outcome

You will see the following result in the Debug text pane of the Visual Studio Output window when opening and running the sample solution.

A message received by the bot:

From:default-user - To:ii845fc9l02209hh6 - Message:Hello bot!

A the message sent to the user:

From:ii845fc9l02209hh6 - To:default-user - Message:You sent Hello bot! which was 10 characters

More Information

To get more information about how to get started in Bot Builder for .NET and Conversational history please review the following resources: