twilio - 我的 Twilio 功能是否适合根据星期几和一天中的时间路由呼叫?
问题描述
我正在尝试使用引用以下函数的 Twilio Studio 根据一天中的时间将呼叫路由到不同的代理,并想知道它是否正确?我不是程序员,所以这是改编自Need help created a Time Gate in Twilio Function
// Time of Day Routing
// Useful for IVR logic, for Example in Studio, to determine which path to route to
// Add moment-timezone 0.5.31 as a dependency under Functions Global Config, Dependencies
const moment = require('moment-timezone');
exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.VoiceResponse();
function businessHours() {
// My timezone East Coast (other choices: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
const now = moment().tz('America/Denver');
// Weekday Check using moment().isoWeekday()
// Monday = 1, Tuesday = 2 ... Sunday = 7
if(now.isoWeekday() == 1 || 3 || 5 /* Check for Normal Work Week Monday - Friday */) {
//Work Hours Check, 9 am to 5pm (17:00 24 hour Time)
if((now.hour() >= 8 && now.hour() < 9:30) || (now.hour() >= 12 && now.hour() < 17) /* 24h basis */) {
return true
}
}
if(now.isoWeekday() == 2 /* Check for Normal Work Week Monday - Friday */) {
//Work Hours Check, 9 am to 5pm (17:00 24 hour Time)
if((now.hour() >= 8:30 && now.hour() < 11) /* 24h basis */) {
return true
}
}
if(now.isoWeekday() == 4 /* Check for Normal Work Week Monday - Friday */) {
//Work Hours Check, 9 am to 5pm (17:00 24 hour Time)
if((now.hour() >= 8 && now.hour() < 10:30) || (now.hour() >= 15 && now.hour() < 17) /* 24h basis */) {
return true
}
}
// Outside of business hours, return false
return false
};
const isOpen = businessHours();
if (isOpen) {
twiml.say("Business is Open");
} else {
twiml.say("Business is Closed");
}
callback(null, twiml);
};
解决方案
Twilio 开发人员布道者在这里。
Stack Overflow 不是问“这是正确的吗?”的最佳场所。最好附带一个您遇到的实际问题以及您尝试解决该问题的事情的描述。也很难回答“这是正确的吗?” 如果我们不知道你真正想要的结果。
但是,我可以看到上面代码的一个问题,那就是处理超出小时测试的工作时间。
now.hour()
将返回一个整数,即当前小时。例如,您无法将其与此进行比较9:30
。相反,我们必须同时查看小时和分钟,
在您的第一个条件小时检查中,您有:
if((now.hour() >= 8 && now.hour() < 9:30) || (now.hour() >= 12 && now.hour() < 17) /* 24h basis */) {
return true
}
换句话说,您似乎想要:如果时间在上午 8 点到上午 9:30 之间或时间在下午 12 点到下午 5 点之间,则返回 true。
为了应对 9:30 的时间,我们必须检查时间是否在上午 8 点到上午 10 点之间,如果超过 9 分钟,则该分钟数不超过 30。只是为了减少我们正在寻找的代码在,问题是这个谓词:
(now.hour() >= 8 && now.hour() < 9:30)
我们可以将其替换为:
((now.hour() >= 8 && now.hour() < 9) || (now.hour() == 9 && now.minute() < 30))
这现在测试小时大于或等于 8且小于 9 或小时等于 9并且分钟小于 30。
由于“大于或等于 8 且小于 9”实际上与“等于 8”相同,我们可以将其缩短为:
(now.hour() === 8 || (now.hour() == 9 && now.minute() < 30))
但是当您想要修复进一步的比较时,您将需要使用完整版本,例如在 8:30 和 11 之间或在 8 和 10:30 之间。
希望这能让您很好地了解如何进行所有时间比较。
推荐阅读
- jquery - 使用 Parceljs,如何防止 web 扩展打包时 jquery 和 bootstrap 最小化
- python - 如何多次复制列表,修改一个并让其他保持不变?
- c# - .NET Framework 中的实体框架中的数据库未更新
- c# - Selenium chromeDriver 代理身份验证不起作用
- sql - SQL 查询以获取特定模式名称下的所有标题名称
- arrays - NumPy 数组中的列表推导
- reactjs - 当通过 axios 从 Spring Boot 中的后端访问数据时,Reactjs useTable 挂钩中未显示表
- angular - 路由困境——最好的架构方法是什么?
- javascript - WebTorrent 播放器 reactjs
- itk - 如何对不同范围的医学图像进行插值