gpt4 book ai didi

c# - 如何在自定义网络聊天实现机器人框架中更改用户名/用户ID

转载 作者:行者123 更新时间:2023-12-03 04:18:13 24 4
gpt4 key购买 nike

我需要在自定义网络聊天控件中动态填充用户名。我知道网络聊天会呈现脚本中存在的用户名,但如何更改它?我什至尝试使用 .NET SDK 从服务器端发送具有不同用户名的事件,但仍然无法实现。

Custom Web Chat

<script>

var user = {
id: 'user-id',
name: 'user name'
};

var botConnection = new BotChat.DirectLine({
token: '[directlinesecret]',
user: user
});

BotChat.App({
user: user,
botConnection: botConnection,
bot: { id: 'bot-id', name: 'bot name' },
resize: 'detect'
}, document.getElementById("bot"));

botConnection
.postActivity({
from: user,
name: 'requestWelcomeDialog',
type: 'event',
value: ''
})
.subscribe(function (id) {
console.log('"trigger requestWelcomeDialog" sent');
});

</script>

最佳答案

I need to populate the username dynamically in custom webchat control.

我假设您希望将网络聊天嵌入到您的网站中,并根据当前用户(登录用户)动态指定 user: { id: user_id, name: user_name },您可以当用户登录您的网站时,尝试将用户信息保留在cookie中,然后您可以从cookie中检索当前用户的信息并发起网络聊天。

示例:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet" />
<script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
<style>
.wc-chatview-panel {
width: 350px;
height: 500px;
position: relative;
}
</style>
</head>
<body onload="initiatewebchat()">
<div id="chat"></div>
</body>
</html>
<script>
function initiatewebchat() {

var botuser = getCookie("botuser");

if (botuser!=null) {
var userinfo = JSON.parse(botuser);

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

var user = {
id: userinfo.user_id,
name: userinfo.user_name
};

var bot = {
id: 'fehanbasicbot',
name: 'botname'
};

BotChat.App({
botConnection: botConnection,
user: user,
bot: bot,
}, document.getElementById('chat'));

} else {
alert("do not find user identity for web chat! please login to our website.");
}
}

function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
</script>

测试结果:

enter image description here

注意:

在上面的测试示例中,我在 cookie 中保留了 "{"user_id":"0001","user_name":"user1"}"

enter image description here

关于c# - 如何在自定义网络聊天实现机器人框架中更改用户名/用户ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51238866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com