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?
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:
session.options is not accessible (see typings)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.
Most helpful comment
Would be great to have session available in middleware.