You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:基于Twilio Functions配置一周每日独立营业时间

为一周每一天单独设置营业时间(Twilio Functions 实现)

下面是修改后的代码,支持为一周中的每一天单独定义营业时间,替换你原来的统一工作日设置逻辑:

exports.handler = function(context, event, callback) {
    // 确保已在Functions/Configure中添加moment-timezone依赖(0.5.14版本)
    let moment = require('moment-timezone');
    
    // 时区,可通过event传入或使用默认值
    let timezone = event.timezone || 'America/New_York';
    console.log("+ timezone: " + timezone);
    
    // 获取当前时间的小时(24小时制)和星期几(0=周日,1=周一,...,6=周六)
    const currentHour = parseInt(moment().tz(timezone).format('H'), 10);
    const currentDay = parseInt(moment().tz(timezone).format('d'), 10);
    
    // 定义一周每一天的营业时间,按需修改
    const businessHours = {
        0: { open: 9, close: 17 },    // 周日:9点到17点
        1: { open: 7, close: 19 },    // 周一:7点到19点
        2: { open: 7, close: 19 },    // 周二:7点到19点
        3: { open: 8, close: 18 },    // 周三:8点到18点
        4: { open: 7, close: 19 },    // 周四:7点到19点
        5: { open: 8, close: 18 },    // 周五:8点到18点
        6: { open: 10, close: 16 }    // 周六:10点到16点
    };
    
    let response;
    // 检查当前时间是否在当天的营业时间范围内
    if (currentHour >= businessHours[currentDay].open && currentHour < businessHours[currentDay].close) {
        response = "open";
    } else {
        response = "after";
    }
    
    const theResponse = `${response} : ${currentHour} ${currentDay}`;
    console.log("+ Time request: " + theResponse);
    callback(null, theResponse);
};

关键修改说明:

  • 新增businessHours对象,通过星期数字(0到6)映射每一天的开门/关门时间,你可以直接修改对应的值来调整营业时间
  • 将hour和dayOfWeek转为整数,避免字符串比较可能出现的问题
  • 修复了原代码中未声明response和theResponse的变量提升问题,添加了let/const声明
  • 用模板字符串优化了结果拼接的写法

你可以根据自己的需求修改businessHours里的每一项时间,测试时可以通过Twilio Functions的测试功能传入不同的timezone参数验证逻辑。

内容的提问来源于stack exchange,提问作者Yarseo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 05:05:17