将自定义参数从网络聊天控件传递到机器人框架

Pass Custom Parameters from webchat control to bot framework

所以,我目前正在使用这个:

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

它工作正常,但是我有多个针对不同客户端应用程序的 QnA 知识库。所以我想通过 'applicationname' 的自定义参数来决定我的 BOT 框架工作(V4)中 OnTurnAsync 方法中的 QNA KB。

我试过了

var d1 = window.WebChat.createDirectLine({ token })
window.WebChat.renderWebChat({
        directLine: d1,
         styleSet,
postActivity: activity => {
     var newActivity = Object.assign({}, activity, {channelData: { "userparam": "test" } });
     return dl.postActivity(newActivity);
    }

}, document.getElementById('webchat'));
})();

但 Context.Activity.ChannelData 在 bot 中返回 Null

也尝试过

var d1 = window.WebChat.createDirectLine({ token })
window.WebChat.renderWebChat({
        directLine: d1,
        user: { id: 'userid', userparam:'test'},
            styleSet

}, document.getElementById('webchat'));
})();

仍然Context.Activity.From.Properties["userparam"]returns空

从客户端

var d1 = window.WebChat.createDirectLine({ token })
window.WebChat.renderWebChat({
        directLine: Object.assign({}, d1, {
     postActivity: activity => {
     var newActivity = Object.assign({}, activity, { channelData: { "param1": "test" } });
     return d1.postActivity(newActivity);
    }
  }),
            styleSet,
            botAvatarInitials: 'CAB',
            userAvatarInitials: 'You'

}, document.getElementById('webchat'));
})();

来自 BOt 框架

var channelObj = turnContext.Activity.ChannelData.ToString();
var channeldata = Newtonsoft.Json.Linq.JObject.Parse(channelObj);
var customdata = channeldata["param1"].ToString();

提供的其他答案很有帮助,但它会覆盖 channelData。对于找到此答案并只想发送自定义参数的其他人,这将有所帮助:

const originalDirectline = props.webchat.createDirectLine({
    token,
})

const directLine = Object.assign({}, originalDirectline, {
    postActivity: (activity: any) => {
        const newActivity = Object.assign({}, activity)
        newActivity.customParam = "custom value"
        return originalDirectline.postActivity(newActivity)
    }
})