Search code examples
twiliotwilio-functions

Twilio Function - Using Moment for Open Office Hours


Hi Im trying to get this working properly. I'd like the open time to be set between 14:00 UTC and 22:00 UTC. But it doesn't seem to work. When I call the function url I get a return of "open" when I call it outside of the open times. Can anyone help me with this? Thanks.

exports.handler = function(context, event, callback) {
  const moment = require('moment');

  let callerId = event.Caller; // || "+1-000-000-0000"; // default caller ID

  let twiml = new Twilio.twiml.VoiceResponse();

  if ((moment().hour() >= 14 || moment().hour() < 22) && moment().isoWeekday() <= 5) {
    twiml.say("Open");
  } else {
    twiml.say("Closed");
  }
  twiml.redirect("http://twimlets.com/voicemail?Email=eeemail@email.com&Message=Please%20leave%20a%20message.&Transcribe=true");
  callback(null, twiml);
};

Solution

  • Your code sample you have the hour 17 not 14, anyway, this thing:

    (moment().hour() >= 17 || moment().hour() < 22)

    will always return true, change it to:

    (moment().hour() >= 17 && moment().hour() < 22)

    to get the time between 5 PM and 10 PM