Botframework NodeJS - session.userData 首次打开聊天时未定义



我想在用户第一次打开包含聊天机器人的页面时用他们的名字问候他们(我将其用作网络聊天(。所以我需要这样的东西:欢迎回来乔。

我实现了这个:

bot.on('conversationUpdate', function (message) {
if (message.membersAdded) {
message.membersAdded.forEach(function (identity) {
if (identity.id === message.address.bot.id) {
bot.beginDialog(message.address, 'WelcomeDialog');
}
});
}
});

在欢迎对话框中,我有这个:

bot.dialog('WelcomeDialog',
(session) => {
session.sendTyping();
if (!session.userData.PreferredName) {
//write logic to get user preferred name to call him/her with
session.userData.PreferredName = "new name"
}
else if (!session.conversationData.loggedBefore) {
session.send(`Welcome back ${session.userData.PreferredName}`)
session.conversationData.loggedBefore = true;
}
session.endDialog()
})

但是,当我在网上聊天中运行它时,session.userData总是空的,即使仅在从conversationUpdate调用的WelcomeDialog中填写它。但是,如果我调用另一个对话框并尝试检查 session.userData,它将包含我添加的值。所以基本上,当我第一次打开机器人时,session.userData 总是为空的,否则它可以正常工作。

此外,它在模拟器中工作正常,只是不能在网络聊天中工作。知道如何解决这个问题吗?

这是一个已知问题。 此博客文章介绍了一种解决方法:https://blog.botframework.com/2018/07/12/how-to-properly-send-a-greeting-message-and-common-issues-from-customers/

基本上,它归结为连接器服务是发送对话更新活动的事实。 当conversationUpdate进入机器人时,它没有正确的 userId,因此机器人无法构造数据包。 如果在初始化 BotChat 后从客户端发送事件,则该事件将具有正确的 userId,并且一切都将按预期工作。 (它在模拟器中工作的原因是模拟器不使用连接器服务,而是发送conversationUpdate本身。

最新更新