当前位置: 首页 > 知识库问答 >
问题:

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

端木存
2023-03-14

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

此外,当用户最终在第一次聊天中输入他们的名字和姓氏时,他们会再次回到同一个聊天,欢迎消息

这是重现此问题所需的最少代码。让restify=require('restify');let builder=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的web应用程序。

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

更新日志:

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();
    }
})

共有2个答案

管峻
2023-03-14

对那些仍然有问题的人来说

在Web通道和机器人之间建立第一次对话时,会引发ConversationUpdate活动两次。一个由用户,另一个由信道,因此我们得到两次欢迎消息。我们需要确保我们为用户提出的活动发送了欢迎消息。在这里找到工作代码

东郭存
2023-03-14

实际上,当bot连接器第一次连接到bot服务器时,bot首先加入会话,因此将为您的bot触发conversationUpdate事件,其中不包含会话。用户数据对象。

一旦用户在bot webchat中输入了一些信息,用户就会有第二次会话更新。此时,bot在会话包含会话的会话更新事件中开始执行'\'。用户数据对象。

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

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

不幸的是,当webchat初始化时,我找不到一种方法让bot为用户触发conversationUpdate

您可以利用网站上的webchat js sdk和反向通道机制来实现您的要求。

//定义用户

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, '/');
  }
})

 类似资料:
  • 对于不改变实例状态的函数,方法的javadoc注释通常与Java-API中@return-tag的注释相同或非常相似。 boolean Collection.IsEmpty() 如果此集合不包含元素,则返回true。 如果此集合不包含元素,则返回:true 现在我正在为许多简单的方法编写javadoc,比如getExpression(),在这些方法中我遇到了同样的问题。我应该像在API中那样做还是

  • 我正在尝试将图像从SD卡设置为,但它不起作用。我做错了什么?我确定该文件存在。 这是的代码: 以下是布局的代码: : 最后,图像200 × 149像素:

  • 因此,很明显,它识别出输入的名称太短,但我在视图中没有得到任何错误消息,正如这里所预期的:。

  • 这其实是我关于堆栈溢出的第一个问题,所以我挺兴奋的。 提问:我做了一个需要Flash操作的游戏。我想用不是Chrome的浏览器向网站的访问者显示一条消息。 它将是一个字符串:“如果游戏没有正确加载,尝试使用谷歌Chrome”。 如果你们知道一种方法,在不访问谷歌Chrome时显示这个消息(因为那会有点多余),我将非常感谢!

  • 我正在构建代码段块,以便在

  • 我知道以前有人问过这个问题,但那篇文章中的解决方案对我不起作用。 我刚刚开始学习Android编程,当时正在做一个模型。它可以在Android Studio中正确显示,但不能在我的设备上显示。图像和背景色显示正确,但没有文本显示。我试过改变字体和字体颜色。需要改变什么? 我有另一个使用RelativeLayout的示例,它显示正确,但这个ConstraintLayout不正确。 第一个图像来自AS