如何自动加入Microsoft网络聊天中的对话(机器人框架)

时间:2019-01-15 08:10:44

标签: javascript botframework web-chat

我正在使用https://github.com/Microsoft/BotFramework-WebChat/blob/master/README.md

每当网站上显示网络聊天小部件时,我都希望该机器人收到“加入”消息。

这个想法是人类不必发起对话。机器人可以通过欢迎消息来响应加入对话的人。

这怎么可能?

2 个答案:

答案 0 :(得分:2)

自Webchat v3以来,此“欢迎功能”一直是长期的讨论和主题。看来此请求在2天前已得到修复:https://github.com/Microsoft/BotFramework-WebChat/pull/1286

现在有一个有关如何执行此操作的示例,位于此处: https://github.com/Microsoft/BotFramework-WebChat/blob/master/samples/15.d.backchannel-send-welcome-event/index.html

简而言之,演示如下:

  (async function () {
    // In this demo, we are using Direct Line token from MockBot.
    // To talk to your bot, you should use the token exchanged using your Direct Line secret.
    // You should never put the Direct Line secret in the browser or client app.
    // https://docs.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-direct-line-3-0-authentication
    const res = await fetch('https://webchat-mockbot.azurewebsites.net/directline/token', { method: 'POST' });
    const { token } = await res.json();

    // We are using a customized store to add hooks to connect event
    const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
      if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
        // When we receive DIRECT_LINE/CONNECT_FULFILLED action, we will send an event activity using WEB_CHAT/SEND_EVENT
        dispatch({
          type: 'WEB_CHAT/SEND_EVENT',
          payload: {
            name: 'webchat/join',
            value: { language: window.navigator.language }
          }
        });
      }
      return next(action);
    });

    window.WebChat.renderWebChat({
      directLine: window.WebChat.createDirectLine({ token }),
      store
    }, document.getElementById('webchat'));

    document.querySelector('#webchat > *').focus();
  })().catch(err => console.error(err));

请注意,由于此PR是一个相当新的事实,它没有嵌入在latest版本中,因此您必须指向master文件的webchat.js版本,而不是latest

<script src="https://cdn.botframework.com/botframework-webchat/master/webchat.js"></script>

它正在工作:机器人端收到Event类型的活动通知,您可以在用户键入任何内容之前对其进行回复:

demo

答案 1 :(得分:0)

您可以检查ActivityTypes.ConversationUpdate事件,并将欢迎消息发送给用户。请参阅本文以获取详细步骤-https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-send-welcome-message?view=azure-bot-service-4.0&tabs=csharp%2Ccsharpmulti%2Ccsharpwelcomeback