Microsoft Bot 在 WebChat 中显示不必要的重复消息?

2024-04-17

当用户第一次访问我的聊天室时,他们会收到欢迎消息,并立即被要求提供他们的名字。一旦用户输入他们的名字,就会出现欢迎消息,并再次显示输入名字的文本提示。只有在他们第二次输入名字后,机器人才会继续处理下一个有关姓氏的问题。

此外,当用户最终在第一次聊天中输入他们的名字和姓氏并再次返回同一个聊天时,仅当用户提供一些输入机器人发送欢迎返回消息时,才会显示欢迎消息和名字提示。

这是重现此问题所需的最少代码。 让restify = require('restify'); 让构建器 = require('botbuilder');

// Setup Restify Server
let server = restify.createServer();

let connector = new builder.ChatConnector({
    appId: process.env.MICROSOFT_APP_ID,
    appPassword: process.env.MICROSOFT_APP_PASSWORD
});

server.post('/api/messages', connector.listen());

let bot = new builder.UniversalBot(connector);

// Define default Dialog
bot.dialog('/', [
  function (session) {
    if (!session.userData.firstName) {
      // firstName used as a flag to assess whether the user is coming back
      // or new user - we can use it because the very first dialog is asking them
      // for their first name.
      session.send("Welcome!");
    } else {
      session.send("Welcome back %s!", session.userData.firstName);
    }

    session.beginDialog('mainConversationFlow');
  }
])

bot.dialog('mainConversationFlow', [
    function(session, args, next) {
      if (!session.userData.firstName)
        session.beginDialog('getFirstName');
      else 
        next();
    },
    function(session, args, next) {
      if(!session.userData.lastName) 
        session.beginDialog('getLastName');
      else
        next();
    }, 
    function(session, args, next) {
      session.endConversation('The End');
    }
])

bot.dialog('getFirstName', [
  function(session, args) {
    let msg = "What's your first name?";

    builder.Prompts.text(session, msg);
  },
  function(session, results) {
    session.userData.firstName = results.response.trim();
    session.endDialog();
  }
])

bot.dialog('getLastName', [
  function(session, args) {
    let msg = builder.Message.composePrompt(session,
      ["Hi %s, what's your last name?"], session.userData.firstName);

    builder.Prompts.text(session, msg);
  },
  function(session, results) {
    session.userData.lastName = results.response.trim();

    session.endDialog();
  }
])

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

server.listen(process.env.port || process.env.PORT || 3978, function () {
   console.log('%s listening to %s', server.name, server.url); 
})

首次运行应用程序时,应向用户显示欢迎消息并询问其名字。一旦他们输入名字,机器人就应该立即转到下一个关于姓氏的问题,并且在用户回答后机器人应该结束对话。

当用户输入他们的名字和姓氏并返回时,机器人应该只显示欢迎回来消息并结束对话。

这是我使用 BotFramework-WebChat 的客户端代码片段:

let directLineSecret = 'mysecret';
let directLineSpecUrl = 'https://docs.botframework.com/en-us/restapi/directline3/swagger.json';

BotChat.App({
  directLine: {secret: directLineSecret},
  user: { id: localStorage.getItem('email')},
  bot: { id: 'testbot' },
  resize: 'detect'
}, this.botChatContainer.nativeElement);

let directLineClient = rp(directLineSpecUrl)
  .then(function(spec) {
    return new Swagger({
      spec: JSON.parse(spec.trim()),
      usePromise: true
    });
  })
  .then(function(client) {
    return rp({
      url: 'https://directline.botframework.com/v3/directline/tokens/generate',
      method: 'POST',
      headers: {
        'Authorization': 'Bearer ' + directLineSecret
      },
      json: true
    }).then(function(response) {
      let token = response.token;
      client.clientAuthorizations.add('AuthorizationBotConnector', new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + token, 'header'));
      return client;
    });
  })
  .catch(function(err) {
    console.error('Error initializing DirectLine client', err);
    throw err;
  });

这些屏幕截图是在 dev.botframework.com 测试窗口中拍摄的。然而,同样的行为也适用于我使用 WebChat 的网络应用程序。

你能帮我解决这个问题吗?

Update Logs:

