botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

How can I send tokens/parameters along with messages?

Open mohit-jcc opened this issue 9 years ago • 9 comments

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 ?

mohit-jcc avatar Jan 03 '17 15:01 mohit-jcc

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 avatar Jan 03 '17 17:01 maxpert

@maxpert this works just fine, thanks!

mohit-jcc avatar Jan 03 '17 18:01 mohit-jcc

@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

abhishan avatar Jan 03 '17 19:01 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.

mohit-jcc avatar Jan 06 '17 11:01 mohit-jcc

@abhishan : please have a look at this - https://github.com/Microsoft/BotBuilder/issues/1996

mohit-jcc avatar Jan 09 '17 14:01 mohit-jcc

@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 avatar Jan 10 '17 03:01 maxpert

@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.

mohit-jcc avatar Jan 11 '17 10:01 mohit-jcc

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.

Stevenic avatar Feb 23 '17 01:02 Stevenic

Closing issue as it has been answered.

stevengum avatar Apr 09 '17 00:04 stevengum