gpt4 book ai didi

node.js - Azure Bot Framework(Node.js) - waterfall 在使用网络聊天的对话框中不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 22:22:57 25 4
gpt4 key购买 nike

我有这个代码:

bot.on('conversationUpdate', (message) => {
if (message.membersAdded) {
message.membersAdded.forEach((identity) => {
if (identity.id === message.address.bot.id) {
bot.beginDialog(message.address, 'start');
}
});
}
});
bot.dialog('start', [
(session) => {
var msg = new builder.Message(session);
msg.attachments([
new builder.HeroCard(session)
.title('test')
.buttons([{ title: 'testButton', type: 'imBack', value: 'testButton' }])
]);
builder.Prompts.choice(session, msg, ['testButton']);
},
(session, results) => {
session.send('Reached 2nd function!');
console.dir(results);
var message = results.response.entity;
session.beginDialog('anotherDialog', message);
}
]);

使用 Bot Framework Emulator 可以正常工作。 Bot Framework Emulator Result

但是,使用 Web 聊天(Azure 控制台)无法达到 waterfall 步骤中的第二个功能。 Test in Web Chat Result

Bot Framework Emulator 和 Web Chat 之间的行为有什么区别?

我应该在代码中修改什么?

你有什么想法吗?

  • Node.js 版本:8.10.0
  • Bot Framework 模拟器版本:4.0.15-alpha

最佳答案

我知道您想要做的是让机器人开始对话,而不是等待用户说些什么,这是一个非常常见的目标。不幸的是,这对于内置功能来说并不是一项简单的任务,但幸运的是有一个 blog post explaining how to do it 。该博客文章取自 GitHub issue 中发布的解决方法链接到韩斐链接的那个。

要点是 conversationUpdate 事件不包含足够的信息来允许机器人状态,因此不应从事件处理程序中生成对话框和提示等。您可以通过在客户端代码中生成自己的事件来解决此问题。当然,在 Azure 门户中进行测试时,这可能对您没有帮助。

一般来说,您应该预料到不同 channel 之间存在许多差异,特别是当涉及 channel 产生的事件的性质时。 conversationUpdate 是一个特别有争议的事件,众所周知,它在 Bot Emulator 中的行为与其他 channel 不同。来自博客文章(强调我的):

If you’re using WebChat or directline, the bot’s ConversationUpdate is sent when the conversation is created and the user sides’ ConversationUpdate is sent when they first send a message. When ConversationUpdate is initially sent, there isn’t enough information in the message to construct the dialog stack. The reason that this appears to work in the emulator, is that the emulator simulates a sort of pseudo DirectLine, but both conversationUpdates are resolved at the same time in the emulator, and this is not the case for how the actual service performs.

如果您想避免编写客户端代码,并且确定您的机器人只会在支持 conversationUpdate 事件的 channel 中使用,我可能会为您提供另一种解决方法。尽管博客文章明确指出您不应该使用 conversationUpdate,但在您只需要发送一条消息的情况下,它仍然可以接受。您可以通过在事件处理程序中发送一条消息来模拟提示,然后表现得就像在根对话框中跟进该消息一样。这是概念证明:

bot.on('conversationUpdate', (message) => {
if (message.membersAdded) {
message.membersAdded.forEach((identity) => {
if (identity.id === message.address.bot.id) {
var msg = new builder.Message()
.address(message.address)
.attachments([
new builder.HeroCard()
.title('test')
.buttons([{ title: 'testButton', type: 'imBack', value: 'testButton' }])
]);

bot.send(msg);
}
});
}
});

bot.dialog('/', function (session) {
if (session.message.text == "testButton") {
session.send('Reached 2nd function!');
session.beginDialog('/getStarted');
} else {
builder.Prompts.choice(session, "I didn’t understand. Please choose an option from the list.", ['testButton']);
}
});

请注意,这个概念证明远非稳健。由于根对话框可能会从真实机器人中的许多不同位置访问,因此您可能需要在其中放置一个条件以确保它只响应介绍提示一次,并且您可能还希望生成其他对话框。

关于node.js - Azure Bot Framework(Node.js) - waterfall 在使用网络聊天的对话框中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51856664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com