我们如何为Microsoft Bot Framework中的不同用户维护不同的会话?

我已经创build了一个使用Bot框架的机器人,并想知道在使用Directline时是否有任何方法为不同的用户维护不同的会话。
在使用Skype通道时,用户会话是为个人用户维护的,我希望在我的directline客户端中实现相同的function。
在我的情况下,以前的会话数据正在被下一个会话数据覆盖。
我正在使用Node.js来构build机器人。

你需要为每个用户开始一个新的对话。

假设你的工作基于Direct Line(WebSockets)示例,与我一样(使用Swagger-JS v2)。

如果您使用您的密码生成令牌,并将该令牌附加到将开始对话的客户端,如下所示:

// 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; }); 

这个客户端只能启动一个对话。 这就是为什么你有重写对话的问题。

为了让客户启动多个对话。 您需要将您的秘密放在客户端授权标头中,如下所示:

 .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; }) 

使用这种方法,每次你使用这个开始新的对话:

 client.Conversations.Conversations_StartConversation(); 

每个对话都会生成一个令牌,您可以为每个用户进行对话。 当然,您需要在应用程序的用户ID和Direct Line的对话ID之间添加映射。

假设您已经知道用户是谁,并拥有userId,这需要通过用户手动或通过OAuth提供。 您需要这样做,因为在构造对话数据时,它将用作行条目的关键字的一部分。 每个条目的id是userId +':'+ conversationId。 您可以通过userId查询您的对话以检索conversationId。 然后,您可以在会话对象中设置conversationId。