2

I have included middleware to log the chats in firebase db. Now I am able to call the middleware once after the user sends some text to the bot then after bot sends the data to the user. In the previous version bot framework v3 the activity payload will change the message.text and from and to as user-bot/bot-user. But now in bot framework v4 when the bot sends message/response to the user I can't find the data sent anywhere in the payload. Is there a way to save the data from bot-user. Please suggest me any ideas.

tdurnford
  • 3,632
  • 2
  • 6
  • 15

1 Answers1

2

If you want to get in the middle of activities being sent outwards from the bot from a piece of middleware you would want to hook into the ITurnContext::OnSendActivities like so:

public class MyActivityLoggingMiddleware : IMiddleware
{
    public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default(CancellationToken))
    {
        // log incoming activity from turnContext.Activity here

        // Hook the turn context's OnSendActivities
        turnContext.OnSendActivities(HandleSendActivities);

        await next(cancellationToken);
    }

    private async Task<ResourceResponse[]> HandleSendActivities(ITurnContext turnContext, List<Activity> activities, Func<Task<ResourceResponse[]>> next)
    {
        // log activities being sent here

        return await next();
    }
}
Drew Marsh
  • 33,111
  • 3
  • 82
  • 100