实例化后更新 Bot Framework WebChat 控件的属性?

Updating properties of Bot Framework WebChat control after it has been instantiated?

我想知道是否有任何方法可以在 Bot Framework WebChat 控件实例化后更新其属性?我知道我可以使用 DirectLine Backchannel 更新控件嵌入的页面元素,但我还没有弄清楚是否有可能通过 Backchannel 更新 BotChat.App 属性。

(想法是根据用户在机器人中给出的响应更新聊天气泡下方显示的用户名 属性。)

感谢指点,谢谢!

update the user name property shown underneath the chat bubbles based on a response a user gives inside the bot.

您可以通过the backchannel mechanism实现需求,以下代码片段供您参考。

在您的机器人应用程序中:

var replymes = activity.CreateReply();
replymes.Type = "event";
replymes.Name = "ChangeUnameEvent";

replymes.Value = uname;

await context.PostAsync(replymes);

await context.PostAsync($"Your name is {uname}.");

在您的网络聊天客户端中:

var botConnection = new BotChat.DirectLine({ secret: 'your_directline_secret' });

var userinfo = { id: 'You' };
BotChat.App({
    botConnection: botConnection,
    user: userinfo,
    bot: { id: 'your_bot_id' },
    resize: 'detect'
}, document.getElementById("bot"));


botConnection.activity$
    .filter(activity => activity.type === "event" && activity.name === "ChangeUnameEvent")
    .subscribe(activity => changeUName(activity.value));

function changeUName(val) {
    //alert(val);
    userinfo.id = val;
}

botConnection.activity$
    .filter(activity => activity.type === "message")
    .subscribe(activity => hidemessagesfordefaultuser(activity));

function hidemessagesfordefaultuser(activity) {
    if (activity.from.id == "You") {
        activity.type = "event";
    }

    console.log(activity);
}

测试结果:

注:

如果您想在 MessagesController 中实现它,请尝试:

else if (message.Type == ActivityTypes.Event && message.Name == "SendUserNameEvent")
{
    var client = new ConnectorClient(new System.Uri(message.ServiceUrl), new MicrosoftAppCredentials());

    var reply = message.CreateReply();
    reply.Value = message.Value;
    reply.Type = "event";
    reply.Name = "ChangeUnameEvent";

    await client.Conversations.ReplyToActivityAsync(reply);
}

向机器人发送事件:

botConnection.postActivity({
    type: 'event',
    from: userinfo,
    name: 'SendUserNameEvent',
    value: "fei han"
}).subscribe(function (id) { console.log('UName: fei han'); });