We have a webchat window on our website that can be minimized/reopened (this keeps the same token, if still valid) or closed/reopened (this ensures we retrieve a new token). Our bot should greet us when we dispatch a specific event to the bot, like this example: Welcome Event Example. However, when following a specific pattern, the webchat becomes stuck on the "WEB_CHAT/SET_REFERENCE_GRAMMAR_ID" action and our bot does not greet us with our welcome message.
Could someone please help me to understand why the webchat freezes on this action?
Does this below sample approach look feasible to recomposing the UI using javascript?
Steps to reproduce:
- Using the below code, create an html file. - be sure to update the secret
- Open the file in Chrome
- Click on "Open Chat"
- Click on "Minimize Chat"
- Click on "Open Chat"
- Click on "Close Chat"
- Open developer console
- Click on "Open Chat"
<!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>
Expected behavior
You will not be greeted and, in the developer console, the last action written is ">>> HTML DISPATCH action: WEB_CHAT/SET_REFERENCE_GRAMMAR_ID"
Additional context
If you move the below line of code from the "closeChat" function to the "minimizeChat" function, the bot will properly greet us.
store.dispatch({type: 'DIRECT_LINE/DISCONNECT'});