【发布时间】: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