簡體   English   中英

營業時間 Twilio 職能

[英]Business hours Twilio Funtion

我想為一周中的每一天創建營業時間,而不是周一到周五以及周六和周日。 我正在使用 Twilio 函數和 Twilio Studio,但是我在網上找到的所有內容都不起作用。 目前我正在使用他們提供的示例,我想知道是否有人可以幫助我解決這個問題。 這是我正在使用的代碼:

exports.handler = function(context, event, callback) {
    // With timezone:
    // In Functions/Configure, add NPM name: moment-timezone, version: 0.5.14
    // Timezone function reference: https://momentjs.com/timezone/
    let moment = require('moment-timezone');
    //
    // timezone needed for Daylight Saving Time adjustment
    let timezone = event.timezone || 'America/New_York';
    console.log("+ timezone: " + timezone);
    //
    const hour = moment().tz(timezone).format('H');
    const dayOfWeek = moment().tz(timezone).format('d');
    if ((hour >= 7 && hour < 19) && (dayOfWeek >= 1 && dayOfWeek <= 5)) {
        // "open" from 7am to 7pm, PST.
        response = "open";
    } else {
        response = "after";
    }
    theResponse = response + " : " + hour + " " + dayOfWeek;
    console.log("+ Time request: " + theResponse);
    callback(null, theResponse);
};

先感謝您!

好的,仍然是前面提到的相同代碼位置,需要更新此代碼塊以滿足您的需要。

if ((hour >= 7 && hour < 19) && (dayOfWeek >= 1 && dayOfWeek <= 5)) {
    // "open" from 7am to 7pm, PST.
    response = "open";
} else {
    response = "after";
}

需要更改為:
(正如您在上一條評論中給出的 2 個示例,我在下面顯示了前兩個條件。)

if ((hour >= 7 && hour < 15) && dayOfWeek == 1) { // Monday, 7am to 3pm
    response = "open";
} else if ((hour >= 11 && hour < 20) && dayOfWeek == 2) { // Tuesday, 11am to 8pm
    response = "open";
} else if ((/* Wednesday's hour range */) && dayOfWeek == 3) { // Wednesday
    response = "open";
} else if ((/* Thursday's hour range */) && dayOfWeek == 4) { // Thursday
    response = "open";
} else if ((/* Friday's hour range */) && dayOfWeek == 5) { // Friday
    response = "open";
} else if ((/* Saturday's hour range */) && dayOfWeek == 6) { // Saturday
    response = "open";
} else if ((/* Sunday's hour range */) && dayOfWeek == 0) { // Sunday
    response = "open";
} else {
    response = "after";
}

然后只需更新周三至周日的小時范圍。 請注意,周日的dayOfWeek是 0,而不是 7,因此這不是錯字。

我知道有人已經回答了這個問題,但這可能對您或將來的其他人有所幫助。 這是改編自 Twilio 上的教程,並作為其無服務器功能之一運行。

有一個 JSON object 包含節假日、辦公時間和部分日。 function 返回一個 object,帶有一些標志(開放、假期等)、問候語(早上、下午、晚上),以及假期或部分時間的原因。

const Moment = require( 'moment-timezone' );
const MomentRange = require( 'moment-range' );
const moment = MomentRange.extendMoment( Moment );
const timezone = "America/New_York";

exports.handler = async function(context, event, callback) {

  let response = new Twilio.Response();
  let client = context.getTwilioClient();

  response.setStatusCode( 200 );
  response.appendHeader('Access-Control-Allow-Origin', '*');
  response.appendHeader('Access-Control-Allow-Methods', 'OPTIONS, POST, GET');
  response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
  response.appendHeader('Content-Type', 'application/json');

  let body = getCalendarInfo();

  response.setBody(body);
  callback(null, response);

};

function checkIfInRange( begin, end, timezone ) {
  const currentDate = moment().tz( timezone ).format( 'MM/DD/YYYY' );
  const now = moment().tz( timezone );

  const beginMomentObject = moment.tz( `${currentDate} ${begin}`, 'MM/DD/YYYY HH:mm:ss', timezone );
  const endMomentObject = moment.tz( `${currentDate} ${end}`, 'MM/DD/YYYY HH:mm:ss', timezone );
  const range = moment.range( beginMomentObject, endMomentObject );

  return now.within( range );
}

