问题描述
我已经使用 Bot框架创建了一个漫游器,并想知道在使用 directline 时是否有任何方法可以为不同的用户维护不同的会话.
在使用Skype通道时,将为单个用户维护用户会话,我想在我的Directline客户端中实现相同的功能.
对于我来说,上一个会话数据将被下一个会话数据覆盖.
我正在使用 Node.js 来构建机器人.
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.
推荐答案
您需要为每个用户启动一个新对话.
You need to start a new conversation for each user.
假设您的工作基于直接线(WebSockets) 样例(它使用Swagger-JS v2).
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.
为了让客户端开始多个对话.您需要在客户端的Authorization标头中放置您的机密,如下所示:
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();
将为每个对话生成一个令牌,并且您可以为每个用户建立一个对话.当然,您需要在应用程序的用户ID和Direct Line的对话ID之间添加映射.
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.
这篇关于我们如何在Microsoft Bot Framework中为不同的用户维护不同的会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!