我们如何在Microsoft Bot框架中为不同用户维护不同的会话



我已经使用 bot Framework 创建了一个机器人,并想知道是否有任何方法在使用 DirectLine
在使用Skype频道时,为个别用户维护了用户会话,我想在直线客户端中获得相同的功能。
在我的情况下,下一个会话数据被下一个会话数据覆盖。
我正在使用 node.js 来构建bot。

您需要为每个用户启动一个新对话。

假设您像我一样基于直接行(WebSocket)样本(使用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之间添加映射。

假设您已经知道用户是谁并且拥有用户ID,则需要手动或通过OAuth提供用户。您需要这一点,因为当构建对话时,它被用作行条目的密钥的一部分。每个条目的ID是UserId ':' ConvertyId。您可以通过用户ID查询对话以检索对话ID。然后,您可以在会话对象中设置对话ID。

最新更新