【发布时间】:2020-02-21 09:34:39
【问题描述】:
我们的网站上有一个网络聊天窗口,可以最小化/重新打开(保持相同的令牌,如果仍然有效)或关闭/重新打开(确保我们检索新令牌)。当我们向机器人发送特定事件时,机器人应该向我们致意,例如:Welcome Event Example。但是,当遵循特定模式时,网络聊天会卡在“WEB_CHAT/SET_REFERENCE_GRAMMAR_ID”操作上,我们的机器人不会向我们发送欢迎消息。
有人可以帮助我了解为什么网络聊天会冻结此操作吗?
下面的示例方法对于使用 javascript 重构 UI 是否可行?
重现步骤:
- 使用下面的代码,创建一个 html 文件。 - 一定要更新 秘密
- 在 Chrome 中打开文件
- 点击“打开聊天”
- 点击“最小化聊天”
- 点击“打开聊天”
- 点击“关闭聊天”
- 打开开发者控制台
- 点击“打开聊天”
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Web Chat</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
<style>
html,
body {
height: 100%;
}
body {
margin: 0;
}
#webchat {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div style="width: 100%;">
<div style="width: 200px; float: left;">
<input type="button" onclick="openChat()" id="btnOpen" value="Open Chat" />
</div>
<div style="width: 200px; float: left;">
<input type="button" onclick="closeChat()" id="btnClose" value="Close Chat" />
</div>
<div style="width: 200px; float: left;">
<input type="button" onclick="minimizeChat()" id="btnMin" value="Minimize Chat" />
</div>
<div style="clear: both" id="webchat" role="main"></div>
</div>
<script>
const styleOptions = {
bubbleBackground: 'rgba(0, 0, 255, .1)',
bubbleFromUserBackground: 'rgba(0, 255, 0, .1)',
hideSendBox: false,
hideUploadButton: true, // default false
microphoneButtonColorOnDictate: '#F33',
sendBoxBackground: 'White',
sendBoxButtonColor: undefined, // defaults to subtle
sendBoxButtonColorOnDisabled: '#CCC',
sendBoxButtonColorOnFocus: '#333',
sendBoxButtonColorOnHover: '#999', // default '#333'
sendBoxDisabledTextColor: undefined, // defaults to subtle
sendBoxHeight: 40,
sendBoxMaxHeight: 200,
sendBoxTextColor: 'Black',
sendBoxBorderBottom: '',
sendBoxBorderLeft: '',
sendBoxBorderRight: '',
sendBoxBorderTop: 'solid 1px #E6E6E6',
sendBoxPlaceholderColor: undefined, // defaults to subtle
sendBoxTextWrap: true,
};
var secret = 'YOUR SECRET HERE';
var res = "";
var token = "";
const storeMiddleware = () => next => action => {
console.log(">>> HTML DISPATCH action: " + action.type);
if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
sendEvent();
}
if (action.type === 'DIRECT_LINE/DISCONNECT_FULFILLED'){
setDirectLine(null);
setStore();
}
return next(action);
};
var newT = false;
var store = window.WebChat.createStore({}, storeMiddleware);
var wc = document.getElementById('webchat');
async function getRes() {
res = await fetch(
'https://directline.botframework.com/v3/directline/tokens/generate',
{
headers: {
Authorization: `Bearer ${secret}`,
'Content-type': 'application/json'
},
method: 'POST'
}
);
}
async function openChat() {
wc.style.display = "";
if (token == "") {
newT = true;
await getRes();
token = await res.json();
}
else {
newT = false;
}
window.WebChat.renderWebChat(
{
directLine: window.WebChat.createDirectLine({ "token": token.token }), store,
styleOptions
},
wc);
document.querySelector('#webchat > *').focus();
}
function sendEvent() {
if (newT) {
store.dispatch({
type: 'WEB_CHAT/SEND_EVENT',
payload: { name: 'webchat/join' }
});
}
}
function minimizeChat() {
wc.style.display = "none";
}
function closeChat() {
minimizeChat();
store.dispatch({type: 'DIRECT_LINE/DISCONNECT'});
token ="";
}
function setDirectLine(dl){
window.WebChat.directLine = dl;
}
function setStore(){
store = window.WebChat.createStore({}, storeMiddleware);
}
</script>
</body>
</html>
预期行为
您不会受到欢迎,在开发者控制台中,最后写入的操作是 >>> HTML DISPATCH 操作:WEB_CHAT/SET_REFERENCE_GRAMMAR_ID"
附加上下文
如果您将以下代码行从“closeChat”函数移动到“minimizeChat”函数,机器人将正确地向我们打招呼。
store.dispatch({type: 'DIRECT_LINE/DISCONNECT'});
【问题讨论】:
-
当我尝试时,你的代码是有效的。只有两个区别。首先,我通过
res = await fetch('http://localhost:3500/directline/token', { method: 'POST' });代替您的await fetch()。我在为开发目的运行的“令牌服务器”中单独生成一个令牌。我使用请求并发布选项,但选项基本相同,这让我想到...二,我传入json: { user: { ID: 'dl_123' } },需要通过哪个Direct Line。该值是静态的,因为我不需要它是动态的进行测试。如果我使用你的fetch()电话,它会失败。试试看。 -
出于好奇,您为什么不使用已经开发的minimizable web chat 示例?
-
嗯,从我读过的文档来看,用户是可选的。我们还没有探索过传递它,因为它要么总是相同的,要么需要我们想出一些方法来使它独一无二。如果这是一个重复用户,这也将引导我们加载以前的对话历史记录,我们还没有准备好解决这个问题:)。
-
我没有使用该示例,因为我们正在寻找一种在 javascript 中实现功能的方法,并且该示例用于 React。我整理的代码是我重写 React 示例的尝试。
-
@StevenKanberg 感谢您的 cmets!我在 'json: {user: { ID: 'dl_123' } }' 中添加了,当我按照步骤进行复制时,机器人仍然卡住。
标签: javascript botframework direct-line-botframework