How do we maintain different session for different users in Microsoft Bot Framework?

1k views Asked by At

I have created a bot using Bot framework and would like to know if there is any way to maintain different sessions for different users while using directline.
While using the skype channel, user session is maintained for individual users and I would like to achieve the same feature in my directline client.
In my case the previous session data is being overridden by the next session data.
I am using Node.js to build bots.

2

There are 2 answers

0
D4RKCIDE On

Assuming that you already know who the user is and have the userId, which needs to be provided from the user manually or through OAuth. You need that, because when the conversationdata is constructed, it's used as part of the key for the row entry. The id of each entry is the userId + ':' + conversationId. You could query you conversations by userId to retrieve the conversationId. Then you could set the conversationId in the session object.

3
Ghassen Rjab On

You need to start a new conversation for each user.

Assuming you based your work on Direct Line (WebSockets) sample as I did (It uses Swagger-JS v2).

If you generate a token with your secret and attach that token to the client that will be starting conversations, like this :

// Obtain a token using the Direct Line secret
return rp({
    url: 'https://directline.botframework.com/v3/directline/tokens/generate',
    method: 'POST',
    headers: {
        'Authorization': 'Bearer ' + directLineSecret
    },
    json: true
}).then(function (response) {
    // Then, replace the client's auth secret with the new token
    var token = response.token;
    client.clientAuthorizations.add('AuthorizationBotConnector', new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + token, 'header'));
    return client;
});

This client will only be able to start only one conversation. That is why you have the override conversation problem.

In order to let the client start multiple conversations. You need to put your secret in the client Authorization header, like this :

.then(function (client) {
    // No need to obtain a token using the Direct Line secret
    // Use the Direct Line secret directly
    client.clientAuthorizations.add('AuthorizationBotConnector', new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + directLineSecret, 'header'));
    return client;
})

Using this method, every time you start a new conversation using this :

client.Conversations.Conversations_StartConversation();

A token will be generated for every conversation and you can have a conversation for each user. Of course you need to add mapping between you app's users IDs and Direct Line's conversations IDs.