【问题标题】:MS BOT Framework (adaptive cards): How to send value (Stepcontext.Value) from directlineMS BOT 框架(自适应卡):如何从直线发送值(Stepcontext.Value)
【发布时间】:2020-07-30 14:10:54
【问题描述】:

我在 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"
    }
}

【问题讨论】:

  • 您能否详细说明“它不起作用”是什么意思?您目前已将其设置为在 WebChat 连接到机器人后立即发送该消息,我认为这不是您想要的。或者你是说消息根本没有通过?
  • 我正在使用来自 MS Web Chat 示例的 HTML/Java 脚本模板。我可以将消息作为文本发送,即 stepcontext.context.activity.text,但在我的情况下,机器人期待 stepcontext.context.activity.value 中的数据。所以我试图以价值发送数据,但消息没有到达机器人。我使用邮递员尝试了相同的步骤并且它有效。我正在寻找一个答案来了解如何将数据发送到 stepcontext.context.activity.value。如果需要更多信息,请告诉我..
  • 补充以上内容,我想在网络聊天连接后立即发送值。我尝试调试但未确定为什么当我在有效负载中发送值时消息没有到达 Chatbot,我没有看到任何错误。

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


【解决方案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. 机器人的对话更新
  2. 用户的对话更新
  3. 带有您想要的用户数据的 WebChat 事件:

【讨论】:

  • 我已经尝试了上述两个选项,但我无法让它工作。它没有显示任何错误,但数据没有到达 Bot。我不确定我是否遗漏了什么。 1)var reply = stepContext.Context.Activity.Value 2)var reply = stepContext.Context.Activity.ChannelData
  • @KrishnaS 你能用完整的html(减去秘密)更新你的问题吗?
  • @KrishnaS 我已经更新了我的答案以表明您的代码的行为符合预期/期望。
猜你喜欢
  • 2019-03-31
  • 2020-03-21
  • 1970-01-01
  • 2019-08-27
  • 1970-01-01
  • 2020-05-30
  • 2018-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多