将自定义参数从Webchat控件传递到Bot框架

时间:2019-01-18 15:55:06

标签: azure bots azure-bot-service direct-line-botframework

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

<!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'传递自定义参数,以便在OnTurnAsync方法的BOT框架工作(V4)中确定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'));
})();

但是bot中的Context.Activity.ChannelData返回Null

也尝试过

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

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

still Context.Activity.From.Properties [“ userparam”]返回Null

2 个答案:

答案 0 :(得分:0)

来自Cilent方面

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 Framework

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

答案 1 :(得分:0)

提供的另一个答案是有帮助的,但它会覆盖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)
    }
})