2018-01-13 19:29:46.876 INFO - Container logs 2018-01-13T19:29:45.006255595Z
UserConversation message: , user: undefined 2018-01-13T19:29:45.006543896Z {"typ
e":"conversationUpdate","timestamp":"2018-01-13T19:29:44.4543348Z","membersAdded
":[{"id":"mybot@j4OUxKYkEpQ","name":"MyBot"}],"text":"","attachments":[],"entiti
es":[],"address":{"id":"C27bFaQ1Ohr","channelId":"webchat","user":{"id":"8f8399d
115774c86b83634bf7086f354"},"conversation":{"id":"8f8399d115774c86b83634bf7086f3
54"},"bot":{"id":"mybot@j4OUxKYkEpQ","name":"MyBot"},"serviceUrl":"https://webch
at.botframework.com/"},"source":"webchat","agent":"botbuilder","user":{"id":"8f8
399d115774c86b83634bf7086f354"}} 
2018-01-13T19:29:45.006562196Z ----------------------------  
2018-01-13T19:29:45.937402126Z Incoming message:
2018-01-13T19:29:45.937559026Z ----------------------------
2018-01-13T19:29:46.291227879Z Outgoing message: Welcome!
2018-01-13T19:29:46.291465679Z {"type":"message","agent":"botbuilder","source":"
webchat","address":{"id":"C27bFaQ1Ohr","channelId":"webchat","user":{"id":"8f839
9d115774c86b83634bf7086f354"},"conversation":{"id":"8f8399d115774c86b83634bf7086
f354"},"bot":{"id":"mybot@j4OUxKYkEpQ","name":"MyBot"},"serviceUrl":"https://web
chat.botframework.com/"},"text":"Welcome!"} 
2018-01-13T19:29:46.291479179Z ----------------------------  
2018-01-13T19:29:46.291708779Z Outgoing message:
What's your first name? 2018-01-13T19:29:46.291740980Z {"text":"What's your
first name?","inputHint":"expectingInput","type":"message","address":{"id":"C27b
FaQ1Ohr","channelId":"webchat","user":{"id":"8f8399d115774c86b83634bf7086f354"},
"conversation":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":"mybot@j4OU
xKYkEpQ","name":"MyBot"},"serviceUrl":"https://webchat.botframework.com/"}}
2018-01-13T19:29:46.291759880Z  ----------------------------  
2018-01-13 19:29:56.876 INFO - Container logs 2018-01-13T19:29:53.471348251Z
UserConversation message: , user: undefined 2018-01-13T19:29:53.471657052Z {"typ
e":"conversationUpdate","timestamp":"2018-01-13T19:29:53.3233269Z","membersAdded
":[{"id":"AvfenKwcS1o","name":"You"}],"text":"","attachments":[],"entities":[],"
address":{"id":"DbpPwxf2m7T","channelId":"webchat","user":{"id":"8f8399d115774c8
6b83634bf7086f354"},"conversation":{"id":"8f8399d115774c86b83634bf7086f354"},"bo
t":{"id":"mybot@j4OUxKYkEpQ","name":"MyBot"},"serviceUrl":"https://webchat.botfr
amework.com/"},"source":"webchat","agent":"botbuilder","user":{"id":"8f8399d1157
74c86b83634bf7086f354"}} 
2018-01-13T19:29:53.471672552Z ----------------------------  
2018-01-13T19:29:53.515781796Z UserConversation
message: John, user: You 2018-01-13T19:29:53.515792596Z {"type":"message","times
tamp":"2018-01-13T19:29:53.1827153Z","textFormat":"plain","text":"John","entitie
s":[{"type":"ClientCapabilities","requiresBotState":true,"supportsTts":true,"sup
portsListening":true}],"textLocale":"en","sourceEvent":{"clientActivityId":"1515
871784086.6213104132628995.0"},"attachments":[],"address":{"id":"8f8399d115774c8
6b83634bf7086f354|0000002","channelId":"webchat","user":{"id":"AvfenKwcS1o","nam
e":"You"},"conversation":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":"
mybot@j4OUxKYkEpQ","name":"MyBot"},"serviceUrl":"https://webchat.botframework.co
m/"},"source":"webchat","agent":"botbuilder","user":{"id":"AvfenKwcS1o","name":"
You"}} 
2018-01-13T19:29:53.515801796Z ----------------------------
2018-01-13T19:29:53.545361425Z Incoming message: John
2018-01-13T19:29:53.545373525Z ----------------------------
2018-01-13T19:29:53.802571982Z Outgoing message: Welcome!
2018-01-13T19:29:53.802593382Z {"type":"message","agent":"botbuilder","source":"
webchat","textLocale":"en","address":{"id":"8f8399d115774c86b83634bf7086f354|000
0002","channelId":"webchat","user":{"id":"AvfenKwcS1o","name":"You"},"conversati
on":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":"mybot@j4OUxKYkEpQ","n
ame":"MyBot"},"serviceUrl":"https://webchat.botframework.com/"},"text":"Welcome!
"} 
2018-01-13T19:29:53.802600382Z  ----------------------------
2018-01-13T19:29:53.802602782Z Outgoing message: What's your first name?
2018-01-13T19:29:53.802604982Z {"text":"What's your first name?","inputHint":"ex
pectingInput","type":"message","address":{"id":"8f8399d115774c86b83634bf7086f354
|0000002","channelId":"webchat","user":{"id":"AvfenKwcS1o","name":"You"},"conver
sation":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":"mybot@j4OUxKYkEpQ
","name":"MyBot"},"serviceUrl":"https://webchat.botframework.com/"},"textLocale"
:"en"} 
2018-01-13T19:29:53.802610082Z  ----------------------------  
2018-01-13 19:30:01.878 INFO - Container logs 2018-01-13T19:29:57.806548081Z
UserConversation message: John, user: You 2018-01-13T19:29:57.809735285Z {"type"
:"message","timestamp":"2018-01-13T19:29:57.6990081Z","textFormat":"plain","text
":"John","textLocale":"en","sourceEvent":{"clientActivityId":"1515871784086.6213
104132628995.2"},"attachments":[],"entities":[],"address":{"id":"8f8399d115774c8
6b83634bf7086f354|0000005","channelId":"webchat","user":{"id":"AvfenKwcS1o","nam
e":"You"},"conversation":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":"
mybot@j4OUxKYkEpQ","name":"MyBot"},"serviceUrl":"https://webchat.botframework.co
m/"},"source":"webchat","agent":"botbuilder","user":{"id":"AvfenKwcS1o","name":"
You"}} 
2018-01-13T19:29:57.809755085Z ----------------------------
2018-01-13T19:29:57.828015903Z Incoming message: John
2018-01-13T19:29:57.828028303Z ----------------------------
2018-01-13T19:29:58.122706697Z Outgoing message: Got response as: John
2018-01-13T19:29:58.122972998Z {"type":"message","agent":"botbuilder","source":"
webchat","textLocale":"en","address":{"id":"8f8399d115774c86b83634bf7086f354|000
0005","channelId":"webchat","user":{"id":"AvfenKwcS1o","name":"You"},"conversati
on":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":"mybot@j4OUxKYkEpQ","n
ame":"MyBot"},"serviceUrl":"https://webchat.botframework.com/"},"text":"Got
response as: John"} 
2018-01-13T19:29:58.122997998Z ----------------------------
2018-01-13T19:29:58.123366398Z Outgoing message: Hello John! What is your last
name? 2018-01-13T19:29:58.123377798Z {"text":"Hello John! What is your last name
?","inputHint":"expectingInput","type":"message","address":{"id":"8f8399d115774c
86b83634bf7086f354|0000005","channelId":"webchat","user":{"id":"AvfenKwcS1o","na
me":"You"},"conversation":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":
"mybot@j4OUxKYkEpQ","name":"MyBot"},"serviceUrl":"https://webchat.botframework.c
om/"},"textLocale":"en"} 
2018-01-13T19:29:58.123395698Z ----------------------------  
2018-01-13T19:30:00.551811524Z UserConversation
message: Doe, user: You 2018-01-13T19:30:00.552098924Z {"type":"message","timest
amp":"2018-01-13T19:30:00.4252782Z","textFormat":"plain","text":"Doe","textLocal
e":"en","sourceEvent":{"clientActivityId":"1515871784086.6213104132628995.4"},"a
ttachments":[],"entities":[],"address":{"id":"8f8399d115774c86b83634bf7086f354|0
000008","channelId":"webchat","user":{"id":"AvfenKwcS1o","name":"You"},"conversa
tion":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":"mybot@j4OUxKYkEpQ",
"name":"MyBot"},"serviceUrl":"https://webchat.botframework.com/"},"source":"webc
hat","agent":"botbuilder","user":{"id":"AvfenKwcS1o","name":"You"}}
2018-01-13T19:30:00.552114924Z ----------------------------
2018-01-13T19:30:00.590356662Z Incoming message: Doe
2018-01-13T19:30:00.590371762Z ----------------------------
2018-01-13T19:30:00.857187129Z Outgoing message: Got last name as: Doe
2018-01-13T19:30:00.857206229Z {"type":"message","agent":"botbuilder","source":"
webchat","textLocale":"en","address":{"id":"8f8399d115774c86b83634bf7086f354|000
0008","channelId":"webchat","user":{"id":"AvfenKwcS1o","name":"You"},"conversati
on":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":"mybot@j4OUxKYkEpQ","n
ame":"MyBot"},"serviceUrl":"https://webchat.botframework.com/"},"text":"Got last
name as: Doe"} 
2018-01-13T19:30:00.857220329Z  ----------------------------
2018-01-13T19:30:00.857222929Z Outgoing message: End of "mainConversationFlow"
dialog. 2018-01-13T19:30:00.857225229Z {"type":"message","agent":"botbuilder","s
ource":"webchat","textLocale":"en","address":{"id":"8f8399d115774c86b83634bf7086
f354|0000008","channelId":"webchat","user":{"id":"AvfenKwcS1o","name":"You"},"co
nversation":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":"mybot@j4OUxKY
kEpQ","name":"MyBot"},"serviceUrl":"https://webchat.botframework.com/"},"text":"
End of \"mainConversationFlow\" dialog."} 
2018-01-13T19:30:00.857230729Z ----------------------------

我用于日志的代码:

const logUserConversation = (event) => {
    console.log('UserConversation message: ' + event.text + ', user: ' + event.address.user.name);
    console.log(JSON.stringify(event));
    console.log('----------------------------');
};

const logIncomingMessage = function (session) {
    console.log('Incoming message: ' + session.message.text);
    console.log(JSON.stringify(session.user));
    console.log('----------------------------');
};

const logOutgoingMessage = function (event) {
    console.log('Outgoing message: ' + event.text);
    console.log(JSON.stringify(event));
    console.log('----------------------------');
};

bot.use({
    receive: function (event, next) {
        logUserConversation(event);
        next();
    },
    botbuilder: function (session, next) {
        logIncomingMessage(session);
        next();
    },
    send: function (event, next) {
        logOutgoingMessage(event);
        next();
    }
})

实际上,当机器人连接器第一次连接到机器人服务器时,机器人首先加入对话,所以conversationUpdate将为您的机器人触发事件,该事件不包含session.userData object.

一旦用户在机器人网络聊天中输入某些内容,何时会出现第二次conversationUpdate对于用户来说。这时,bot开始Dialog'\'在 - 的里面conversationUpdate会话中的事件包含session.userData目的。

您可以添加以下中间件来检测此问题:

bot.use({
    receive: function (event, next) {
        console.log(event)
        next();
    },
    send: function (event, next) {
        console.log(event)
        next();
    }
});

不幸的是,我找不到让机器人触发的方法conversationUpdate网络聊天初始化时为用户提供。

update

您可以利用网站上的webchat js sdk https://github.com/Microsoft/BotFramework-WebChat#easy-in-your-non-react-website-run-web-chat-inline and 反向渠道机制 https://learn.microsoft.com/en-us/bot-framework/nodejs/bot-builder-nodejs-backchannel达到你的要求。

网站客户端:

//定义一个用户

const user = {id:'userid',name:'username'};

const botConnection = new BotChat.DirectLine({
        domain: params['domain'],
        secret: '<secrect>',
        webSocket: params['webSocket'] && params['webSocket'] === 'true' // defaults to true
      });
botConnection .postActivity({ type: "event", from: user, name: "ConversationUpdate", value: "" }) .subscribe(id => console.log("Conversation updated"));
BotChat.App({
    botConnection: botConnection,
    bot: bot,
    user: user,
    resize: 'detect'
}, document.getElementById("BotChatGoesHere"));

机器人服务器:

bot.on('event',(event)=>{
  console.log(event)
  if(event.name==='ConversationUpdate'){
    bot.beginDialog(event.address, '/');
  }
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Microsoft Bot 在 WebChat 中显示不必要的重复消息? 的相关文章

随机推荐

  • 更改 MySQL 表以添加外键约束会导致错误

    问题 为什么在尝试更改具有外键约束的表时会出现错误 Details 我有1张桌子 HSTORY我将其用作所有其他特定历史表 即USER HISTORY BROWSER HISTORY PICTURE HISTORY 我还包括了PICTURE
  • 是否可以使用 __rmod__ 覆盖 str 的 % 行为?

    我想做 x doSomething y 对于任何 x 和任何 y 来说 这都很容易做到 参见下面的代码 但 x 是 str 的情况除外 有没有什么方法 例如添加特殊方法或引发特定错误 导致旧式字符串格式化失败 类似于 1 doSomthin
  • 实体框架多重聚合性能

    我有一个关于实体框架查询构建的问题 Schema 我有一个这样的表结构 CREATE TABLE dbo DataLogger ID bigint IDENTITY 1 1 NOT NULL ProjectID bigint NULL CO
  • 使用 Ransack 搜索值数组

    我是 Ransack 的新手 我遇到了 Ransack 未明确涵盖的案例 我基本上试图搜索一个值 但搜索到的值包含在一个数组中 CODE 最后还有这一段user rep code list cont这是用户的默认数组属性 目前看起来像这样
  • 如何在javascript函数中获取Table的所有td值

    我有一个数据表 其中显示子行展开折叠功能 它运行良好 但我想获取表的最后一个 td 的内容 现在我创建了一个函数 该函数在数据表中放置一些硬编码值扩大的地方 在那个地方我想得到那些 td 值 这是我发布的代码
  • 如何向JTable中插入数据?

    我编写此代码用于在表中显示字符串 但它没有显示并且没有任何效果 有什么问题吗 public pamnel initComponents String columnNames First Name Last Name Sport of Yea
  • ASP.NET MVC 和 Web 服务

    向我的 ASP NET MVC 项目添加 Web 服务是否会破坏 MVC 的整个概念 该 Web 服务 WCF 依赖于我的 MVC 项目中的模型层来与后端进行通信 因此在我看来 它需要成为 MVC 解决方案的一部分 我应该将其添加到控制器层
  • 让 Scala 在 .net 上运行的分步指南?

    我从未使用过 Net 框架 需要向某人证明 Scala 确实可以在 Net 上运行 我需要使用 Scala 进行 快速而肮脏 的 Net 设置 以处理一些现有的 JVM Scala 代码 我找不到这方面的分步指南 我将不胜感激一些这方面的资
  • 如何在 Xcode 中禁用一个文件的优化

    我的 Xcode 项目依赖于另一个库 当我使用以下命令构建项目时 这会导致项目出现错误 O3 option 这些错误仅存在于一个文件中 所以我想关掉 O3 该文件的选项 是否可以 打开目标 看下Build Phases 打开Compile
  • 向数据框添加行的有效方法

    由此question https stackoverflow com questions 28056171 how to build and fill pandas dataframe from for loop和其他人似乎不建议使用con
  • 如何在第一个选项卡验证完成后启用第二个选项卡

    我有包含以下字段的选项卡 div ul li a href tabs 1 Tab1 a li li a href tabs 2 Tab2 a li li a href tabs 3 Tab3 a li li a href tabs 4 Ta
  • 将字节转换为图像时出现错误“参数无效”

    我正在将字节转换为图像 但出现错误 参数无效 我正在粘贴我的代码 请检查代码并建议我做对还是错 Image arr1 byteArrayToImage Bytess 这就是函数 public static Image byteArrayTo
  • 为什么thread_local不能应用于非静态数据成员以及如何实现线程本地非静态数据成员?

    Why may thread local不适用于非静态数据成员 接受的答案这个问题 https stackoverflow com questions 10999131 can you use thread local variables
  • Solr 高亮显示

    我看到了这个帖子here https stackoverflow com questions 4058913 how to highlighting search results using apache solr with php cod
  • SAP Web IDE 显示有关 ES6+ 功能的错误

    for var items in selectedContexts var downloadModel parsed parsed items toString split 1 parsed items toString split 2 v
  • Gradle 发出错误“无法创建类型为‘AppPlugin’的插件”

    我正在尝试使用 gradle 创建一个简单的 android 项目 我在一台装有 Debian GNU Linux 7 wheezy 的计算机上工作 我遵循了中的建议Gradle 插件用户指南 Android 工具项目网站 http too
  • 如何将 Twitter 小部件集成到 Reactjs 中?

    我想将 Twitter 小部件添加到 React 中 但我不知道从哪里开始或如何做 我对 React JS 很陌生 下面是 HTML 版本的代码 div class Twitter a class twitter timeline href
  • 如何在 Meteor 中缓存数据?

    感谢大家 最近我想在meteor上建立一个小型cms 但有一些问题 1 缓存 页面缓存 数据缓存等 例如 当人们搜索某篇文章时 在服务器端 Meteor publist articles function keyword return Ar
  • chrome.storage 在 chrome 扩展中未定义

    我正在开发一个 Google Chrome 扩展程序 并且已经为此工作了一段时间 所以它已经安装了一段时间 我更新了清单文件以包含 存储 权限并重新加载扩展 但是 当我在控制台中尝试时 chrome storage is undefined
  • Microsoft Bot 在 WebChat 中显示不必要的重复消息?

    当用户第一次访问我的聊天室时 他们会收到欢迎消息 并立即被要求提供他们的名字 一旦用户输入他们的名字 就会出现欢迎消息 并再次显示输入名字的文本提示 只有在他们第二次输入名字后 机器人才会继续处理下一个有关姓氏的问题 此外 当用户最终在第一