【问题标题】:Microsoft Bot Framework: Same name across sessionsMicrosoft Bot Framework:跨会话同名
【发布时间】:2018-02-08 18:46:35
【问题描述】:

我有以下代码尝试在聊天会话期间设置用户名。例如。如果用户写了消息“嗨,我的名字是 Bob”,机器人将通过用他的名字问候用户来响应,但前提是该名称被标记为“名称”实体。

问题是该名称是为当前与机器人聊天的每个用户全局设置的,而不是仅针对当前用户。换句话说,在这种情况下,机器人将呼叫当前正在与 Bob 聊天的每个用户。

var intents = new builder.IntentDialog({ recognizers: [recognizer] })

    // Check if the message user sent matches LUIS intent "greetingsWithName"
    .matches('greetingsWithName', (session, args) => {
        // Attempt to set content of LUIS entity "name" to variable "name" 
        name = builder.EntityRecognizer.findEntity(args.entities, 'navn');

        // Check whether the name was successfully set or not
        if (!name) {
            session.send('Sorry, I didn't catch your name.')');
        } else {
        session.userData.userName = name.entity;
        session.send('Hi, ' + session.userData.userName);
    });

稍后我在代码中检查用户是否在会话期间将他的名字提供给了机器人。如果是这样,那就用他的名字跟用户说再见吧:

.matches('goodBye', (session) => {
    if (!session.userData.userName) {
        session.send('Good bye!');
    } else {
        session.send('Good bye, ' + session.userData.userName);
    }
})

bot.dialog('/', intents);

以下是启动网络聊天会话的文件中的 HTML 代码:

<!DOCTYPE html>
<html>
  <head>
    <link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet" />
  </head>
  <body>
    <div id="bot"/>
    <script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
    <script>
      BotChat.App({
        directLine: { secret: direct_line_secret },
        user: { id: 'userid' },
        bot: { id: 'botid' },
        resize: 'detect'
      }, document.getElementById("bot"));
    </script>
  </body>
</html>

【问题讨论】:

  • “问题在于,该名称是为当前正在与机器人聊天的每个用户全局设置的,而不是仅针对当前用户”:您在哪个频道上看到了这种行为?模拟器?
  • @NicolasR 在本地 HTML 文件中,该文件使用 BotFramework-WebChat 通过 DirectLine 通道启动机器人。我尝试在其他具有不同 IP/连接到不同网络的设备上传递 HTML 文件,但问题仍然存在。
  • 好吧,我想你在你的 html 中的网络聊天中总是有相同的用户 ID?这样就可以解释问题了……你能添加这个 html 文件的代码吗?
  • @NicolasR 啊,实际上可能就是这样。我使用了sample code from BotFramework-WebChat on GitHub。在主帖中添加了 HTML 代码
  • @NicolasR user: { id: 'userid' } 字段当前设置为 user: { id: 'You'}

标签: node.js session botframework azure-language-understanding


【解决方案1】:

您将用户名存储在userData 中,这是合乎逻辑的。

但是在您的测试器中,您为每个网络聊天用户设置了相同的用户 ID:

user: { id: 'userid' }

所以每个用户都会指向同一个userData,因为键是频道名称和用户 ID 的组合(C# 中的activity.ChannelIdactivity.From.Id)。

您可以通过为每个网络聊天用户生成唯一标识符来避免这种情况,例如使用https://www.npmjs.com/package/uuid-random

在这种情况下,user: { id: uuid() }

另外如果需要设置显示名,在用户定义中添加name: '...'user: { id: uuid(), name: 'You' }

【讨论】:

  • 通过创建一个生成随机用户 ID 并将其传递到 user: { id: createUserId() } 的函数,机器人能够区分会话。谢谢!
  • 有很多解决方案可以改变Id 是的,主要的事实是相同的user.id 被用于accross 调用。很高兴它有帮助
猜你喜欢
  • 2017-12-04
  • 2017-04-27
  • 2020-02-25
  • 1970-01-01
  • 2023-03-11
  • 2017-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多