MS BOT框架(自适应卡):如何从directline发送值(Stepcontext.Value)?

问题描述 投票:2回答:1

我在Azure中部署了一个Bot,Bot显示欢迎消息OnMemberAdd。它的自适应卡,所以输入的值都发送到stepcontext.value。我已经将其与多个渠道集成,对于directline,我想绕过欢迎卡,直接将消息传递到stepcontext.value,因此显示第二个提示而不是第一个。我试过下面的方法,但没有用,请大家帮忙。

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <title>Web Chat: Send welcome event</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
    <style>
      html,
      body {
        height: 100%;
      }
      body {
        margin: 0;
      }

      #webchat {
        height: 100%;
        width: 100%;
      }
    </style>
  </head>
  <body>
    <div id="webchat"></div>
    <script>
      (async function() {
        // In this demo, we are using Direct Line token from MockBot.
        // Your client code must provide either a secret or a token to talk to your bot.
        // Tokens are more secure. To learn about the differences between secrets and tokens
        // and to understand the risks associated with using secrets, visit https://docs.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-direct-line-3-0-authentication?view=azure-bot-service-4.0

        const { token } = { token};

        // 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') {
   dispatch({
     type: 'WEB_CHAT/SEND_EVENT',
     payload: { 
                name: 'userInfo',
       value: { fname:'user', lname:'test', pnumber:'0678775453'} 
                }
   });
 }

 return next(action);
        });

        const styleOptions = {
 botAvatarImage:
   '',
 botAvatarInitials: 'Chatbot',
 userAvatarImage: '',
 userAvatarInitials: 'User',
 showNub: true,
 bubbleFromUserNubOffset: 'bottom',
 bubbleFromUserNubSize: 10,
 bubbleFromUserBorderColor: '#0077CC',
 bubbleNubOffset: 'top',
 bubbleNubSize: 0,
 bubbleBorderColor: '#009900',
 sendBoxButtonColor: '#009900',
 hideUploadButton: true,
 hideSendBox : true
        };
        window.WebChat.renderWebChat(
 {
   directLine: window.WebChat.createDirectLine({ token }),
   store,
            styleOptions
 },
 document.getElementById('webchat')
        );

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

我试过通过postman发送数据,效果很好,但是当我用上面的代码发送时,却不能正常工作。

邮递员正文

{
    "type": "message",
    "from": {
        "id": "user1"
    },
    "value": 
    {
        "fname":"user",
        "lname":"test",
        "pnumber":"0678787543"
    }
}
javascript node.js botframework chatbot adaptive-cards
1个回答
0
投票

你是如此接近! 你有两个选择。

  1. 换成... WEB_CHAT/SEND_EVENT 并包括 name 财产。
const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
          if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
            dispatch({
              type: 'WEB_CHAT/SEND_EVENT',
              payload: {
                name: 'userInfo',
                value: { fname:'user', lname:'test', pnumber:'0678775453'}
              }
            });
          }

          return next(action);
        });
  1. 使用 WEB_CHAT/SEND_MESSAGE包括 text 财产,并改成 channelData
const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
          if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
            dispatch({
              type: 'WEB_CHAT/SEND_MESSAGE',
              payload: {
                text: 'userInfo',
                channelData: { fname:'user', lname:'test', pnumber:'0678775453'}
              }
            });
          }

          return next(action);
        });

===

更新

我用你的代码就能看出来。在你的代码中加入一个断点 onTurnOnTurnAsync 你会看到,当用户连接时,你会得到。

  1. bot的对话更新
  2. 用户的对话更新
  3. 在WebChat的Event中加入你想要的用户数据。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.