有没有一种方法可以根据对话结果将信息从Microsoft聊天机器人发送到网站

时间:2019-01-08 03:40:09

标签: javascript html azure botframework limesurvey

我将Microsoft Bot Framework与Node.js结合使用来实现聊天机器人。我尝试在“ LimeSurvey” -Survey中使用此聊天机器人(您可以使用HTML代码将该机器人集成到调查中,因此基本上可以假定它是一个正常的网站)。根据对话的结果,我想在此网站上显示不同的信息。假设对话可以有10个不同的结果。有什么方法可以将对话的“结果”传达到网站并根据该信息显示不同的信息?能够将1到10之间的数字发送到网站就足够了。我可以使用iframe集成机器人,也可以按照以下代码中的说明进行操作:

<!DOCTYPE html>
<html>
  <body>
    <div id="webchat" role="main"></div>
    <script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
    <script>
      window.WebChat.renderWebChat({
        directLine: window.WebChat.createDirectLine({ secret: 'M_BOT_SECRET_FROM_AZURE_PORTAL' }),
        userID: 'YOUR_USER_ID'
      }, document.getElementById('webchat'));
    </script>
  </body>
</html>

2 个答案:

答案 0 :(得分:1)

我希望您已使用直接通道设置了聊天机器人,并且它处于工作状态。

要获得漫游器的响应,您可以收听直接渠道的活动事件。

const botConnection = new BotChat.DirectLine({
    domain: params['domain'],
    secret: <secret_key>,
    token: <any_params>,
    webSocket: params['webSocket'] && params['webSocket'] === 'true' // defaults to true
 });

  BotChat.App({
    bot: bot,
    botConnection: botConnection,
    user: user,
    locale: userDetail.userLanguage,
  }, document.getElementById('chatBot'));


    //listening to activities sent and received
    botConnection.activity$
    .subscribe(function(activity){
        if(activity.type == "message" && activity.text == <some_response_from_bot>){
            //your logic to show on the web page
        }
    });

因此,在侦听器中,您可以跟踪所有活动消息并采取措施。根据您的情况,根据对话显示不同的结果。

我建议您阅读这些内容-

https://github.com/Microsoft/BotFramework-DirectLineJS

https://docs.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-direct-line-3-0-receive-activities?view=azure-bot-service-4.0

答案 1 :(得分:0)

要在网页上显示取决于对话结果的信息,您需要配置机器人以发送包含带有结果的channelData的活动,并将包含结果的活动的侦听器添加到WebChat组件。

首先,在您的漫游器中,当您到达会话流的结尾并获得结果时,请向用户发送一个活动,该活动带有name属性和包含结果的channelDataname属性将用于过滤会话侧的所有传入活动。机器人中的代码应如下所示:

await turnContext.sendActivity({
    name: 'result',
    channelData: {
        payload: {
            result: turnContext.activity.text
        }
    }
});

现在,我们将向WebChat组件添加自定义中间件以处理传入的活动。收到活动后,我们可以通过name属性对其进行过滤,然后根据channelData的结果值执行正确的操作。您的WebChat应该如下所示:

<script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
<div id="webchat" role="main"></div>
<script>
  (async function () {
    // 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.
    // It is being included here only for simplicity
    // https://docs.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-direct-line-3-0-authentication

    const res = await fetch('https://directline.botframework.com/v3/directline/tokens/generate', {
              method: 'POST',
              headers: {
                'Authorization': 'Bearer ' + secret
              },
          json: true
        });
    const { token } = await res.json();

    // We are adding a new middleware to customize the behavior of DIRECT_LINE/INCOMING_ACTIVITY.
    const store = window.WebChat.createStore(
      {},
      ({ dispatch }) => next => action => {
        if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY') {

          // Get activity from action
          const { activity } = action.payload;

          // Check if the name attribute in the activity is `result`
          if (activity.name === 'result'){

              // Get result from channel data
              const { result } = activity.channelData.payload;

              switch (result) {
                case 'result1':
                  alert(result)
                  // TODO: Action 1
                  break;
                case 'result2':
                  // TODO: Action 2
                  break;
                default:
                  // TODO: Default Action
            }
          }
        } 
        return next(action);
      }
    );

    window.WebChat.renderWebChat({
      directLine: window.WebChat.createDirectLine({ token }),
      // We will use a custom version of Redux store, which we added middleware to handle backchannel messages.
      store
    }, document.getElementById('webchat'));

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

请注意,您不应该将Direct Line机密存储在浏览器或客户端应用中。仅出于简化目的将其包括在此处。我还建议您查看BotFramework-WebChat Samples-特别是示例number 15

希望这会有所帮助!