在Twilio函数中创建时间门需要帮助 [英] Need help creating a Time Gate in Twilio Function

查看:16
本文介绍了在Twilio函数中创建时间门需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉Twilio Studio、函数和扩展名node.js。我正在尝试创建一个将计算当前日期和时间的函数。如果时间在窗口之外,我想返回FALSE,否则返回TRUE。这是我到目前为止所拥有的:

    exports.handler = function(context, event, callback) {
    let twiml = new Twilio.twiml.VoiceResponse();
    
    var day = Twilio.Date.toString();
    twiml.say(day);
    
    callback(null, twiml);
};

Twilio

查看下面的推荐答案函数,并进行相应的修改。

// 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/New_York');
  
  // Weekday Check using moment().isoWeekday()
  // Monday = 1, Tuesday = 2 ... Sunday = 7 
  if(now.isoWeekday() <= 5 /* Check for Normal Work Week Monday - Friday */) {
   
    //Work Hours Check, 9 am to 5pm (17:00 24 hour Time)
    if(now.hour() >= 9 && 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函数中创建时间门需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