简体   繁体   English

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

[英]Is there a way to send information from a Microsoft chatbot to a website depending on conversation result

I use the Microsoft Bot Framework with Node.js to implement a chatbot. 我将Microsoft Bot Framework与Node.js结合使用以实现聊天机器人。 I try to use this chatbot within a "LimeSurvey"-Survey (you can use HTML code to integrate the bot into the survey, so you can basically assume it's a normal website). 我尝试在“ LimeSurvey” -Survey中使用此聊天机器人(您可以使用HTML代码将该机器人集成到调查中,因此基本上可以假定它是一个正常的网站)。 Depending on the result of the conversation, I want to show different information on this website. 根据对话的结果,我想在此网站上显示不同的信息。 Let's say the conversation can have 10 different results. 假设对话可以有10个不同的结果。 Is there any way I can communicate the conversation's "result" to the website and show different information depending on that? 有什么方法可以将对话的“结果”传达到网站并根据该信息显示不同的信息? It would be sufficient to be able to send a number between 1 and 10 to the website. 能够将1到10之间的数字发送到网站就足够了。 I can either use an iframe to integrate the bot or do it as you can see in the code below: 我可以使用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>

I hope you have setup the Chatbot using directline channel and it is in working condition. 我希望您已使用直接通道设置了聊天机器人,并且它处于工作状态。

To get the response from the bot, you can listen to the activity events of the directline channel. 要获得漫游器的响应,您可以收听直接渠道的活动事件。

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

So, within the listener you can trace all the activity messages and take action on what to do. 因此,您可以在侦听器中跟踪所有活动消息并采取措施。 In your case show different results depending on the conversation. 根据您的情况,显示不同的结果。

I recommend you reading these- 我建议您阅读这些-

https://github.com/Microsoft/BotFramework-DirectLineJS 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 https://docs.microsoft.com/zh-CN/azure/bot-service/rest-api/bot-framework-rest-direct-line-3-0-receive-activities?view=azure-bot-service- 4.0

To show information on a webpage that depends on the result of a conversation, you need to configure your bot to send activities that contain channelData with the result and add a listener for the activities that contain a result to your WebChat component. 要在取决于对话结果的网页上显示信息,您需要配置机器人以发送包含带有结果的channelData的活动,并将包含结果的活动的侦听器添加到WebChat组件。

First, in your bot, when you get to the end of your conversation flow and have the result, send an activity to the user with a name attribute and channelData that contains the result. 首先,在您的漫游器中,当您到达对话流程的最后并获得结果时,将活动发送给用户,该活动具有name属性和包含结果的channelData The name attribute will be used to filter all the incoming activities on the conversation side. name属性将用于过滤对话方的所有传入活动。 The code in your bot should look like this: 机器人中的代码应如下所示:

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

Now, we are going to add a custom middleware to our WebChat component to handle incoming activities. 现在,我们将向WebChat组件添加自定义中间件,以处理传入的活动。 When activities are received, we can filter them by the name attribute and then perform the correct action based on the result value from the channelData . 收到活动后,我们可以通过name属性对其进行过滤,然后根据channelData的结果值执行正确的操作。 Your WebChat should look like this: 您的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>

Please note, you shouldn't store your Direct Line secret in the browser or client app. 请注意,您不应将您的Direct Line机密存储在浏览器或客户端应用中。 It is included here only for simplicity. 仅出于简化目的将其包括在此处。 I would also recommend looking at the BotFramework-WebChat Samples - particularly sample number 15 . 我还建议您查看BotFramework-WebChat示例 -特别是示例编号15

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在我的网站中添加使用Watson Conversation的chatbot小部件? - How to add a chatbot widget that uses Watson Conversation in my website? 有没有办法将 Google Dialogflow 中整个聊天机器人对话的日志获取到 Firebase 数据库? - Is there a way to get the logs of the entire chatbot conversation in Google Dialogflow to Firebase Database? 有没有办法在Ajax中安全地发送信息? - Is there a way to securely send information in Ajax? 发送文件 nodemailer 和 chatbot - send documents nodemailer and chatbot 如何将表格信息从网站发送到电话号码的邮件收件箱? - How to send form information from website to a phone number's message inbox? 如何从嵌入的 iframe 向我的网站发送信息(嵌入 iframe 的位置) - How can I send information from an embeded iframe to my website( where that iframe is embeded on) 通过网站将信息从一个人即时发送到另一个人(就像聊天室一样) - Send information through a website from one person to another instantaneously (like how a chatroom does) 从网站获取詹金斯信息 - Fetch Jenkins information from a website 从生成的网站中提取信息 - Extracting information from generated website 根据文本框空间计数将人员发送到某个网站 - Send person to certain website depending on text box space count
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM