window.onload = function() {
// Check use of cookies
const allowCookies = false;
// Import direct line for some front end checks.
const ConnectionStatus = Object.freeze({
// the status when the DirectLine object is first created/constructed
"Uninitialized": 0,
// currently trying to connect to the conversation
"Connecting": 1,
// successfully connected to the conversation. Connection is healthy so far as we know.
"Online": 2,
// last operation had an error with an expired token. Possibly waiting for someone to supply a new one.
"ExpiredToken": 3,
// the initial attempt to connect to the conversation failed. No recovery possible.
"FailedToConnect": 4,
// the bot ended the conversation
"Ended": 5
});
// Not supported by Edge version 41, possibly < version 44
navigator.browserSpecs = (function() {
const ua = navigator.userAgent;
var tem,
M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if (/trident/i.test(M[1])) {
tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
return { name: "IE", version: (tem[1] || "") };
}
if (M[1] === "Chrome") {
tem = ua.match(/\b(OPR|Edge)\/(\d+)/);
if (tem != null) return { name: tem[1].replace("OPR", "Opera"), version: tem[2] };
}
M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, "-?"];
if ((tem = ua.match(/version\/(\d+)/i)) != null)
M.splice(1, 1, tem[1]);
const [name, version] = M;
return { name, version };
})();
function browserSupported() {
if (navigator.browserSpecs.name === "Edge" && navigator.browserSpecs.version >= 16 ||
navigator.browserSpecs.name === "Chrome" && navigator.browserSpecs.version >= 76 ||
navigator.browserSpecs.name === "Firefox" && navigator.browserSpecs.version >= 69 ||
navigator.browserSpecs.name === "Safari" && navigator.browserSpecs.version >= 11) {
return true;
} else {
return false;
}
}
function IsValueInEmpty(value) {
if (value === "" || value == null || value === "undefined") {
return true;
}
return false;
}
// Create function for fonts within chatbot
// ReSharper disable once UnusedLocals
function fontFamily(fonts) {
return fonts.map(font => `'${font}'`).join(", ");
}
function getCookieObject() {
const name = "agileDatumChatbot=";
const ca = document.cookie.split(";");
var cookieStr = "{}";
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === " ") {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
cookieStr = c.substring(name.length, c.length);
}
}
return JSON.parse(cookieStr);
}
function setCookieObject(cookieObj) {
document.cookie = `agileDatumChatbot=${JSON.stringify(cookieObj)};expires=0;path=/`;
}
function getCookie(cookieKey) {
const cookieObj = getCookieObject();
return cookieObj[cookieKey];
}
function setCookie(cookieKey, cookieValue) {
const cookieObj = getCookieObject();
cookieObj[cookieKey] = cookieValue;
setCookieObject(cookieObj);
}
var chatbotActive = false;
// Create elements on page...
const avatarButton = document.createElement("div");
document.body.appendChild(avatarButton);
avatarButton.outerHTML = '
' +
'
';
const div = document.createElement("div");
document.body.appendChild(div);
div.outerHTML = '
' +
'
' +
'
' +
'
' +
'' +
"
" +
"
" +
"
";
async function ConnectChatbot() {
if (!browserSupported()) {
document.getElementById("chatbot").innerHTML =
'
Please use the latest version of Chrome, Edge, Firefox or Safari to use our Chatbot
';
return;
}
var directLine;
var webChat;
var styleOptions = window.agileDatumChatbotConfig.defaultStyleOptions;
var customStyleOptions = window.agileDatumChatbotConfig.customStyleOptions;
// apply the customisation...
for (var option in customStyleOptions) {
if (customStyleOptions.hasOwnProperty(option)) {
styleOptions[option] = customStyleOptions[option];
}
}
const store = window.WebChat.createStore({},
({ dispatch }) => next => action => {
if (action.type === "DIRECT_LINE/CONNECT_FULFILLED") {
setCookie("conversationId", directLine.conversationId);
setCookie("streamUrl", directLine.streamUrl);
setCookie("token", window.agileDatumChatbotAuth.token);
setCookie("userID", window.agileDatumChatbotAuth.userID);
// When we receive DIRECT_LINE/CONNECT_FULFILLED action, we will send an event activity using WEB_CHAT/SEND_EVENT
dispatch({
type: "WEB_CHAT/SEND_EVENT",
payload: {
name: "webchat/join",
value: { language: window.navigator.language }
}
});
showOrHideChatInput(true, false);
}
return next(action);
});
// Check for conversation in cookie.
const conversationId = getCookie("conversationId");
// Use cookie data stored to load conversation data and do not render the welcome message...
if (!IsValueInEmpty(conversationId) && allowCookies) {
window.agileDatumChatbotAuth.userID = getCookie("userID");
directLine = new window.WebChat.createDirectLine({
token: getCookie("token"),
streamUrl: getCookie("streamUrl"),
conversationId: conversationId,
watermark: "0",
webSocket: false
});
webChat = {
directLine: directLine,
userID: window.agileDatumChatbotAuth.userID,
styleOptions
};
} else { // Create new connection and show welcome message...
directLine = new window.WebChat.createDirectLine({
token: window.agileDatumChatbotAuth.token,
webSocket: false
});
webChat = {
directLine: directLine,
userID: window.agileDatumChatbotAuth.userID,
styleOptions,
store
};
}
window.WebChat.renderWebChat(webChat, document.getElementById("chatbot"));
// add accessibility feature...
const hiddentext = document.createElement("span");
document.querySelector('#webchat button[title="Send"]').appendChild(hiddentext);
hiddentext.outerHTML = 'Send message';
// Subscribing to connectionStatus of DirectLine enables listening for ConnectionStatus.
// Online which is fired after ConnectionStatus.ExpiredToken.Then, update the cookie:
// https://stackoverflow.com/questions/52516124/store-refresh-token-in-a-cookie
// https://github.com/microsoft/BotFramework-DirectLineJS
directLine.connectionStatus$
.subscribe(connectionStatus => {
switch (connectionStatus) {
// the status when the DirectLine object is first created/constructed
case ConnectionStatus.Uninitialized:
console.log("Connection Status: Uninitialized");
break;
// currently trying to connect to the conversation
case ConnectionStatus.Connecting:
console.log("Connection Status: Connecting");
showOrHideChatInput(false, false);
break;
// successfully connected to the conversation. Connection is healthy so far as we know.
// now show UI for chatbot messages.
case ConnectionStatus.Online:
console.log("Connection Status: Online");
break;
// last operation had an error with an expired token. Your app should supply a new one.
case ConnectionStatus.ExpiredToken:
console.log("Connection Status: ExpiredToken");
setCookie("conversationId", "");
showTimeOutMessage();
break;
// the initial attempt to connect to the conversation failed. No recovery possible.
case ConnectionStatus.FailedToConnect:
console.log("Connection Status: FailedToConnect");
break;
// the bot ended the conversation
case ConnectionStatus.Ended:
console.log("Connection Status: Ended");
break;
}
});
document.querySelector("#chatbot > *").focus();
}
function showOrHideChatInput(isVisible, isChatEnded) {
const inputForm = document.querySelector('#webchat .main form');
const inputMain = document.querySelector('#webchat .main');
inputForm.style.height = null;
inputMain.style.visibility = null;
if (!isVisible) {
inputForm.style.height = "31px";
if (isChatEnded) {
inputForm.style.height = "0";
}
inputMain.style.visibility = "hidden";
}
}
function showTimeOutMessage() {
const htmlMsg = ''+
'
Your Chatbot session has ended, please refresh the page to start again.