Botframework-sdk: [nodejs] Access to the Session in a middleware

Created on 13 Jan 2017  路  7Comments  路  Source: microsoft/botframework-sdk

Is it possible to access the Session in a middleware, before sending a message to the user?

For example, in this middleware, the Session is accessible in the botbuilder function, but not in the receive function (makes sense) nor in the send function:

let middleware: BotBuilder.IMiddlewareMap = {
    botbuilder: (session: BotBuilder.Session, next: Function) => {
        next();
    },
    receive: (event: BotBuilder.IEvent, next: Function) => {
        next();
    },
    send: (event: BotBuilder.IEvent, next: Function) => {
        next();
    }
};

I want to use a middleware to convert to speech all the messages the bot sends to the users, but I am not able to access the user's preferred language because it is part of the session. Any ideas?

Most helpful comment

Would be great to have session available in middleware.

All 7 comments

You need to manually hook the sessions onSend() method:

    public middleware(): IMiddlewareMap {
        return {
            botbuilder: (session, next) => {
                // Hook onSend
                let _onSend = session.options.onSend;
                session.options.onSend = function (messages, done:) {
                        // Process messages...

                       // Call original function
                      _onSend(messages, done);
                };

                // Continue
                next();
            }
        };
    }

closing but please feel free to comment.

Thanks @Stevenic. Two comments:

  1. session.options is not accessible (see typings)
  2. It's a bit tricky and forces us to process an array of messages. I think it would be better to include it officially in the IMiddlewareMap.

@Stevenic any chance you could add session.options to the typings?

This would be really useful for us too.
What's the logic behind the decision to not have session on send hook?

Would be great to have session available in middleware.

Any updates on this? I tried but still not access session from within the middleware.

Was this page helpful?
0 / 5 - 0 ratings