3

I have implemented a middleware in sdk V4 Bot to intercept each message between bot & user and log that custom mongo Db. I am trying to implement similar concept for Bot built using SDK v4. Looks like I can use following piece of code to add a middleware but, not sure how to differentiate message between bot to user & user to bot.

V3 bot code

bot.use({
    botbuilder: function (session, next) {
        logUserConversation(session)
        next()
    },
    send: function (event, next) {
        logBotsConversation(event)
        next()
    }
})

V4 bot code for Middleware

botAdapter.use(async (turnContext, next) => {
    // How to find which messages/activity object is from user to bot

    await next();
    // How to find which messages/activity object is from bot to user.
});

1 Answer 1

7

So the function that you pass to .use represents a piece of middleware that can do pre and post processing of an incoming activity. You access the "current" activity from the turn context via the turnContext.Activity property. Those activities can either be sent from the user or from some other system that sends them via the DirectLine API to the bot (assuming you're using the Bot Framework Service).

Outgoing activities, that is activities that are sent from the bot in response to the incoming activities, can also be intercepted by middleware, but the middleware needs to involve itself in the sending of those activities more explicitly. It does this by registering a handler with the turn context using the onSendActivities API.

This all comes together to look a little something like this:

botAdapter.use(async (turnContext, next) => {
    // pre-processing of the current incoming activity
    console.log(`Processing activity ${turnContext.activity.id} starting... `);

    // hook up a handler to process any outgoing activities sent during this turn
    turnContext.onSendActivities(async (sendContext, activities, nextSend) => {
       // pre-processing of outgoing activities

       await nextSend();       

       // post-processing outgoing activities
    });

    await next();

    // post-processing of the current incoming activity 
    console.log(`Processing activity ${turnContext.activity.id} finishing. `);    

});

The one thing to note is that outgoing activity handlers can be invoked 0..* times as they are basically triggered by downstream logic calling turnContext.sendActivit[y|ies]. So if, during the turn, there are multiple activities sent, your handler will be invoked for each batch.

7
  • I am not able to capture activities going from Bot to User using above logic. turnContext.activity.text . - gives text that user typed.
    – vijay
    Jan 28, 2019 at 17:15
  • Inside the closure passed to onSendActivities you need to make sure you're using the sendContext variable, not turnContext.
    – Drew Marsh
    Jan 28, 2019 at 20:34
  • I did that and it didn't work. Ex-I sent text 'help' to bot & after first console.log from above code I did ( turnContext.activity.text ) which resulted into ''help". Sounds good. when Bots reply was ready which is 'I can help you get your task done.' to capture this inside turnContext.onSendActivities(.....) I did sendContext.activity.text which again resulted into 'help' which is what user had typed.
    – vijay
    Jan 30, 2019 at 17:10
  • It looks like activities object has data that's from Bot to User.
    – vijay
    Jan 30, 2019 at 17:25
  • I'm sorry, you're looking at the turnContext.activity... that's always the incoming activity. In the scope of the onSendActivities the outgoing activities are in the activities parameter which is an array of Activity.
    – Drew Marsh
    Jan 30, 2019 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.