function getCalendarInfo() {
  let body = {
    isOpen: false,
    isHoliday: false,
    isPartialDay: false,
    isRegularDay: false,
    greeting: '',
    reason: '',
    date: '',
    time: ''
  };

  //load JSON with schedule
  const schedule = {
    "holidays": {
      "01/03/2023": {
        "reason": "the New Year Holiday"
      },
      "01/16/2023": {
        "reason": "Martin Luther King Jr Day"
      }
    },
    "partialDays": {
    //    "12/26/2019": {
    //      "begin": "10:00:00",
    //      "end": "14:00:00",
    //      "reason": "the Day after Christmas"
    //    }
    },
    "regularHours": {
      "Monday": {
        "begin": "08:00:00",
        "end": "18:00:00"
      },
      "Tuesday": {
        "begin": "08:00:00",
        "end": "18:00:00"
      },
      "Wednesday": {
        "begin": "08:00:00",
        "end": "18:00:00"
      },
      "Thursday": {
        "begin": "08:00:00",
        "end": "18:00:00"
      },
      "Friday": {
        "begin": "08:00:00",
        "end": "23:59:00"
      },
      "Saturday": {
        "begin": null,
        "end": null
      },
      "Sunday": {
        "begin": null,
        "end": null
      }
    }
  }

  body.date = moment().tz( timezone ).format( 'MM/DD/YYYY' );
  body.time = moment().tz( timezone ).format( 'HH:mm:ss' );

  if ( body.time < "12:00:00" ) {
    body.greeting = "morning";
  } else if ( body.time < "18:00:00" ) {
    body.greeting = "afternoon";
  } else {
    body.greeting = "evening";
  }
  
  const currentDate = moment().tz( timezone ).format( 'MM/DD/YYYY' );

  const isHoliday = currentDate in schedule.holidays;
  const isPartialDay = currentDate in schedule.partialDays;

  if ( isHoliday ) {
    body.isHoliday = true;

    if ( typeof( schedule.holidays[ currentDate ].reason ) !== 'undefined' ) {
      body.reason = schedule.holidays[ currentDate ].reason;
    }

  } else if ( isPartialDay ) {
    body.isPartialDay = true;

    if ( typeof( schedule.partialDays[ currentDate ].reason ) !== 'undefined' ) {
      body.reason = schedule.partialDays[ currentDate ].reason;
    }

    if ( checkIfInRange( schedule.partialDays[ currentDate ].begin,
         schedule.partialDays[ currentDate ].end, timezone ) === true ) {
      body.isOpen = true;
    }
  } else {
    //regular hours
    const dayOfWeek = moment().tz( timezone ).format( 'dddd' );

    body.isRegularDay = true;
    
    if ( checkIfInRange( schedule.regularHours[ dayOfWeek ].begin,
        schedule.regularHours[ dayOfWeek ].end, timezone ) === true ) {
      body.isOpen = true;
    }
  }
  return body;
}
exports.handler = function(context, event, callback) {
const currentDay = new Date().getDay();
const currentHour = new Date().getHours();
const operatingHours = {
0: { start: 11, end: 16 }, // Sunday: 11am to 4pm
1: { start: 7, end: 20 }, // Monday: 7am to 8pm
2: { start: 7, end: 20 }, // Tuesday: 7am to 8pm
3: { start: 7, end: 20 }, // Wednesday: 7am to 8pm
4: { start: 7, end: 20 }, // Thursday: 7am to 8pm
5: { start: 7, end: 20 }, // Friday: 7am to 8pm
6: { start: 8, end: 16 } // Saturday: 8am to 8pm
};
if (operatingHours[currentDay] && currentHour >= 
operatingHours[currentDay].start && currentHour < 
operatingHours[currentDay].end) {
// Perform allowed action
} else {
// Return an error or take some other action
}
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM