本文介绍了将每条消息发送channelData到Webchat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从页面中的bot webchat控件发送的每条消息中注入channelData.我环顾四周,发现此示例("> https://cmsdk.com/javascript/how-to-send-custom-channel-data-when-using-web-chat-c​​lient-with-bot-framework.html ),我的代码如下所示.

I am trying to inject channelData with each message that is sent from a bot webchat control in a page. I looked around and found this sample (https://cmsdk.com/javascript/how-to-send-custom-channel-data-when-using-web-chat-client-with-bot-framework.html) and my code looks like the code below.

问题在于,它可以在Chrome中使用,但传播算符(…)在Edge或IE上不起作用.是否有适用于所有浏览器的替代语法?

The issue is that this works in Chrome but the spread operator (…) doesn’t work on Edge or IE. Is there an alternative syntax that would work in all browsers?

var user = {
    id: '@User.Identity.Name',
    name: '@User.Identity.Name'
};

var bot = {
    id: BotId,
    name: 'BotName'
};

var botConnect = new BotChat.DirectLine({
    secret: '@ViewData["BotSecret"]',
    webSockets: 'true'
});

var v = { ...botConnect };
debugger;

BotChat.App({
    botConnection: {
        ...botConnect,
        postActivity: activity => {
            activity.channelData = {
                StudentId: '@User.Identity.Name'
            };
            return botConnect.postActivity(activity);
        }
    },
    user: user,
    bot: bot,
    resize: 'detect'
}, document.getElementById("bot"));

推荐答案

我只是与一些了解其JS的人合作,因此我实现了一个在IE,Chrome和Edge(尚未在Safari中进行测试,但我想它也应该在那里工作).

just closing the loop on this one, I worked with some guys that know their JS and we implemented a "spread equivalent" function that works on IE, Chrome and Edge (haven't tested in Safari but I guess it should work there too).

IE不喜欢=>运算符,因此我们也将其更改为函数,这是结果代码:

IE didn't like the => operator so we changed that to a function too, here is the resulting code:

var user = {
    id: '@User.Identity.Name',
    name: '@User.Identity.Name'
};

var bot = {
    id: 'TheBotId',
    name: 'TheBotName'
};

var botConnect = new BotChat.DirectLine({
    secret: 'TheBotSecret',
    webSockets: 'true'
});

// Spread equivalent function
function getBotConnectionDetail(botconnection) {
    var botConnectionDetail = {};
    var keys = Object.keys(botconnection);
    for (var i = 0; i < keys.length; i++) {
        botConnectionDetail[keys[i]] = botconnection[keys[i]];
    };
    botConnectionDetail['postActivity'] = function (activity) {
        activity.channelData = {
            StudentId: '@User.Identity.Name'
        };
        return botconnection.postActivity(activity)
    };
    return botConnectionDetail;
}

// Invokes Bot
BotChat.App({
        botConnection: getBotConnectionDetail(botConnect),
        user: user,
        bot: bot,
        resize: 'detect'
    },
    document.getElementById("bot")
);

这篇关于将每条消息发送channelData到Webchat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 07:30