How to Set Up Business Hours Using a Script May 09, 2024 15:27 Updated Index: Add the "SetWorkSchedule" Script Edit the "CheckWorkTime" Script Customize Your Template As we know, all business hours setup is done by running a script, and considering users of all skill levels, this article will create a chatbot template that allows easy setup of business hours. Add the "SetWorkSchedule" Script By adding this script to the "2.0 - Check Business Hours" block, be sure to maintain the order of the scripts. The "SetWorkSchedule" script must be executed before the "CheckWorkTime" script: In this particular script, there are no input variables. However, the output will be saved to the variable workSchedule. For our example, let's assume we need the following business hours: Sunday Monday Tuesday Wednesday Thursday Friday Saturday Start of business hours [1] 8:30 8:30 8:30 8:30 8:30 8:30 8:30 End of business hours [1] 12:00 12:00 18:00 12:00 12:00 12:00 12:00 Start of business hours [2] ----- 13:00 ----- 13:00 13:00 13:00 ----- End of business hours [2] ----- 15:00 ----- 18:00 18:00 18:00 ----- Start of business hours [3] ----- 15:30 ----- ----- ----- ----- ----- End of business hours [3] ----- 18:00 ----- ----- ----- ----- ----- The resulting script is shown below and can be customized as needed. function run() { let workSchedule = [ { num: 0, name: "Sunday", workTime: [ { start: "8:30", end: "12:00" } ] }, { num: 1, name: "Monday", workTime: [ { start: "8:30", end: "12:00" }, { start: "13:00", end: "15:00" }, { start: "15:30", end: "18:00" } ] }, { num: 2, name: "Tuesday", workTime: [ { start: "8:30", end: "18:00" } ] }, { num: 3, name: "Wednesday", workTime: [ { start: "8:30", end: "12:00" }, { start: "13:00", end: "18:00" } ] }, { num: 4, name: "Thursday", workTime: [ { start: "8:30", end: "12:00" }, { start: "13:00", end: "18:00" } ] }, { num: 5, name: "Friday", workTime: [ { start: "8:30", end: "12:00" }, { start: "13:00", end: "18:00" } ] }, { num: 6, name: "Saturday", workTime: [ { start: "8:30", end: "12:00" } ] } ]; return JSON.stringify(workSchedule); //Return value will be saved as "Return value variable" field name } Edit the "CheckWorkTime" Script Finally, we will modify "CheckWorkTime", which should be the second script in "2.0-Check Business Hours". The first change is in the input variables. For this example, our input variables are: config.dateTimeOffset and workSchedule. The script's output will still be stored in the variable isWorkTime. Next, here's the script itself, presented below and ready to be transferred to your bot. It will work perfectly as long as you follow the steps outlined here. // 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, today);return checkTime(intervalTime, today);}return false;}function getIntervalTime(todaySchedule, today) {let intervalTime = [];for (let i = 0; i < todaySchedule.workTime.length; i++) {intervalTime.push({start: utcDate(todaySchedule.workTime[i].start, today),end: utcDate(todaySchedule.workTime[i].end, today)});}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.getUTCDay()) 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, today) {let hour = getValueByString("hour", timeString);let minutes = getValueByString("minutes", timeString);let utc_timestamp = Date.UTC(today.getUTCFullYear(),today.getUTCMonth(),today.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;} Observation: Since this example was used for checking human service hours, the place to change the business hours settings will not be the same as in the official business template. Here's where you can adjust the settings. Edit the "SetWorkSchedule" script if you want to: Add or remove business days. Add, modify, or remove business hours for a specific day. Change the chatbot configuration variables if you want to: Change the time zone. Customize Your Template All the content of 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 at our community or videos on our channel. 😃 Related articles How to configure the new interface in your bot's opening hours Adjusting Builder Time How to customize the service template Creating interactive messages in WhatsApp How to identify users coming from a Click to Chat WhatsApp ad published on Facebook