Skip to content

Instantly share code, notes, and snippets.

@kayheunen
Created February 14, 2017 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kayheunen/a98cbce59eedd9a1e4041063e383d788 to your computer and use it in GitHub Desktop.
Save kayheunen/a98cbce59eedd9a1e4041063e383d788 to your computer and use it in GitHub Desktop.
Zendesk Chat - Toggle custom chat button on availability of team
var all_departments;
var department;
var $custom_button_chat = $('.help-widget .chat');
// map url to chat departments
var url_path = window.location.pathname.split( '/' );
var languages = {
'en-gb' : 'ENGLISH',
'nl' : 'DUTCH',
'de' : 'GERMAN',
'fr' : 'FRENCH',
'da' : 'DANISH',
'sv' : 'SWEDISH',
'es' : 'SPANISH',
'it' : 'ITALIAN',
'no' : 'NORWEGIAN',
'fi' : 'FINNISH'
};
autoControlDepartment = function controlDepartmentDropdown(){
// remove dropdown from chat
$zopim.livechat.departments.filter('');
// set the correct visitor department
$zopim.livechat.departments.setVisitorDepartment(department[0].name);
}
checkDepartmentStatus = function departmentStatus(){
zE(function() {
$zopim(function() {
all_departments = $zopim.livechat.departments.getAllDepartments();
department = all_departments.filter(function (department) {
return department.name.toLowerCase() == languages[url_path[2]].toLowerCase();
});
});
});
}
toggleChatButton = function chatButton(){
if ( (!department) || ( department[0].status === "offline" || department[0].status === "away" )) {
$custom_button_chat.hide().prev('li').addClass('last');
} else {
$custom_button_chat.show().prev('li').removeClass('last');
}
}
zE(function() {
$zopim(function() {
$zopim.livechat.hideAll();
$zopim.livechat.setOnConnected(function() {
// get all departments
checkDepartmentStatus();
autoControlDepartment();
// hide the chat button if we are offline or away
toggleChatButton();
// re-check status of department every 10 seconds and toggle the custom chat button
window.setInterval(checkDepartmentStatus, 1000);
window.setInterval(toggleChatButton, 1000);
});
});
zE.hide();
});
$('#startchat').on('click', function(e){
e.preventDefault();
$zopim.livechat.window.show();
zE.activate()
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment