MS BOT Framework(自适应卡):如何从直接线发送值(Stepcontext.Value)

时间:2020-04-17 06:35:10

标签: javascript node.js botframework chatbot adaptive-cards

我已经在Azure中部署了Bot,该Bot显示欢迎消息OnMemberAdd。它的自适应卡,因此将输入的值发送到stepcontext.value。我已将其与多个渠道集成在一起,对于直接线路,我想绕过欢迎卡并将消息直接传递给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>

我试图通过邮递员发送数据,并且效果很好,但是当我使用上面的代码进行操作时,它就无法工作。

邮递员的身体

{
    "type": "message",
    "from": {
        "id": "user1"
    },
    "value": 
    {
        "fname":"user",
        "lname":"test",
        "pnumber":"0678787543"
    }
}

1 个答案:

答案 0 :(得分:1)

您是如此亲密!您有两种选择:

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

===

更新

使用您的代码,我可以很好地看到它。在onTurn / OnTurnAsync中放置一个断点,当用户连接时您会看到:

    该机器人的
  1. conversationUpdate
  2. 用户的
  3. conversationUpdate
  4. 所需的用户数据的WebChat活动:

enter image description here