How to configure service hours Help Center January 23, 2023 15:01 Updated Index: Structuring service bot Create the service bot Adapting attendance template to check available attendants per queue Add the script "SetWorkSchedule" Change the "CheckWorkTime" script Customize your template As we know all the configuration of the service hours is done by executing a script, and considering all levels of user knowledge, in this article we will create a chatbot template that allows easy configuration of the service hours. Structuring service bot Create the service bot We will start structuring the bot from the service template. Adapting attendance template to check available attendants per queue In the service template flow, we will only change the block "2.0 - Check Service Hours", so our flow remains as well as the image of the service template below. Add the script "SetWorkSchedule" Adding this script in the block "2.0 - Verifies Service Hours", pay attention to the order of the scripts. The "SetWorkSchedule" script must be executed before the "CheckWorkTime" script. In this script, there are no input variables. In turn, our return will be saved in the variable workSchedule. For our example, suppose we need the following availability times. Sunday Monday Tuesday Wednesday Thursday Friday Saturday Start of attendance [1] 8:30 8:30 8:30 8:30 8:30 8:30 8:30 End of attendance [1] 12:00 12:00 18:00 12:00 12:00 12:00 12:00 Start of attendance [2] ----- 13:00 ----- 13:00 13:00 13:00 ----- End of attendance [2] ----- 15:00 ----- 18:00 18:00 18:00 ----- Start of attendance [3] ----- 15:30 ----- ----- ----- ----- ----- End of attendance [3] ----- 18:00 ----- ----- ----- ----- ----- The resulting script is presented below and can be customized as needed. function run() { let workSchedule = [ { num: 0, name: "Sunday", englishName: "Sunday", workTime: [ { start: "8:30", end: "12:00" } ] }, { num: 1, name: "Monday", englishName: "Monday", workTime: [ { start: "8:30", end: "12:00" }, { start: "13:00", end: "15:00" }, { start: "15:30", end: "18:00" } ] }, { num: 2, name: "Tuesday", englishName: "Tuesday", workTime: [ { start: "8:30", end: "18:00" } ] }, { num: 3, name: "Wednesday", englishName: "Wednesday", workTime: [ { start: "8:30", end: "12:00" }, { start: "13:00", end: "18:00" } ] }, { num: 4, name: "Thursday", englishName: "Thursday", workTime: [ { start: "8:30", end: "12:00" }, { start: "13:00", end: "18:00" } ] }, { num: 5, name: "Friday", englishName: "Friday", workTime: [ { start: "8:30", end: "12:00" }, { start: "13:00", end: "18:00" } ] }, { num: 6, name: "Saturday", englishName: "Saturday", workTime: [ { start: "8:30", end: "12:00" } ] } ]; return JSON.stringify(workSchedule); //Return value will be saved as "Return value variable" field name} Check the results in the image below: Change the "CheckWorkTime" script Finally, we will change the "CheckWorkTime", which should be the second script of "2.0 - Check Service Hours". The first change is in the input variables, for this example our input variables are: config.dateTimeOffset and workSchedule. The return of the script will still be saved in the variable isWorkTime. In turn, we have the script itself, it is presented below and can be transferred to your bot and it will work perfectly, if the steps presented here are followed. // Receive the variables as parametersfunction run(offset, weekSchedule) { offset = parseInt(offset); weekSchedule = JSON.parse(weekSchedule); let today = nowUTC(offset); if (isWorkDay(today, weekSchedule)) { let todaySchedule = getTodaySchedule(weekSchedule, today); let intervalTime = getIntervalTime(todaySchedule); return checkTime(intervalTime, today); } return false;}function getIntervalTime(todaySchedule) { let intervalTime = []; for (let i = 0; i < todaySchedule.workTime.length; i++) { intervalTime.push({ start: utcDate(todaySchedule.workTime[i].start), end: utcDate(todaySchedule.workTime[i].end) }); } return intervalTime;}function checkTime(intervalTime, today) { for (let i = 0; i < intervalTime.length; i++) { if (today - intervalTime[i].start > 0 && intervalTime[i].end - today > 0) return true; } return false;}function getTodaySchedule(weekSchedule, today) { for (let i = 0; i < weekSchedule.length; i++) { if (weekSchedule[i].num == today.getDay()) return weekSchedule[i]; }}//Get now UTC Datefunction nowUTC(offset) { let now = new Date(); let utc_timestamp = Date.UTC( now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds() ); return new Date(utc_timestamp + offset * 3600 * 1000);}//Get UTC Datefunction utcDate(timeString) { let now = new Date(); let hour = getValueByString("hour", timeString); let minutes = getValueByString("minutes", timeString); let utc_timestamp = Date.UTC( now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), hour, minutes, 0, 0 ); return new Date(utc_timestamp);}//Get hour/minute by string with pattern HH:mmfunction getValueByString(type, timeString) { if (type === "hour") { return parseInt(timeString.substring(0, timeString.indexOf(":"))); } else if (type === "minutes") { return parseInt( timeString.substring(timeString.indexOf(":") + 1, timeString.length) ); } return 0;}//Get if today is a work dayfunction isWorkDay(today, workDays) { for (let i = 0; i < workDays.length; i++) { if (workDays[i].num == today.getDay().toString()) return true; } return false;}Comments Since this example was used to check human service times, the location for changing the service time setting will not be the same as the official service template. See where you can change the settings. Change in the "SetWorkSchedule" script, if you want to: Add or remove service days. Add, change or remove one-day service hours. Change the chatbot configuration variables, if you want to: Change time zone. Customize your template All content in this article, generated a template that can be configured, downloaded and imported into your chatbot, using the tool below: For more information, visit the discussion on the subject in our community or the videos on our channel. 😊 Related articles Creating interactive messages in WhatsApp Audience file configuration - Bulk notification sending How to save a contact's WhatsApp number How to set user downtime How to import a bot flow in Builder