Search code examples
c#botframeworkmiddlewareasp.net-core-3.1

How to intercept messages in Bot Framework v4 for logging, using C# Core 3.1


We found this documentation explaining how to intercept messages in bot framework v3: https://learn.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-middleware?view=azure-bot-service-3.0

We want to do exactly that, only in v4 of the bot framework.

We could not find documentation for intercepting messages for bot framework v4 here: https://learn.microsoft.com/en-us/azure/bot-service/?view=azure-bot-service-4.0

Is there a way to intercept the messages in bot framework v4, so we can log them?

We do already log the user messages in the OnMessageActivityAsync method. So we only need to log the messages that are sent from the bot to the user.

Thanks in advance.


Solution

  • First we tried creating our own middleware, but we could not get it to work.

    After some more research we decided to use turnContext.OnSendActivities in the MyBot.OnTurnAsync method. Like this:

    turnContext.OnSendActivities(async (sendContext, activities, nextSend) =>
    {
      // Log activities here
      return await nextSend();
    });
    

    For us this solution is perfect and way easier to implement then creating custom middleware. Thanks anyway.