Microsoft Bot Framework:跨会话同名
Microsoft Bot Framework: Same name across sessions
我有以下代码尝试在聊天会话期间设置用户名。例如。如果用户写消息 "Hi, my name is Bob",机器人将通过用他的名字问候用户来响应,但前提是该名字被标记为 "name" 实体。
问题是该名称是为当前正在与机器人聊天的每个用户全局设置的,而不是仅为当前用户设置的。换句话说,在这种情况下,机器人将呼叫当前正在与 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>
您将用户名存储在 userData
中,这是合乎逻辑的。
但是在您的测试仪中,您为每个网络聊天用户设置了相同的用户 ID:
user: { id: 'userid' }
因此每个用户都将指向相同的 userData
,因为键是频道名称和用户 ID 的组合(C# 中的 activity.ChannelId
、activity.From.Id
)。
您可以通过为每个网络聊天用户生成唯一标识符来避免这种情况,例如使用 https://www.npmjs.com/package/uuid-random
那么,user: { id: uuid() }
.
此外,如果您需要设置显示名称,请在用户定义中添加name: '...'
:user: { id: uuid(), name: 'You' }
。
我有以下代码尝试在聊天会话期间设置用户名。例如。如果用户写消息 "Hi, my name is Bob",机器人将通过用他的名字问候用户来响应,但前提是该名字被标记为 "name" 实体。
问题是该名称是为当前正在与机器人聊天的每个用户全局设置的,而不是仅为当前用户设置的。换句话说,在这种情况下,机器人将呼叫当前正在与 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>
您将用户名存储在 userData
中,这是合乎逻辑的。
但是在您的测试仪中,您为每个网络聊天用户设置了相同的用户 ID:
user: { id: 'userid' }
因此每个用户都将指向相同的 userData
,因为键是频道名称和用户 ID 的组合(C# 中的 activity.ChannelId
、activity.From.Id
)。
您可以通过为每个网络聊天用户生成唯一标识符来避免这种情况,例如使用 https://www.npmjs.com/package/uuid-random
那么,user: { id: uuid() }
.
此外,如果您需要设置显示名称,请在用户定义中添加name: '...'
:user: { id: uuid(), name: 'You' }
。