Created
February 28, 2023 20:42
-
-
Save dudanogueira/57475f70830db9d75ed81ed7d6b7f361 to your computer and use it in GitHub Desktop.
Business Hours for Botpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simples Business Hour Verification Function | |
* @title Check for Business Hours | |
* @category Misc | |
* @author Duda Nogueira <duda.nogueira@gmail.com> | |
* @param {dict} business_hours - Business Hours Notation | |
* @param {string} time_zone - Business Hours Timezone | |
*/ | |
function isInRange(value, range) { | |
let match = value >= range[0] && value <= range[1] | |
return match | |
} | |
function isInRanges(value, range) { | |
var matches = [] | |
range.split(',').forEach(function(timeframe) { | |
var times = timeframe.split('-') | |
var match = isInRange(value, times) | |
matches.push(match) | |
}) | |
// return if any true | |
return matches.some(element => element) | |
} | |
// console.log( "SHOULD BE TRUE", | |
// isInRanges("06:30", "00:30-02:30,06:00-12:00,13:00-18:00") | |
// ) | |
// console.log( "SHOULD BE FALSE", | |
// isInRanges("06:30", "00:30-02:30,06:31-12:00,13:00-18:00") | |
// ) | |
function business_day(day, hours, today, rightnow) { | |
let output = false | |
const d = new Date() | |
// day is all or day if today | |
if (day == '*' || day == today) { | |
// hours is all or hours if today | |
if (hours == '*') { | |
output = true | |
} else { | |
if (hours == null) { | |
output = false | |
} else { | |
output = isInRanges(rightnow, hours) | |
} | |
} | |
} | |
return output | |
} | |
const myAction = async (business_hours, time_zone="America/Sao_Paulo") => { | |
let locale_date_string = new Date().toLocaleString("en-US", { | |
timeZone: time_zone | |
}); | |
let d = new Date(locale_date_string); | |
let today = d.getDay() | |
console.log('TODAY', today) | |
let rightnow = d.getHours() + ':' + d.getMinutes() | |
console.log('RIGHTNOW', rightnow) | |
business_hours = { | |
'1': '08:30-18:00', | |
'2': '08:30-18:00', | |
'3': '08:30-18:00', | |
'4': '08:30-18:00', | |
'5': '08:30-18:00' | |
} | |
var matches = [] | |
for (i in business_hours) { | |
let check = business_day(i, business_hours[i], today, rightnow) | |
matches.push(check) | |
console.log('CHECK', i, business_hours[i], today, rightnow, check) | |
} | |
var result = matches.some(element => element) | |
user.business_hours = result | |
bp.logger.info('ROCKETCHAT:BUSINESS HOURS: TODAY/RIGHNOW/RESULT' + today + '/' + rightnow + '/' + result) | |
} | |
return myAction(args.business_hours, args.time_zone) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment