How can I send tokens/parameters along with messages?
The server listens to messages via this which does not expose a request object.
server.post('/api/messages', connector.listen());
For my purpose, I need to receive a parameter token form the client app (webchat) at the time of connections.
What is the best approach for this ?
Hey seems like you are trying to intercept the request and you want to forward the request only if you get the right token. If that is the case it's pretty simple. The listen() method returns a closure function back that takes the restify request and response object you can simply wrap the object in your own closure and intercept the request example:
var botListener = connector.listen();
server.post('/api/messages', (req, resp) => {
if (req.query.token == '<WHATEVER YOUR TOKEN IS>') {
botListener(req, resp);
}
});
Let me know if this works for you.
@maxpert this works just fine, thanks!
@mohit-jcc Are you doing this as another level of authentication/authorization for the user with the bot? We are attempting something similar and would appreciate your thoughts/approach in this regard. ~abhishan
@maxpert @abhishan
Well what Im attempting is to bind the token value passed with the session created -
var botListener = connector.listen();
server.post('/api/messages', (req, resp) => {
// How can I save req.query.token here so it's available within the bot session?
botListener(req, resp);
});
var bot = new builder.UniversalBot(connector, function(session){
session.send("Hello");
//this is where i need to ensure that the session is unique for a token
session.beginDialog('login');
});
While the solution by @maxpert works, I am still not completely through with this. Thanks.
@abhishan : please have a look at this - https://github.com/Microsoft/BotBuilder/issues/1996
@mohit-jcc Nops session object won't directly carry the query parameters by default. But you should be able to write a middleware, I can see a set of middlewares where we invoke middlewares in loop. This might be an interesting feature to add though. I can totally see a use-case here. @Stevenic how do you feel about it? Is there a way for middle wares to extend session object?
EDIT Before you ask, you can add middlewares like this:
bot.use([...])
@maxpert Thanks for the heads up. So if this is added as a feature, do you suggest I wait for it or it will be good to use the middleware and have a work around? How soon can the feature be expected? Thanks.
You should be able to write a piece of middleware that extracts the token and saves it to the session object for each request. That's one of the intended reasons for supporting middleware.
Closing issue as it has been answered.