我需要在自定义网络聊天控件中动态填充用户名。我知道网络聊天会呈现脚本中存在的用户名,但是我该如何更改呢?我什至尝试使用.NET SDK从服务器端发送具有不同用户名的活动,但仍然无法实现。
<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>
答案 0 :(得分:0)
我需要在自定义网络聊天控件中动态填充用户名。
我假设您想将Web Chat嵌入到您的网站并根据当前用户(登录用户)动态指定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>
测试结果:
注意:
在上述测试示例中,我在Cookie中保留了"{"user_id":"0001","user_name":"user1"}"
。