首页 > 解决方案 > 我可以将信息从网页传递到 Bot Framework 网络聊天吗

问题描述

有什么方法可以将信息从网页传递到机器人。我有一个 Bot Framework 机器人,它使用嵌入在 aspx 页面上的 iframe 中的网络聊天。最终,我想根据用户所在的业务线动态设置 QnA Maker 在机器人中使用的过滤标签。而不是让他们告诉机器人他们代表哪个业务线,我希望能够通过自动向机器人发送信息。

这是 iframe 仅供参考:

<iframe src="https://webchat.botframework.com/embed/bdous-prod-newhire-bcr?s={my secret}" id="webchat"></iframe>

这可能吗?

谢谢,
迪伦

标签: botframework

解决方案


正如 Nicolas R 所指出的,您应该使用反向渠道来执行您的询问。

您应该查看两个示例(它们实际上需要一起使用才能运行完整的示例)。它们是这个backChannelBotBotFramework-WebChat/samples/backchannel示例。

在第一个示例中,您需要检查的重要代码可以在app.js文件中找到:

//Bot listening for inbound backchannel events - in this case it only listens for events named "buttonClicked"
bot.on("event", function (event) {
  var msg = new builder.Message().address(event.address);
  msg.textLocale("en-us");
  if (event.name === "buttonClicked") {
    msg.text("I see that you just pushed that button");
  }
  bot.send(msg);
})

//Basic root dialog which takes an inputted color and sends a changeBackground event. No NLP, regex, validation here - just grabs input and sends it back as an event. 
bot.dialog('/', [
  function (session) {
    var reply = createEvent("changeBackground", session.message.text, session.message.address);
    session.endDialog(reply);
  }
]);

//Creates a backchannel event
const createEvent = (eventName, value, address) => {
  var msg = new builder.Message().address(address);
  msg.data.type = "event";
  msg.data.name = eventName;
  msg.data.value = value;
  return msg;
}

WebChat 示例中的相关代码部分是本

  botConnection.activity$
    .filter(function (activity) {
      return activity.type === 'event' && activity.name === 'changeBackgroundColor';
    })
    .subscribe(function (activity) {
      console.log('"changeBackground" received with value: ' + activity.value);
      changeBackgroundColor(activity.value);
    });

  function changeBackgroundColor(newColor) {
    document.getElementsByClassName('wc-message-groups')[0].style.backgroundColor = newColor;
  }

  function postButtonMessage(color) {
    botConnection
      .postActivity({
        from: { id: 'me' },
        name: 'changeBackgroundColor',
        type: 'event',
        value: color
      })
      .subscribe(function (id) {
        console.log('"changeBackgroundColor" sent');
      });
  }

推荐阅读