Last active
December 15, 2020 18:41
-
-
Save ambiorixg12/7d580324127b8bebac8f9517d13444c3 to your computer and use it in GitHub Desktop.
Twilio function json
This file contains hidden or 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
| exports.handler = function(context, event, callback) { | |
| const axios = require('axios'); | |
| const Moment1 = require('moment-timezone'); | |
| const MomentRange = require('moment-range'); | |
| const moment = MomentRange.extendMoment(Moment1); | |
| //create Twilio Response | |
| let response = new Twilio.Response(); | |
| response.appendHeader('Access-Control-Allow-Origin', '*'); | |
| response.appendHeader('Access-Control-Allow-Methods', 'OPTIONS POST'); | |
| response.appendHeader('Content-Type', 'application/json'); | |
| response.appendHeader('Access-Control-Allow-Headers', 'Content-Type'); | |
| //create default response body | |
| response.body = { | |
| isOpen: false, | |
| isHoliday: false, | |
| isPartialDay: false, | |
| isRegularDay: false, | |
| description: '' | |
| } | |
| let timezone = event.timezone || 'America/New_York'; | |
| //load JSON with schedule | |
| const jsonFile = `http://45.32.165.238/Schedule.json`; | |
| axios.get(jsonFile) | |
| .then(function (axiosResponse) { | |
| const schedule = axiosResponse.data; | |
| const currentDate = moment().tz(timezone).format('MM/DD/YYYY'); | |
| console.log("Log here ***" + currentDate ); | |
| const isHoliday = currentDate in schedule.holidays; | |
| console.log("**true of false***" + isHoliday); | |
| response.body.isHoliday = true; | |
| if (isHoliday) { | |
| response.body= true; | |
| callback(null, response); | |
| } | |
| else{ | |
| response.body= false; | |
| callback(null, response); | |
| } | |
| if (isPartialDay) { | |
| response.body.isPartialDay = true; | |
| if (typeof(schedule.partialDays[currentDate].description) !== 'undefined') { | |
| response.body.description = schedule.partialDays[currentDate].description; | |
| } | |
| if (checkIfInRange(schedule.partialDays[currentDate].begin, schedule.partialDays[currentDate].end, timezone) === true) { | |
| response.body.isOpen = true; | |
| callback(null, response); | |
| } else { | |
| callback(null, response); | |
| } | |
| } else { | |
| //regular hours | |
| const dayOfWeek = moment().tz(timezone).format('dddd'); | |
| response.body.isRegularDay = true; | |
| if (checkIfInRange(schedule.regularHours[dayOfWeek].begin, schedule.regularHours[dayOfWeek].end, timezone) === true) { | |
| response.body.isOpen = true; | |
| callback(null, response); | |
| } else { | |
| callback(null, response); | |
| } | |
| } | |
| }) | |
| .catch(function (error) { | |
| callback(error); | |
| }); | |
| }; | |
| 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); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment