我需要更改网络聊天机器人的布局以在公司网站中使用,但 iframe 不允许更改。
如果我们使用提供的代码,它指向 Microsoft 托管的网络聊天实例。正如您所说,使用自定义 CSS 和 JavaScript 代码来更改网络聊天的布局并不容易。
如果你只是想大致改变一下webchat的显示/布局,如士大夫建议的,你可以使用botchat.js运行webchat inline,然后使用F12开发者工具检查webchat的html结构并应用自定义覆盖默认样式的 CSS 样式。
但据我所知,一些需求/布局修改(例如showing bot icon with each message)无法通过使用自定义CSS和JavaScript覆盖默认样式来实现,我们需要克隆BotFramework-WebChat repo,修改它,构建它,并引用我们自定义/构建的 botchat.css 和 botchat.js 文件,这需要您安装 NPM。
如何将我自己的 Iframe BotFramework 发布到 Azure?
如果您想构建您的自定义网络聊天并将其托管在您自己的 Azure 网站中,以便其他网站可以使用 <iframe> 嵌入您的网络聊天实例:
<iframe src="/path/to/your/webchat/instance" height="height" width="width" />
您可以参考以下步骤:
1) 克隆 BotFramework-WebChat 仓库和Build your own Web Chat
2) 创建一个网页(以下示例代码供您参考)并引用您构建的botchat.css 和botchat.js 文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link href="BotChat/botchat.css" rel="stylesheet" />
<script src="BotChat/botchat.js"></script>
</head>
<body>
<div id="bot"></div>
<style>
.botIcon {
float: left !important;
border-radius: 50%;
}
.userIcon {
float: right !important;
border-radius: 50%;
}
</style>
</body>
</html>
<script>
const params = BotChat.queryParams(location.search);
const user = {
id: params['userid'] || 'You', name: params['username'] || 'You',
};
BotChat.App({
bot: { id: "{your_botid_here}"},
resize: 'window',
user: user,
directLine: {
secret: params['s']
}
}, document.getElementById('bot'));
</script>
3) 将其发布到 Azure 网站并将上述网页添加为默认文档
4) 在其他网站中,要嵌入您构建的网络聊天,您可以使用
<iframe src='http://{your_webapp_name}.azurewebsites.net/?s={directline_secret}&userid={user_id}' width="400px" height="500px"></iframe>
测试结果: