Created
February 1, 2019 09:03
-
-
Save SalesforceBobLightning/cd50fd59e4e8e6aeaea31813de3a3f14 to your computer and use it in GitHub Desktop.
Twilo Flex Function for use with a Studio Run Function Widget to determine if workers are available or it's within opening hours
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
const moment = require('moment-timezone'); | |
const TIMEZONE = 'Europe/London'; // timezone needed for Daylight Saving Time adjustment | |
const OPEN_TIME = 9; // 9AM | |
const CLOSE_TIME = 18; // 8PM | |
const FRIDAY = 5; | |
const SATURDAY = 6; | |
const SAT_OPEN_TIME = 10; // 10AM | |
const SAT_CLOSE_TIME = 15; // 3PM | |
process.on('unhandledRejection', error => { | |
console.log('unhandledRejection', error.message); | |
}); | |
exports.handler = function (context, event, callback) { | |
const client = context.getTwilioClient(); | |
doWork(client, callback); | |
}; | |
async function doWork(client, callback) { | |
const hour = moment().tz(TIMEZONE).format('H'); | |
const dayOfWeek = moment().tz(TIMEZONE).format('d'); | |
const workers = await getAvailableWorkerSize(client); | |
if (workers === 0) { | |
console.log('closed - no workers'); | |
response = false; | |
callback(null, 'false'); | |
return; | |
} | |
if ((hour >= OPEN_TIME && hour < CLOSE_TIME) && dayOfWeek <= FRIDAY) { | |
console.log('open - during the week'); | |
callback(null, true); | |
return; | |
} else if (dayOfWeek === SATURDAY && (hour >= SAT_OPEN_TIME && hour < SAT_CLOSE_TIME)) { | |
console.log('open - saturday'); | |
callback(null, 'true'); | |
return; | |
} | |
console.log('closed - out of hours'); | |
callback(null, 'false'); | |
} | |
async function getAvailableWorkerSize(client) { | |
const taskrouter = client.taskrouter.workspaces('WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'); | |
const workers = await taskrouter.workers.list({ available: true }); | |
return workers.length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment